This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/main by this push: new 944251bd426 CAMEL-20509: Polished 944251bd426 is described below commit 944251bd426a71734c106f905fba3505f025b745 Author: Claus Ibsen <claus.ib...@gmail.com> AuthorDate: Sat Mar 2 11:16:25 2024 +0100 CAMEL-20509: Polished --- .../impl/engine/DefaultResourceResolvers.java | 70 +++++++--------------- .../apache/camel/support/RouteTemplateHelper.java | 3 +- 2 files changed, 24 insertions(+), 49 deletions(-) diff --git a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultResourceResolvers.java b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultResourceResolvers.java index 21e29e19c55..21f85c3239d 100644 --- a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultResourceResolvers.java +++ b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultResourceResolvers.java @@ -41,6 +41,7 @@ import org.apache.camel.support.ResourceSupport; import org.apache.camel.util.FileUtil; public final class DefaultResourceResolvers { + private DefaultResourceResolvers() { } @@ -57,8 +58,7 @@ public final class DefaultResourceResolvers { @Override public Resource createResource(String location, String remaining) { - final File path = new File(tryDecodeUri(remaining)); - + final File path = new File(getPath(remaining)); return new ResourceSupport(SCHEME, location) { @Override public boolean exists() { @@ -78,23 +78,10 @@ public final class DefaultResourceResolvers { if (path.isDirectory()) { throw new FileNotFoundException(path + " is a directory"); } - return new FileInputStream(path); } }; } - - protected String tryDecodeUri(String uri) { - try { - // try to decode as the uri may contain %20 for spaces etc - uri = URLDecoder.decode(uri, StandardCharsets.UTF_8); - } catch (Exception e) { - getLogger().trace("Error URL decoding uri using UTF-8 encoding: {}. This exception is ignored.", uri); - // ignore - } - - return uri; - } } /** @@ -145,7 +132,6 @@ public final class DefaultResourceResolvers { @Override public Resource createResource(String location, String remaining) { final String path = getPath(remaining); - return new ResourceSupport(SCHEME, location) { @Override public boolean exists() { @@ -157,13 +143,11 @@ public final class DefaultResourceResolvers { URL url = getCamelContext() .getClassResolver() .loadResourceAsURL(path); - try { return url != null ? url.toURI() : null; } catch (URISyntaxException e) { throw new IllegalArgumentException(e); } - } @Override @@ -174,27 +158,6 @@ public final class DefaultResourceResolvers { } }; } - - private String getPath(String location) { - // skip leading double slashes - if (location.startsWith("//")) { - location = location.substring(2); - } - String uri = tryDecodeUri(location); - return FileUtil.compactPath(uri, '/'); - } - - protected String tryDecodeUri(String uri) { - try { - // try to decode as the uri may contain %20 for spaces etc - uri = URLDecoder.decode(uri, StandardCharsets.UTF_8); - } catch (Exception e) { - getLogger().trace("Error URL decoding uri using UTF-8 encoding: {}. This exception is ignored.", uri); - // ignore - } - - return uri; - } } /** @@ -224,7 +187,6 @@ public final class DefaultResourceResolvers { if (!exists()) { throw new IOException("There is no bean in the registry with name " + remaining + " and type String"); } - return new ByteArrayInputStream(val.getBytes()); } }; @@ -255,7 +217,6 @@ public final class DefaultResourceResolvers { if (!exists()) { throw new IOException("No base64 content defined"); } - final byte[] decoded = Base64.getDecoder().decode(remaining); return new ByteArrayInputStream(decoded); } @@ -288,10 +249,8 @@ public final class DefaultResourceResolvers { if (!exists()) { throw new IOException("No gzip content defined"); } - final byte[] decoded = Base64.getDecoder().decode(remaining); final InputStream is = new ByteArrayInputStream(decoded); - return new GZIPInputStream(is); } }; @@ -322,7 +281,6 @@ public final class DefaultResourceResolvers { if (!exists()) { throw new IOException("No memory content defined"); } - return new ByteArrayInputStream(remaining.getBytes()); } }; @@ -339,14 +297,11 @@ public final class DefaultResourceResolvers { @Override public boolean exists() { URLConnection connection = null; - try { connection = new URL(getLocation()).openConnection(); - if (connection instanceof HttpURLConnection) { return ((HttpURLConnection) connection).getResponseCode() == HttpURLConnection.HTTP_OK; } - return connection.getContentLengthLong() > 0; } catch (IOException e) { throw new IllegalArgumentException(e); @@ -363,7 +318,6 @@ public final class DefaultResourceResolvers { public InputStream getInputStream() throws IOException { URLConnection con = new URL(getLocation()).openConnection(); con.setUseCaches(false); - try { setContentType(con.getContentType()); return con.getInputStream(); @@ -387,4 +341,24 @@ public final class DefaultResourceResolvers { this.contentType = contentType; } } + + private static String getPath(String location) { + // skip leading double slashes + if (location.startsWith("//")) { + location = location.substring(2); + } + String uri = tryDecodeUri(location); + return FileUtil.compactPath(uri, '/'); + } + + private static String tryDecodeUri(String uri) { + try { + // try to decode as the uri may contain %20 for spaces etc + uri = URLDecoder.decode(uri, StandardCharsets.UTF_8); + } catch (Exception e) { + // ignore + } + return uri; + } + } diff --git a/core/camel-support/src/main/java/org/apache/camel/support/RouteTemplateHelper.java b/core/camel-support/src/main/java/org/apache/camel/support/RouteTemplateHelper.java index 0f8c8cfb052..8fb14497ba5 100644 --- a/core/camel-support/src/main/java/org/apache/camel/support/RouteTemplateHelper.java +++ b/core/camel-support/src/main/java/org/apache/camel/support/RouteTemplateHelper.java @@ -38,7 +38,7 @@ public final class RouteTemplateHelper { /** * Loads the route template with the given template id from a given location. After the template is loaded, it is - * automatic added to the {@link CamelContext}. + * automatically added to the {@link CamelContext}. * * @param camelContext the camel context * @param listener optional listener for when a route template is being loaded @@ -50,6 +50,7 @@ public final class RouteTemplateHelper { CamelContext camelContext, RouteTemplateLoaderListener listener, String templateId, String location) throws Exception { + if (location == null) { throw new IllegalArgumentException("Location is empty"); }