This is an automated email from the ASF dual-hosted git repository.
cziegeler pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/felix-dev.git
The following commit(s) were added to refs/heads/master by this push:
new 39f09f7f1f FELIX-6532 : Remove dependency to commons-io
39f09f7f1f is described below
commit 39f09f7f1f813794d5448bf8deece40b23a245b2
Author: Carsten Ziegeler <[email protected]>
AuthorDate: Thu May 19 15:04:40 2022 +0200
FELIX-6532 : Remove dependency to commons-io
---
webconsole/pom.xml | 4 +-
.../felix/webconsole/AbstractWebConsolePlugin.java | 164 ++++++++++-----------
.../felix/webconsole/DefaultBrandingPlugin.java | 18 +--
.../internal/core/BaseUpdateInstallHelper.java | 10 +-
.../i18n/ConsolePropertyResourceBundle.java | 20 +--
.../webconsole/internal/misc/LicenseServlet.java | 103 +++++--------
.../webconsole/internal/servlet/OsgiManager.java | 25 ++--
7 files changed, 142 insertions(+), 202 deletions(-)
diff --git a/webconsole/pom.xml b/webconsole/pom.xml
index 438570d060..48847c2e75 100644
--- a/webconsole/pom.xml
+++ b/webconsole/pom.xml
@@ -197,8 +197,8 @@
<profiles>
<!--
The "all-in-one-bundle" profile builds a bundle including certain
- 3rd party libraries and classes, namely: Commons IO, Commons
- FileUpload, and the OSGi ServiceTracker.
+ 3rd party libraries and classes, namely: Commons IO (needed by
+ Commons FileUpload), Commons FileUpload.
The "all-in-one-bundle" can be used in deployments where these
dependencies need not be deployed separately.
-->
diff --git
a/webconsole/src/main/java/org/apache/felix/webconsole/AbstractWebConsolePlugin.java
b/webconsole/src/main/java/org/apache/felix/webconsole/AbstractWebConsolePlugin.java
index ab9b7f6f8a..5b15f9a487 100644
---
a/webconsole/src/main/java/org/apache/felix/webconsole/AbstractWebConsolePlugin.java
+++
b/webconsole/src/main/java/org/apache/felix/webconsole/AbstractWebConsolePlugin.java
@@ -17,21 +17,33 @@
package org.apache.felix.webconsole;
-import java.io.*;
-import java.lang.reflect.*;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.PrintWriter;
+import java.io.StringWriter;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
import java.net.URL;
import java.net.URLConnection;
+import java.nio.charset.StandardCharsets;
import java.security.AccessController;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
-import java.util.*;
+import java.util.Iterator;
+import java.util.Locale;
+import java.util.Map;
+import java.util.SortedMap;
+import java.util.TreeMap;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
-import javax.servlet.http.*;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
-import org.apache.commons.io.IOUtils;
import org.apache.felix.webconsole.internal.servlet.OsgiManager;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
@@ -518,10 +530,10 @@ public abstract class AbstractWebConsolePlugin extends
HttpServlet
// (java.lang.RuntimePermission "accessDeclaredMembers")
// (java.lang.reflect.ReflectPermission "suppressAccessChecks")
// See also https://issues.apache.org/jira/browse/FELIX-4652
- final Boolean ret = (Boolean) AccessController.doPrivileged(new
PrivilegedExceptionAction()
+ final Boolean ret = AccessController.doPrivileged(new
PrivilegedExceptionAction<Boolean>()
{
- public Object run() throws Exception
+ public Boolean run() throws Exception
{
return spoolResource0(request, response) ? Boolean.TRUE :
Boolean.FALSE;
}
@@ -546,15 +558,13 @@ public abstract class AbstractWebConsolePlugin extends
HttpServlet
}
String pi = request.getPathInfo();
- InputStream ins = null;
try
{
// check for a resource, fail if none
URL url = ( URL ) getResourceMethod.invoke( getResourceProvider(),
new Object[]
{ pi } );
- if ( url == null )
- {
+ if ( url == null ) {
return false;
}
@@ -562,62 +572,52 @@ public abstract class AbstractWebConsolePlugin extends
HttpServlet
// to at least hint to close the connection because there is no
// method to explicitly close the conneciton, unfortunately)
URLConnection connection = url.openConnection();
- ins = connection.getInputStream();
-
- // FELIX-2017 Equinox may return an URL for a non-existing
- // resource but then (instead of throwing) return null on
- // getInputStream. We should account for this situation and
- // just assume a non-existing resource in this case.
- if (ins == null) {
- return false;
- }
+ try ( InputStream ins = connection.getInputStream()) {
+ // FELIX-2017 Equinox may return an URL for a non-existing
+ // resource but then (instead of throwing) return null on
+ // getInputStream. We should account for this situation and
+ // just assume a non-existing resource in this case.
+ if (ins == null) {
+ return false;
+ }
- // check whether we may return 304/UNMODIFIED
- long lastModified = connection.getLastModified();
- if ( lastModified > 0 )
- {
- long ifModifiedSince = request.getDateHeader(
"If-Modified-Since" ); //$NON-NLS-1$
- if ( ifModifiedSince >= ( lastModified / 1000 * 1000 ) )
+ // check whether we may return 304/UNMODIFIED
+ long lastModified = connection.getLastModified();
+ if ( lastModified > 0 )
{
- // Round down to the nearest second for a proper compare
- // A ifModifiedSince of -1 will always be less
- response.setStatus( HttpServletResponse.SC_NOT_MODIFIED );
+ long ifModifiedSince = request.getDateHeader(
"If-Modified-Since" ); //$NON-NLS-1$
+ if ( ifModifiedSince >= ( lastModified / 1000 * 1000 ) )
+ {
+ // Round down to the nearest second for a proper
compare
+ // A ifModifiedSince of -1 will always be less
+ response.setStatus(
HttpServletResponse.SC_NOT_MODIFIED );
+
+ return true;
+ }
- return true;
+ // have to send, so set the last modified header now
+ response.setDateHeader( "Last-Modified", lastModified );
//$NON-NLS-1$
}
- // have to send, so set the last modified header now
- response.setDateHeader( "Last-Modified", lastModified );
//$NON-NLS-1$
- }
+ // describe the contents
+ response.setContentType( getServletContext().getMimeType( pi )
);
+ response.setIntHeader( "Content-Length",
connection.getContentLength() ); //$NON-NLS-1$
- // describe the contents
- response.setContentType( getServletContext().getMimeType( pi ) );
- response.setIntHeader( "Content-Length",
connection.getContentLength() ); //$NON-NLS-1$
+ // spool the actual contents
+ OutputStream out = response.getOutputStream();
+ byte[] buf = new byte[2048];
+ int rd;
+ while ( ( rd = ins.read( buf ) ) >= 0 )
+ {
+ out.write( buf, 0, rd );
+ }
- // spool the actual contents
- OutputStream out = response.getOutputStream();
- byte[] buf = new byte[2048];
- int rd;
- while ( ( rd = ins.read( buf ) ) >= 0 )
- {
- out.write( buf, 0, rd );
+ // over and out ...
+ return true;
}
- // over and out ...
- return true;
- }
- catch ( IllegalAccessException iae )
- {
- // log or throw ???
- }
- catch ( InvocationTargetException ite )
- {
+ } catch ( IllegalAccessException | InvocationTargetException ignore ) {
// log or throw ???
- // Throwable cause = ite.getTargetException();
- }
- finally
- {
- IOUtils.closeQuietly(ins);
}
return false;
@@ -935,33 +935,33 @@ public abstract class AbstractWebConsolePlugin extends
HttpServlet
return readTemplateFile( getClass(), templateFile );
}
- private final String readTemplateFile( final Class clazz, final String
templateFile)
- {
- InputStream templateStream = clazz.getResourceAsStream( templateFile );
- if ( templateStream != null )
- {
- try
- {
- String str = IOUtils.toString( templateStream, "UTF-8" );
//$NON-NLS-1$
- switch ( str.charAt(0) )
- { // skip BOM
- case 0xFEFF: // UTF-16/UTF-32, big-endian
- case 0xFFFE: // UTF-16, little-endian
- case 0xEFBB: // UTF-8
- return str.substring(1);
- }
- return str;
- }
- catch ( IOException e )
- {
- // don't use new Exception(message, cause) because cause is
1.4+
- throw new RuntimeException( "readTemplateFile: Error loading "
+ templateFile + ": " + e ); //$NON-NLS-1$ //$NON-NLS-2$
- }
- finally
- {
- IOUtils.closeQuietly( templateStream );
+ private final String readTemplateFile( final Class clazz, final String
templateFile) {
+
+ try(InputStream templateStream = clazz.getResourceAsStream(
templateFile )) {
+ if ( templateStream != null ) {
+ try ( final StringWriter w = new StringWriter()) {
+ final byte[] buf = new byte[2048];
+ int l;
+ while ( ( l = templateStream.read(buf)) > 0 ) {
+ w.write(new String(buf, 0, l, StandardCharsets.UTF_8));
+ }
+ String str = w.toString();
+ switch ( str.charAt(0) )
+ { // skip BOM
+ case 0xFEFF: // UTF-16/UTF-32, big-endian
+ case 0xFFFE: // UTF-16, little-endian
+ case 0xEFBB: // UTF-8
+ return str.substring(1);
+ }
+ return str;
+ }
}
}
+ catch ( IOException e )
+ {
+ // don't use new Exception(message, cause) because cause is 1.4+
+ throw new RuntimeException( "readTemplateFile: Error loading " +
templateFile + ": " + e ); //$NON-NLS-1$ //$NON-NLS-2$
+ }
// template file does not exist, return an empty string
log( LogService.LOG_ERROR, "readTemplateFile: File '" + templateFile +
"' not found through class " + clazz ); //$NON-NLS-1$ //$NON-NLS-2$
@@ -1015,7 +1015,7 @@ public abstract class AbstractWebConsolePlugin extends
HttpServlet
private SortedMap sortMenuCategoryMap( Map map, String appRoot )
{
- SortedMap sortedMap = new TreeMap( String.CASE_INSENSITIVE_ORDER );
+ SortedMap sortedMap = new TreeMap<>( String.CASE_INSENSITIVE_ORDER );
Iterator keys = map.keySet().iterator();
while ( keys.hasNext() )
{
diff --git
a/webconsole/src/main/java/org/apache/felix/webconsole/DefaultBrandingPlugin.java
b/webconsole/src/main/java/org/apache/felix/webconsole/DefaultBrandingPlugin.java
index 147a5bc3cf..0a0a897f99 100644
---
a/webconsole/src/main/java/org/apache/felix/webconsole/DefaultBrandingPlugin.java
+++
b/webconsole/src/main/java/org/apache/felix/webconsole/DefaultBrandingPlugin.java
@@ -23,8 +23,6 @@ import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
-import org.apache.commons.io.IOUtils;
-
/**
* The <code>DefaultBrandingPlugin</code> class is the default implementation
@@ -123,20 +121,12 @@ public class DefaultBrandingPlugin implements
BrandingPlugin
Properties props = new Properties();
// try to load the branding properties
- InputStream ins = getClass().getResourceAsStream( BRANDING_PROPERTIES
);
- if ( ins != null )
- {
- try
- {
+ try (InputStream ins = getClass().getResourceAsStream(
BRANDING_PROPERTIES )) {
+ if ( ins != null ) {
props.load( ins );
}
- catch ( IOException ignore )
- { /* ignore - will use defaults */
- }
- finally
- {
- IOUtils.closeQuietly( ins );
- }
+ } catch ( IOException ignore ) {
+ // ignore - will use defaults
}
// set the fields from the properties now
diff --git
a/webconsole/src/main/java/org/apache/felix/webconsole/internal/core/BaseUpdateInstallHelper.java
b/webconsole/src/main/java/org/apache/felix/webconsole/internal/core/BaseUpdateInstallHelper.java
index 97eb46cf7b..718034896c 100644
---
a/webconsole/src/main/java/org/apache/felix/webconsole/internal/core/BaseUpdateInstallHelper.java
+++
b/webconsole/src/main/java/org/apache/felix/webconsole/internal/core/BaseUpdateInstallHelper.java
@@ -28,7 +28,6 @@ import java.util.Collections;
import java.util.Iterator;
import java.util.List;
-import org.apache.commons.io.IOUtils;
import org.apache.felix.webconsole.SimpleWebConsolePlugin;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
@@ -97,16 +96,9 @@ abstract class BaseUpdateInstallHelper implements Runnable
protected Bundle doRun() throws Exception
{
// now deploy the resolved bundles
- InputStream bundleStream = null;
- try
- {
- bundleStream = new FileInputStream( bundleFile );
+ try( final InputStream bundleStream = new FileInputStream( bundleFile
)) {
return doRun( bundleStream );
}
- finally
- {
- IOUtils.closeQuietly( bundleStream );
- }
}
diff --git
a/webconsole/src/main/java/org/apache/felix/webconsole/internal/i18n/ConsolePropertyResourceBundle.java
b/webconsole/src/main/java/org/apache/felix/webconsole/internal/i18n/ConsolePropertyResourceBundle.java
index 7ed707e15c..c1d389b3e6 100644
---
a/webconsole/src/main/java/org/apache/felix/webconsole/internal/i18n/ConsolePropertyResourceBundle.java
+++
b/webconsole/src/main/java/org/apache/felix/webconsole/internal/i18n/ConsolePropertyResourceBundle.java
@@ -26,8 +26,6 @@ import java.util.Enumeration;
import java.util.Properties;
import java.util.ResourceBundle;
-import org.apache.commons.io.IOUtils;
-
class ConsolePropertyResourceBundle extends ResourceBundle
{
@@ -40,22 +38,12 @@ class ConsolePropertyResourceBundle extends ResourceBundle
setParent( parent );
props = new Properties();
- if ( source != null )
- {
- InputStream ins = null;
- try
- {
- ins = source.openStream();
+ if ( source != null ) {
+ try(InputStream ins = source.openStream()) {
props.load( ins );
+ } catch ( IOException ignore ) {
+ // ignore
}
- catch ( IOException ignore )
- { /* ignore */
- }
- finally
- {
- IOUtils.closeQuietly( ins );
- }
-
}
}
diff --git
a/webconsole/src/main/java/org/apache/felix/webconsole/internal/misc/LicenseServlet.java
b/webconsole/src/main/java/org/apache/felix/webconsole/internal/misc/LicenseServlet.java
index 12cbb0fd6b..a608469616 100644
---
a/webconsole/src/main/java/org/apache/felix/webconsole/internal/misc/LicenseServlet.java
+++
b/webconsole/src/main/java/org/apache/felix/webconsole/internal/misc/LicenseServlet.java
@@ -21,8 +21,12 @@ package org.apache.felix.webconsole.internal.misc;
import java.io.IOException;
import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.Reader;
import java.io.StringWriter;
+import java.io.Writer;
import java.net.URL;
+import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Iterator;
@@ -35,7 +39,6 @@ import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
-import org.apache.commons.io.IOUtils;
import org.apache.felix.utils.json.JSONWriter;
import org.apache.felix.utils.manifest.Clause;
import org.apache.felix.utils.manifest.Parser;
@@ -124,11 +127,8 @@ public final class LicenseServlet extends
SimpleWebConsolePlugin implements Osgi
final JSONWriter jw = new JSONWriter(json);
jw.array();
- for (int i = 0; i < bundles.length; i++)
- {
- Bundle bundle = bundles[i];
-
- List files = findResource(bundle, LICENSE_FILES);
+ for (final Bundle bundle : bundles) {
+ List<Entry> files = findResource(bundle, LICENSE_FILES);
addLicensesFromHeader(bundle, files);
if (!files.isEmpty())
{ // has resources
@@ -139,7 +139,7 @@ public final class LicenseServlet extends
SimpleWebConsolePlugin implements Osgi
jw.object();
jw.key("__res__");
jw.array();
- Iterator iter = files.iterator();
+ Iterator<Entry> iter = files.iterator();
while ( iter.hasNext() ) {
jw.object();
Entry entry = (Entry) iter.next();
@@ -167,7 +167,7 @@ public final class LicenseServlet extends
SimpleWebConsolePlugin implements Osgi
return path.substring( path.lastIndexOf( '/' ) + 1 );
}
- private static final void addLicensesFromHeader(Bundle bundle, List files)
+ private static final void addLicensesFromHeader(Bundle bundle, List<Entry>
files)
{
String target = (String) bundle.getHeaders("").get("Bundle-License");
if (target != null)
@@ -206,18 +206,18 @@ public final class LicenseServlet extends
SimpleWebConsolePlugin implements Osgi
}
}
- private static final List findResource( Bundle bundle, String[] patterns )
throws IOException
+ private static final List<Entry> findResource( Bundle bundle, String[]
patterns ) throws IOException
{
- final List files = new ArrayList();
+ final List<Entry> files = new ArrayList<>();
for ( int i = 0; i < patterns.length; i++ )
{
- Enumeration entries = bundle.findEntries( "/", patterns[i] + "*",
true );
+ Enumeration<URL> entries = bundle.findEntries( "/", patterns[i] +
"*", true );
if ( entries != null )
{
while ( entries.hasMoreElements() )
{
- URL url = ( URL ) entries.nextElement();
+ URL url = entries.nextElement();
Entry entry = new Entry();
entry.path = url.getPath();
entry.url = getName( url.getPath() ) ;
@@ -226,18 +226,14 @@ public final class LicenseServlet extends
SimpleWebConsolePlugin implements Osgi
}
}
- Enumeration entries = bundle.findEntries( "/", "*.jar", true );
+ Enumeration<URL> entries = bundle.findEntries( "/", "*.jar", true );
if ( entries != null )
{
while ( entries.hasMoreElements() )
{
- URL url = ( URL ) entries.nextElement();
+ URL url = entries.nextElement();
- InputStream ins = null;
- try
- {
- ins = url.openStream();
- ZipInputStream zin = new ZipInputStream( ins );
+ try(ZipInputStream zin = new ZipInputStream( url.openStream()
)) {
for ( ZipEntry zentry = zin.getNextEntry(); zentry !=
null; zentry = zin.getNextEntry() )
{
String name = zentry.getName();
@@ -263,11 +259,6 @@ public final class LicenseServlet extends
SimpleWebConsolePlugin implements Osgi
}
}
}
- finally
- {
- IOUtils.closeQuietly( ins );
- }
-
}
}
@@ -280,14 +271,12 @@ public final class LicenseServlet extends
SimpleWebConsolePlugin implements Osgi
final String name = pathInfo.licenseFile.substring(
pathInfo.licenseFile.lastIndexOf( '/' ) + 1 );
boolean isLicense = false;
- for ( int i = 0; !isLicense && i < LICENSE_FILES.length; i++ )
- {
+ for ( int i = 0; !isLicense && i < LICENSE_FILES.length; i++ ) {
isLicense = name.startsWith( LICENSE_FILES[i] );
}
final Bundle bundle = getBundleContext().getBundle( pathInfo.bundleId
);
- if ( bundle == null )
- {
+ if ( bundle == null ) {
return false;
}
@@ -295,62 +284,44 @@ public final class LicenseServlet extends
SimpleWebConsolePlugin implements Osgi
WebConsoleUtil.setNoCache( response );
response.setContentType( "text/plain" );
- if ( pathInfo.innerJar == null )
- {
+ if ( pathInfo.innerJar == null ) {
URL resource = bundle.getEntry( pathInfo.licenseFile );
- if ( resource == null)
- {
+ if ( resource == null) {
resource = bundle.getResource( pathInfo.licenseFile );
}
-
- if ( resource != null )
- {
- final InputStream input = resource.openStream();
- try
- {
- IOUtils.copy( input, response.getWriter() );
- return true;
- }
- finally
- {
- IOUtils.closeQuietly( input );
+ if ( resource != null ){
+ try( InputStream input = resource.openStream()) {
+ copy( input, response.getWriter() );
}
+ return true;
}
- }
- else
- {
+ } else {
// license is in a nested JAR
final URL zipResource = bundle.getResource( pathInfo.innerJar );
- if ( zipResource != null )
- {
- final InputStream input = zipResource.openStream();
- ZipInputStream zin = null;
- try
- {
- zin = new ZipInputStream( input );
- for ( ZipEntry zentry = zin.getNextEntry(); zentry !=
null; zentry = zin.getNextEntry() )
- {
- if ( pathInfo.licenseFile.equals( zentry.getName() ) )
- {
- IOUtils.copy( zin, response.getWriter() );
+ if ( zipResource != null ) {
+ try(final ZipInputStream zin = new ZipInputStream(
zipResource.openStream() )) {
+ for ( ZipEntry zentry = zin.getNextEntry(); zentry !=
null; zentry = zin.getNextEntry() ) {
+ if ( pathInfo.licenseFile.equals( zentry.getName() ) )
{
+ copy( zin, response.getWriter() );
return true;
}
}
}
- finally
- {
-
- IOUtils.closeQuietly( zin );
- IOUtils.closeQuietly( input );
- }
}
}
- // throw new ServletException("License file:" + url + " not found!");
return false;
}
+ private void copy(final InputStream in, final Writer out) throws
IOException {
+ final Reader r = new InputStreamReader(in, StandardCharsets.UTF_8);
+ final char[] buf = new char[4096];
+ int l;
+ while ((l = r.read(buf)) > 0 ) {
+ out.write(buf, 0, l);
+ }
+ }
// package private for unit testing of the parse method
static class PathInfo
{
diff --git
a/webconsole/src/main/java/org/apache/felix/webconsole/internal/servlet/OsgiManager.java
b/webconsole/src/main/java/org/apache/felix/webconsole/internal/servlet/OsgiManager.java
index e90cd43ae5..0d22599a24 100644
---
a/webconsole/src/main/java/org/apache/felix/webconsole/internal/servlet/OsgiManager.java
+++
b/webconsole/src/main/java/org/apache/felix/webconsole/internal/servlet/OsgiManager.java
@@ -50,7 +50,6 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;
-import org.apache.commons.io.FilenameUtils;
import org.apache.felix.webconsole.AbstractWebConsolePlugin;
import org.apache.felix.webconsole.BrandingPlugin;
import org.apache.felix.webconsole.User;
@@ -1243,29 +1242,29 @@ public class OsgiManager extends GenericServlet
return Collections.unmodifiableSet(values);
}
- private Map langMap;
+ private Map<String, String> langMap;
- private final Map getLangMap()
+ private final Map<String, String> getLangMap()
{
if (null != langMap)
return langMap;
- final Map map = new HashMap();
+ final Map<String, String> map = new HashMap<>();
final Bundle bundle = bundleContext.getBundle();
- final Enumeration e = bundle.findEntries("res/flags", null, false);
//$NON-NLS-1$
+ final Enumeration<URL> e = bundle.findEntries("res/flags", null,
false); //$NON-NLS-1$
while (e != null && e.hasMoreElements())
{
- final URL img = (URL) e.nextElement();
- final String name = FilenameUtils.getBaseName(img.getFile());
- try
- {
+ final URL img = e.nextElement();
+ final String path = img.getPath();
+ try {
+ final int lastSlash = path.lastIndexOf('/');
+ final int dot = path.indexOf('.', lastSlash);
+ final String name = (dot == -1 ? path.substring(lastSlash+1) :
path.substring(lastSlash + 1, dot));
final String locale = new Locale(name,
"").getDisplayLanguage(); //$NON-NLS-1$
map.put(name, null != locale ? locale : name);
}
- catch (Throwable t)
- {
- t.printStackTrace();
- /* ignore invalid locale? */
+ catch (Throwable t) {
+ // Ignore invalid locale?
}
}
return langMap = map;