You are viewing a plain text version of this content. The canonical link for it is here.
Posted to oak-commits@jackrabbit.apache.org by th...@apache.org on 2012/04/23 14:15:24 UTC
svn commit: r1329201 - in
/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/mk/server:
BoundaryInputStream.java HttpProcessor.java MicroKernelServlet.java
Request.java Response.java
Author: thomasm
Date: Mon Apr 23 12:15:24 2012
New Revision: 1329201
URL: http://svn.apache.org/viewvc?rev=1329201&view=rev
Log:
OAK-57 Cleanup o.a.j.mk from oak-core (formatting)
Modified:
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/mk/server/BoundaryInputStream.java
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/mk/server/HttpProcessor.java
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/mk/server/MicroKernelServlet.java
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/mk/server/Request.java
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/mk/server/Response.java
Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/mk/server/BoundaryInputStream.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/mk/server/BoundaryInputStream.java?rev=1329201&r1=1329200&r2=1329201&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/mk/server/BoundaryInputStream.java (original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/mk/server/BoundaryInputStream.java Mon Apr 23 12:15:24 2012
@@ -26,22 +26,22 @@ import java.io.IOException;
class BoundaryInputStream extends InputStream {
private InputStream in;
-
+
private final byte[] boundary;
-
+
private final byte[] buf;
-
+
private int offset;
-
+
private int count;
-
+
private int boundaryIndex;
-
+
private boolean eos;
/**
* Create a new instance of this class.
- *
+ *
* @param in base input
* @param boundary boundary
*/
@@ -51,7 +51,7 @@ class BoundaryInputStream extends InputS
/**
* Create a new instance of this class.
- *
+ *
* @param in base input
* @param boundary boundary
* @param size size of internal read-ahead buffer
@@ -123,7 +123,7 @@ class BoundaryInputStream extends InputS
i -= boundaryIndex;
if (i < 0) {
offset += i;
- count += (-i);
+ count += -i;
i = 0;
}
boundaryIndex = 0;
Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/mk/server/HttpProcessor.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/mk/server/HttpProcessor.java?rev=1329201&r1=1329200&r2=1329201&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/mk/server/HttpProcessor.java (original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/mk/server/HttpProcessor.java Mon Apr 23 12:15:24 2012
@@ -29,28 +29,28 @@ import org.apache.jackrabbit.mk.util.IOU
* Process all HTTP requests on a single socket.
*/
class HttpProcessor {
-
+
private static final int INITIAL_SO_TIMEOUT = 1000;
-
+
private static final int DEFAULT_SO_TIMEOUT = 30000;
-
+
private static final int MAX_KEEP_ALIVE_REQUESTS = 100;
private final Socket socket;
-
+
private final Servlet servlet;
-
+
private InputStream socketIn;
-
+
private OutputStream socketOut;
-
- private final Request request = new Request();
- private final Response response = new Response();
+ private final Request request = new Request();
+
+ private final Response response = new Response();
/**
* Create a new instance of this class.
- *
+ *
* @param socket socket
* @param servlet servlet to invoke for incoming requests
*/
@@ -58,20 +58,20 @@ class HttpProcessor {
this.socket = socket;
this.servlet = servlet;
}
-
+
/**
* Process all requests on a single socket.
- *
+ *
* @throws IOException if an I/O error occurs
*/
public void process() throws IOException {
try {
socketIn = new BufferedInputStream(socket.getInputStream());
socketOut = new BufferedOutputStream(socket.getOutputStream());
-
+
socket.setSoTimeout(INITIAL_SO_TIMEOUT);
-
- for (int requestNum = 0; ; requestNum++) {
+
+ for (int requestNum = 0;; requestNum++) {
if (!process(requestNum)) {
break;
}
@@ -85,7 +85,7 @@ class HttpProcessor {
IOUtils.closeQuietly(socket);
}
}
-
+
/**
* Process a single request.
*
Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/mk/server/MicroKernelServlet.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/mk/server/MicroKernelServlet.java?rev=1329201&r1=1329200&r2=1329201&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/mk/server/MicroKernelServlet.java (original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/mk/server/MicroKernelServlet.java Mon Apr 23 12:15:24 2012
@@ -33,13 +33,13 @@ import org.apache.jackrabbit.mk.util.IOU
* Servlet handling requests directed at a {@code MicroKernel} instance.
*/
class MicroKernelServlet {
-
+
/** The one and only instance of this servlet. */
public static MicroKernelServlet INSTANCE = new MicroKernelServlet();
-
+
/** Just one instance, no need to make constructor public */
private MicroKernelServlet() {}
-
+
public void service(MicroKernel mk, Request request, Response response) throws IOException {
String file = request.getFile();
int dotIndex = file.indexOf('.');
@@ -63,13 +63,13 @@ class MicroKernelServlet {
}
response.setStatusCode(404);
}
-
+
private static interface Command {
-
+
void execute(MicroKernel mk, Request request, Response response)
throws IOException, MicroKernelException;
}
-
+
private static final Map<String, Command> COMMANDS = new HashMap<String, Command>();
static {
@@ -88,24 +88,24 @@ class MicroKernelServlet {
COMMANDS.put("read", new Read());
COMMANDS.put("write", new Write());
}
-
+
static class GetHeadRevision implements Command {
-
+
@Override
public void execute(MicroKernel mk, Request request, Response response)
throws IOException, MicroKernelException {
response.setContentType("text/plain");
response.write(mk.getHeadRevision());
- }
+ }
}
static class GetRevisionHistory implements Command {
-
+
@Override
public void execute(MicroKernel mk, Request request, Response response)
throws IOException, MicroKernelException {
-
+
long since = request.getParameter("since", 0L);
int maxEntries = request.getParameter("max_entries", 10);
@@ -115,9 +115,9 @@ class MicroKernelServlet {
json = JsopBuilder.prettyPrint(json);
}
response.write(json);
- }
+ }
}
-
+
static class WaitForCommit implements Command {
@Override
@@ -147,9 +147,9 @@ class MicroKernelServlet {
@Override
public void execute(MicroKernel mk, Request request, Response response)
throws IOException, MicroKernelException {
-
+
String headRevision = mk.getHeadRevision();
-
+
String fromRevisionId = request.getParameter("from_revision_id", headRevision);
String toRevisionId = request.getParameter("to_revision_id", headRevision);
String filter = request.getParameter("filter", "");
@@ -160,7 +160,7 @@ class MicroKernelServlet {
json = JsopBuilder.prettyPrint(json);
}
response.write(json);
- }
+ }
}
static class Diff implements Command {
@@ -221,7 +221,7 @@ class MicroKernelServlet {
@Override
public void execute(MicroKernel mk, Request request, Response response)
throws IOException, MicroKernelException {
-
+
String headRevision = mk.getHeadRevision();
String path = request.getParameter("path", "/");
@@ -241,7 +241,7 @@ class MicroKernelServlet {
json = JsopBuilder.prettyPrint(json);
}
response.write(json);
- }
+ }
}
static class Commit implements Command {
@@ -249,19 +249,19 @@ class MicroKernelServlet {
@Override
public void execute(MicroKernel mk, Request request, Response response)
throws IOException, MicroKernelException {
-
+
String headRevision = mk.getHeadRevision();
String path = request.getParameter("path", "/");
String jsonDiff = request.getParameter("json_diff");
String revisionId = request.getParameter("revision_id", headRevision);
String message = request.getParameter("message");
-
+
String newRevision = mk.commit(path, jsonDiff, revisionId, message);
response.setContentType("text/plain");
response.write(newRevision);
- }
+ }
}
static class Branch implements Command {
@@ -319,8 +319,8 @@ class MicroKernelServlet {
String blobId = request.getParameter("blob_id", "");
long pos = request.getParameter("pos", 0L);
- int length = request.getParameter("length", -1);
-
+ int length = request.getParameter("length", -1);
+
OutputStream out = response.getOutputStream();
if (pos == 0L && length == -1) {
/* return the complete binary */
@@ -349,6 +349,6 @@ class MicroKernelServlet {
response.setContentType("text/plain");
response.write(blobId);
- }
+ }
}
}
Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/mk/server/Request.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/mk/server/Request.java?rev=1329201&r1=1329200&r2=1329201&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/mk/server/Request.java (original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/mk/server/Request.java Mon Apr 23 12:15:24 2012
@@ -33,46 +33,46 @@ import org.apache.jackrabbit.mk.util.IOU
* HTTP Request implementation.
*/
class Request implements Closeable {
-
+
private static final String HTTP_11_PROTOCOL = "HTTP/1.1";
private InputStream in;
-
+
private String method;
-
+
private String file;
-
+
private String queryString;
-
+
private String protocol;
-
- private final Map<String,String> headers = new LinkedHashMap<String,String>();
-
+
+ private final Map<String, String> headers = new LinkedHashMap<String, String>();
+
private boolean paramsChecked;
-
- private final Map<String, String> params = new LinkedHashMap<String,String>();
-
+
+ private final Map<String, String> params = new LinkedHashMap<String, String>();
+
private final ChunkedInputStream chunkedIn = new ChunkedInputStream(null);
private InputStream reqIn;
-
+
/**
* Parse a request. This automatically resets any internal state, so it can be
* used multiple times
- *
+ *
* @param in input stream
* @throws IOException if an I/O error occurs
*/
void parse(InputStream in) throws IOException {
String requestLine = readLine(in);
-
+
String[] parts = requestLine.split(" ");
if (parts.length != 3) {
String msg = String.format("Bad HTTP request line: %s", requestLine);
throw new IOException(msg);
}
method = parts[0];
-
+
String uri = parts[1];
int index = uri.lastIndexOf('?');
if (index == -1) {
@@ -82,11 +82,11 @@ class Request implements Closeable {
file = uri.substring(0, index);
queryString = uri.substring(index + 1);
}
-
+
protocol = parts[2];
-
+
headers.clear();
-
+
for (;;) {
String headerLine = readLine(in);
if (headerLine.length() == 0) {
@@ -97,14 +97,14 @@ class Request implements Closeable {
headers.put(parts[0].trim(), parts[1].trim());
}
}
-
+
params.clear();
paramsChecked = false;
reqIn = null;
-
+
this.in = in;
}
-
+
/**
* Read a single line, terminated by a CR LF combination from an {@code InputStream}.
*
@@ -113,7 +113,7 @@ class Request implements Closeable {
*/
private static String readLine(InputStream in) throws IOException {
StringBuilder line = new StringBuilder(128);
-
+
for (;;) {
int c = in.read();
switch (c) {
@@ -129,7 +129,7 @@ class Request implements Closeable {
}
}
}
-
+
public String getMethod() {
return method;
}
@@ -137,7 +137,7 @@ class Request implements Closeable {
public String getFile() {
return file;
}
-
+
private String getContentType() {
String ct = headers.get("Content-Type");
if (ct != null) {
@@ -148,7 +148,7 @@ class Request implements Closeable {
}
return ct;
}
-
+
private int getContentLength() {
String s = headers.get("Content-Length");
if (s != null) {
@@ -160,11 +160,11 @@ class Request implements Closeable {
}
return -1;
}
-
+
public Map<String, String> getHeaders() {
return headers;
}
-
+
public String getQueryString() {
return queryString;
}
@@ -184,7 +184,7 @@ class Request implements Closeable {
}
return params.get(name);
}
-
+
public String getParameter(String name, String defaultValue) throws IOException {
String s = getParameter(name);
if (s != null) {
@@ -240,7 +240,7 @@ class Request implements Closeable {
return new BoundaryInputStream(body, boundary);
}
- private static void collectParameters(String s, Map<String,String> map) throws IOException {
+ private static void collectParameters(String s, Map<String, String> map) throws IOException {
for (String param : s.split("&")) {
String[] nv = param.split("=", 2);
if (nv.length == 2) {
@@ -248,7 +248,7 @@ class Request implements Closeable {
}
}
}
-
+
public InputStream getInputStream() {
if (reqIn == null) {
String encoding = headers.get("Transfer-Encoding");
@@ -265,11 +265,11 @@ class Request implements Closeable {
}
return reqIn;
}
-
+
boolean isKeepAlive() {
return HTTP_11_PROTOCOL.equals(protocol);
}
-
+
@Override
public void close() {
if (in != null) {
Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/mk/server/Response.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/mk/server/Response.java?rev=1329201&r1=1329200&r2=1329201&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/mk/server/Response.java (original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/mk/server/Response.java Mon Apr 23 12:15:24 2012
@@ -30,30 +30,30 @@ import static org.apache.jackrabbit.mk.u
* HTTP Response implementation.
*/
class Response implements Closeable {
-
+
private OutputStream out;
-
+
private boolean keepAlive;
-
+
private boolean headersSent;
-
+
private boolean committed;
-
+
private boolean chunked;
-
+
private int statusCode;
-
+
private String contentType;
-
+
private final BodyOutputStream bodyOut = new BodyOutputStream();
-
+
private OutputStream respOut;
-
- private final Map<String,String> headers = new LinkedHashMap<String, String>();
-
+
+ private final Map<String, String> headers = new LinkedHashMap<String, String>();
+
/**
* Recycle this instance, using another output stream and a keep-alive flag.
- *
+ *
* @param out output stream
* @param keepAlive whether to keep alive the connection
*/
@@ -68,10 +68,10 @@ class Response implements Closeable {
respOut = null;
headers.clear();
}
-
+
/**
* Return the status message associated with a status code.
- *
+ *
* @param sc status code
* @return associated status message
*/
@@ -89,14 +89,14 @@ class Response implements Closeable {
return "Internal server error";
}
}
-
+
private void sendHeaders() throws IOException {
if (headersSent) {
return;
}
-
+
headersSent = true;
-
+
int statusCode = this.statusCode;
if (statusCode == 0) {
statusCode = 200;
@@ -114,9 +114,9 @@ class Response implements Closeable {
setContentType("text/html");
write(body);
}
-
+
writeLine(String.format("HTTP/1.1 %d %s", statusCode, msg));
-
+
if (committed) {
writeLine(String.format("Content-Length: %d", bodyOut.getCount()));
} else {
@@ -134,9 +134,9 @@ class Response implements Closeable {
writeLine(String.format("%s: %s", header.getKey(), header.getValue()));
}
}
-
+
writeLine("");
-
+
if (out != null) {
out.flush();
}
@@ -145,11 +145,11 @@ class Response implements Closeable {
@Override
public void close() throws IOException {
committed = true;
-
+
try {
sendHeaders();
IOUtils.closeQuietly(respOut);
-
+
if (out != null) {
out.flush();
}
@@ -157,7 +157,7 @@ class Response implements Closeable {
out = null;
}
}
-
+
private void writeLine(String s) throws IOException {
if (out == null) {
return;
@@ -188,7 +188,7 @@ class Response implements Closeable {
out.write(("\r\n").getBytes());
}
}
-
+
public void setContentType(String contentType) {
this.contentType = contentType;
}
@@ -203,32 +203,32 @@ class Response implements Closeable {
public void setStatusCode(int statusCode) {
this.statusCode = statusCode;
}
-
+
public void addHeader(String name, String value) {
headers.put(name, value);
}
-
+
public void write(String s) throws IOException {
getOutputStream().write(s.getBytes("8859_1"));
}
-
+
/**
* Internal {@code OutputStream} passed to servlet handlers.
*/
class BodyOutputStream extends OutputStream {
-
+
/**
* Buffer size chosen intentionally to not exceed maximum chunk
* size we'd like to transmit.
*/
private final byte[] buf = new byte[MAX_CHUNK_SIZE];
-
+
private int offset;
/**
* Return the number of valid bytes in the buffer.
- *
- * @return number of bytes
+ *
+ * @return number of bytes
*/
public int getCount() {
return offset;
@@ -257,7 +257,7 @@ class Response implements Closeable {
offset += n;
}
}
-
+
@Override
public void flush() throws IOException {
if (offset > 0) {
@@ -265,15 +265,15 @@ class Response implements Closeable {
offset = 0;
}
}
-
+
public void reset() {
offset = 0;
}
-
+
@Override
public void close() throws IOException {
flush();
-
+
writeBody(buf, 0, 0);
}
}