summaryrefslogtreecommitdiff
path: root/demo/src/com/google/phonenumbers/PhoneNumberParserServlet.java
blob: 40f0fa8f9834b8b01c4494349a4af20716cc51a6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
/*
 * Copyright (C) 2011 The Libphonenumber Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * @author Shaopeng Jia
 */

package com.google.phonenumbers;

import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.Locale.ENGLISH;

import com.google.i18n.phonenumbers.AsYouTypeFormatter;
import com.google.i18n.phonenumbers.NumberParseException;
import com.google.i18n.phonenumbers.PhoneNumberToCarrierMapper;
import com.google.i18n.phonenumbers.PhoneNumberToTimeZonesMapper;
import com.google.i18n.phonenumbers.PhoneNumberUtil;
import com.google.i18n.phonenumbers.PhoneNumberUtil.PhoneNumberFormat;
import com.google.i18n.phonenumbers.PhoneNumberUtil.PhoneNumberType;
import com.google.i18n.phonenumbers.PhoneNumberUtil.ValidationResult;
import com.google.i18n.phonenumbers.Phonenumber.PhoneNumber;
import com.google.i18n.phonenumbers.ShortNumberInfo;
import com.google.i18n.phonenumbers.geocoding.PhoneNumberOfflineGeocoder;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Locale;
import java.util.StringTokenizer;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItemIterator;
import org.apache.commons.fileupload.FileItemStream;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.fileupload.util.Streams;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringEscapeUtils;

/**
 * A servlet that accepts requests that contain strings representing a phone number and a default
 * country, and responds with results from parsing, validating and formatting the number. The
 * default country is a two-letter region code representing the country that we are expecting the
 * number to be from.
 */
@SuppressWarnings("serial")
public class PhoneNumberParserServlet extends HttpServlet {
  private PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
  private ShortNumberInfo shortInfo = ShortNumberInfo.getInstance();

  public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    String phoneNumber = null;
    String defaultCountry = null;
    String languageCode = "en"; // Default languageCode to English if nothing is entered.
    String regionCode = "";
    String fileContents = null;
    ServletFileUpload upload = new ServletFileUpload();
    upload.setSizeMax(50000);
    try {
      FileItemIterator iterator = upload.getItemIterator(req);
      while (iterator.hasNext()) {
        FileItemStream item = iterator.next();
        InputStream in = item.openStream();
        if (item.isFormField()) {
          String fieldName = item.getFieldName();
          if (fieldName.equals("phoneNumber")) {
            phoneNumber = Streams.asString(in, UTF_8.name());
          } else if (fieldName.equals("defaultCountry")) {
            defaultCountry = Streams.asString(in).toUpperCase();
          } else if (fieldName.equals("languageCode")) {
            String languageEntered = Streams.asString(in).toLowerCase();
            if (languageEntered.length() > 0) {
              languageCode = languageEntered;
            }
          } else if (fieldName.equals("regionCode")) {
            regionCode = Streams.asString(in).toUpperCase();
          }
        } else {
          try {
            fileContents = IOUtils.toString(in);
          } finally {
            IOUtils.closeQuietly(in);
          }
        }
      }
    } catch (FileUploadException e1) {
      e1.printStackTrace();
    }

