You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2017/09/20 21:36:39 UTC
svn commit: r1809087 - in /commons/proper/vfs/trunk: ./
commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/http/test/
commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/url/test/
commons-vfs2/src/test/java/org/apache/commons/vfs2/util/
Author: ggregory
Date: Wed Sep 20 21:36:38 2017
New Revision: 1809087
URL: http://svn.apache.org/viewvc?rev=1809087&view=rev
Log:
Update tests from Apache HttpComponents HttpCore 4.3.2 to 4.4.7.
Added:
commons/proper/vfs/trunk/commons-vfs2/src/test/java/org/apache/commons/vfs2/util/NHttpFileServer.java
- copied, changed from r1809073, commons/proper/vfs/trunk/commons-vfs2/src/test/java/org/apache/commons/vfs2/util/NHttpServer.java
Removed:
commons/proper/vfs/trunk/commons-vfs2/src/test/java/org/apache/commons/vfs2/util/NHttpServer.java
Modified:
commons/proper/vfs/trunk/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/http/test/HttpProviderTestCase.java
commons/proper/vfs/trunk/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/url/test/UrlProviderHttpTestCase.java
commons/proper/vfs/trunk/pom.xml
Modified: commons/proper/vfs/trunk/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/http/test/HttpProviderTestCase.java
URL: http://svn.apache.org/viewvc/commons/proper/vfs/trunk/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/http/test/HttpProviderTestCase.java?rev=1809087&r1=1809086&r2=1809087&view=diff
==============================================================================
--- commons/proper/vfs/trunk/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/http/test/HttpProviderTestCase.java (original)
+++ commons/proper/vfs/trunk/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/http/test/HttpProviderTestCase.java Wed Sep 20 21:36:38 2017
@@ -18,6 +18,7 @@ package org.apache.commons.vfs2.provider
import java.io.File;
import java.io.IOException;
+import java.util.concurrent.TimeUnit;
import junit.framework.Test;
@@ -33,7 +34,7 @@ import org.apache.commons.vfs2.provider.
import org.apache.commons.vfs2.test.AbstractProviderTestConfig;
import org.apache.commons.vfs2.test.ProviderTestSuite;
import org.apache.commons.vfs2.util.FreeSocketPortUtil;
-import org.apache.commons.vfs2.util.NHttpServer;
+import org.apache.commons.vfs2.util.NHttpFileServer;
import org.junit.Assert;
/**
@@ -41,7 +42,7 @@ import org.junit.Assert;
*
*/
public class HttpProviderTestCase extends AbstractProviderTestConfig {
- private static NHttpServer Server;
+ private static NHttpFileServer Server;
private static int SocketPort;
@@ -62,10 +63,7 @@ public class HttpProviderTestCase extend
* @throws Exception
*/
private static void setUpClass() throws Exception {
- Server = new NHttpServer();
- if (!Server.run(SocketPort, new File(getTestDirectory()), 5000)) {
- throw new IllegalStateException("The embedded HTTP server has not completed startup, increase wait time");
- }
+ Server = NHttpFileServer.start(SocketPort, new File(getTestDirectory()), 5000);
}
/**
@@ -108,7 +106,7 @@ public class HttpProviderTestCase extend
*/
private static void tearDownClass() throws IOException {
if (Server != null) {
- Server.stop();
+ Server.shutdown(5000, TimeUnit.SECONDS);
}
}
Modified: commons/proper/vfs/trunk/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/url/test/UrlProviderHttpTestCase.java
URL: http://svn.apache.org/viewvc/commons/proper/vfs/trunk/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/url/test/UrlProviderHttpTestCase.java?rev=1809087&r1=1809086&r2=1809087&view=diff
==============================================================================
--- commons/proper/vfs/trunk/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/url/test/UrlProviderHttpTestCase.java (original)
+++ commons/proper/vfs/trunk/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/url/test/UrlProviderHttpTestCase.java Wed Sep 20 21:36:38 2017
@@ -18,6 +18,7 @@ package org.apache.commons.vfs2.provider
import java.io.File;
import java.io.IOException;
+import java.util.concurrent.TimeUnit;
import junit.framework.Test;
@@ -28,14 +29,14 @@ import org.apache.commons.vfs2.provider.
import org.apache.commons.vfs2.test.AbstractProviderTestConfig;
import org.apache.commons.vfs2.test.ProviderTestSuite;
import org.apache.commons.vfs2.util.FreeSocketPortUtil;
-import org.apache.commons.vfs2.util.NHttpServer;
+import org.apache.commons.vfs2.util.NHttpFileServer;
/**
* Test cases for HTTP with the default provider.
*
*/
public class UrlProviderHttpTestCase extends AbstractProviderTestConfig {
- private static NHttpServer Server;
+ private static NHttpFileServer Server;
private static int SocketPort;
@@ -56,10 +57,7 @@ public class UrlProviderHttpTestCase ext
* @throws Exception
*/
private static void setUpClass() throws Exception {
- Server = new NHttpServer();
- if (!Server.run(SocketPort, new File(getTestDirectory()), 5000)) {
- throw new IllegalStateException("The embedded HTTP server has not completed startup, increase wait time");
- }
+ Server = NHttpFileServer.start(SocketPort, new File(getTestDirectory()), 5000);
}
public static Test suite() throws Exception {
@@ -87,7 +85,7 @@ public class UrlProviderHttpTestCase ext
*/
private static void tearDownClass() throws IOException {
if (Server != null) {
- Server.stop();
+ Server.shutdown(5000, TimeUnit.SECONDS);
}
}
Copied: commons/proper/vfs/trunk/commons-vfs2/src/test/java/org/apache/commons/vfs2/util/NHttpFileServer.java (from r1809073, commons/proper/vfs/trunk/commons-vfs2/src/test/java/org/apache/commons/vfs2/util/NHttpServer.java)
URL: http://svn.apache.org/viewvc/commons/proper/vfs/trunk/commons-vfs2/src/test/java/org/apache/commons/vfs2/util/NHttpFileServer.java?p2=commons/proper/vfs/trunk/commons-vfs2/src/test/java/org/apache/commons/vfs2/util/NHttpFileServer.java&p1=commons/proper/vfs/trunk/commons-vfs2/src/test/java/org/apache/commons/vfs2/util/NHttpServer.java&r1=1809073&r2=1809087&rev=1809087&view=diff
==============================================================================
--- commons/proper/vfs/trunk/commons-vfs2/src/test/java/org/apache/commons/vfs2/util/NHttpServer.java (original)
+++ commons/proper/vfs/trunk/commons-vfs2/src/test/java/org/apache/commons/vfs2/util/NHttpFileServer.java Wed Sep 20 21:36:38 2017
@@ -27,45 +27,33 @@ package org.apache.commons.vfs2.util;
import java.io.File;
import java.io.IOException;
-import java.io.InputStream;
-import java.io.InterruptedIOException;
-import java.net.InetSocketAddress;
-import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLDecoder;
import java.security.KeyManagementException;
-import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;
import java.util.Date;
import java.util.Locale;
-import java.util.concurrent.Executors;
+import java.util.concurrent.TimeUnit;
-import javax.net.ssl.KeyManager;
-import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
-import org.apache.commons.httpclient.util.DateUtil;
-import org.apache.commons.io.IOUtils;
+import org.apache.http.ExceptionLogger;
+import org.apache.http.HttpConnection;
+import org.apache.http.HttpEntity;
import org.apache.http.HttpException;
import org.apache.http.HttpHeaders;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
-import org.apache.http.HttpResponseInterceptor;
import org.apache.http.HttpStatus;
import org.apache.http.MethodNotSupportedException;
import org.apache.http.entity.ContentType;
-import org.apache.http.impl.DefaultConnectionReuseStrategy;
-import org.apache.http.impl.nio.DefaultHttpServerIODispatch;
-import org.apache.http.impl.nio.DefaultNHttpServerConnection;
-import org.apache.http.impl.nio.DefaultNHttpServerConnectionFactory;
-import org.apache.http.impl.nio.SSLNHttpServerConnectionFactory;
-import org.apache.http.impl.nio.reactor.DefaultListeningIOReactor;
-import org.apache.http.nio.NHttpConnection;
-import org.apache.http.nio.NHttpConnectionFactory;
-import org.apache.http.nio.NHttpServerConnection;
+import org.apache.http.impl.cookie.DateUtils;
+import org.apache.http.impl.nio.bootstrap.HttpServer;
+import org.apache.http.impl.nio.bootstrap.ServerBootstrap;
+import org.apache.http.impl.nio.reactor.IOReactorConfig;
import org.apache.http.nio.entity.NFileEntity;
import org.apache.http.nio.entity.NStringEntity;
import org.apache.http.nio.protocol.BasicAsyncRequestConsumer;
@@ -73,39 +61,15 @@ import org.apache.http.nio.protocol.Basi
import org.apache.http.nio.protocol.HttpAsyncExchange;
import org.apache.http.nio.protocol.HttpAsyncRequestConsumer;
import org.apache.http.nio.protocol.HttpAsyncRequestHandler;
-import org.apache.http.nio.protocol.HttpAsyncRequestHandlerRegistry;
-import org.apache.http.nio.protocol.HttpAsyncService;
-import org.apache.http.nio.reactor.IOEventDispatch;
-import org.apache.http.nio.reactor.IOReactorException;
-import org.apache.http.nio.reactor.ListeningIOReactor;
-import org.apache.http.params.CoreConnectionPNames;
-import org.apache.http.params.CoreProtocolPNames;
-import org.apache.http.params.HttpParams;
-import org.apache.http.params.SyncBasicHttpParams;
-import org.apache.http.protocol.ExecutionContext;
import org.apache.http.protocol.HttpContext;
-import org.apache.http.protocol.HttpProcessor;
-import org.apache.http.protocol.ImmutableHttpProcessor;
-import org.apache.http.protocol.ResponseConnControl;
-import org.apache.http.protocol.ResponseContent;
-import org.apache.http.protocol.ResponseDate;
-import org.apache.http.protocol.ResponseServer;
+import org.apache.http.protocol.HttpCoreContext;
+import org.apache.http.ssl.SSLContexts;
/**
- * Adapted from org.apache.http.examples.nio.NHttpServer.
- *
- * <p>
- * HTTP/1.1 file server based on the non-blocking I/O model and capable of direct channel (zero copy) data transfer.
- * </p>
- * <p>
- * Please note the purpose of this application is demonstrate the usage of HttpCore APIs. It is NOT intended to
- * demonstrate the most efficient way of building an HTTP server.
- * </p>
- *
- * @version $Id$
- * @since 2.1
+ * Embedded HTTP/1.1 file server based on a non-blocking I/O model and capable of direct channel (zero copy) data
+ * transfer.
*/
-public class NHttpServer {
+public class NHttpFileServer {
static class HttpFileHandler implements HttpAsyncRequestHandler<HttpRequest> {
@@ -120,7 +84,7 @@ public class NHttpServer {
public void handle(final HttpRequest request, final HttpAsyncExchange httpexchange, final HttpContext context)
throws HttpException, IOException {
final HttpResponse response = httpexchange.getResponse();
- this.handleInternal(request, response, context);
+ handleInternal(request, response, context);
httpexchange.submitResponse(new BasicAsyncResponseProducer(response));
}
@@ -134,32 +98,37 @@ public class NHttpServer {
final String target = request.getRequestLine().getUri();
final File file = new File(this.docRoot, URLDecoder.decode(target, "UTF-8"));
+ final String mimeType = "text/html";
if (!file.exists()) {
response.setStatusCode(HttpStatus.SC_NOT_FOUND);
final NStringEntity entity = new NStringEntity(
- "<html><body><h1>File" + file.getPath() + " not found</h1></body></html>",
- ContentType.create("text/html", "UTF-8"));
+ "<html><body><h1>File " + file.getPath() + " not found</h1></body></html>",
+ ContentType.create(mimeType, "UTF-8"));
response.setEntity(entity);
- NHttpServer.debug("File " + file.getPath() + " not found");
+ debug("File " + file.getPath() + " not found");
- } else if (!file.canRead()) {
+ } else if (!file.canRead() /* || file.isDirectory() */) {
response.setStatusCode(HttpStatus.SC_FORBIDDEN);
final NStringEntity entity = new NStringEntity("<html><body><h1>Access denied</h1></body></html>",
- ContentType.create("text/html", "UTF-8"));
+ ContentType.create(mimeType, "UTF-8"));
response.setEntity(entity);
- NHttpServer.debug("Cannot read file " + file.getPath());
+ debug("Cannot read file " + file.getPath());
} else {
- final NHttpConnection conn = (NHttpConnection) context.getAttribute(ExecutionContext.HTTP_CONNECTION);
+
+ final HttpCoreContext coreContext = HttpCoreContext.adapt(context);
+ final HttpConnection conn = coreContext.getConnection(HttpConnection.class);
response.setStatusCode(HttpStatus.SC_OK);
- final NFileEntity body = new NFileEntity(file, ContentType.create("text/html"));
+ final HttpEntity body = file.isDirectory()
+ ? new NStringEntity(file.toString(), ContentType.create(mimeType))
+ : new NFileEntity(file, ContentType.create(mimeType));
response.setEntity(body);
if (!response.containsHeader(HttpHeaders.LAST_MODIFIED)) {
- response.addHeader(HttpHeaders.LAST_MODIFIED, DateUtil.formatDate(new Date(file.lastModified())));
+ response.addHeader(HttpHeaders.LAST_MODIFIED, DateUtils.formatDate(new Date(file.lastModified())));
}
- NHttpServer.debug(conn + ": serving file " + file.getPath());
+ debug(conn + ": serving file " + file.getPath());
}
}
@@ -172,151 +141,96 @@ public class NHttpServer {
}
- static final boolean Debug = false;
+ public static boolean DEBUG = false;
- private static void debug(final String s) {
- if (Debug) {
- System.out.println(s);
+ private static void debug(final String message) {
+ if (DEBUG) {
+ System.out.println(message);
}
}
- public static void main(final String[] args) throws Exception {
- new NHttpServer().run(Integer.valueOf(args[0]), new File(args[1]), 0);
+ public static void main(final String[] args) throws KeyManagementException, UnrecoverableKeyException,
+ NoSuchAlgorithmException, KeyStoreException, CertificateException, IOException, InterruptedException {
+ if (args.length < 1) {
+ System.err.println("Please specify document root directory");
+ System.exit(1);
+ }
+ // Document root directory
+ final File docRoot = new File(args[0]);
+ int port = 8080;
+ if (args.length >= 2) {
+ port = Integer.parseInt(args[1]);
+ }
+ new NHttpFileServer(port, docRoot).start().awaitTermination();
}
- volatile ListeningIOReactor ioReactor;
+ public static NHttpFileServer start(final int port, final File docRoot, final long waitMillis)
+ throws KeyManagementException, UnrecoverableKeyException, NoSuchAlgorithmException, KeyStoreException,
+ CertificateException, IOException, InterruptedException {
+ return new NHttpFileServer(port, docRoot).start();
+ }
- public boolean run(final int port, final File docRoot, final long waitMillis)
- throws IOReactorException, InterruptedException {
- Executors.newSingleThreadExecutor().execute(new Runnable() {
+ private final File docRoot;
+ private final int port;
+
+ private HttpServer server;
+
+ private NHttpFileServer(final int port, final File docRoot) {
+ this.port = port;
+ this.docRoot = docRoot;
+ }
+
+ private void awaitTermination() throws InterruptedException {
+ server.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS);
+
+ Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
- try {
- NHttpServer.this.runBlock(port, docRoot);
- } catch (final IOReactorException e) {
- throw new IllegalStateException(e);
- } catch (final UnrecoverableKeyException e) {
- throw new IllegalStateException(e);
- } catch (final KeyStoreException e) {
- throw new IllegalStateException(e);
- } catch (final NoSuchAlgorithmException e) {
- throw new IllegalStateException(e);
- } catch (final CertificateException e) {
- throw new IllegalStateException(e);
- } catch (final IOException e) {
- throw new IllegalStateException(e);
- } catch (final KeyManagementException e) {
- throw new IllegalStateException(e);
- }
+ server.shutdown(5, TimeUnit.SECONDS);
}
});
- return this.waitForServerStartup(port, waitMillis);
}
- public void runBlock(final int port, final File docRoot) throws KeyStoreException, NoSuchAlgorithmException,
- CertificateException, IOException, UnrecoverableKeyException, KeyManagementException {
- if (docRoot == null) {
- throw new IllegalArgumentException("No doc root specified.");
+ public void shutdown(final long gracePeriod, final TimeUnit timeUnit) {
+ if (server != null) {
+ server.shutdown(gracePeriod, timeUnit);
}
- // HTTP parameters for the server
- final HttpParams params = new SyncBasicHttpParams();
- params.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, 5000)
- .setIntParameter(CoreConnectionPNames.SOCKET_BUFFER_SIZE, 8 * 1024)
- .setBooleanParameter(CoreConnectionPNames.TCP_NODELAY, true)
- .setParameter(CoreProtocolPNames.ORIGIN_SERVER, "HttpTest/1.1");
- // Create HTTP protocol processing chain
- final HttpProcessor httpproc = new ImmutableHttpProcessor(new HttpResponseInterceptor[] {
- // Use standard server-side protocol interceptors
- new ResponseDate(), new ResponseServer(), new ResponseContent(), new ResponseConnControl() });
- // Create request handler registry
- final HttpAsyncRequestHandlerRegistry reqistry = new HttpAsyncRequestHandlerRegistry();
- // Register the default handler for all URIs
- reqistry.register("*", new HttpFileHandler(docRoot));
- // Create server-side HTTP protocol handler
- final HttpAsyncService protocolHandler = new HttpAsyncService(httpproc, new DefaultConnectionReuseStrategy(),
- reqistry, params) {
-
- @Override
- public void closed(final NHttpServerConnection conn) {
- NHttpServer.debug(conn + ": connection closed");
- super.closed(conn);
- }
- @Override
- public void connected(final NHttpServerConnection conn) {
- NHttpServer.debug(conn + ": connection open");
- super.connected(conn);
- }
+ }
- };
- // Create HTTP connection factory
- NHttpConnectionFactory<DefaultNHttpServerConnection> connFactory;
+ private NHttpFileServer start() throws KeyManagementException, UnrecoverableKeyException, NoSuchAlgorithmException,
+ KeyStoreException, CertificateException, IOException, InterruptedException {
+ SSLContext sslContext = null;
if (port == 8443) {
// Initialize SSL context
- final ClassLoader cl = NHttpServer.class.getClassLoader();
- final URL url = cl.getResource("my.keystore");
+ final URL url = NHttpFileServer.class.getResource("/test.keystore");
if (url == null) {
- NHttpServer.debug("Keystore not found");
+ debug("Keystore not found");
System.exit(1);
}
- final KeyStore keystore = KeyStore.getInstance("jks");
- keystore.load(url.openStream(), "secret".toCharArray());
- final KeyManagerFactory kmfactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
- kmfactory.init(keystore, "secret".toCharArray());
- final KeyManager[] keymanagers = kmfactory.getKeyManagers();
- final SSLContext sslcontext = SSLContext.getInstance("TLS");
- sslcontext.init(keymanagers, null, null);
- connFactory = new SSLNHttpServerConnectionFactory(sslcontext, null, params);
- } else {
- connFactory = new DefaultNHttpServerConnectionFactory(params);
- }
- // Create server-side I/O event dispatch
- final IOEventDispatch ioEventDispatch = new DefaultHttpServerIODispatch(protocolHandler, connFactory);
- // Create server-side I/O reactor
- this.ioReactor = new DefaultListeningIOReactor();
- try {
- // Listen of the given port
- this.ioReactor.listen(new InetSocketAddress(port));
- // Ready to go!
- this.ioReactor.execute(ioEventDispatch);
- } catch (final InterruptedIOException ex) {
- System.err.println("Interrupted");
- } catch (final IOException e) {
- System.err.println("I/O error: " + e.getMessage());
- }
- NHttpServer.debug("Shutdown");
- }
-
- public void stop() throws IOException {
- if (this.ioReactor != null) {
- this.ioReactor.shutdown(2000);
- }
- }
-
- private boolean waitForServerStartup(final int port, final long waitMillis) throws InterruptedException {
- final long endWait = System.currentTimeMillis() + waitMillis;
- final String urlSpec = "http://localhost:" + port;
- try {
- final URL url = new URL(urlSpec);
- InputStream inputStream = null;
- while (System.currentTimeMillis() < endWait && inputStream == null) {
- try {
- inputStream = url.openStream();
- if (inputStream != null) {
- IOUtils.closeQuietly(inputStream);
- return true;
- }
- } catch (final IOException e) {
- // ignore
- // debug("While waiting: " + e);
- // e.printStackTrace();
- }
- Thread.sleep(100);
- }
- } catch (final MalformedURLException e) {
- throw new IllegalStateException("Error in test code for URL " + urlSpec);
- }
- return false;
+ debug("Loading keystore " + url);
+ sslContext = SSLContexts.custom()
+ .loadKeyMaterial(url, "nopassword".toCharArray(), "nopassword".toCharArray()).build();
+ }
+
+ final IOReactorConfig config = IOReactorConfig.custom().setSoTimeout(15000).setTcpNoDelay(true).build();
+
+ // @formatter:off
+ server = ServerBootstrap.bootstrap()
+ .setListenerPort(port)
+ .setServerInfo("Test/1.1")
+ .setIOReactorConfig(config)
+ .setSslContext(sslContext)
+ .setExceptionLogger(ExceptionLogger.STD_ERR)
+ .registerHandler("*", new HttpFileHandler(docRoot)).create();
+ // @formatter:on
+
+ server.start();
+ debug("Serving " + docRoot + " on " + server.getEndpoint().getAddress()
+ + (sslContext == null ? "" : " with " + sslContext.getProvider() + " " + sslContext.getProtocol()));
+ server.getEndpoint().waitFor();
+ // Thread.sleep(startWaitMillis); // hack
+ return this;
}
}
Modified: commons/proper/vfs/trunk/pom.xml
URL: http://svn.apache.org/viewvc/commons/proper/vfs/trunk/pom.xml?rev=1809087&r1=1809086&r2=1809087&view=diff
==============================================================================
--- commons/proper/vfs/trunk/pom.xml (original)
+++ commons/proper/vfs/trunk/pom.xml Wed Sep 20 21:36:38 2017
@@ -505,7 +505,7 @@
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore-nio</artifactId>
- <version>4.3.2</version>
+ <version>4.4.7</version>
</dependency>
<!-- Test WebDAV with Apache Jackrabbit -->
<dependency>