Author: dpfister Date: Fri Sep 9 15:22:05 2011 New Revision: 1167244 URL: http://svn.apache.org/viewvc?rev=1167244&view=rev Log: HTTP interface to microkernel Added: jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/server/SocketConnection.java - copied, changed from r1166763, jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/server/SocketHandler.java jackrabbit/sandbox/microkernel/src/main/resources/ jackrabbit/sandbox/microkernel/src/main/resources/org/ jackrabbit/sandbox/microkernel/src/main/resources/org/apache/ jackrabbit/sandbox/microkernel/src/main/resources/org/apache/jackrabbit/ jackrabbit/sandbox/microkernel/src/main/resources/org/apache/jackrabbit/mk/ jackrabbit/sandbox/microkernel/src/main/resources/org/apache/jackrabbit/mk/server/ Modified: jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/server/Server.java jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/server/SocketHandler.java jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/util/IOUtils.java Modified: jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/server/Server.java URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/server/Server.java?rev=1167244&r1=1167243&r2=1167244&view=diff ============================================================================== --- jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/server/Server.java (original) +++ jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/server/Server.java Fri Sep 9 15:22:05 2011 @@ -32,14 +32,17 @@ import org.apache.jackrabbit.mk.api.Micr */ public class Server { - private AtomicReference mk; + private AtomicReference mkref; + private MicroKernelServlet mkServlet; private AtomicBoolean started = new AtomicBoolean(); private AtomicBoolean stopped = new AtomicBoolean(); private ServerSocket ss; private ExecutorService es; public Server(MicroKernel mk) { - this.mk = new AtomicReference(mk); + this.mkref = new AtomicReference(mk); + + mkServlet = new MicroKernelServlet(mkref); } public void start() throws IOException { @@ -74,17 +77,32 @@ public class Server { /* ignore */ } try { - new SocketHandler(s).process(); + new SocketConnection(s, new ServletImpl()).process(); } catch (IOException e) { /* ignore */ } } + class ServletImpl implements Servlet { + + private final Servlet fileServlet = new FileServlet(); + + public void service(Request request, Response response) + throws IOException { + + if (request.getMethod().equals("POST")) { + mkServlet.service(request, response); + } else { + fileServlet.service(request, response); + } + } + } + public void stop() { if (!stopped.compareAndSet(false, true)) { return; } - MicroKernel mk = this.mk.getAndSet(null); + MicroKernel mk = mkref.getAndSet(null); if (mk != null) { mk.dispose(); } Copied: jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/server/SocketConnection.java (from r1166763, jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/server/SocketHandler.java) URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/server/SocketConnection.java?p2=jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/server/SocketConnection.java&p1=jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/server/SocketHandler.java&r1=1166763&r2=1167244&rev=1167244&view=diff ============================================================================== --- jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/server/SocketHandler.java (original) +++ jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/server/SocketConnection.java Fri Sep 9 15:22:05 2011 @@ -18,101 +18,78 @@ package org.apache.jackrabbit.mk.server; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; -import java.io.EOFException; import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; import java.net.Socket; import org.apache.jackrabbit.mk.util.IOUtils; /** - * Socket handler for HTTP requests directed at a server. + * Process all HTTP requests on a single socket. */ -class SocketHandler { +class SocketConnection { private final Socket sock; - private boolean keepAlive; - private BufferedInputStream in; - private BufferedOutputStream out; - private int sc; - private String ctype; - private StringBuilder body; - - public SocketHandler(Socket sock) { + private final Servlet servlet; + private InputStream sockIn; + private OutputStream sockOut; + + /** + * Create a new instance of this class. + * + * @param sock socket + * @param servlet servlet to invoke for incoming requests + */ + public SocketConnection(Socket sock, Servlet servlet) { this.sock = sock; + this.servlet = servlet; } + /** + * Process all requests on a single socket. + * + * @throws IOException if an I/O error occurs + */ public void process() throws IOException { - in = new BufferedInputStream(sock.getInputStream()); - out = new BufferedOutputStream(sock.getOutputStream()); - body = new StringBuilder(); - - do { - String requestLine = readLine(); - for (;;) { - String header = readLine(); - if (header.length() == 0) { - break; - } - } - try { - // TODO handle - - sendNotFound(); - } finally { - sendHeaders(); - } + try { + sockIn = new BufferedInputStream(sock.getInputStream()); + sockOut = new BufferedOutputStream(sock.getOutputStream()); - } while (keepAlive); - - IOUtils.closeQuietly(out); - IOUtils.closeQuietly(in); - IOUtils.closeQuietly(sock); - } - - private String readLine() throws IOException { - StringBuilder line = new StringBuilder(128); + do { + process1(); + + } while (false /* TODO: check for keep-alive */); - for (;;) { - int c = in.read(); - switch (c) { - case '\r': - break; - case '\n': - return line.toString(); - case -1: - throw new EOFException(); - default: - line.append((char) c); - } + } finally { + IOUtils.closeQuietly(sockOut); + IOUtils.closeQuietly(sockIn); + IOUtils.closeQuietly(sock); } } - private void sendNotFound() throws IOException { - sc = 404; - - body.append("" + - "" + - "404 Not Found" + - "" + - "

Not Found

" + - "

The requested URL /test.html was not found on this server.

" + - ""); - } - - private void sendHeaders() throws IOException { - String msg = (sc == 404 ? "Not found" : "OK"); + /** + * Process a single request. + * + * @throws IOException if an I/O error occurs + */ + private void process1() throws IOException { + Request request = null; + Response response = new Response(sockOut); - writeLine(String.format("HTTP/1.1 %d %s", sc, msg)); - writeLine(String.format("Content-Length: %d", body.length())); - if (!keepAlive) { - writeLine("Connection: Close"); + try { + request = Request.parse(sockIn); + if (request == null) { + response.setStatusCode(400); + } else { + servlet.service(request, response); + } + } finally { + if (request != null) { + request.finish(); + } + response.finish(); } - writeLine(""); - out.write(body.toString().getBytes()); } - private void writeLine(String s) throws IOException { - out.write(s.getBytes()); - out.write("\r\n".getBytes()); - } } Modified: jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/util/IOUtils.java URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/util/IOUtils.java?rev=1167244&r1=1167243&r2=1167244&view=diff ============================================================================== --- jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/util/IOUtils.java (original) +++ jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/util/IOUtils.java Fri Sep 9 15:22:05 2011 @@ -274,4 +274,28 @@ public class IOUtils { } } } + + /** + * Copy bytes from an InputStream to an + * OutputStream. + *

+ * This method buffers the input internally, so there is no need to use a + * BufferedInputStream. + * + * @param input the InputStream to read from + * @param output the OutputStream to write to + * @return the number of bytes copied + * @throws IOException if an I/O error occurs + */ + public static long copy(InputStream input, OutputStream output) + throws IOException { + byte[] buffer = new byte[4096]; + long count = 0; + int n = 0; + while (-1 != (n = input.read(buffer))) { + output.write(buffer, 0, n); + count += n; + } + return count; + } }