Browse Source

Merge pull request #935 from dlyongemallo/demo_permalink

add permalink from result page to come back to that result
pull/942/head
David Yonge-Mallo 10 years ago
parent
commit
6adb169fcf
1 changed files with 77 additions and 25 deletions
  1. +77
    -25
      java/demo/src/com/google/phonenumbers/PhoneNumberParserServlet.java

+ 77
- 25
java/demo/src/com/google/phonenumbers/PhoneNumberParserServlet.java View File

@ -19,6 +19,7 @@
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;
@ -103,16 +104,40 @@ public class PhoneNumberParserServlet extends HttpServlet {
resp.setContentType("text/html");
resp.setCharacterEncoding(UTF_8.name());
if (fileContents.length() == 0) {
resp.getWriter().println(
getOutputForSingleNumber(phoneNumber, defaultCountry, languageCode, regionCode));
// 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));
} 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.getWriter().println(
getOutputForSingleNumber(phoneNumber, defaultCountry, geocodingLocale));
}
private StringBuilder getOutputForFile(String defaultCountry, String fileContents) {
StringBuilder output = new StringBuilder();
output.append("<HTML><HEAD><TITLE>Results generated from phone numbers in the file provided:"
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>");
@ -160,19 +185,43 @@ public class PhoneNumberParserServlet extends HttpServlet {
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) {
StringBuilder permaLink = new StringBuilder(
"http://libphonenumber.appspot.com/phonenumberparser");
try {
permaLink.append("?number=" + URLEncoder.encode(phoneNumber, UTF_8.name()));
if (!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();
}
/**
* Returns a link to create a new github issue with the relevant information.
*/
private String getNewIssueLink(String phoneNumber, String defaultCountry)
throws UnsupportedEncodingException {
private String getNewIssueLink(
String phoneNumber, String defaultCountry, Locale geocodingLocale) {
boolean hasDefaultCountry = !defaultCountry.isEmpty() && defaultCountry != "ZZ";
String issueTitle = "Validation issue with " + phoneNumber
+ (hasDefaultCountry ? " (" + defaultCountry + ")" : "");
// Issue template. This must be kept in sync with the template in
// https://github.com/googlei18n/libphonenumber/blob/master/CONTRIBUTING.md.
StringBuilder issueTemplate = new StringBuilder();
issueTemplate.append("Please read the \"guidelines for contributing\" (linked above) and fill "
StringBuilder issueTemplate = new StringBuilder(
"Please read the \"guidelines for contributing\" (linked above) and fill "
+ "in the template below.\n\n");
issueTemplate.append("Country/region affected (e.g., \"US\"): ")
.append(defaultCountry).append("\n\n");
@ -188,20 +237,27 @@ public class PhoneNumberParserServlet extends HttpServlet {
"Supporting evidence (for example, national numbering plan, announcement from mobile "
+ "carrier, news article): **IMPORTANT - anything posted here is made public. "
+ "Read the guidelines first!** \n\n");
return "https://github.com/googlei18n/libphonenumber/issues/new?title="
+ URLEncoder.encode(issueTitle, UTF_8.name()) + "&body="
issueTemplate.append("[link to demo]("
+ getPermaLinkURL(phoneNumber, defaultCountry, geocodingLocale)
+ ")\n\n");
String newIssueLink = "https://github.com/googlei18n/libphonenumber/issues/new?title=";
try {
newIssueLink += URLEncoder.encode(issueTitle, UTF_8.name()) + "&body="
+ URLEncoder.encode(issueTemplate.toString(), 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 languageCode and regionCode are
* used to specify the language used for displaying the area descriptions generated from phone
* number geocoding.
* 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, String languageCode, String regionCode) {
StringBuilder output = new StringBuilder();
output.append("<HTML><HEAD>");
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>");
@ -209,9 +265,8 @@ public class PhoneNumberParserServlet extends HttpServlet {
output.append("Phone Number entered: " + StringEscapeUtils.escapeHtml(phoneNumber) + "<BR>");
output.append("defaultCountry entered: " + StringEscapeUtils.escapeHtml(defaultCountry)
+ "<BR>");
output.append("Language entered: " + StringEscapeUtils.escapeHtml(languageCode) +
(regionCode.isEmpty() ? "" : " (" + StringEscapeUtils.escapeHtml(regionCode) + ")")
+ "<BR>");
output.append("Language entered: "
+ StringEscapeUtils.escapeHtml(geocodingLocale.toLanguageTag()) + "<BR>");
try {
PhoneNumber number = phoneUtil.parseAndKeepRawInput(phoneNumber, defaultCountry);
output.append("<DIV>");
@ -330,7 +385,7 @@ public class PhoneNumberParserServlet extends HttpServlet {
appendLine(
"Location",
PhoneNumberOfflineGeocoder.getInstance().getDescriptionForNumber(
number, new Locale(languageCode, regionCode)),
number, geocodingLocale),
output);
output.append("</TABLE>");
output.append("</DIV>");
@ -353,23 +408,20 @@ public class PhoneNumberParserServlet extends HttpServlet {
output.append("<TR><TD colspan=2>PhoneNumberToCarrierMapper Results</TD></TR>");
appendLine(
"Carrier",
PhoneNumberToCarrierMapper.getInstance().getNameForNumber(
number, new Locale(languageCode, regionCode)),
PhoneNumberToCarrierMapper.getInstance().getNameForNumber(number, geocodingLocale),
output);
output.append("</TABLE>");
output.append("</DIV>");
}
}
String newIssueLink = getNewIssueLink(phoneNumber, defaultCountry);
String newIssueLink = getNewIssueLink(phoneNumber, defaultCountry, geocodingLocale);
String guidelinesLink =
"https://github.com/googlei18n/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(UnsupportedEncodingException e) {
output.append(StringEscapeUtils.escapeHtml(e.toString()));
} catch (NumberParseException e) {
output.append(StringEscapeUtils.escapeHtml(e.toString()));
}


Loading…
Cancel
Save