This is an automated email from the ASF dual-hosted git repository. acosentino pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/master by this push: new 38e5ee1 Fixed CS for Camel-Util 38e5ee1 is described below commit 38e5ee102ba5393ed0b821e31eae408a716c1e60 Author: Andrea Cosentino <anco...@gmail.com> AuthorDate: Thu Jan 31 11:30:48 2019 +0100 Fixed CS for Camel-Util --- .../main/java/org/apache/camel/util/IOHelper.java | 93 ++++++++------ .../src/main/java/org/apache/camel/util/Pair.java | 13 +- .../java/org/apache/camel/util/URISupport.java | 141 ++++++++++++--------- 3 files changed, 141 insertions(+), 106 deletions(-) diff --git a/camel-util/src/main/java/org/apache/camel/util/IOHelper.java b/camel-util/src/main/java/org/apache/camel/util/IOHelper.java index 3acc734..fbac259 100644 --- a/camel-util/src/main/java/org/apache/camel/util/IOHelper.java +++ b/camel-util/src/main/java/org/apache/camel/util/IOHelper.java @@ -48,7 +48,7 @@ import org.slf4j.LoggerFactory; * IO helper class. */ public final class IOHelper { - + public static Supplier<Charset> defaultCharset = Charset::defaultCharset; public static final int DEFAULT_BUFFER_SIZE = 1024 * 4; @@ -56,17 +56,18 @@ public final class IOHelper { private static final Logger LOG = LoggerFactory.getLogger(IOHelper.class); private static final Charset UTF8_CHARSET = Charset.forName("UTF-8"); - // allows to turn on backwards compatible to turn off regarding the first read byte with value zero (0b0) as EOL. + // allows to turn on backwards compatible to turn off regarding the first + // read byte with value zero (0b0) as EOL. // See more at CAMEL-11672 - private static final boolean ZERO_BYTE_EOL_ENABLED = - "true".equalsIgnoreCase(System.getProperty("camel.zeroByteEOLEnabled", "true")); + private static final boolean ZERO_BYTE_EOL_ENABLED = "true".equalsIgnoreCase(System.getProperty("camel.zeroByteEOLEnabled", "true")); private IOHelper() { // Utility Class } - + /** - * Use this function instead of new String(byte[]) to avoid surprises from non-standard default encodings. + * Use this function instead of new String(byte[]) to avoid surprises from + * non-standard default encodings. */ public static String newStringFromBytes(byte[] bytes) { try { @@ -77,8 +78,8 @@ public final class IOHelper { } /** - * Use this function instead of new String(byte[], int, int) - * to avoid surprises from non-standard default encodings. + * Use this function instead of new String(byte[], int, int) to avoid + * surprises from non-standard default encodings. */ public static String newStringFromBytes(byte[] bytes, int start, int length) { try { @@ -195,8 +196,7 @@ public final class IOHelper { } if (LOG.isTraceEnabled()) { - LOG.trace("Copying InputStream: {} -> OutputStream: {} with buffer: {} and flush on each write {}", - new Object[]{input, output, bufferSize, flushOnEachWrite}); + LOG.trace("Copying InputStream: {} -> OutputStream: {} with buffer: {} and flush on each write {}", new Object[] {input, output, bufferSize, flushOnEachWrite}); } int total = 0; @@ -205,7 +205,8 @@ public final class IOHelper { boolean hasData; if (ZERO_BYTE_EOL_ENABLED) { - // workaround issue on some application servers which can return 0 (instead of -1) + // workaround issue on some application servers which can return 0 + // (instead of -1) // as first byte to indicate end of stream (CAMEL-11672) hasData = n > 0; } else { @@ -227,11 +228,11 @@ public final class IOHelper { } return total; } - + public static void copyAndCloseInput(InputStream input, OutputStream output) throws IOException { copyAndCloseInput(input, output, DEFAULT_BUFFER_SIZE); } - + public static void copyAndCloseInput(InputStream input, OutputStream output, int bufferSize) throws IOException { copy(input, output, bufferSize); close(input, null, LOG); @@ -251,11 +252,13 @@ public final class IOHelper { } /** - * Forces any updates to this channel's file to be written to the storage device that contains it. + * Forces any updates to this channel's file to be written to the storage + * device that contains it. * * @param channel the file channel * @param name the name of the resource - * @param log the log to use when reporting warnings, will use this class's own {@link Logger} if <tt>log == null</tt> + * @param log the log to use when reporting warnings, will use this class's + * own {@link Logger} if <tt>log == null</tt> */ public static void force(FileChannel channel, String name, Logger log) { try { @@ -276,11 +279,13 @@ public final class IOHelper { } /** - * Forces any updates to a FileOutputStream be written to the storage device that contains it. + * Forces any updates to a FileOutputStream be written to the storage device + * that contains it. * * @param os the file output stream * @param name the name of the resource - * @param log the log to use when reporting warnings, will use this class's own {@link Logger} if <tt>log == null</tt> + * @param log the log to use when reporting warnings, will use this class's + * own {@link Logger} if <tt>log == null</tt> */ public static void force(FileOutputStream os, String name, Logger log) { try { @@ -305,9 +310,11 @@ public final class IOHelper { * An associated FileOutputStream can optionally be forced to disk. * * @param writer the writer to close - * @param os an underlying FileOutputStream that will to be forced to disk according to the force parameter + * @param os an underlying FileOutputStream that will to be forced to disk + * according to the force parameter * @param name the name of the resource - * @param log the log to use when reporting warnings, will use this class's own {@link Logger} if <tt>log == null</tt> + * @param log the log to use when reporting warnings, will use this class's + * own {@link Logger} if <tt>log == null</tt> * @param force forces the FileOutputStream to disk */ public static void close(Writer writer, FileOutputStream os, String name, Logger log, boolean force) { @@ -332,11 +339,13 @@ public final class IOHelper { } /** - * Closes the given resource if it is available, logging any closing exceptions to the given log. + * Closes the given resource if it is available, logging any closing + * exceptions to the given log. * * @param closeable the object to close * @param name the name of the resource - * @param log the log to use when reporting closure warnings, will use this class's own {@link Logger} if <tt>log == null</tt> + * @param log the log to use when reporting closure warnings, will use this + * class's own {@link Logger} if <tt>log == null</tt> */ public static void close(Closeable closeable, String name, Logger log) { if (closeable != null) { @@ -355,13 +364,14 @@ public final class IOHelper { } } } - + /** - * Closes the given resource if it is available and don't catch the exception + * Closes the given resource if it is available and don't catch the + * exception * * @param closeable the object to close * @throws IOException - */ + */ public static void closeWithException(Closeable closeable) throws IOException { if (closeable != null) { closeable.close(); @@ -369,12 +379,14 @@ public final class IOHelper { } /** - * Closes the given channel if it is available, logging any closing exceptions to the given log. - * The file's channel can optionally be forced to disk. + * Closes the given channel if it is available, logging any closing + * exceptions to the given log. The file's channel can optionally be forced + * to disk. * * @param channel the file channel * @param name the name of the resource - * @param log the log to use when reporting warnings, will use this class's own {@link Logger} if <tt>log == null</tt> + * @param log the log to use when reporting warnings, will use this class's + * own {@link Logger} if <tt>log == null</tt> * @param force forces the file channel to disk */ public static void close(FileChannel channel, String name, Logger log, boolean force) { @@ -416,10 +428,10 @@ public final class IOHelper { public static void closeIterator(Object it) throws IOException { if (it instanceof Closeable) { - IOHelper.closeWithException((Closeable) it); + IOHelper.closeWithException((Closeable)it); } if (it instanceof java.util.Scanner) { - IOException ioException = ((java.util.Scanner) it).ioException(); + IOException ioException = ((java.util.Scanner)it).ioException(); if (ioException != null) { throw ioException; } @@ -463,14 +475,15 @@ public final class IOHelper { close(isr, in); } } - + /** * Get the charset name from the content type string + * * @param contentType * @return the charset name, or <tt>UTF-8</tt> if no found */ public static String getCharsetNameFromContentType(String contentType) { - String[] values = contentType.split(";"); + String[] values = contentType.split(";"); String charset = ""; for (String value : values) { @@ -481,7 +494,7 @@ public final class IOHelper { } } if ("".equals(charset)) { - charset = "UTF-8"; + charset = "UTF-8"; } return normalizeCharset(charset); @@ -504,7 +517,7 @@ public final class IOHelper { return null; } } - + /** * Encoding-aware input stream. */ @@ -553,7 +566,7 @@ public final class IOHelper { } /** - * Encoding-aware file reader. + * Encoding-aware file reader. */ public static class EncodingFileReader extends InputStreamReader { @@ -563,8 +576,7 @@ public final class IOHelper { * @param in file to read * @param charset character set to use */ - public EncodingFileReader(FileInputStream in, String charset) - throws FileNotFoundException, UnsupportedEncodingException { + public EncodingFileReader(FileInputStream in, String charset) throws FileNotFoundException, UnsupportedEncodingException { super(in, charset); this.in = in; } @@ -578,9 +590,9 @@ public final class IOHelper { } } } - + /** - * Encoding-aware file writer. + * Encoding-aware file writer. */ public static class EncodingFileWriter extends OutputStreamWriter { @@ -590,8 +602,7 @@ public final class IOHelper { * @param out file to write * @param charset character set to use */ - public EncodingFileWriter(FileOutputStream out, String charset) - throws FileNotFoundException, UnsupportedEncodingException { + public EncodingFileWriter(FileOutputStream out, String charset) throws FileNotFoundException, UnsupportedEncodingException { super(out, charset); this.out = out; } @@ -605,7 +616,7 @@ public final class IOHelper { } } } - + public static BufferedReader toReader(File file, String charset) throws IOException { FileInputStream in = new FileInputStream(file); return IOHelper.buffered(new EncodingFileReader(in, charset)); diff --git a/camel-util/src/main/java/org/apache/camel/util/Pair.java b/camel-util/src/main/java/org/apache/camel/util/Pair.java index 8f4e3b4..4cdc670 100644 --- a/camel-util/src/main/java/org/apache/camel/util/Pair.java +++ b/camel-util/src/main/java/org/apache/camel/util/Pair.java @@ -41,11 +41,14 @@ public class Pair<T> { @Override public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - Pair<?> that = (Pair<?>) o; - return Objects.equals(left, that.left) && - Objects.equals(right, that.right); + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + Pair<?> that = (Pair<?>)o; + return Objects.equals(left, that.left) && Objects.equals(right, that.right); } @Override diff --git a/camel-util/src/main/java/org/apache/camel/util/URISupport.java b/camel-util/src/main/java/org/apache/camel/util/URISupport.java index 7c93201..215da85 100644 --- a/camel-util/src/main/java/org/apache/camel/util/URISupport.java +++ b/camel-util/src/main/java/org/apache/camel/util/URISupport.java @@ -33,22 +33,22 @@ import java.util.regex.Pattern; public final class URISupport { public static final String RAW_TOKEN_PREFIX = "RAW"; - public static final char[] RAW_TOKEN_START = { '(', '{' }; - public static final char[] RAW_TOKEN_END = { ')', '}' }; + public static final char[] RAW_TOKEN_START = {'(', '{'}; + public static final char[] RAW_TOKEN_END = {')', '}'}; // Match any key-value pair in the URI query string whose key contains // "passphrase" or "password" or secret key (case-insensitive). // First capture group is the key, second is the value. - private static final Pattern SECRETS = Pattern.compile( - "([?&][^=]*(?:passphrase|password|secretKey)[^=]*)=(RAW[({].*[)}]|[^&]*)", - Pattern.CASE_INSENSITIVE); + private static final Pattern SECRETS = Pattern.compile("([?&][^=]*(?:passphrase|password|secretKey)[^=]*)=(RAW[({].*[)}]|[^&]*)", Pattern.CASE_INSENSITIVE); // Match the user password in the URI as second capture group - // (applies to URI with authority component and userinfo token in the form "user:password"). + // (applies to URI with authority component and userinfo token in the form + // "user:password"). private static final Pattern USERINFO_PASSWORD = Pattern.compile("(.*://.*:)(.*)(@)"); // Match the user password in the URI path as second capture group - // (applies to URI path with authority component and userinfo token in the form "user:password"). + // (applies to URI path with authority component and userinfo token in the + // form "user:password"). private static final Pattern PATH_USERINFO_PASSWORD = Pattern.compile("(.*:)(.*)(@)"); private static final String CHARSET = "UTF-8"; @@ -58,12 +58,13 @@ public final class URISupport { } /** - * Removes detected sensitive information (such as passwords) from the URI and returns the result. + * Removes detected sensitive information (such as passwords) from the URI + * and returns the result. * * @param uri The uri to sanitize. * @see #SECRETS and #USERINFO_PASSWORD for the matched pattern - * - * @return Returns null if the uri is null, otherwise the URI with the passphrase, password or secretKey sanitized. + * @return Returns null if the uri is null, otherwise the URI with the + * passphrase, password or secretKey sanitized. */ public static String sanitizeUri(String uri) { // use xxxxx as replacement as that works well with JMX also @@ -92,9 +93,10 @@ public final class URISupport { } /** - * Extracts the scheme specific path from the URI that is used as the remainder option when creating endpoints. + * Extracts the scheme specific path from the URI that is used as the + * remainder option when creating endpoints. * - * @param u the URI + * @param u the URI * @param useRaw whether to force using raw values * @return the remainder path */ @@ -116,9 +118,10 @@ public final class URISupport { /** * Parses the query part of the uri (eg the parameters). * <p/> - * The URI parameters will by default be URI encoded. However you can define a parameter - * values with the syntax: <tt>key=RAW(value)</tt> which tells Camel to not encode the value, - * and use the value as is (eg key=value) and the value has <b>not</b> been encoded. + * The URI parameters will by default be URI encoded. However you can define + * a parameter values with the syntax: <tt>key=RAW(value)</tt> which tells + * Camel to not encode the value, and use the value as is (eg key=value) and + * the value has <b>not</b> been encoded. * * @param uri the uri * @return the parameters, or an empty map if no parameters (eg never null) @@ -134,9 +137,10 @@ public final class URISupport { /** * Parses the query part of the uri (eg the parameters). * <p/> - * The URI parameters will by default be URI encoded. However you can define a parameter - * values with the syntax: <tt>key=RAW(value)</tt> which tells Camel to not encode the value, - * and use the value as is (eg key=value) and the value has <b>not</b> been encoded. + * The URI parameters will by default be URI encoded. However you can define + * a parameter values with the syntax: <tt>key=RAW(value)</tt> which tells + * Camel to not encode the value, and use the value as is (eg key=value) and + * the value has <b>not</b> been encoded. * * @param uri the uri * @param useRaw whether to force using raw values @@ -153,13 +157,16 @@ public final class URISupport { /** * Parses the query part of the uri (eg the parameters). * <p/> - * The URI parameters will by default be URI encoded. However you can define a parameter - * values with the syntax: <tt>key=RAW(value)</tt> which tells Camel to not encode the value, - * and use the value as is (eg key=value) and the value has <b>not</b> been encoded. + * The URI parameters will by default be URI encoded. However you can define + * a parameter values with the syntax: <tt>key=RAW(value)</tt> which tells + * Camel to not encode the value, and use the value as is (eg key=value) and + * the value has <b>not</b> been encoded. * * @param uri the uri * @param useRaw whether to force using raw values - * @param lenient whether to parse lenient and ignore trailing & markers which has no key or value which can happen when using HTTP components + * @param lenient whether to parse lenient and ignore trailing & markers + * which has no key or value which can happen when using HTTP + * components * @return the parameters, or an empty map if no parameters (eg never null) * @throws URISyntaxException is thrown if uri has invalid syntax. * @see #RAW_TOKEN_PREFIX @@ -174,8 +181,7 @@ public final class URISupport { // must check for trailing & as the uri.split("&") will ignore those if (!lenient && uri.endsWith("&")) { - throw new URISyntaxException(uri, "Invalid uri syntax: Trailing & marker found. " - + "Check the uri and remove the trailing & marker."); + throw new URISyntaxException(uri, "Invalid uri syntax: Trailing & marker found. " + "Check the uri and remove the trailing & marker."); } URIScanner scanner = new URIScanner(CHARSET); @@ -183,14 +189,15 @@ public final class URISupport { } /** - * Scans RAW tokens in the string and returns the list of pair indexes which tell where - * a RAW token starts and ends in the string. + * Scans RAW tokens in the string and returns the list of pair indexes which + * tell where a RAW token starts and ends in the string. * <p/> - * This is a companion method with {@link #isRaw(int, List)} and the returned value is - * supposed to be used as the parameter of that method. + * This is a companion method with {@link #isRaw(int, List)} and the + * returned value is supposed to be used as the parameter of that method. * * @param str the string to scan RAW tokens - * @return the list of pair indexes which represent the start and end positions of a RAW token + * @return the list of pair indexes which represent the start and end + * positions of a RAW token * @see #isRaw(int, List) * @see #RAW_TOKEN_PREFIX * @see #RAW_TOKEN_START @@ -201,15 +208,18 @@ public final class URISupport { } /** - * Tests if the index is within any pair of the start and end indexes which represent - * the start and end positions of a RAW token. + * Tests if the index is within any pair of the start and end indexes which + * represent the start and end positions of a RAW token. * <p/> - * This is a companion method with {@link #scanRaw(String)} and is supposed to consume - * the returned value of that method as the second parameter <tt>pairs</tt>. + * This is a companion method with {@link #scanRaw(String)} and is supposed + * to consume the returned value of that method as the second parameter + * <tt>pairs</tt>. * * @param index the index to be tested - * @param pairs the list of pair indexes which represent the start and end positions of a RAW token - * @return <tt>true</tt> if the index is within any pair of the indexes, <tt>false</tt> otherwise + * @param pairs the list of pair indexes which represent the start and end + * positions of a RAW token + * @return <tt>true</tt> if the index is within any pair of the indexes, + * <tt>false</tt> otherwise * @see #scanRaw(String) * @see #RAW_TOKEN_PREFIX * @see #RAW_TOKEN_START @@ -244,9 +254,10 @@ public final class URISupport { } /** - * Traverses the given parameters, and resolve any parameter values which uses the RAW token - * syntax: <tt>key=RAW(value)</tt>. This method will then remove the RAW tokens, and replace - * the content of the value, with just the value. + * Traverses the given parameters, and resolve any parameter values which + * uses the RAW token syntax: <tt>key=RAW(value)</tt>. This method will then + * remove the RAW tokens, and replace the content of the value, with just + * the value. * * @param parameters the uri parameters * @see #parseQuery(String) @@ -263,7 +274,7 @@ public final class URISupport { // if the value is a list then we need to iterate Object value = entry.getValue(); if (value instanceof List) { - List list = (List) value; + List list = (List)value; for (int i = 0; i < list.size(); i++) { Object obj = list.get(i); if (obj == null) { @@ -294,7 +305,8 @@ public final class URISupport { public static URI createURIWithQuery(URI uri, String query) throws URISyntaxException { ObjectHelper.notNull(uri, "uri"); - // assemble string as new uri and replace parameters with the query instead + // assemble string as new uri and replace parameters with the query + // instead String s = uri.toString(); String before = StringHelper.before(s, "?"); if (before == null) { @@ -318,7 +330,7 @@ public final class URISupport { * <p/> * Returns the value as-is if not starting with the prefix. * - * @param value the value + * @param value the value * @param prefix the prefix to remove from value * @return the value without the prefix */ @@ -358,8 +370,9 @@ public final class URISupport { /** * Assembles a query from the given map. * - * @param options the map with the options (eg key/value pairs) - * @return a query string with <tt>key1=value&key2=value2&...</tt>, or an empty string if there is no options. + * @param options the map with the options (eg key/value pairs) + * @return a query string with <tt>key1=value&key2=value2&...</tt>, or an + * empty string if there is no options. * @throws URISyntaxException is thrown if uri has invalid syntax. */ @SuppressWarnings("unchecked") @@ -375,16 +388,18 @@ public final class URISupport { rc.append("&"); } - String key = (String) o; + String key = (String)o; Object value = options.get(key); - // the value may be a list since the same key has multiple values + // the value may be a list since the same key has multiple + // values if (value instanceof List) { - List<String> list = (List<String>) value; + List<String> list = (List<String>)value; for (Iterator<String> it = list.iterator(); it.hasNext();) { String s = it.next(); appendQueryStringParameter(key, s, rc); - // append & separator if there is more in the list to append + // append & separator if there is more in the list + // to append if (it.hasNext()) { rc.append("&"); } @@ -440,13 +455,14 @@ public final class URISupport { /** * Appends the given parameters to the given URI. * <p/> - * It keeps the original parameters and if a new parameter is already defined in - * {@code originalURI}, it will be replaced by its value in {@code newParameters}. + * It keeps the original parameters and if a new parameter is already + * defined in {@code originalURI}, it will be replaced by its value in + * {@code newParameters}. * - * @param originalURI the original URI + * @param originalURI the original URI * @param newParameters the parameters to add * @return the URI with all the parameters - * @throws URISyntaxException is thrown if the uri syntax is invalid + * @throws URISyntaxException is thrown if the uri syntax is invalid * @throws UnsupportedEncodingException is thrown if encoding error */ public static String appendParametersToURI(String originalURI, Map<String, Object> newParameters) throws URISyntaxException, UnsupportedEncodingException { @@ -457,12 +473,13 @@ public final class URISupport { } /** - * Normalizes the uri by reordering the parameters so they are sorted and thus - * we can use the uris for endpoint matching. + * Normalizes the uri by reordering the parameters so they are sorted and + * thus we can use the uris for endpoint matching. * <p/> - * The URI parameters will by default be URI encoded. However you can define a parameter - * values with the syntax: <tt>key=RAW(value)</tt> which tells Camel to not encode the value, - * and use the value as is (eg key=value) and the value has <b>not</b> been encoded. + * The URI parameters will by default be URI encoded. However you can define + * a parameter values with the syntax: <tt>key=RAW(value)</tt> which tells + * Camel to not encode the value, and use the value as is (eg key=value) and + * the value has <b>not</b> been encoded. * * @param uri the uri * @return the normalized uri @@ -499,10 +516,14 @@ public final class URISupport { path = UnsafeUriCharactersEncoder.encode(path); } - // okay if we have user info in the path and they use @ in username or password, - // then we need to encode them (but leave the last @ sign before the hostname) - // this is needed as Camel end users may not encode their user info properly, but expect - // this to work out of the box with Camel, and hence we need to fix it for them + // okay if we have user info in the path and they use @ in username or + // password, + // then we need to encode them (but leave the last @ sign before the + // hostname) + // this is needed as Camel end users may not encode their user info + // properly, but expect + // this to work out of the box with Camel, and hence we need to fix it + // for them String userInfoPath = path; if (userInfoPath.contains("/")) { userInfoPath = userInfoPath.substring(0, userInfoPath.indexOf("/"));