You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2011/07/22 16:31:22 UTC
svn commit: r1149619 - in /httpcomponents/httpcore/trunk:
httpcore-nio/src/examples/org/apache/http/examples/nio/
httpcore/src/examples/org/apache/http/examples/
Author: olegk
Date: Fri Jul 22 14:31:21 2011
New Revision: 1149619
URL: http://svn.apache.org/viewvc?rev=1149619&view=rev
Log:
Removed calls to deprecated methods from examples
Removed:
httpcomponents/httpcore/trunk/httpcore-nio/src/examples/org/apache/http/examples/nio/NHttpFileServer.java
Modified:
httpcomponents/httpcore/trunk/httpcore-nio/src/examples/org/apache/http/examples/nio/NHttpSSLServer.java
httpcomponents/httpcore/trunk/httpcore-nio/src/examples/org/apache/http/examples/nio/NHttpServer.java
httpcomponents/httpcore/trunk/httpcore/src/examples/org/apache/http/examples/ElementalHttpServer.java
Modified: httpcomponents/httpcore/trunk/httpcore-nio/src/examples/org/apache/http/examples/nio/NHttpSSLServer.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/examples/org/apache/http/examples/nio/NHttpSSLServer.java?rev=1149619&r1=1149618&r2=1149619&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-nio/src/examples/org/apache/http/examples/nio/NHttpSSLServer.java (original)
+++ httpcomponents/httpcore/trunk/httpcore-nio/src/examples/org/apache/http/examples/nio/NHttpSSLServer.java Fri Jul 22 14:31:21 2011
@@ -30,8 +30,6 @@ package org.apache.http.examples.nio;
import java.io.File;
import java.io.IOException;
import java.io.InterruptedIOException;
-import java.io.OutputStream;
-import java.io.OutputStreamWriter;
import java.net.InetSocketAddress;
import java.net.URL;
import java.net.URLDecoder;
@@ -50,21 +48,21 @@ 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.ContentProducer;
-import org.apache.http.entity.EntityTemplate;
-import org.apache.http.entity.FileEntity;
+import org.apache.http.entity.ContentType;
import org.apache.http.impl.DefaultConnectionReuseStrategy;
import org.apache.http.impl.DefaultHttpResponseFactory;
import org.apache.http.impl.nio.reactor.DefaultListeningIOReactor;
import org.apache.http.impl.nio.ssl.SSLServerIOEventDispatch;
import org.apache.http.nio.NHttpConnection;
-import org.apache.http.nio.protocol.EventListener;
+import org.apache.http.nio.entity.NFileEntity;
+import org.apache.http.nio.entity.NStringEntity;
import org.apache.http.nio.protocol.BufferingHttpServiceHandler;
+import org.apache.http.nio.protocol.EventListener;
import org.apache.http.nio.reactor.IOEventDispatch;
import org.apache.http.nio.reactor.ListeningIOReactor;
import org.apache.http.params.CoreConnectionPNames;
-import org.apache.http.params.HttpParams;
import org.apache.http.params.CoreProtocolPNames;
+import org.apache.http.params.HttpParams;
import org.apache.http.params.SyncBasicHttpParams;
import org.apache.http.protocol.HttpContext;
import org.apache.http.protocol.HttpProcessor;
@@ -78,12 +76,12 @@ import org.apache.http.protocol.Response
import org.apache.http.util.EntityUtils;
/**
- * Basic, yet fully functional and spec compliant, HTTPS/1.1 server based on the non-blocking
+ * Basic, yet fully functional and spec compliant, HTTPS/1.1 server based on the non-blocking
* I/O model.
* <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.
- *
+ * It is NOT intended to demonstrate the most efficient way of building an HTTP server.
+ *
*
*/
public class NHttpSSLServer {
@@ -93,7 +91,7 @@ public class NHttpSSLServer {
System.err.println("Please specify document root directory");
System.exit(1);
}
-
+
ClassLoader cl = NHttpSSLServer.class.getClassLoader();
URL url = cl.getResource("test.keystore");
KeyStore keystore = KeyStore.getInstance("jks");
@@ -101,10 +99,10 @@ public class NHttpSSLServer {
KeyManagerFactory kmfactory = KeyManagerFactory.getInstance(
KeyManagerFactory.getDefaultAlgorithm());
kmfactory.init(keystore, "nopassword".toCharArray());
- KeyManager[] keymanagers = kmfactory.getKeyManagers();
+ KeyManager[] keymanagers = kmfactory.getKeyManagers();
SSLContext sslcontext = SSLContext.getInstance("TLS");
sslcontext.init(keymanagers, null, null);
-
+
HttpParams params = new SyncBasicHttpParams();
params
.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, 5000)
@@ -119,27 +117,27 @@ public class NHttpSSLServer {
new ResponseContent(),
new ResponseConnControl()
});
-
+
BufferingHttpServiceHandler handler = new BufferingHttpServiceHandler(
httpproc,
new DefaultHttpResponseFactory(),
new DefaultConnectionReuseStrategy(),
params);
-
+
// Set up request handlers
HttpRequestHandlerRegistry reqistry = new HttpRequestHandlerRegistry();
reqistry.register("*", new HttpFileHandler(args[0]));
-
+
handler.setHandlerResolver(reqistry);
-
+
// Provide an event logger
handler.setEventListener(new EventLogger());
-
+
IOEventDispatch ioEventDispatch = new SSLServerIOEventDispatch(
- handler,
+ handler,
sslcontext,
params);
-
+
ListeningIOReactor ioReactor = new DefaultListeningIOReactor(2, params);
try {
ioReactor.listen(new InetSocketAddress(8080));
@@ -153,22 +151,22 @@ public class NHttpSSLServer {
}
static class HttpFileHandler implements HttpRequestHandler {
-
+
private final String docRoot;
-
+
public HttpFileHandler(final String docRoot) {
super();
this.docRoot = docRoot;
}
-
+
public void handle(
- final HttpRequest request,
+ final HttpRequest request,
final HttpResponse response,
final HttpContext context) throws HttpException, IOException {
String method = request.getRequestLine().getMethod().toUpperCase(Locale.ENGLISH);
if (!method.equals("GET") && !method.equals("HEAD") && !method.equals("POST")) {
- throw new MethodNotSupportedException(method + " method not supported");
+ throw new MethodNotSupportedException(method + " method not supported");
}
if (request instanceof HttpEntityEnclosingRequest) {
@@ -176,59 +174,41 @@ public class NHttpSSLServer {
byte[] entityContent = EntityUtils.toByteArray(entity);
System.out.println("Incoming entity content (bytes): " + entityContent.length);
}
-
+
String target = request.getRequestLine().getUri();
final File file = new File(this.docRoot, URLDecoder.decode(target, "UTF-8"));
if (!file.exists()) {
response.setStatusCode(HttpStatus.SC_NOT_FOUND);
- EntityTemplate body = new EntityTemplate(new ContentProducer() {
-
- public void writeTo(final OutputStream outstream) throws IOException {
- OutputStreamWriter writer = new OutputStreamWriter(outstream, "UTF-8");
- writer.write("<html><body><h1>");
- writer.write("File ");
- writer.write(file.getPath());
- writer.write(" not found");
- writer.write("</h1></body></html>");
- writer.flush();
- }
-
- });
- body.setContentType("text/html; charset=UTF-8");
- response.setEntity(body);
+ NStringEntity entity = new NStringEntity(
+ "<html><body><h1>File" + file.getPath() +
+ " not found</h1></body></html>",
+ ContentType.create("text/html", "UTF-8"));
+ entity.setContentType("text/html; charset=UTF-8");
+ response.setEntity(entity);
System.out.println("File " + file.getPath() + " not found");
-
+
} else if (!file.canRead() || file.isDirectory()) {
-
+
response.setStatusCode(HttpStatus.SC_FORBIDDEN);
- EntityTemplate body = new EntityTemplate(new ContentProducer() {
-
- public void writeTo(final OutputStream outstream) throws IOException {
- OutputStreamWriter writer = new OutputStreamWriter(outstream, "UTF-8");
- writer.write("<html><body><h1>");
- writer.write("Access denied");
- writer.write("</h1></body></html>");
- writer.flush();
- }
-
- });
- body.setContentType("text/html; charset=UTF-8");
- response.setEntity(body);
+ NStringEntity entity = new NStringEntity(
+ "<html><body><h1>Access denied</h1></body></html>",
+ ContentType.create("text/html", "UTF-8"));
+ entity.setContentType("text/html; charset=UTF-8");
+ response.setEntity(entity);
System.out.println("Cannot read file " + file.getPath());
-
+
} else {
-
+
response.setStatusCode(HttpStatus.SC_OK);
- FileEntity body = new FileEntity(file, "text/html");
+ NFileEntity body = new NFileEntity(file, ContentType.create("text/html", null));
response.setEntity(body);
System.out.println("Serving file " + file.getPath());
-
}
}
-
+
}
-
+
static class EventLogger implements EventListener {
public void connectionOpen(final NHttpConnection conn) {
@@ -250,7 +230,7 @@ public class NHttpSSLServer {
public void fatalProtocolException(final HttpException ex, final NHttpConnection conn) {
System.err.println("HTTP error: " + ex.getMessage());
}
-
+
}
-
+
}
Modified: httpcomponents/httpcore/trunk/httpcore-nio/src/examples/org/apache/http/examples/nio/NHttpServer.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/examples/org/apache/http/examples/nio/NHttpServer.java?rev=1149619&r1=1149618&r2=1149619&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-nio/src/examples/org/apache/http/examples/nio/NHttpServer.java (original)
+++ httpcomponents/httpcore/trunk/httpcore-nio/src/examples/org/apache/http/examples/nio/NHttpServer.java Fri Jul 22 14:31:21 2011
@@ -41,6 +41,7 @@ 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.DefaultHttpResponseFactory;
import org.apache.http.impl.nio.DefaultServerIOEventDispatch;
@@ -68,12 +69,12 @@ import org.apache.http.protocol.Response
import org.apache.http.util.EntityUtils;
/**
- * Basic, yet fully functional and spec compliant, HTTP/1.1 server based on the non-blocking
+ * Basic, yet fully functional and spec compliant, HTTP/1.1 server based on the non-blocking
* I/O model.
* <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.
- *
+ * It is NOT intended to demonstrate the most efficient way of building an HTTP server.
+ *
*
*/
public class NHttpServer {
@@ -97,7 +98,7 @@ public class NHttpServer {
new ResponseContent(),
new ResponseConnControl()
});
-
+
BufferingHttpServiceHandler handler = new BufferingHttpServiceHandler(
httpproc,
new DefaultHttpResponseFactory(),
@@ -158,8 +159,8 @@ public class NHttpServer {
response.setStatusCode(HttpStatus.SC_NOT_FOUND);
NStringEntity entity = new NStringEntity(
"<html><body><h1>File" + file.getPath() +
- " not found</h1></body></html>", "UTF-8");
- entity.setContentType("text/html; charset=UTF-8");
+ " not found</h1></body></html>",
+ ContentType.create("text/html", "UTF-8"));
response.setEntity(entity);
System.out.println("File " + file.getPath() + " not found");
@@ -168,18 +169,16 @@ public class NHttpServer {
response.setStatusCode(HttpStatus.SC_FORBIDDEN);
NStringEntity entity = new NStringEntity(
"<html><body><h1>Access denied</h1></body></html>",
- "UTF-8");
- entity.setContentType("text/html; charset=UTF-8");
+ ContentType.create("text/html", "UTF-8"));
response.setEntity(entity);
System.out.println("Cannot read file " + file.getPath());
} else {
response.setStatusCode(HttpStatus.SC_OK);
- NFileEntity body = new NFileEntity(file, "text/html");
+ NFileEntity body = new NFileEntity(file, ContentType.create("text/html", null));
response.setEntity(body);
System.out.println("Serving file " + file.getPath());
-
}
}
Modified: httpcomponents/httpcore/trunk/httpcore/src/examples/org/apache/http/examples/ElementalHttpServer.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/examples/org/apache/http/examples/ElementalHttpServer.java?rev=1149619&r1=1149618&r2=1149619&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/examples/org/apache/http/examples/ElementalHttpServer.java (original)
+++ httpcomponents/httpcore/trunk/httpcore/src/examples/org/apache/http/examples/ElementalHttpServer.java Fri Jul 22 14:31:21 2011
@@ -30,8 +30,6 @@ package org.apache.http.examples;
import java.io.File;
import java.io.IOException;
import java.io.InterruptedIOException;
-import java.io.OutputStream;
-import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.URLDecoder;
@@ -47,9 +45,9 @@ import org.apache.http.HttpResponseInter
import org.apache.http.HttpServerConnection;
import org.apache.http.HttpStatus;
import org.apache.http.MethodNotSupportedException;
-import org.apache.http.entity.ContentProducer;
-import org.apache.http.entity.EntityTemplate;
+import org.apache.http.entity.ContentType;
import org.apache.http.entity.FileEntity;
+import org.apache.http.entity.StringEntity;
import org.apache.http.impl.DefaultConnectionReuseStrategy;
import org.apache.http.impl.DefaultHttpResponseFactory;
import org.apache.http.impl.DefaultHttpServerConnection;
@@ -74,8 +72,8 @@ import org.apache.http.util.EntityUtils;
* Basic, yet fully functional and spec compliant, HTTP/1.1 file server.
* <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 file server.
- *
+ * It is NOT intended to demonstrate the most efficient way of building an HTTP file server.
+ *
*
*/
public class ElementalHttpServer {
@@ -89,24 +87,24 @@ public class ElementalHttpServer {
t.setDaemon(false);
t.start();
}
-
+
static class HttpFileHandler implements HttpRequestHandler {
-
+
private final String docRoot;
-
+
public HttpFileHandler(final String docRoot) {
super();
this.docRoot = docRoot;
}
-
+
public void handle(
- final HttpRequest request,
+ final HttpRequest request,
final HttpResponse response,
final HttpContext context) throws HttpException, IOException {
String method = request.getRequestLine().getMethod().toUpperCase(Locale.ENGLISH);
if (!method.equals("GET") && !method.equals("HEAD") && !method.equals("POST")) {
- throw new MethodNotSupportedException(method + " method not supported");
+ throw new MethodNotSupportedException(method + " method not supported");
}
String target = request.getRequestLine().getUri();
@@ -115,64 +113,44 @@ public class ElementalHttpServer {
byte[] entityContent = EntityUtils.toByteArray(entity);
System.out.println("Incoming entity content (bytes): " + entityContent.length);
}
-
- final File file = new File(this.docRoot, URLDecoder.decode(target));
+
+ final File file = new File(this.docRoot, URLDecoder.decode(target, "UTF-8"));
if (!file.exists()) {
response.setStatusCode(HttpStatus.SC_NOT_FOUND);
- EntityTemplate body = new EntityTemplate(new ContentProducer() {
-
- public void writeTo(final OutputStream outstream) throws IOException {
- OutputStreamWriter writer = new OutputStreamWriter(outstream, "UTF-8");
- writer.write("<html><body><h1>");
- writer.write("File ");
- writer.write(file.getPath());
- writer.write(" not found");
- writer.write("</h1></body></html>");
- writer.flush();
- }
-
- });
- body.setContentType("text/html; charset=UTF-8");
- response.setEntity(body);
+ StringEntity entity = new StringEntity(
+ "<html><body><h1>File" + file.getPath() +
+ " not found</h1></body></html>",
+ ContentType.create("text/html", "UTF-8"));
+ response.setEntity(entity);
System.out.println("File " + file.getPath() + " not found");
-
+
} else if (!file.canRead() || file.isDirectory()) {
-
+
response.setStatusCode(HttpStatus.SC_FORBIDDEN);
- EntityTemplate body = new EntityTemplate(new ContentProducer() {
-
- public void writeTo(final OutputStream outstream) throws IOException {
- OutputStreamWriter writer = new OutputStreamWriter(outstream, "UTF-8");
- writer.write("<html><body><h1>");
- writer.write("Access denied");
- writer.write("</h1></body></html>");
- writer.flush();
- }
-
- });
- body.setContentType("text/html; charset=UTF-8");
- response.setEntity(body);
+ StringEntity entity = new StringEntity(
+ "<html><body><h1>Access denied</h1></body></html>",
+ ContentType.create("text/html", "UTF-8"));
+ response.setEntity(entity);
System.out.println("Cannot read file " + file.getPath());
-
+
} else {
-
+
response.setStatusCode(HttpStatus.SC_OK);
- FileEntity body = new FileEntity(file, "text/html");
+ FileEntity body = new FileEntity(file, ContentType.create("text/html", null));
response.setEntity(body);
System.out.println("Serving file " + file.getPath());
-
}
}
-
+
}
-
+
static class RequestListenerThread extends Thread {
private final ServerSocket serversocket;
- private final HttpParams params;
+ private final HttpParams params;
private final HttpService httpService;
-
+
public RequestListenerThread(int port, final String docroot) throws IOException {
this.serversocket = new ServerSocket(port);
this.params = new SyncBasicHttpParams();
@@ -190,20 +168,20 @@ public class ElementalHttpServer {
new ResponseContent(),
new ResponseConnControl()
});
-
+
// Set up request handlers
HttpRequestHandlerRegistry reqistry = new HttpRequestHandlerRegistry();
reqistry.register("*", new HttpFileHandler(docroot));
-
+
// Set up the HTTP service
this.httpService = new HttpService(
- httpproc,
- new DefaultConnectionReuseStrategy(),
+ httpproc,
+ new DefaultConnectionReuseStrategy(),
new DefaultHttpResponseFactory(),
reqistry,
this.params);
}
-
+
public void run() {
System.out.println("Listening on port " + this.serversocket.getLocalPort());
while (!Thread.interrupted()) {
@@ -221,27 +199,27 @@ public class ElementalHttpServer {
} catch (InterruptedIOException ex) {
break;
} catch (IOException e) {
- System.err.println("I/O error initialising connection thread: "
+ System.err.println("I/O error initialising connection thread: "
+ e.getMessage());
break;
}
}
}
}
-
+
static class WorkerThread extends Thread {
private final HttpService httpservice;
private final HttpServerConnection conn;
-
+
public WorkerThread(
- final HttpService httpservice,
+ final HttpService httpservice,
final HttpServerConnection conn) {
super();
this.httpservice = httpservice;
this.conn = conn;
}
-
+
public void run() {
System.out.println("New connection thread");
HttpContext context = new BasicHttpContext(null);
@@ -263,5 +241,5 @@ public class ElementalHttpServer {
}
}
-
+
}