• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 The Libphonenumber Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * @author Shaopeng Jia
17  */
18 
19 package com.google.phonenumbers.demo.helper;
20 
21 import static java.nio.charset.StandardCharsets.UTF_8;
22 import static java.util.Locale.ENGLISH;
23 
24 import java.io.UnsupportedEncodingException;
25 import java.net.URLEncoder;
26 import java.util.Locale;
27 
28 public class WebHelper {
29   private static final String NEW_ISSUE_BASE_URL =
30       "https://issuetracker.google.com/issues/new?component=192347&title=";
31   /** Returns a stable URL pointing to the result page for the given input. */
getPermaLinkURL( String phoneNumber, String defaultCountry, Locale geocodingLocale, boolean absoluteURL)32   public static String getPermaLinkURL(
33       String phoneNumber, String defaultCountry, Locale geocodingLocale, boolean absoluteURL) {
34     // If absoluteURL is false, generate a relative path. Otherwise, produce an absolute URL.
35     StringBuilder permaLink =
36         new StringBuilder(
37             absoluteURL
38                 ? "http://libphonenumber.appspot.com/phonenumberparser"
39                 : "/phonenumberparser");
40     try {
41       permaLink.append(
42           "?number=" + URLEncoder.encode(phoneNumber != null ? phoneNumber : "", UTF_8.name()));
43       if (defaultCountry != null && !defaultCountry.isEmpty()) {
44         permaLink.append("&country=" + URLEncoder.encode(defaultCountry, UTF_8.name()));
45       }
46       if (!geocodingLocale.getLanguage().equals(ENGLISH.getLanguage())
47           || !geocodingLocale.getCountry().isEmpty()) {
48         permaLink.append(
49             "&geocodingLocale=" + URLEncoder.encode(geocodingLocale.toLanguageTag(), UTF_8.name()));
50       }
51     } catch (UnsupportedEncodingException e) {
52       // UTF-8 is guaranteed in Java, so this should be impossible.
53       throw new AssertionError(e);
54     }
55     return permaLink.toString();
56   }
57 
58   /** Returns a link to create a new github issue with the relevant information. */
getNewIssueLink( String phoneNumber, String defaultCountry, Locale geocodingLocale)59   public static String getNewIssueLink(
60       String phoneNumber, String defaultCountry, Locale geocodingLocale) {
61     boolean hasDefaultCountry = !defaultCountry.isEmpty() && defaultCountry != "ZZ";
62     String issueTitle =
63         "Validation issue with "
64             + phoneNumber
65             + (hasDefaultCountry ? " (" + defaultCountry + ")" : "");
66 
67     String newIssueLink = NEW_ISSUE_BASE_URL;
68     try {
69       newIssueLink += URLEncoder.encode(issueTitle, UTF_8.name());
70     } catch (UnsupportedEncodingException e) {
71       // UTF-8 is guaranteed in Java, so this should be impossible.
72       throw new AssertionError(e);
73     }
74     return newIssueLink;
75   }
76 }
77