| 1 |
/* |
| 2 |
* Copyright (C) 2020 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 |
package com.google.phonenumbers; |
| 17 |
|
| 18 |
import com.google.common.base.CharMatcher; |
| 19 |
import com.google.common.collect.ImmutableList; |
| 20 |
import com.google.common.collect.ImmutableSet; |
| 21 |
import com.google.phonenumbers.migrator.MigrationEntry; |
| 22 |
import com.google.phonenumbers.migrator.MigrationFactory; |
| 23 |
import com.google.phonenumbers.migrator.MigrationJob; |
| 24 |
import com.google.phonenumbers.migrator.MigrationResult; |
| 25 |
import org.apache.commons.fileupload.FileItemIterator; |
| 26 |
import org.apache.commons.fileupload.FileItemStream; |
| 27 |
import org.apache.commons.fileupload.FileUploadException; |
| 28 |
import org.apache.commons.fileupload.servlet.ServletFileUpload; |
| 29 |
import org.apache.commons.fileupload.util.Streams; |
| 30 |
import org.apache.commons.io.IOUtils; |
| 31 |
|
| 32 |
import javax.servlet.ServletException; |
| 33 |
import javax.servlet.annotation.WebServlet; |
| 34 |
import javax.servlet.http.HttpServlet; |
| 35 |
import javax.servlet.http.HttpServletRequest; |
| 36 |
import javax.servlet.http.HttpServletResponse; |
| 37 |
import java.io.IOException; |
| 38 |
import java.io.InputStream; |
| 39 |
import java.io.OutputStream; |
| 40 |
import java.util.StringTokenizer; |
| 41 |
|
| 42 |
@WebServlet(name = "Migrate", value = "/migrate") |
| 43 |
public class ServletMain extends HttpServlet { |
| 44 |
|
| 45 |
public static final int MAX_UPLOAD_SIZE = 50000; |
| 46 |
/** |
| 47 |
* Countries with large valid number ranges cannot be migrated using the web application due to request timeouts. The |
| 48 |
* command line tool must be used in such cases. |
| 49 |
*/ |
| 50 |
// TODO: add loading spinner and longer request timeouts to UI to allow for migrations of given country codes |
| 51 |
public static final ImmutableSet<String> LARGE_COUNTRY_RANGES = ImmutableSet.of( |
| 52 |
"1", // US/Canada -- 9.8MB |
| 53 |
"86", // China -- 16.1MB |
| 54 |
"55", // Brazil -- 3.4MB |
| 55 |
"61" // Australia -- 12.8MB |
| 56 |
); |
| 57 |
|
| 58 |
/** |
| 59 |
* Retrieves the form data for either a single number migration or a file migration from the index.jsp file and calls |
| 60 |
* the relevant method to perform the migration |
| 61 |
*/ |
| 62 |
@Override |
| 63 |
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { |
| 64 |
ServletFileUpload upload = new ServletFileUpload(); |
| 65 |
String countryCode = ""; |
| 66 |
String number = ""; |
| 67 |
String file = ""; |
| 68 |
String fileName = ""; |
| 69 |
String customRecipe = ""; |
| 70 |
|
| 71 |
try { |
| 72 |
upload.setSizeMax(MAX_UPLOAD_SIZE); |
| 73 |
FileItemIterator iterator = upload.getItemIterator(req); |
| 74 |
while (iterator.hasNext()) { |
| 75 |
FileItemStream item = iterator.next(); |
| 76 |
InputStream in = item.openStream(); |
| 77 |
|
| 78 |
if (item.isFormField() && (item.getFieldName().equals("numberCountryCode") |
| 79 |
|| item.getFieldName().equals("fileCountryCode"))) { |
| 80 |
countryCode = Streams.asString(in); |
| 81 |
|
| 82 |
} else if (item.isFormField() && item.getFieldName().equals("number")) { |
| 83 |
number = Streams.asString(in); |
| 84 |
|
| 85 |
} else if (item.getFieldName().equals("file")) { |
| 86 |
fileName = item.getName(); |
| 87 |
try { |
| 88 |
file = IOUtils.toString(in); |
| 89 |
} finally { |
| 90 |
IOUtils.closeQuietly(in); |
| 91 |
} |
| 92 |
|
| 93 |
} else { |
| 94 |
try { |
| 95 |
customRecipe = IOUtils.toString(in); |
| 96 |
} finally { |
| 97 |
IOUtils.closeQuietly(in); |
| 98 |
} |
| 99 |
} |
| 100 |
} |
| 101 |
} catch (FileUploadException e) { |
| 102 |
e.printStackTrace(); |
| 103 |
} |
| 104 |
|
| 105 |
if (!number.isEmpty() && !countryCode.isEmpty()) { |
| 106 |
/* |
| 107 |
number and country code are being set again to allow users to see their inputs after the http request has |
| 108 |
re-rendered the page. |
| 109 |
*/ |
| 110 |
req.setAttribute("number", number); |
| 111 |
req.setAttribute("numberCountryCode", countryCode); |
| 112 |
|
| 113 |
handleSingleNumberMigration(req, resp, number, countryCode, customRecipe); |
| 114 |
|
| 115 |
} else if (!file.isEmpty() && !countryCode.isEmpty()) { |
| 116 |
ImmutableList.Builder<String> numbersFromFile = ImmutableList.builder(); |
| 117 |
StringTokenizer tokenizer = new StringTokenizer(file, "\n"); |
| 118 |
while (tokenizer.hasMoreTokens()) { |
| 119 |
numbersFromFile.add(tokenizer.nextToken()); |
| 120 |
} |
| 121 |
|
| 122 |
handleFileMigration(req, resp, numbersFromFile.build(), countryCode, customRecipe, fileName); |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
/** Retrieves the form data for the numbers after a file migration and downloads them as a text file. */ |
| 127 |
@Override |
| 128 |
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { |
| 129 |
CharMatcher matcher = CharMatcher.anyOf("/\\"); |
| 130 |
String fileName = "+" + matcher.removeFrom(req.getParameter("countryCode")) + "_Migration_ " + |
| 131 |
matcher.removeFrom(req.getParameter("fileName")); |
| 132 |
String fileContent = req.getParameter("fileContent"); |
| 133 |
|
| 134 |
resp.setContentType("text/plain"); |
| 135 |
resp.setHeader("Content-Disposition", "attachment; filename=" + fileName); |
| 136 |
try { |
| 137 |
OutputStream outputStream = resp.getOutputStream(); |
| 138 |
outputStream.write(fileContent.getBytes()); |
| 139 |
outputStream.flush(); |
| 140 |
outputStream.close(); |
| 141 |
} catch (IOException e) { |
| 142 |
req.setAttribute("fileError", e.getMessage()); |
| 143 |
req.getRequestDispatcher("index.jsp").forward(req, resp); |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* Performs a single number migration of a given number and country code and sends the details of the migration to the |
| 149 |
* jsp file which outputs it to the user. |
| 150 |
* |
| 151 |
* @throws RuntimeException, which can be any of the given exceptions when going through the process of creating a {@link |
| 152 |
* MigrationJob} and running a migration which includes IllegalArgumentExceptions. |
| 153 |
*/ |
| 154 |
public void handleSingleNumberMigration(HttpServletRequest req, |
| 155 |
HttpServletResponse resp, |
| 156 |
String number, |
| 157 |
String countryCode, |
| 158 |
String customRecipe) |
| 159 |
throws ServletException, IOException { |
| 160 |
|
| 161 |
if (customRecipe.isEmpty() && LARGE_COUNTRY_RANGES.contains(countryCode)) { |
| 162 |
req.setAttribute("numberError", "'+" + countryCode + "' migrations cannot be performed using this web" + |
| 163 |
" application, please follow the documentation link above to use the command line tool instead."); |
| 164 |
req.getRequestDispatcher("index.jsp").forward(req, resp); |
| 165 |
return; |
| 166 |
} |
| 167 |
|
| 168 |
MigrationJob job; |
| 169 |
try { |
| 170 |
if (!customRecipe.isEmpty()) { |
| 171 |
job = MigrationFactory.createCustomRecipeMigration(number, countryCode, MigrationFactory |
| 172 |
.importRecipes(IOUtils.toInputStream(customRecipe))); |
| 173 |
} else { |
| 174 |
job = MigrationFactory.createMigration(number, countryCode); |
| 175 |
} |
| 176 |
MigrationJob.MigrationReport report = job.getMigrationReportForCountry(); |
| 177 |
if (report.getValidMigrations().size() == 1) { |
| 178 |
req.setAttribute("validMigration", report.getValidMigrations().get(0).getMigratedNumber()); |
| 179 |
} else if (report.getInvalidMigrations().size() == 1) { |
| 180 |
req.setAttribute("invalidMigration", report.getInvalidMigrations().get(0).getMigratedNumber()); |
| 181 |
} else if (report.getValidUntouchedEntries().size() == 1) { |
| 182 |
req.setAttribute("alreadyValidNumber", report.getValidUntouchedEntries().get(0).getSanitizedNumber()); |
| 183 |
} |
| 184 |
|
| 185 |
} catch (RuntimeException e) { |
| 186 |
req.setAttribute("numberError", e.getMessage()); |
| 187 |
|
| 188 |
} finally { |
| 189 |
req.getRequestDispatcher("index.jsp").forward(req, resp); |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
/** |
| 194 |
* Performs a file migration of a given numbersList and country code and sends the details of the migration to the |
| 195 |
* jsp file which outputs it to the user. |
| 196 |
* |
| 197 |
* @throws RuntimeException, which can be any of the given exceptions when going through the process of creating a {@link |
| 198 |
* MigrationJob} and running a migration which includes IllegalArgumentExceptions. |
| 199 |
*/ |
| 200 |
public void handleFileMigration(HttpServletRequest req, HttpServletResponse resp, |
| 201 |
ImmutableList<String> numbersList, |
| 202 |
String countryCode, |
| 203 |
String customRecipe, |
| 204 |
String fileName) |
| 205 |
throws ServletException, IOException { |
| 206 |
|
| 207 |
if (customRecipe.isEmpty() && LARGE_COUNTRY_RANGES.contains(countryCode)) { |
| 208 |
req.setAttribute("fileError", "'+" + countryCode + "' migrations cannot be performed using this web" + |
| 209 |
" application, please follow the documentation link above to use the command line tool instead."); |
| 210 |
req.getRequestDispatcher("index.jsp").forward(req, resp); |
| 211 |
return; |
| 212 |
} |
| 213 |
|
| 214 |
MigrationJob job; |
| 215 |
try { |
| 216 |
if (!customRecipe.isEmpty()) { |
| 217 |
job = MigrationFactory.createCustomRecipeMigration(numbersList, countryCode, MigrationFactory |
| 218 |
.importRecipes(IOUtils.toInputStream(customRecipe))); |
| 219 |
} else { |
| 220 |
job = MigrationFactory.createMigration(numbersList, countryCode, /* exportInvalidMigrations= */ false); |
| 221 |
} |
| 222 |
|
| 223 |
MigrationJob.MigrationReport report = job.getMigrationReportForCountry(); |
| 224 |
// List converted into a Set to allow for constant time contains() method below |
| 225 |
ImmutableSet<MigrationEntry> validUntouchedEntriesSet = ImmutableSet.copyOf(report.getValidUntouchedEntries()); |
| 226 |
|
| 227 |
req.setAttribute("fileName", fileName); |
| 228 |
req.setAttribute("fileContent", report.toString()); |
| 229 |
req.setAttribute("fileCountryCode", report.getCountryCode()); |
| 230 |
req.setAttribute("validMigrations", report.getValidMigrations()); |
| 231 |
req.setAttribute("invalidMigrations", report.getInvalidMigrations()); |
| 232 |
req.setAttribute("validUntouchedNumbers", report.getValidUntouchedEntries()); |
| 233 |
req.setAttribute("invalidUntouchedNumbers", report.getUntouchedEntries().stream() |
| 234 |
.filter(entry -> !validUntouchedEntriesSet.contains(entry)).collect(ImmutableList.toImmutableList())); |
| 235 |
|
| 236 |
} catch (RuntimeException e) { |
| 237 |
req.setAttribute("fileError", e.getMessage()); |
| 238 |
|
| 239 |
} finally { |
| 240 |
req.getRequestDispatcher("index.jsp").forward(req, resp); |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
/** |
| 245 |
* Takes a list of {@link MigrationEntry}'s and returns a list with the corresponding strings to output for each value |
| 246 |
* in the given entryList |
| 247 |
*/ |
| 248 |
public static ImmutableList<String> getMigrationEntryOutputList(ImmutableList<MigrationEntry> entryList) { |
| 249 |
if (entryList == null) return ImmutableList.of(); |
| 250 |
return entryList.stream().map(MigrationEntry::getOriginalNumber).collect(ImmutableList.toImmutableList()); |
| 251 |
} |
| 252 |
|
| 253 |
/** |
| 254 |
* Takes a list of {@link MigrationResult}'s and returns a list with the corresponding strings to output for each value |
| 255 |
* in the given resultList |
| 256 |
*/ |
| 257 |
public static ImmutableList<String> getMigrationResultOutputList(ImmutableList<MigrationResult> resultList) { |
| 258 |
if (resultList == null) return ImmutableList.of(); |
| 259 |
return resultList.stream().map(MigrationResult::toString).collect(ImmutableList.toImmutableList()); |
| 260 |
} |
| 261 |
} |