- Timestamp:
- 2020-11-22T22:54:01+01:00 (4 years ago)
- Location:
- trunk/src/org/openstreetmap/josm
- Files:
-
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/actions/downloadtasks/DownloadTask.java
r13927 r17330 4 4 import java.net.URL; 5 5 import java.util.List; 6 import java.util.Objects; 6 7 import java.util.concurrent.Future; 8 import java.util.stream.Collectors; 7 9 8 10 import org.openstreetmap.josm.data.Bounds; 9 11 import org.openstreetmap.josm.gui.progress.NullProgressMonitor; 10 12 import org.openstreetmap.josm.gui.progress.ProgressMonitor; 13 import org.openstreetmap.josm.tools.ExceptionUtil; 11 14 12 15 /** … … 116 119 117 120 /** 121 * Replies the error messages of the task. Empty list, if no error messages are available. 122 * 123 * @return the list of error messages 124 * @since 17330 125 */ 126 default List<String> getErrorMessages() { 127 return getErrorObjects().stream().map(o -> { 128 if (o instanceof String) { 129 return (String) o; 130 } else if (o instanceof Exception) { 131 return ExceptionUtil.explainException((Exception) o).replace("<html>", "").replace("</html>", ""); 132 } else { 133 return (String) null; 134 } 135 }).filter(Objects::nonNull).collect(Collectors.toList()); 136 } 137 138 /** 118 139 * Cancels the asynchronous download task. 119 140 * -
trunk/src/org/openstreetmap/josm/actions/downloadtasks/DownloadTaskList.java
r16770 r17330 9 9 import java.awt.geom.Area; 10 10 import java.awt.geom.Rectangle2D; 11 import java.util.ArrayList;12 11 import java.util.Collection; 13 12 import java.util.LinkedHashSet; … … 35 34 import org.openstreetmap.josm.gui.progress.ProgressMonitor; 36 35 import org.openstreetmap.josm.gui.util.GuiHelper; 37 import org.openstreetmap.josm.tools.ExceptionUtil;38 36 import org.openstreetmap.josm.tools.ImageProvider; 39 37 import org.openstreetmap.josm.tools.Logging; … … 247 245 } 248 246 } 249 Set< Object> errors = tasks.stream().flatMap(t -> t.getErrorObjects().stream()).collect(Collectors.toSet());247 Set<String> errors = tasks.stream().flatMap(t -> t.getErrorMessages().stream()).collect(Collectors.toSet()); 250 248 if (!errors.isEmpty()) { 251 final Collection<String> items = new ArrayList<>();252 for (Object error : errors) {253 if (error instanceof String) {254 items.add((String) error);255 } else if (error instanceof Exception) {256 items.add(ExceptionUtil.explainException((Exception) error));257 }258 }259 260 249 GuiHelper.runInEDT(() -> { 261 if ( items.size() == 1 && PostDownloadHandler.isNoDataErrorMessage(items.iterator().next())) {262 new Notification( items.iterator().next()).setIcon(JOptionPane.WARNING_MESSAGE).show();250 if (errors.size() == 1 && PostDownloadHandler.isNoDataErrorMessage(errors.iterator().next())) { 251 new Notification(errors.iterator().next()).setIcon(JOptionPane.WARNING_MESSAGE).show(); 263 252 } else { 264 253 JOptionPane.showMessageDialog(MainApplication.getMainFrame(), "<html>" 265 254 + tr("The following errors occurred during mass download: {0}", 266 Utils.joinAsHtmlUnorderedList( items)) + "</html>",255 Utils.joinAsHtmlUnorderedList(errors)) + "</html>", 267 256 tr("Errors during download"), JOptionPane.ERROR_MESSAGE); 268 257 return; -
trunk/src/org/openstreetmap/josm/actions/downloadtasks/PostDownloadHandler.java
r15358 r17330 5 5 6 6 import java.awt.GraphicsEnvironment; 7 import java.util.ArrayList;8 7 import java.util.Collection; 9 8 import java.util.HashSet; … … 22 21 import org.openstreetmap.josm.gui.Notification; 23 22 import org.openstreetmap.josm.gui.util.GuiHelper; 24 import org.openstreetmap.josm.tools.ExceptionUtil;25 23 import org.openstreetmap.josm.tools.Logging; 26 24 import org.openstreetmap.josm.tools.Utils; … … 126 124 // multiple error object? prepare a HTML list 127 125 // 128 if (!errors.isEmpty()) { 129 final Collection<String> items = new ArrayList<>(); 130 for (Object error : errors) { 131 if (error instanceof String) { 132 items.add((String) error); 133 } else if (error instanceof Exception) { 134 items.add(ExceptionUtil.explainException((Exception) error)); 135 } 136 } 137 138 if (!GraphicsEnvironment.isHeadless()) { 139 SwingUtilities.invokeLater(() -> JOptionPane.showMessageDialog( 140 MainApplication.getMainFrame(), 141 "<html>"+Utils.joinAsHtmlUnorderedList(items)+"</html>", 142 tr("Errors during download"), 143 JOptionPane.ERROR_MESSAGE)); 144 } 126 if (!errors.isEmpty() && !GraphicsEnvironment.isHeadless()) { 127 SwingUtilities.invokeLater(() -> JOptionPane.showMessageDialog( 128 MainApplication.getMainFrame(), 129 "<html>"+Utils.joinAsHtmlUnorderedList(task.getErrorMessages())+"</html>", 130 tr("Errors during download"), 131 JOptionPane.ERROR_MESSAGE)); 145 132 } 146 133 } -
trunk/src/org/openstreetmap/josm/io/remotecontrol/RequestProcessor.java
r16913 r17330 43 43 import org.openstreetmap.josm.io.remotecontrol.handler.RequestHandler.RequestHandlerErrorException; 44 44 import org.openstreetmap.josm.io.remotecontrol.handler.RequestHandler.RequestHandlerForbiddenException; 45 import org.openstreetmap.josm.io.remotecontrol.handler.RequestHandler.RequestHandlerOsmApiException; 45 46 import org.openstreetmap.josm.io.remotecontrol.handler.VersionHandler; 46 47 import org.openstreetmap.josm.tools.Logging; … … 185 186 String get = in.readLine(); 186 187 if (get == null) { 187 send Error(out);188 sendInternalError(out, null); 188 189 return; 189 190 } … … 192 193 StringTokenizer st = new StringTokenizer(get); 193 194 if (!st.hasMoreTokens()) { 194 send Error(out);195 sendInternalError(out, null); 195 196 return; 196 197 } 197 198 String method = st.nextToken(); 198 199 if (!st.hasMoreTokens()) { 199 send Error(out);200 sendInternalError(out, null); 200 201 return; 201 202 } … … 252 253 String help = "No command specified! The following commands are available:<ul>" + usage 253 254 + "</ul>" + "See <a href=\""+websiteDoc+"\">"+websiteDoc+"</a> for complete documentation."; 254 sendHeader(out, "400 Bad Request", "text/html", true); 255 out.write(String.format( 256 RESPONSE_TEMPLATE, 257 "<title>Bad Request</title>", 258 "<h1>HTTP Error 400: Bad Request</h1>" + 259 "<p>" + help + "</p>")); 260 out.flush(); 255 sendBadRequest(out, help); 261 256 } else { 262 257 // create handler object … … 273 268 out.write(handler.getContent()); 274 269 out.flush(); 270 } catch (RequestHandlerOsmApiException ex) { 271 Logging.debug(ex); 272 sendBadGateway(out, ex.getMessage()); 275 273 } catch (RequestHandlerErrorException ex) { 276 274 Logging.debug(ex); 277 send Error(out);275 sendInternalError(out, ex.getMessage()); 278 276 } catch (RequestHandlerBadRequestException ex) { 279 277 Logging.debug(ex); … … 289 287 Logging.error(e); 290 288 try { 291 send Error(out);289 sendInternalError(out, e.getMessage()); 292 290 } catch (IOException e1) { 293 291 Logging.warn(e1); … … 302 300 } 303 301 304 /** 305 * Sends a 500 error: server error 306 * 307 * @param out 308 * The writer where the error is written 309 * @throws IOException 310 * If the error can not be written 311 */ 312 private static void sendError(Writer out) throws IOException { 313 sendHeader(out, "500 Internal Server Error", "text/html", true); 302 private static void sendError(Writer out, int errorCode, String errorName, String help) throws IOException { 303 sendHeader(out, errorCode + " " + errorName, "text/html", true); 314 304 out.write(String.format( 315 305 RESPONSE_TEMPLATE, 316 "<title>Internal Error</title>", 317 "<h1>HTTP Error 500: Internal Server Error</h1>" 306 "<title>" + errorName + "</title>", 307 "<h1>HTTP Error " + errorCode + ": " + errorName + "</h1>" + 308 (help == null ? "" : "<p>"+Utils.escapeReservedCharactersHTML(help) + "</p>") 318 309 )); 319 310 out.flush(); … … 321 312 322 313 /** 323 * Sends a 501 error: not implemented 324 * 325 * @param out 326 * The writer where the error is written 327 * @throws IOException 328 * If the error can not be written 329 */ 330 private static void sendNotImplemented(Writer out) throws IOException { 331 sendHeader(out, "501 Not Implemented", "text/html", true); 332 out.write(String.format( 333 RESPONSE_TEMPLATE, 334 "<title>Not Implemented</title>", 335 "<h1>HTTP Error 501: Not Implemented</h1>" 336 )); 337 out.flush(); 338 } 339 340 /** 341 * Sends a 403 error: forbidden 314 * Sends a 500 error: internal server error 342 315 * 343 316 * @param out … … 348 321 * If the error can not be written 349 322 */ 323 private static void sendInternalError(Writer out, String help) throws IOException { 324 sendError(out, 500, "Internal Server Error", help); 325 } 326 327 /** 328 * Sends a 501 error: not implemented 329 * 330 * @param out 331 * The writer where the error is written 332 * @throws IOException 333 * If the error can not be written 334 */ 335 private static void sendNotImplemented(Writer out) throws IOException { 336 sendError(out, 501, "Not Implemented", null); 337 } 338 339 /** 340 * Sends a 502 error: bad gateway 341 * 342 * @param out 343 * The writer where the error is written 344 * @param help 345 * Optional HTML help content to display, can be null 346 * @throws IOException 347 * If the error can not be written 348 */ 349 private static void sendBadGateway(Writer out, String help) throws IOException { 350 sendError(out, 502, "Bad Gateway", help); 351 } 352 353 /** 354 * Sends a 403 error: forbidden 355 * 356 * @param out 357 * The writer where the error is written 358 * @param help 359 * Optional HTML help content to display, can be null 360 * @throws IOException 361 * If the error can not be written 362 */ 350 363 private static void sendForbidden(Writer out, String help) throws IOException { 351 sendHeader(out, "403 Forbidden", "text/html", true); 352 out.write(String.format( 353 RESPONSE_TEMPLATE, 354 "<title>Forbidden</title>", 355 "<h1>HTTP Error 403: Forbidden</h1>" + 356 (help == null ? "" : "<p>"+Utils.escapeReservedCharactersHTML(help) + "</p>") 357 )); 358 out.flush(); 364 sendError(out, 403, "Forbidden", help); 359 365 } 360 366 … … 367 373 */ 368 374 private static void sendBadRequest(Writer out, String help) throws IOException { 369 sendHeader(out, "400 Bad Request", "text/html", true); 370 out.write(String.format( 371 RESPONSE_TEMPLATE, 372 "<title>Bad Request</title>", 373 "<h1>HTTP Error 400: Bad Request</h1>" + 374 (help == null ? "" : ("<p>" + Utils.escapeReservedCharactersHTML(help) + "</p>")) 375 )); 376 out.flush(); 375 sendError(out, 400, "Bad Request", help); 377 376 } 378 377 -
trunk/src/org/openstreetmap/josm/io/remotecontrol/handler/LoadAndZoomHandler.java
r16643 r17330 12 12 import java.util.Map; 13 13 import java.util.Set; 14 import java.util.concurrent.ExecutionException; 14 15 import java.util.concurrent.Future; 16 import java.util.concurrent.TimeUnit; 17 import java.util.concurrent.TimeoutException; 15 18 16 19 import javax.swing.JOptionPane; … … 20 23 import org.openstreetmap.josm.actions.downloadtasks.DownloadOsmTask; 21 24 import org.openstreetmap.josm.actions.downloadtasks.DownloadParams; 22 import org.openstreetmap.josm.actions.downloadtasks.DownloadTask;23 25 import org.openstreetmap.josm.actions.downloadtasks.PostDownloadHandler; 24 26 import org.openstreetmap.josm.data.Bounds; … … 36 38 import org.openstreetmap.josm.gui.Notification; 37 39 import org.openstreetmap.josm.gui.util.GuiHelper; 40 import org.openstreetmap.josm.io.OsmApiException; 38 41 import org.openstreetmap.josm.io.remotecontrol.AddTagsDialog; 39 42 import org.openstreetmap.josm.io.remotecontrol.PermissionPrefWithDefault; … … 120 123 @Override 121 124 protected void handleRequest() throws RequestHandlerErrorException { 122 Download Task osmTask = new DownloadOsmTask();125 DownloadOsmTask osmTask = new DownloadOsmTask(); 123 126 try { 124 127 DownloadParams settings = getDownloadParams(); … … 154 157 Future<?> future = osmTask.download(settings, new Bounds(minlat, minlon, maxlat, maxlon), 155 158 null /* let the task manage the progress monitor */); 156 MainApplication.worker.submit(new PostDownloadHandler(osmTask, future)); 159 MainApplication.worker.submit(new PostDownloadHandler(osmTask, future)) 160 .get(OSM_DOWNLOAD_TIMEOUT.get(), TimeUnit.SECONDS); 161 if (osmTask.isFailed()) { 162 Object error = osmTask.getErrorObjects().get(0); 163 throw error instanceof OsmApiException 164 ? new RequestHandlerOsmApiException((OsmApiException) error) 165 : new RequestHandlerErrorException(String.join(", ", osmTask.getErrorMessages())); 166 } 157 167 } 158 168 } 159 169 } 160 } catch (RuntimeException ex) { // NOPMD170 } catch (RuntimeException | InterruptedException | ExecutionException | TimeoutException ex) { // NOPMD 161 171 Logging.warn("RemoteControl: Error parsing load_and_zoom remote control request:"); 162 172 Logging.error(ex); -
trunk/src/org/openstreetmap/josm/io/remotecontrol/handler/LoadObjectHandler.java
r15152 r17330 103 103 } 104 104 } 105 if (ps.isEmpty()) { 106 throw new RequestHandlerBadRequestException(tr("No valid object identifier has been provided")); 107 } 105 108 } 106 109 } -
trunk/src/org/openstreetmap/josm/io/remotecontrol/handler/RequestHandler.java
r16825 r17330 25 25 import org.openstreetmap.josm.data.osm.UploadPolicy; 26 26 import org.openstreetmap.josm.data.preferences.BooleanProperty; 27 import org.openstreetmap.josm.data.preferences.IntegerProperty; 27 28 import org.openstreetmap.josm.gui.MainApplication; 29 import org.openstreetmap.josm.io.OsmApiException; 28 30 import org.openstreetmap.josm.io.remotecontrol.PermissionPrefWithDefault; 29 31 import org.openstreetmap.josm.spi.preferences.Config; … … 43 45 /** preference to determine if remote control loads data in a new layer */ 44 46 public static final BooleanProperty LOAD_IN_NEW_LAYER = new BooleanProperty("remotecontrol.new-layer", false); 47 /** preference to define OSM download timeout in seconds */ 48 public static final IntegerProperty OSM_DOWNLOAD_TIMEOUT = new IntegerProperty("remotecontrol.osm.download.timeout", 5*60); 45 49 46 50 protected static final Pattern SPLITTER_COMMA = Pattern.compile(",\\s*"); … … 416 420 /** 417 421 * Constructs a new {@code RequestHandlerErrorException}. 422 * @param message the detail message. The detail message is saved for later retrieval by the {@link #getMessage()} method. 423 * @since 17330 424 */ 425 public RequestHandlerErrorException(String message) { 426 super(message); 427 } 428 429 /** 430 * Constructs a new {@code RequestHandlerErrorException}. 418 431 * @param cause the cause (which is saved for later retrieval by the {@link #getCause()} method). 419 432 */ 420 433 public RequestHandlerErrorException(Throwable cause) { 434 super(cause); 435 } 436 } 437 438 /** 439 * Error raised for OSM API errors. 440 * @since 17330 441 */ 442 public static class RequestHandlerOsmApiException extends RequestHandlerErrorException { 443 444 /** 445 * Constructs a new {@code RequestHandlerOsmApiException}. 446 * @param cause the cause (which is saved for later retrieval by the {@link #getCause()} method). 447 */ 448 public RequestHandlerOsmApiException(OsmApiException cause) { 421 449 super(cause); 422 450 }
Note:
See TracChangeset
for help on using the changeset viewer.