    StringBuilder output;
    resp.setContentType("text/html");
    resp.setCharacterEncoding(UTF_8.name());
    if (fileContents == null || fileContents.length() == 0) {
      // Redirect to a URL with the given input encoded in the query parameters.
      Locale geocodingLocale = new Locale(languageCode, regionCode);
      resp.sendRedirect(
          getPermaLinkURL(phoneNumber, defaultCountry, geocodingLocale, false /* absoluteURL */));
    } else {
      resp.getWriter().println(getOutputForFile(defaultCountry, fileContents));
    }
  }

  /** Handle the get request to get information about a number based on query parameters. */
  public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    String phoneNumber = req.getParameter("number");
    if (phoneNumber == null) {
      phoneNumber = "";
    }
    String defaultCountry = req.getParameter("country");
    if (defaultCountry == null) {
      defaultCountry = "";
    }
    String geocodingParam = req.getParameter("geocodingLocale");
    Locale geocodingLocale;
    if (geocodingParam == null) {
      geocodingLocale = ENGLISH; // Default languageCode to English if nothing is entered.
    } else {
      geocodingLocale = Locale.forLanguageTag(geocodingParam);
    }
    resp.setContentType("text/html");
    resp.setCharacterEncoding(UTF_8.name());
    resp.getWriter()
        .println(getOutputForSingleNumber(phoneNumber, defaultCountry, geocodingLocale));
  }

  private StringBuilder getOutputForFile(String defaultCountry, String fileContents) {
    StringBuilder output =
        new StringBuilder(
            "<HTML><HEAD><TITLE>Results generated from phone numbers in the file provided:"
                + "</TITLE></HEAD><BODY>");
    output.append("<TABLE align=center border=1>");
    output.append("<TH align=center>ID</TH>");
    output.append("<TH align=center>Raw phone number</TH>");
    output.append("<TH align=center>Pretty formatting</TH>");
    output.append("<TH align=center>International format</TH>");

    int phoneNumberId = 0;
    StringTokenizer tokenizer = new StringTokenizer(fileContents, ",");
    while (tokenizer.hasMoreTokens()) {
      String numberStr = tokenizer.nextToken();
      phoneNumberId++;
      output.append("<TR>");
      output.append("<TD align=center>").append(phoneNumberId).append(" </TD> \n");
      output
          .append("<TD align=center>")
          .append(StringEscapeUtils.escapeHtml(numberStr))
          .append(" </TD> \n");
      try {
        PhoneNumber number = phoneUtil.parseAndKeepRawInput(numberStr, defaultCountry);
        boolean isNumberValid = phoneUtil.isValidNumber(number);
        String prettyFormat =
            isNumberValid ? phoneUtil.formatInOriginalFormat(number, defaultCountry) : "invalid";
        String internationalFormat =
            isNumberValid ? phoneUtil.format(number, PhoneNumberFormat.INTERNATIONAL) : "invalid";

        output
            .append("<TD align=center>")
            .append(StringEscapeUtils.escapeHtml(prettyFormat))
            .append(" </TD> \n");
        output
            .append("<TD align=center>")
            .append(StringEscapeUtils.escapeHtml(internationalFormat))
            .append(" </TD> \n");
      } catch (NumberParseException e) {
        output
            .append("<TD align=center colspan=2>")
            .append(StringEscapeUtils.escapeHtml(e.toString()))
            .append(" </TD> \n");
      }
      output.append("</TR>");
    }
    output.append("</BODY></HTML>");
    return output;
  }

  private void appendLine(String title, String data, StringBuilder output) {
    output.append("<TR>");
    output.append("<TH>").append(title).append("</TH>");
    output.append("<TD>").append(data.length() > 0 ? data : "&nbsp;").append("</TD>");
    output.append("</TR>");
  }

  /** Returns a stable URL pointing to the result page for the given input. */
  private String getPermaLinkURL(
      String phoneNumber, String defaultCountry, Locale geocodingLocale, boolean absoluteURL) {
    // If absoluteURL is false, generate a relative path. Otherwise, produce an absolute URL.
    StringBuilder permaLink =
        new StringBuilder(
            absoluteURL
                ? "http://libphonenumber.appspot.com/phonenumberparser"
                : "/phonenumberparser");
    try {
      permaLink.append(
          "?number=" + URLEncoder.encode(phoneNumber != null ? phoneNumber : "", UTF_8.name()));
      if (defaultCountry != null && !defaultCountry.isEmpty()) {
        permaLink.append("&country=" + URLEncoder.encode(defaultCountry, UTF_8.name()));
      }
      if (!geocodingLocale.getLanguage().equals(ENGLISH.getLanguage())
          || !geocodingLocale.getCountry().isEmpty()) {
        permaLink.append(
            "&geocodingLocale=" + URLEncoder.encode(geocodingLocale.toLanguageTag(), UTF_8.name()));
      }
    } catch (UnsupportedEncodingException e) {
      // UTF-8 is guaranteed in Java, so this should be impossible.
      throw new AssertionError(e);
    }
    return permaLink.toString();
  }

  private static final String NEW_ISSUE_BASE_URL =
      "https://issuetracker.google.com/issues/new?component=192347&title=";

  /** Returns a link to create a new github issue with the relevant information. */
  private String getNewIssueLink(
      String phoneNumber, String defaultCountry, Locale geocodingLocale) {
    boolean hasDefaultCountry = !defaultCountry.isEmpty() && defaultCountry != "ZZ";
    String issueTitle =
        "Validation issue with "
            + phoneNumber
            + (hasDefaultCountry ? " (" + defaultCountry + ")" : "");

    String newIssueLink = NEW_ISSUE_BASE_URL;
    try {
      newIssueLink += URLEncoder.encode(issueTitle, UTF_8.name());
    } catch (UnsupportedEncodingException e) {
      // UTF-8 is guaranteed in Java, so this should be impossible.
      throw new AssertionError(e);
    }
    return newIssueLink;
  }

  /**
   * The defaultCountry here is used for parsing phoneNumber. The geocodingLocale is used to specify
   * the language used for displaying the area descriptions generated from phone number geocoding.
   */
  private StringBuilder getOutputForSingleNumber(
      String phoneNumber, String defaultCountry, Locale geocodingLocale) {
    StringBuilder output = new StringBuilder("<HTML><HEAD>");
    output.append("<LINK type=\"text/css\" rel=\"stylesheet\" href=\"/stylesheets/main.css\" />");
    output.append("</HEAD>");
    output.append("<BODY>");
    output.append("Phone Number entered: " + StringEscapeUtils.escapeHtml(phoneNumber) + "<BR>");
    output.append(
        "defaultCountry entered: " + StringEscapeUtils.escapeHtml(defaultCountry) + "<BR>");
    output.append(
        "Language entered: "
            + StringEscapeUtils.escapeHtml(geocodingLocale.toLanguageTag())
            + "<BR>");
    try {
      PhoneNumber number = phoneUtil.parseAndKeepRawInput(phoneNumber, defaultCountry);
      output.append("<DIV>");
      output.append("<TABLE border=1>");
      output.append("<TR><TD colspan=2>Parsing Result (parseAndKeepRawInput())</TD></TR>");

      appendLine("country_code", Integer.toString(number.getCountryCode()), output);
      appendLine("national_number", Long.toString(number.getNationalNumber()), output);
      appendLine("extension", number.getExtension(), output);
      appendLine("country_code_source", number.getCountryCodeSource().toString(), output);
      appendLine("italian_leading_zero", Boolean.toString(number.isItalianLeadingZero()), output);
      appendLine("raw_input", number.getRawInput(), output);
      output.append("</TABLE>");
      output.append("</DIV>");

      boolean isPossible = phoneUtil.isPossibleNumber(number);
      boolean isNumberValid = phoneUtil.isValidNumber(number);
      PhoneNumberType numberType = phoneUtil.getNumberType(number);
      boolean hasDefaultCountry = !defaultCountry.isEmpty() && defaultCountry != "ZZ";

      output.append("<DIV>");
      output.append("<TABLE border=1>");
      output.append("<TR><TD colspan=2>Validation Results</TD></TR>");
      appendLine("Result from isPossibleNumber()", Boolean.toString(isPossible), output);
      if (isPossible) {
        if (phoneUtil.isPossibleNumberWithReason(number)
            == ValidationResult.IS_POSSIBLE_LOCAL_ONLY) {
          appendLine("Result from isPossibleNumberWithReason()", "IS_POSSIBLE_LOCAL_ONLY", output);
          output.append(
              "<TR><TD colspan=2>Number is considered invalid as it is "
                  + "not a possible national number.</TD></TR>");
        } else {
          appendLine("Result from isValidNumber()", Boolean.toString(isNumberValid), output);
          if (isNumberValid && hasDefaultCountry) {
              appendLine(
                  "Result from isValidNumberForRegion()",
                  Boolean.toString(phoneUtil.isValidNumberForRegion(number, defaultCountry)),
                  output);
          }
          String region = phoneUtil.getRegionCodeForNumber(number);
          appendLine("Phone Number region", region == null ? "" : region, output);
          appendLine("Result from getNumberType()", numberType.toString(), output);
        }
      } else {
        appendLine(
            "Result from isPossibleNumberWithReason()",
            phoneUtil.isPossibleNumberWithReason(number).toString(),
            output);
        output.append(
            "<TR><TD colspan=2>Note: Numbers that are not possible have type UNKNOWN,"
                + " an unknown region, and are considered invalid.</TD></TR>");
      }
      output.append("</TABLE>");
      output.append("</DIV>");

      if (!isNumberValid) {
        output.append("<DIV>");
        output.append("<TABLE border=1>");
        output.append("<TR><TD colspan=2>Short Number Results</TD></TR>");
        boolean isPossibleShort = shortInfo.isPossibleShortNumber(number);
        appendLine(
            "Result from isPossibleShortNumber()", Boolean.toString(isPossibleShort), output);
        if (isPossibleShort) {
          appendLine(
              "Result from isValidShortNumber()",
              Boolean.toString(shortInfo.isValidShortNumber(number)),
              output);
          if (hasDefaultCountry) {
            boolean isPossibleShortForRegion =
                shortInfo.isPossibleShortNumberForRegion(number, defaultCountry);
            appendLine(
                "Result from isPossibleShortNumberForRegion()",
                Boolean.toString(isPossibleShortForRegion),
                output);
            if (isPossibleShortForRegion) {
              appendLine(
                  "Result from isValidShortNumberForRegion()",
                  Boolean.toString(shortInfo.isValidShortNumberForRegion(number, defaultCountry)),
                  output);
            }
          }
        }
        output.append("</TABLE>");
        output.append("</DIV>");
      }

      output.append("<DIV>");
      output.append("<TABLE border=1>");
      output.append("<TR><TD colspan=2>Formatting Results</TD></TR>");
      appendLine(
          "E164 format",
          isNumberValid ? phoneUtil.format(number, PhoneNumberFormat.E164) : "invalid",
          output);
      appendLine(
          "Original format", phoneUtil.formatInOriginalFormat(number, defaultCountry), output);
      appendLine("National format", phoneUtil.format(number, PhoneNumberFormat.NATIONAL), output);
      appendLine(
          "International format",
          isNumberValid ? phoneUtil.format(number, PhoneNumberFormat.INTERNATIONAL) : "invalid",
          output);
      appendLine(
          "Out-of-country format from US",
          isNumberValid ? phoneUtil.formatOutOfCountryCallingNumber(number, "US") : "invalid",
          output);
      appendLine(
          "Out-of-country format from CH",
          isNumberValid ? phoneUtil.formatOutOfCountryCallingNumber(number, "CH") : "invalid",
          output);
      output.append("</TABLE>");
      output.append("</DIV>");

      AsYouTypeFormatter formatter = phoneUtil.getAsYouTypeFormatter(defaultCountry);
      int rawNumberLength = phoneNumber.length();
      output.append("<DIV>");
      output.append("<TABLE border=1>");
      output.append("<TR><TD colspan=2>AsYouTypeFormatter Results</TD></TR>");
      for (int i = 0; i < rawNumberLength; i++) {
        // Note this doesn't handle supplementary characters, but it shouldn't be a big deal as
        // there are no dial-pad characters in the supplementary range.
        char inputChar = phoneNumber.charAt(i);
        appendLine(
            "Char entered: '" + inputChar + "' Output: ", formatter.inputDigit(inputChar), output);
      }
      output.append("</TABLE>");
      output.append("</DIV>");

      if (isNumberValid) {
        output.append("<DIV>");
        output.append("<TABLE border=1>");
        output.append("<TR><TD colspan=2>PhoneNumberOfflineGeocoder Results</TD></TR>");
        appendLine(
            "Location",
            PhoneNumberOfflineGeocoder.getInstance()
                .getDescriptionForNumber(number, geocodingLocale),
            output);
        output.append("</TABLE>");
        output.append("</DIV>");

        output.append("<DIV>");
        output.append("<TABLE border=1>");
        output.append("<TR><TD colspan=2>PhoneNumberToTimeZonesMapper Results</TD></TR>");
        appendLine(
            "Time zone(s)",
            PhoneNumberToTimeZonesMapper.getInstance().getTimeZonesForNumber(number).toString(),
            output);
        output.append("</TABLE>");
        output.append("</DIV>");

        if (numberType == PhoneNumberType.MOBILE
            || numberType == PhoneNumberType.FIXED_LINE_OR_MOBILE
            || numberType == PhoneNumberType.PAGER) {
          output.append("<DIV>");
          output.append("<TABLE border=1>");
          output.append("<TR><TD colspan=2>PhoneNumberToCarrierMapper Results</TD></TR>");
          appendLine(
              "Carrier",
              PhoneNumberToCarrierMapper.getInstance().getNameForNumber(number, geocodingLocale),
              output);
          output.append("</TABLE>");
          output.append("</DIV>");
        }
      }

      String newIssueLink = getNewIssueLink(phoneNumber, defaultCountry, geocodingLocale);
      String guidelinesLink =
          "https://github.com/google/libphonenumber/blob/master/CONTRIBUTING.md";
      output.append(
          "<b style=\"color:red\">File an issue</b>: by clicking on "
              + "<a target=\"_blank\" href=\""
              + newIssueLink
              + "\">this link</a>, I confirm that I "
              + "have read the <a target=\"_blank\" href=\""
              + guidelinesLink
              + "\">contributor's guidelines</a>.");
    } catch (NumberParseException e) {
      output.append(StringEscapeUtils.escapeHtml(e.toString()));
    }
    output.append("</BODY></HTML>");
    return output;
  }
}