From 7e119913da2dc84fd3b907a18d9247db5eedc5ea Mon Sep 17 00:00:00 2001 From: sebr72 Date: Wed, 14 Feb 2024 11:15:51 +0100 Subject: [PATCH] Remove mechanism to hide Exception tree --- .../org/mapfish/print/ExceptionUtils.java | 18 --- .../java/org/mapfish/print/FileUtils.java | 2 +- .../main/java/org/mapfish/print/URIUtils.java | 25 +++- .../print/attribute/ReflectiveAttribute.java | 4 +- .../attribute/map/CenterScaleMapBounds.java | 4 +- .../map/geotools/AbstractGeotoolsLayer.java | 6 +- .../print/map/geotools/GeoJsonLayer.java | 4 +- .../mapfish/print/map/geotools/GmlLayer.java | 8 +- .../grid/LinearCoordinateSequence.java | 4 +- .../map/style/json/JsonStyleParserHelper.java | 8 +- .../style/json/MapfishJsonStyleVersion2.java | 8 +- .../mapfish/print/map/tiled/CoverageTask.java | 10 +- .../print/map/tiled/TilePreparationTask.java | 6 +- .../mapfish/print/parser/MapfishParser.java | 12 +- .../print/processor/ProcessorGraphNode.java | 7 +- .../print/processor/ProcessorUtils.java | 6 +- .../print/processor/http/MapUriProcessor.java | 4 +- .../http/UseHttpForHttpsProcessor.java | 4 +- .../processor/map/CreateMapProcessor.java | 4 +- .../mapfish/print/servlet/BaseMapServlet.java | 6 +- .../print/servlet/MapPrinterServlet.java | 21 ++- .../fileloader/ClasspathConfigFileLoader.java | 6 +- .../servlet/job/impl/PrintJobResultImpl.java | 4 +- .../servlet/job/impl/RegistryJobQueue.java | 126 +++++++----------- .../job/impl/ThreadPoolJobManager.java | 7 +- .../print/servlet/registry/BasicRegistry.java | 8 +- .../print/wrapper/yaml/PYamlArray.java | 8 +- 27 files changed, 149 insertions(+), 181 deletions(-) diff --git a/core/src/main/java/org/mapfish/print/ExceptionUtils.java b/core/src/main/java/org/mapfish/print/ExceptionUtils.java index ee1e1bfb52..33fa59957f 100644 --- a/core/src/main/java/org/mapfish/print/ExceptionUtils.java +++ b/core/src/main/java/org/mapfish/print/ExceptionUtils.java @@ -5,24 +5,6 @@ public final class ExceptionUtils { private ExceptionUtils() {} - /** - * Returns a {@link RuntimeException} for the given exception. - * - * @param exc An exception. - * @return A {@link RuntimeException} - */ - public static RuntimeException getRuntimeException(final Throwable exc) { - Throwable e = exc; - while (e.getCause() instanceof RuntimeException) { - e = e.getCause(); - } - if (e instanceof RuntimeException) { - return (RuntimeException) e; - } else { - return new RuntimeException(exc); - } - } - /** * Because exceptions might get re-thrown several times, an error message like * "java.util.concurrent.ExecutionException: java.lang.IllegalArgumentException: diff --git a/core/src/main/java/org/mapfish/print/FileUtils.java b/core/src/main/java/org/mapfish/print/FileUtils.java index d05169c235..11a0da09d4 100644 --- a/core/src/main/java/org/mapfish/print/FileUtils.java +++ b/core/src/main/java/org/mapfish/print/FileUtils.java @@ -55,7 +55,7 @@ public static URL testForLegalFileUrl(final Configuration configuration, final U } } } catch (MalformedURLException e) { - throw ExceptionUtils.getRuntimeException(e); + throw new PrintException("Malformed URL", e); } } return url; diff --git a/core/src/main/java/org/mapfish/print/URIUtils.java b/core/src/main/java/org/mapfish/print/URIUtils.java index a0246ad2a1..2636133e43 100644 --- a/core/src/main/java/org/mapfish/print/URIUtils.java +++ b/core/src/main/java/org/mapfish/print/URIUtils.java @@ -59,7 +59,7 @@ public static Multimap getParameters(final String rawQuery) { key = URLDecoder.decode(pair.substring(0, pos), "UTF-8"); value = URLDecoder.decode(pair.substring(pos + 1), "UTF-8"); } catch (UnsupportedEncodingException e) { - throw ExceptionUtils.getRuntimeException(e); + throw new PrintException("Failed to get parameters for query " + rawQuery, e); } } @@ -135,7 +135,7 @@ public static URI addParams( try { return new URI(result.toString()); } catch (URISyntaxException e) { - throw ExceptionUtils.getRuntimeException(e); + throw new PrintException("Failed to create URI for " + result, e); } } @@ -154,15 +154,26 @@ private static boolean addParams( } try { result.append(URLEncoder.encode(key, Constants.DEFAULT_ENCODING)); - result.append("="); + } catch (UnsupportedEncodingException e) { + throw createPrintException(key, e); + } + result.append("="); + try { result.append(URLEncoder.encode(val, Constants.DEFAULT_ENCODING)); } catch (UnsupportedEncodingException e) { - throw ExceptionUtils.getRuntimeException(e); + throw createPrintException(val, e); } } return first; } + private static PrintException createPrintException( + final String toEncode, final UnsupportedEncodingException e) { + String message = + String.format("Failed to encode %s using %s", toEncode, Constants.DEFAULT_ENCODING); + return new PrintException(message, e); + } + /** * Add a parameter to the query params (the params map) replacing any parameter that might be * there. @@ -227,7 +238,7 @@ public static URI setQueryParams( initialUri.getFragment()); } } catch (URISyntaxException e) { - throw ExceptionUtils.getRuntimeException(e); + throw new PrintException("Failed to set query parameters", e); } } @@ -258,7 +269,7 @@ public static String toString(final MfClientHttpRequestFactory requestFactory, f try { return toString(requestFactory, url.toURI()); } catch (URISyntaxException e) { - throw ExceptionUtils.getRuntimeException(e); + throw new PrintException("Incorrect syntax for " + url, e); } } @@ -292,7 +303,7 @@ public static URI setPath(final URI initialUri, final String path) { initialUri.getFragment()); } } catch (URISyntaxException e) { - throw ExceptionUtils.getRuntimeException(e); + throw new PrintException("Failed to set path " + path, e); } } } diff --git a/core/src/main/java/org/mapfish/print/attribute/ReflectiveAttribute.java b/core/src/main/java/org/mapfish/print/attribute/ReflectiveAttribute.java index f0f489bdc8..1c9af89e75 100644 --- a/core/src/main/java/org/mapfish/print/attribute/ReflectiveAttribute.java +++ b/core/src/main/java/org/mapfish/print/attribute/ReflectiveAttribute.java @@ -19,7 +19,7 @@ import org.json.JSONWriter; import org.locationtech.jts.util.Assert; import org.locationtech.jts.util.AssertionFailedException; -import org.mapfish.print.ExceptionUtils; +import org.mapfish.print.PrintException; import org.mapfish.print.config.Template; import org.mapfish.print.parser.HasDefaultValue; import org.mapfish.print.parser.MapfishParser; @@ -343,7 +343,7 @@ private void encodeAttributeValue( try { value = typeOrComponentType.getDeclaredConstructor().newInstance(); } catch (InvocationTargetException | NoSuchMethodException | InstantiationException e) { - throw ExceptionUtils.getRuntimeException(e); + throw new PrintException("Failed to encode Attribute for " + typeOrComponentType, e); } } } diff --git a/core/src/main/java/org/mapfish/print/attribute/map/CenterScaleMapBounds.java b/core/src/main/java/org/mapfish/print/attribute/map/CenterScaleMapBounds.java index 4aab57d98d..368cd390c5 100644 --- a/core/src/main/java/org/mapfish/print/attribute/map/CenterScaleMapBounds.java +++ b/core/src/main/java/org/mapfish/print/attribute/map/CenterScaleMapBounds.java @@ -9,8 +9,8 @@ import org.geotools.geometry.jts.ReferencedEnvelope; import org.geotools.referencing.GeodeticCalculator; import org.locationtech.jts.geom.Coordinate; -import org.mapfish.print.ExceptionUtils; import org.mapfish.print.FloatingPointUtil; +import org.mapfish.print.PrintException; import org.mapfish.print.map.DistanceUnit; import org.mapfish.print.map.Scale; @@ -175,7 +175,7 @@ private ReferencedEnvelope computeGeodeticBBox( rollLatitude(maxGeoY), crs); } catch (TransformException e) { - throw ExceptionUtils.getRuntimeException(e); + throw new PrintException("Failed to compute geodetic bbox", e); } } diff --git a/core/src/main/java/org/mapfish/print/map/geotools/AbstractGeotoolsLayer.java b/core/src/main/java/org/mapfish/print/map/geotools/AbstractGeotoolsLayer.java index eb89c64335..c38d107def 100644 --- a/core/src/main/java/org/mapfish/print/map/geotools/AbstractGeotoolsLayer.java +++ b/core/src/main/java/org/mapfish/print/map/geotools/AbstractGeotoolsLayer.java @@ -19,8 +19,8 @@ import org.geotools.map.MapContent; import org.geotools.renderer.lite.StreamingRenderer; import org.locationtech.jts.util.Assert; -import org.mapfish.print.ExceptionUtils; import org.mapfish.print.FloatingPointUtil; +import org.mapfish.print.PrintException; import org.mapfish.print.attribute.map.MapLayer; import org.mapfish.print.attribute.map.MapfishMapContext; import org.mapfish.print.http.HttpRequestFetcher; @@ -133,8 +133,10 @@ public final void render( layerTransformer.getBounds().toReferencedEnvelope(paintArea); renderer.paint(graphics2D, paintArea, mapArea); graphics2D.setTransform(originalTransform); + } catch (RuntimeException e) { + throw e; } catch (Exception e) { - throw ExceptionUtils.getRuntimeException(e); + throw new PrintException("Failed to render.", e); } finally { content.dispose(); } diff --git a/core/src/main/java/org/mapfish/print/map/geotools/GeoJsonLayer.java b/core/src/main/java/org/mapfish/print/map/geotools/GeoJsonLayer.java index 647f90f0b8..f1dab546da 100644 --- a/core/src/main/java/org/mapfish/print/map/geotools/GeoJsonLayer.java +++ b/core/src/main/java/org/mapfish/print/map/geotools/GeoJsonLayer.java @@ -6,7 +6,7 @@ import org.geotools.api.data.FeatureSource; import org.geotools.data.collection.CollectionFeatureSource; import org.geotools.data.simple.SimpleFeatureCollection; -import org.mapfish.print.ExceptionUtils; +import org.mapfish.print.PrintException; import org.mapfish.print.attribute.map.MapfishMapContext; import org.mapfish.print.config.Template; import org.mapfish.print.http.MfClientHttpRequestFactory; @@ -87,7 +87,7 @@ public FeatureSource load( featureCollection = parser.autoTreat(template, geoJsonString); return new CollectionFeatureSource(featureCollection); } catch (IOException e) { - throw ExceptionUtils.getRuntimeException(e); + throw new PrintException("Failed to load " + mapContext, e); } } }; diff --git a/core/src/main/java/org/mapfish/print/map/geotools/GmlLayer.java b/core/src/main/java/org/mapfish/print/map/geotools/GmlLayer.java index d7dc7674db..12981af32f 100644 --- a/core/src/main/java/org/mapfish/print/map/geotools/GmlLayer.java +++ b/core/src/main/java/org/mapfish/print/map/geotools/GmlLayer.java @@ -16,8 +16,8 @@ import org.geotools.gml2.GMLConfiguration; import org.geotools.xsd.Configuration; import org.geotools.xsd.Parser; -import org.mapfish.print.ExceptionUtils; import org.mapfish.print.FileUtils; +import org.mapfish.print.PrintException; import org.mapfish.print.URIUtils; import org.mapfish.print.attribute.map.MapfishMapContext; import org.mapfish.print.config.Template; @@ -98,7 +98,7 @@ public FeatureSource load( try { featureCollection = createFeatureSource(template, requestFactory, url); } catch (IOException e) { - throw ExceptionUtils.getRuntimeException(e); + throw new PrintException("Failed to create feature source for " + url, e); } if (featureCollection == null) { throw new IllegalArgumentException(url + " does not reference a GML file"); @@ -126,7 +126,7 @@ private SimpleFeatureCollection createFeatureSource( return parseGml3(gmlData); } } catch (URISyntaxException e) { - throw ExceptionUtils.getRuntimeException(e); + throw new PrintException("Failed to create URI for " + url, e); } } catch (MalformedURLException e) { return null; @@ -177,7 +177,7 @@ private SimpleFeatureCollection parseGml32(final String gmlData) throws IOExcept } } catch (SAXException | ParserConfigurationException e) { - throw ExceptionUtils.getRuntimeException(e); + throw new PrintException("Failed to parse Gml32 " + gmlData, e); } } diff --git a/core/src/main/java/org/mapfish/print/map/geotools/grid/LinearCoordinateSequence.java b/core/src/main/java/org/mapfish/print/map/geotools/grid/LinearCoordinateSequence.java index dff78a10a3..6d41b04e12 100644 --- a/core/src/main/java/org/mapfish/print/map/geotools/grid/LinearCoordinateSequence.java +++ b/core/src/main/java/org/mapfish/print/map/geotools/grid/LinearCoordinateSequence.java @@ -4,7 +4,7 @@ import org.locationtech.jts.geom.Coordinate; import org.locationtech.jts.geom.CoordinateSequence; import org.locationtech.jts.geom.Envelope; -import org.mapfish.print.ExceptionUtils; +import org.mapfish.print.PrintException; /** The LinearCoordinateSequence class. */ public final class LinearCoordinateSequence implements CoordinateSequence, Cloneable { @@ -210,7 +210,7 @@ public CoordinateSequence copy() { clone.spacing = this.spacing; return clone; } catch (CloneNotSupportedException e) { - throw ExceptionUtils.getRuntimeException(e); + throw new PrintException("Failed to create a copy", e); } } } diff --git a/core/src/main/java/org/mapfish/print/map/style/json/JsonStyleParserHelper.java b/core/src/main/java/org/mapfish/print/map/style/json/JsonStyleParserHelper.java index eae6c8743d..98b46cbaf3 100644 --- a/core/src/main/java/org/mapfish/print/map/style/json/JsonStyleParserHelper.java +++ b/core/src/main/java/org/mapfish/print/map/style/json/JsonStyleParserHelper.java @@ -48,8 +48,8 @@ import org.geotools.filter.text.cql2.CQLException; import org.geotools.filter.text.ecql.ECQL; import org.geotools.styling.StyleBuilder; -import org.mapfish.print.ExceptionUtils; import org.mapfish.print.FontTools; +import org.mapfish.print.PrintException; import org.mapfish.print.SetsUtils; import org.mapfish.print.config.Configuration; import org.mapfish.print.map.DistanceUnit; @@ -286,8 +286,7 @@ private String validateURL(final String externalGraphicUrl) { final URL fileURL = new URL("file://" + externalGraphicUrl); return testForLegalFileUrl(this.configuration, fileURL).toExternalForm(); } catch (MalformedURLException e1) { - // unable to convert to file url so give up and throw exception; - throw ExceptionUtils.getRuntimeException(e); + throw new PrintException("Unable to convert to file URL " + externalGraphicUrl, e); } } return externalGraphicUrl; @@ -868,7 +867,8 @@ private Expression toExpressionFromCQl(final String property) { try { return ECQL.toExpression(property, this.styleBuilder.getFilterFactory()); } catch (CQLException e) { - throw ExceptionUtils.getRuntimeException(e); + throw new PrintException( + "Failed to convert from CQL to expression, the property " + property, e); } } diff --git a/core/src/main/java/org/mapfish/print/map/style/json/MapfishJsonStyleVersion2.java b/core/src/main/java/org/mapfish/print/map/style/json/MapfishJsonStyleVersion2.java index 78bfa0ec14..c837d47f61 100644 --- a/core/src/main/java/org/mapfish/print/map/style/json/MapfishJsonStyleVersion2.java +++ b/core/src/main/java/org/mapfish/print/map/style/json/MapfishJsonStyleVersion2.java @@ -17,9 +17,7 @@ import org.geotools.filter.text.cql2.CQLException; import org.geotools.filter.text.ecql.ECQL; import org.geotools.styling.StyleBuilder; -import org.json.JSONException; import org.json.JSONObject; -import org.mapfish.print.ExceptionUtils; import org.mapfish.print.config.Configuration; import org.mapfish.print.wrapper.json.PJsonArray; import org.mapfish.print.wrapper.json.PJsonObject; @@ -162,11 +160,7 @@ private void updateSymbolizerProperties( final PJsonObject ruleJson, final PJsonObject symbolizerJson) { Map values = buildValuesMap(ruleJson, symbolizerJson); for (Map.Entry entry : values.entrySet()) { - try { - symbolizerJson.getInternalObj().put(entry.getKey(), entry.getValue()); - } catch (JSONException e) { - throw ExceptionUtils.getRuntimeException(e); - } + symbolizerJson.getInternalObj().put(entry.getKey(), entry.getValue()); } } diff --git a/core/src/main/java/org/mapfish/print/map/tiled/CoverageTask.java b/core/src/main/java/org/mapfish/print/map/tiled/CoverageTask.java index c5019d6000..4093f58ff5 100644 --- a/core/src/main/java/org/mapfish/print/map/tiled/CoverageTask.java +++ b/core/src/main/java/org/mapfish/print/map/tiled/CoverageTask.java @@ -6,6 +6,8 @@ import java.awt.Graphics2D; import java.awt.image.BufferedImage; import java.io.IOException; +import java.io.UnsupportedEncodingException; +import java.net.URISyntaxException; import java.nio.charset.StandardCharsets; import java.util.concurrent.Callable; import java.util.concurrent.RecursiveTask; @@ -16,7 +18,7 @@ import org.geotools.coverage.grid.GridCoverage2D; import org.geotools.coverage.grid.GridCoverageFactory; import org.geotools.geometry.GeneralBounds; -import org.mapfish.print.ExceptionUtils; +import org.mapfish.print.PrintException; import org.mapfish.print.StatsUtils; import org.mapfish.print.config.Configuration; import org.mapfish.print.map.style.json.ColorParser; @@ -147,8 +149,8 @@ public GridCoverage2D call() { this.tilePreparationInfo.getGridCoverageMaxY()); return factory.create( this.tiledLayer.createCommonUrl(), coverageImage, gridEnvelope, null, null, null); - } catch (Exception e) { - throw ExceptionUtils.getRuntimeException(e); + } catch (URISyntaxException | UnsupportedEncodingException e) { + throw new PrintException("Failed to call the coverage task", e); } } @@ -276,7 +278,7 @@ protected Tile compute() { return new Tile(image, getTileIndexX(), getTileIndexY()); } catch (IOException e) { this.registry.counter(baseMetricName + ".error").inc(); - throw ExceptionUtils.getRuntimeException(e); + throw new PrintException("Failed to compute Coverage Task", e); } }); } diff --git a/core/src/main/java/org/mapfish/print/map/tiled/TilePreparationTask.java b/core/src/main/java/org/mapfish/print/map/tiled/TilePreparationTask.java index 5712b9e198..3f2123012d 100644 --- a/core/src/main/java/org/mapfish/print/map/tiled/TilePreparationTask.java +++ b/core/src/main/java/org/mapfish/print/map/tiled/TilePreparationTask.java @@ -17,8 +17,8 @@ import org.locationtech.jts.geom.Coordinate; import org.locationtech.jts.geom.Geometry; import org.locationtech.jts.geom.GeometryFactory; -import org.mapfish.print.ExceptionUtils; import org.mapfish.print.FloatingPointUtil; +import org.mapfish.print.PrintException; import org.mapfish.print.attribute.map.MapBounds; import org.mapfish.print.attribute.map.MapfishMapContext; import org.mapfish.print.http.HttpRequestFetcher; @@ -197,8 +197,10 @@ public TilePreparationInfo call() { gridCoverageMaxX, gridCoverageMaxY, mapProjection); + } catch (RuntimeException e) { + throw e; } catch (Exception e) { - throw ExceptionUtils.getRuntimeException(e); + throw new PrintException("Failed to call Tile preparation task", e); } }); } diff --git a/core/src/main/java/org/mapfish/print/parser/MapfishParser.java b/core/src/main/java/org/mapfish/print/parser/MapfishParser.java index 9cebdc20f4..a5c5f175a5 100644 --- a/core/src/main/java/org/mapfish/print/parser/MapfishParser.java +++ b/core/src/main/java/org/mapfish/print/parser/MapfishParser.java @@ -16,9 +16,9 @@ import java.util.HashSet; import java.util.Iterator; import java.util.Map; -import org.mapfish.print.ExceptionUtils; import org.mapfish.print.ExtraPropertyException; import org.mapfish.print.MissingPropertyException; +import org.mapfish.print.PrintException; import org.mapfish.print.attribute.PrimitiveAttribute; import org.mapfish.print.url.data.Handler; import org.mapfish.print.wrapper.ObjectMissingException; @@ -116,7 +116,7 @@ public static void parse( requiresTracker.markAsVisited(property); property.set(objectToPopulate, value); } catch (IllegalAccessException e) { - throw ExceptionUtils.getRuntimeException(e); + throw new PrintException("Failed to set the property of " + objectToPopulate, e); } } catch (ObjectMissingException e) { final HasDefaultValue hasDefaultValue = property.getAnnotation(HasDefaultValue.class); @@ -228,7 +228,7 @@ private static Object parseValue( try { value = new URL(null, layer.getString(name), new Handler()); } catch (MalformedURLException e) { - throw ExceptionUtils.getRuntimeException(e); + throw new PrintException("Failed to create URL for " + name, e); } } else if (type.isArray()) { final PArray array = layer.getArray(name); @@ -257,7 +257,7 @@ private static Object parseValue( } catch (InvocationTargetException | NoSuchMethodException | InstantiationException e) { throw new UnsupportedTypeException(type, e); } catch (IllegalAccessException e) { - throw ExceptionUtils.getRuntimeException(e); + throw new PrintException("Failed to create instance of type" + type, e); } } return value; @@ -326,7 +326,7 @@ private static Object parseArrayValue( try { value = new URL(null, array.getString(i), new Handler()); } catch (MalformedURLException e) { - throw ExceptionUtils.getRuntimeException(e); + throw new PrintException("Failed to create URL for index" + i, e); } } else if (type.isEnum()) { value = parseEnum(type, array.getPath("" + i), array.getString(i)); @@ -338,7 +338,7 @@ private static Object parseArrayValue( } catch (InvocationTargetException | NoSuchMethodException | InstantiationException e) { throw new UnsupportedTypeException(type, e); } catch (IllegalAccessException e) { - throw ExceptionUtils.getRuntimeException(e); + throw new PrintException("Failed to create instance for " + type, e); } } diff --git a/core/src/main/java/org/mapfish/print/processor/ProcessorGraphNode.java b/core/src/main/java/org/mapfish/print/processor/ProcessorGraphNode.java index 73fa7ae26f..65ef19adb4 100644 --- a/core/src/main/java/org/mapfish/print/processor/ProcessorGraphNode.java +++ b/core/src/main/java/org/mapfish/print/processor/ProcessorGraphNode.java @@ -12,7 +12,7 @@ import java.util.concurrent.TimeUnit; import javax.annotation.Nonnull; import org.locationtech.jts.util.Assert; -import org.mapfish.print.ExceptionUtils; +import org.mapfish.print.PrintException; import org.mapfish.print.output.Values; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -194,12 +194,15 @@ protected Values compute() { LOGGER.debug("Executing process: {}", process); output = process.execute(inputParameter, this.execContext.getContext()); LOGGER.debug("Succeeded in executing process: {}", process); + } catch (RuntimeException e) { + throw e; } catch (Exception e) { + throw new PrintException("Failed to execute process:" + process, e); + } finally { // the processor is already canceled, so we don't care if something fails this.execContext.getContext().stopIfCanceled(); LOGGER.info("Error while executing process: {}", process); registry.counter(name + ".error").inc(); - throw ExceptionUtils.getRuntimeException(e); } if (output != null) { diff --git a/core/src/main/java/org/mapfish/print/processor/ProcessorUtils.java b/core/src/main/java/org/mapfish/print/processor/ProcessorUtils.java index f39cb8acb6..211deb93a3 100644 --- a/core/src/main/java/org/mapfish/print/processor/ProcessorUtils.java +++ b/core/src/main/java/org/mapfish/print/processor/ProcessorUtils.java @@ -12,7 +12,7 @@ import javax.annotation.Nonnull; import javax.annotation.Nullable; import org.apache.commons.lang3.StringUtils; -import org.mapfish.print.ExceptionUtils; +import org.mapfish.print.PrintException; import org.mapfish.print.output.Values; import org.mapfish.print.parser.HasDefaultValue; @@ -48,7 +48,7 @@ public static IN populateInputParameter( try { field.set(inputObject, value); } catch (IllegalAccessException e) { - throw ExceptionUtils.getRuntimeException(e); + throw new PrintException("Failed to set field of " + inputObject, e); } } else { if (field.getAnnotation(HasDefaultValue.class) == null) { @@ -94,7 +94,7 @@ public static void writeProcessorOutputToValues( values.remove(name); } } catch (IllegalAccessException e) { - throw ExceptionUtils.getRuntimeException(e); + throw new PrintException("Failed to get field of " + output, e); } } } diff --git a/core/src/main/java/org/mapfish/print/processor/http/MapUriProcessor.java b/core/src/main/java/org/mapfish/print/processor/http/MapUriProcessor.java index d9be5f5547..9717aa8357 100644 --- a/core/src/main/java/org/mapfish/print/processor/http/MapUriProcessor.java +++ b/core/src/main/java/org/mapfish/print/processor/http/MapUriProcessor.java @@ -8,7 +8,7 @@ import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; -import org.mapfish.print.ExceptionUtils; +import org.mapfish.print.PrintException; import org.mapfish.print.config.Configuration; import org.mapfish.print.http.AbstractMfClientHttpRequestFactoryWrapper; import org.mapfish.print.http.MfClientHttpRequestFactory; @@ -73,7 +73,7 @@ protected ClientHttpRequest createRequest( try { return requestFactory.createRequest(new URI(finalUri), httpMethod); } catch (URISyntaxException e) { - throw ExceptionUtils.getRuntimeException(e); + throw new PrintException("Failed to create URI for " + finalUri, e); } } else { LOGGER.debug("URI {} did not match {}", uriString, entry.getKey()); diff --git a/core/src/main/java/org/mapfish/print/processor/http/UseHttpForHttpsProcessor.java b/core/src/main/java/org/mapfish/print/processor/http/UseHttpForHttpsProcessor.java index 6a40b09b13..2438943496 100644 --- a/core/src/main/java/org/mapfish/print/processor/http/UseHttpForHttpsProcessor.java +++ b/core/src/main/java/org/mapfish/print/processor/http/UseHttpForHttpsProcessor.java @@ -9,7 +9,7 @@ import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; -import org.mapfish.print.ExceptionUtils; +import org.mapfish.print.PrintException; import org.mapfish.print.RegexpUtil; import org.mapfish.print.config.Configuration; import org.mapfish.print.http.AbstractMfClientHttpRequestFactoryWrapper; @@ -90,7 +90,7 @@ protected ClientHttpRequest createRequest( } return requestFactory.createRequest(httpUri, httpMethod); } catch (URISyntaxException e) { - throw ExceptionUtils.getRuntimeException(e); + throw new PrintException("Failed to update Port and Scheme for " + uri, e); } } return requestFactory.createRequest(uri, httpMethod); diff --git a/core/src/main/java/org/mapfish/print/processor/map/CreateMapProcessor.java b/core/src/main/java/org/mapfish/print/processor/map/CreateMapProcessor.java index 02fdd66657..8e2fe35034 100644 --- a/core/src/main/java/org/mapfish/print/processor/map/CreateMapProcessor.java +++ b/core/src/main/java/org/mapfish/print/processor/map/CreateMapProcessor.java @@ -57,8 +57,8 @@ import org.locationtech.jts.geom.Coordinate; import org.locationtech.jts.geom.Polygon; import org.mapfish.print.Constants; -import org.mapfish.print.ExceptionUtils; import org.mapfish.print.ImageUtils; +import org.mapfish.print.PrintException; import org.mapfish.print.SvgUtil; import org.mapfish.print.attribute.map.AreaOfInterest; import org.mapfish.print.attribute.map.BBoxMapBounds; @@ -628,7 +628,7 @@ private ReferencedEnvelope getFeatureBounds( try { features = featureSource.getFeatures(); } catch (IOException e) { - throw ExceptionUtils.getRuntimeException(e); + throw new PrintException("Failed to get features from " + featureSource, e); } if (!features.isEmpty()) { diff --git a/core/src/main/java/org/mapfish/print/servlet/BaseMapServlet.java b/core/src/main/java/org/mapfish/print/servlet/BaseMapServlet.java index d0fcf69da8..a56e1ce55d 100644 --- a/core/src/main/java/org/mapfish/print/servlet/BaseMapServlet.java +++ b/core/src/main/java/org/mapfish/print/servlet/BaseMapServlet.java @@ -7,7 +7,7 @@ import java.util.Date; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; -import org.mapfish.print.ExceptionUtils; +import org.mapfish.print.PrintException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.http.HttpStatus; @@ -69,7 +69,7 @@ protected static void error( LOGGER.warn("Error while processing request: {}", message); } catch (IOException ex) { - throw ExceptionUtils.getRuntimeException(ex); + throw new PrintException("Failed to send an error", ex); } } @@ -95,7 +95,7 @@ protected final void error(final HttpServletResponse httpServletResponse, final out.println("Error while processing request:"); LOGGER.warn("Error while processing request", e); } catch (IOException ex) { - throw ExceptionUtils.getRuntimeException(ex); + throw new PrintException("", e); } } diff --git a/core/src/main/java/org/mapfish/print/servlet/MapPrinterServlet.java b/core/src/main/java/org/mapfish/print/servlet/MapPrinterServlet.java index 2655fe560c..920b163b79 100644 --- a/core/src/main/java/org/mapfish/print/servlet/MapPrinterServlet.java +++ b/core/src/main/java/org/mapfish/print/servlet/MapPrinterServlet.java @@ -40,10 +40,10 @@ import org.json.JSONObject; import org.json.JSONWriter; import org.mapfish.print.Constants; -import org.mapfish.print.ExceptionUtils; import org.mapfish.print.FontTools; import org.mapfish.print.MapPrinter; import org.mapfish.print.MapPrinterFactory; +import org.mapfish.print.PrintException; import org.mapfish.print.config.Configuration; import org.mapfish.print.config.Template; import org.mapfish.print.processor.Processor; @@ -295,7 +295,7 @@ private static PJsonObject parseJson( try { requestData = URLDecoder.decode(requestData, Constants.DEFAULT_ENCODING); } catch (UnsupportedEncodingException e) { - throw ExceptionUtils.getRuntimeException(e); + throw createPrintException(e, requestData); } } if (requestData.startsWith("spec=")) { @@ -308,7 +308,7 @@ private static PJsonObject parseJson( try { return MapPrinter.parseSpec(URLDecoder.decode(requestData, Constants.DEFAULT_ENCODING)); } catch (UnsupportedEncodingException uee) { - throw ExceptionUtils.getRuntimeException(e); + throw createPrintException(uee, requestData); } } } catch (RuntimeException e) { @@ -317,6 +317,13 @@ private static PJsonObject parseJson( } } + private static PrintException createPrintException( + final UnsupportedEncodingException e, final String requestData) { + String message = + String.format("Failed to decode %s using %s", requestData, Constants.DEFAULT_ENCODING); + return new PrintException(message, e); + } + /** * If the request contains a header that specifies a request ID, add it to the ref. The ref shows * up in every logs, that way, we can trace the request ID across applications. @@ -411,8 +418,8 @@ public final void getStatus( } json.endObject(); } - } catch (JSONException | IOException e) { - throw ExceptionUtils.getRuntimeException(e); + } catch (IOException e) { + throw new PrintException("Failed to get writer from " + statusResponse, e); } catch (NoSuchReferenceException e) { error(statusResponse, e.getMessage(), HttpStatus.NOT_FOUND); } @@ -1059,8 +1066,8 @@ public final void listAvailableFonts(final HttpServletResponse response) { } json.endArray(); json.endObject(); - } catch (JSONException | IOException e) { - throw ExceptionUtils.getRuntimeException(e); + } catch (IOException e) { + throw new PrintException("Failed to get writer from " + response, e); } } diff --git a/core/src/main/java/org/mapfish/print/servlet/fileloader/ClasspathConfigFileLoader.java b/core/src/main/java/org/mapfish/print/servlet/fileloader/ClasspathConfigFileLoader.java index 49cc6e4ad5..63a9057790 100644 --- a/core/src/main/java/org/mapfish/print/servlet/fileloader/ClasspathConfigFileLoader.java +++ b/core/src/main/java/org/mapfish/print/servlet/fileloader/ClasspathConfigFileLoader.java @@ -11,7 +11,7 @@ import java.util.Enumeration; import java.util.NoSuchElementException; import java.util.Optional; -import org.mapfish.print.ExceptionUtils; +import org.mapfish.print.PrintException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -51,7 +51,7 @@ public Optional lastModified(final URI fileURI) { try { return Optional.of(new File(url.toURI()).lastModified()); } catch (URISyntaxException e) { - throw ExceptionUtils.getRuntimeException(e); + throw new PrintException("Failed to create URI for " + url, e); } } else { return Optional.empty(); @@ -183,7 +183,7 @@ private Optional loadResources(final URI fileURI) { try { return Optional.of(file.toURI().toURL()); } catch (MalformedURLException e) { - throw ExceptionUtils.getRuntimeException(e); + throw new PrintException("Failed to create URL from " + file.toURI(), e); } } else { return Optional.empty(); diff --git a/core/src/main/java/org/mapfish/print/servlet/job/impl/PrintJobResultImpl.java b/core/src/main/java/org/mapfish/print/servlet/job/impl/PrintJobResultImpl.java index 5274644397..906b0bdeea 100644 --- a/core/src/main/java/org/mapfish/print/servlet/job/impl/PrintJobResultImpl.java +++ b/core/src/main/java/org/mapfish/print/servlet/job/impl/PrintJobResultImpl.java @@ -12,7 +12,7 @@ import org.hibernate.annotations.OnDelete; import org.hibernate.annotations.OnDeleteAction; import org.hibernate.annotations.Type; -import org.mapfish.print.ExceptionUtils; +import org.mapfish.print.PrintException; import org.mapfish.print.servlet.job.PrintJobResult; import org.mapfish.print.servlet.job.PrintJobStatus; @@ -82,7 +82,7 @@ public URI getReportURI() { try { return this.reportURI == null ? null : new URI(this.reportURI); } catch (URISyntaxException e) { - throw ExceptionUtils.getRuntimeException(e); + throw new PrintException("Failed to create URI", e); } } diff --git a/core/src/main/java/org/mapfish/print/servlet/job/impl/RegistryJobQueue.java b/core/src/main/java/org/mapfish/print/servlet/job/impl/RegistryJobQueue.java index 66e4ddc27e..ae2a2d9fe4 100644 --- a/core/src/main/java/org/mapfish/print/servlet/job/impl/RegistryJobQueue.java +++ b/core/src/main/java/org/mapfish/print/servlet/job/impl/RegistryJobQueue.java @@ -5,7 +5,7 @@ import java.util.List; import org.json.JSONException; import org.json.JSONObject; -import org.mapfish.print.ExceptionUtils; +import org.mapfish.print.PrintException; import org.mapfish.print.config.access.AccessAssertion; import org.mapfish.print.config.access.AccessAssertionPersister; import org.mapfish.print.servlet.job.JobQueue; @@ -65,96 +65,76 @@ public final long getTimeToKeepAfterAccessInMillis() { @Override public final synchronized void add(final PrintJobEntry jobEntry) { this.registry.incrementInt(NEW_PRINT_COUNT, 1); - try { - store(new PrintJobStatusImpl(jobEntry, getNumberOfRequestsMade())); - } catch (JSONException e) { - throw ExceptionUtils.getRuntimeException(e); - } + store(new PrintJobStatusImpl(jobEntry, getNumberOfRequestsMade())); this.registry.put(LAST_POLL + jobEntry.getReferenceId(), System.currentTimeMillis()); } @Override public final synchronized void start(final String referenceId) throws NoSuchReferenceException { - try { - PrintJobStatusImpl jobStatus = load(referenceId); - if (jobStatus.getStatus() == PrintJobStatus.Status.WAITING) { - jobStatus.setStatus(PrintJobStatus.Status.RUNNING); - jobStatus.setWaitingTime(0); - store(jobStatus); - } - } catch (JSONException e) { - throw ExceptionUtils.getRuntimeException(e); + PrintJobStatusImpl jobStatus = load(referenceId); + if (jobStatus.getStatus() == PrintJobStatus.Status.WAITING) { + jobStatus.setStatus(PrintJobStatus.Status.RUNNING); + jobStatus.setWaitingTime(0); + store(jobStatus); } } @Override public final synchronized void done(final String referenceId, final PrintJobResult result) throws NoSuchReferenceException { - try { - PrintJobStatusImpl status = load(referenceId); - if (!status.isDone()) { - this.registry.incrementInt(NB_PRINT_DONE, 1); - this.registry.incrementLong(TOTAL_PRINT_TIME, status.getElapsedTime()); - this.registry.incrementInt(LAST_PRINT_COUNT, 1); - } - - status.setCompletionTime(System.currentTimeMillis()); - status.setStatus(PrintJobStatus.Status.FINISHED); - status.setResult(result); - store(status); - } catch (JSONException e) { - throw ExceptionUtils.getRuntimeException(e); + PrintJobStatusImpl status = load(referenceId); + if (!status.isDone()) { + this.registry.incrementInt(NB_PRINT_DONE, 1); + this.registry.incrementLong(TOTAL_PRINT_TIME, status.getElapsedTime()); + this.registry.incrementInt(LAST_PRINT_COUNT, 1); } + + status.setCompletionTime(System.currentTimeMillis()); + status.setStatus(PrintJobStatus.Status.FINISHED); + status.setResult(result); + store(status); } @Override public final synchronized void cancel( final String referenceId, final String message, final boolean forceFinal) throws NoSuchReferenceException { - try { - PrintJobStatusImpl status = load(referenceId); - - if (!forceFinal && status.getStatus() == PrintJobStatus.Status.RUNNING) { - status.setStatus(PrintJobStatus.Status.CANCELING); - } else { - if (!status.isDone()) { - this.registry.incrementInt(NB_PRINT_DONE, 1); - this.registry.incrementLong(TOTAL_PRINT_TIME, status.getElapsedTime()); - this.registry.incrementInt(LAST_PRINT_COUNT, 1); - } - // even if the job is already finished, we store it as "canceled" in the registry, - // so that all subsequent status requests return "canceled" - status.setCompletionTime(System.currentTimeMillis()); - status.setStatus(PrintJobStatus.Status.CANCELED); - } + PrintJobStatusImpl status = load(referenceId); - status.setError(message); - store(status); - } catch (JSONException e) { - throw ExceptionUtils.getRuntimeException(e); - } - } - - @Override - public final synchronized void fail(final String referenceId, final String message) - throws NoSuchReferenceException { - try { - PrintJobStatusImpl status = load(referenceId); + if (!forceFinal && status.getStatus() == PrintJobStatus.Status.RUNNING) { + status.setStatus(PrintJobStatus.Status.CANCELING); + } else { if (!status.isDone()) { this.registry.incrementInt(NB_PRINT_DONE, 1); this.registry.incrementLong(TOTAL_PRINT_TIME, status.getElapsedTime()); this.registry.incrementInt(LAST_PRINT_COUNT, 1); } - // even if the job is already finished, we store it as "canceled" in the registry, // so that all subsequent status requests return "canceled" status.setCompletionTime(System.currentTimeMillis()); - status.setStatus(PrintJobStatus.Status.ERROR); - status.setError(message); - store(status); - } catch (JSONException e) { - throw ExceptionUtils.getRuntimeException(e); + status.setStatus(PrintJobStatus.Status.CANCELED); } + + status.setError(message); + store(status); + } + + @Override + public final synchronized void fail(final String referenceId, final String message) + throws NoSuchReferenceException { + PrintJobStatusImpl status = load(referenceId); + if (!status.isDone()) { + this.registry.incrementInt(NB_PRINT_DONE, 1); + this.registry.incrementLong(TOTAL_PRINT_TIME, status.getElapsedTime()); + this.registry.incrementInt(LAST_PRINT_COUNT, 1); + } + + // even if the job is already finished, we store it as "canceled" in the registry, + // so that all subsequent status requests return "canceled" + status.setCompletionTime(System.currentTimeMillis()); + status.setStatus(PrintJobStatus.Status.ERROR); + status.setError(message); + store(status); } @Override @@ -190,19 +170,15 @@ public final long getWaitingJobsCount() { @Override public final PrintJobStatusImpl get(final String referenceId, final boolean external) throws NoSuchReferenceException { - try { - PrintJobStatusImpl status = load(referenceId); - status.setStatusTime(System.currentTimeMillis()); + PrintJobStatusImpl status = load(referenceId); + status.setStatusTime(System.currentTimeMillis()); - if (!status.isDone() && external) { - // remember when the status was polled for the last time - this.registry.put(LAST_POLL + referenceId, System.currentTimeMillis()); - } - - return status; - } catch (JSONException e) { - throw ExceptionUtils.getRuntimeException(e); + if (!status.isDone() && external) { + // remember when the status was polled for the last time + this.registry.put(LAST_POLL + referenceId, System.currentTimeMillis()); } + + return status; } /** @@ -266,7 +242,7 @@ private PrintJobStatusImpl load(final String referenceId) try { reportURI = new URI(metadata.getString(JSON_REPORT_URI)); } catch (URISyntaxException e) { - throw ExceptionUtils.getRuntimeException(e); + throw new PrintException("Failed to create URI for " + JSON_REPORT_URI, e); } String fileName = metadata.getString(JSON_FILENAME); String fileExt = metadata.getString(JSON_FILE_EXT); diff --git a/core/src/main/java/org/mapfish/print/servlet/job/impl/ThreadPoolJobManager.java b/core/src/main/java/org/mapfish/print/servlet/job/impl/ThreadPoolJobManager.java index aeddba05f6..e55a03e1ca 100644 --- a/core/src/main/java/org/mapfish/print/servlet/job/impl/ThreadPoolJobManager.java +++ b/core/src/main/java/org/mapfish/print/servlet/job/impl/ThreadPoolJobManager.java @@ -23,6 +23,7 @@ import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; import org.mapfish.print.ExceptionUtils; +import org.mapfish.print.PrintException; import org.mapfish.print.config.WorkingDirectories; import org.mapfish.print.servlet.job.JobManager; import org.mapfish.print.servlet.job.JobQueue; @@ -406,7 +407,7 @@ private void pollRegistry() { try { cancelJobIfRunning(stat.getReferenceId()); } catch (NoSuchReferenceException e) { - throw ExceptionUtils.getRuntimeException(e); + throw new PrintException("Failed to cancel job with id " + stat.getReferenceId(), e); } } // get new jobs to execute @@ -466,8 +467,8 @@ private boolean updateRegistry() { printJob.getEntry().getReferenceId(), "task canceled (timeout)", true); } notifyIfStopped(); - } catch (NoSuchReferenceException e) { // shouldn't really happen - throw ExceptionUtils.getRuntimeException(e); + } catch (NoSuchReferenceException e) { + throw new PrintException("Failed to update registry for " + printJob, e); } } } diff --git a/core/src/main/java/org/mapfish/print/servlet/registry/BasicRegistry.java b/core/src/main/java/org/mapfish/print/servlet/registry/BasicRegistry.java index 4998038f3d..ceb59ae415 100644 --- a/core/src/main/java/org/mapfish/print/servlet/registry/BasicRegistry.java +++ b/core/src/main/java/org/mapfish/print/servlet/registry/BasicRegistry.java @@ -5,9 +5,7 @@ import java.net.URI; import java.util.concurrent.TimeUnit; import javax.annotation.PostConstruct; -import org.json.JSONException; import org.json.JSONObject; -import org.mapfish.print.ExceptionUtils; /** * A simple implementation of {@link org.mapfish.print.servlet.registry.Registry} based on a {@link @@ -112,10 +110,6 @@ public final synchronized JSONObject getJSON(final String key) { synchronized (this) { source = (String) this.registry.getIfPresent(key); } - try { - return new JSONObject(source); - } catch (JSONException e) { - throw ExceptionUtils.getRuntimeException(e); - } + return new JSONObject(source); } } diff --git a/core/src/main/java/org/mapfish/print/wrapper/yaml/PYamlArray.java b/core/src/main/java/org/mapfish/print/wrapper/yaml/PYamlArray.java index 9dc474cb1e..d589c7c67e 100644 --- a/core/src/main/java/org/mapfish/print/wrapper/yaml/PYamlArray.java +++ b/core/src/main/java/org/mapfish/print/wrapper/yaml/PYamlArray.java @@ -4,8 +4,6 @@ import java.util.List; import java.util.Map; import org.json.JSONArray; -import org.json.JSONException; -import org.mapfish.print.ExceptionUtils; import org.mapfish.print.wrapper.ObjectMissingException; import org.mapfish.print.wrapper.PArray; import org.mapfish.print.wrapper.PElement; @@ -108,11 +106,7 @@ public final Object get(final int i) { @Override public final String toString() { - try { - return "PYaml(" + getCurrentPath() + ":" + toJSON().getInternalArray().toString(2) + ")"; - } catch (JSONException e) { - throw ExceptionUtils.getRuntimeException(e); - } + return "PYaml(" + getCurrentPath() + ":" + toJSON().getInternalArray().toString(2) + ")"; } /** Convert this object to a json array. */