Author: cutting
Date: Fri Oct 9 20:52:47 2009
New Revision: 823682
URL: http://svn.apache.org/viewvc?rev=823682&view=rev
Log:
AVRO-139. Refactor HTTP servlet to a separate, public class.
Added:
hadoop/avro/trunk/src/java/org/apache/avro/ipc/ResponderServlet.java
Modified:
hadoop/avro/trunk/CHANGES.txt
hadoop/avro/trunk/build.xml
hadoop/avro/trunk/src/java/org/apache/avro/ipc/HttpServer.java
hadoop/avro/trunk/src/java/org/apache/avro/ipc/HttpTransceiver.java
Modified: hadoop/avro/trunk/CHANGES.txt
URL:
http://svn.apache.org/viewvc/hadoop/avro/trunk/CHANGES.txt?rev=823682&r1=823681&r2=823682&view=diff
==============================================================================
--- hadoop/avro/trunk/CHANGES.txt (original)
+++ hadoop/avro/trunk/CHANGES.txt Fri Oct 9 20:52:47 2009
@@ -37,6 +37,8 @@
AVRO-24. Add a simple bulk-data benchmark. (cutting)
+ AVRO-139. Refactor HTTP servlet to separate, public class. (cutting)
+
IMPROVEMENTS
AVRO-99. Use Boost framework for C++ unit tests.
Modified: hadoop/avro/trunk/build.xml
URL:
http://svn.apache.org/viewvc/hadoop/avro/trunk/build.xml?rev=823682&r1=823681&r2=823682&view=diff
==============================================================================
--- hadoop/avro/trunk/build.xml (original)
+++ hadoop/avro/trunk/build.xml Fri Oct 9 20:52:47 2009
@@ -65,6 +65,8 @@
value="http://java.sun.com/javase/6/docs/api/"/>
<property name="javadoc.link.jackson"
value="http://jackson.codehaus.org/0.9.3/javadoc/"/>
+ <property name="javadoc.link.servlet"
+ value="http://java.sun.com/products/servlet/2.3/javadoc/"/>
<property name="javadoc.packages" value="org.${org}.${name}.*"/>
<property name="javac.encoding" value="ISO-8859-1"/>
@@ -470,6 +472,7 @@
<link href="${javadoc.link.java}"/>
<link href="${javadoc.link.jackson}"/>
+ <link href="${javadoc.link.servlet}"/>
<classpath >
<path refid="java.classpath" />
Modified: hadoop/avro/trunk/src/java/org/apache/avro/ipc/HttpServer.java
URL:
http://svn.apache.org/viewvc/hadoop/avro/trunk/src/java/org/apache/avro/ipc/HttpServer.java?rev=823682&r1=823681&r2=823682&view=diff
==============================================================================
--- hadoop/avro/trunk/src/java/org/apache/avro/ipc/HttpServer.java (original)
+++ hadoop/avro/trunk/src/java/org/apache/avro/ipc/HttpServer.java Fri Oct 9
20:52:47 2009
@@ -19,27 +19,25 @@
package org.apache.avro.ipc;
import java.io.IOException;
-import java.nio.ByteBuffer;
-import java.util.List;
-
-import javax.servlet.ServletException;
-import javax.servlet.http.HttpServlet;
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
import org.apache.avro.AvroRuntimeException;
import org.mortbay.jetty.servlet.Context;
import org.mortbay.jetty.servlet.ServletHolder;
-public class HttpServer extends HttpServlet implements Server {
- private Responder responder;
+/** An HTTP-based RPC {...@link Server}. */
+public class HttpServer implements Server {
private org.mortbay.jetty.Server server;
+ /** Starts a server on the named port. */
public HttpServer(Responder responder, int port) throws IOException {
- this.responder = responder;
+ this(new ResponderServlet(responder), port);
+ }
+
+ /** Starts a server on the named port. */
+ public HttpServer(ResponderServlet servlet, int port) throws IOException {
this.server = new org.mortbay.jetty.Server(port);
- new Context(server,"/").addServlet(new ServletHolder(this), "/*");
+ new Context(server,"/").addServlet(new ServletHolder(servlet), "/*");
try {
server.start();
} catch (Exception e) {
@@ -58,19 +56,4 @@
throw new AvroRuntimeException(e);
}
}
-
- public void doPost(HttpServletRequest request, HttpServletResponse response)
- throws IOException, ServletException {
- response.setContentType("avro/binary");
- List<ByteBuffer> requestBuffers =
- HttpTransceiver.readBuffers(request.getInputStream());
- try {
- List<ByteBuffer> responseBuffers =
- responder.respond(requestBuffers);
- response.setContentLength(HttpTransceiver.getLength(responseBuffers));
- HttpTransceiver.writeBuffers(responseBuffers,
response.getOutputStream());
- } catch (AvroRuntimeException e) {
- throw new ServletException(e);
- }
- }
}
Modified: hadoop/avro/trunk/src/java/org/apache/avro/ipc/HttpTransceiver.java
URL:
http://svn.apache.org/viewvc/hadoop/avro/trunk/src/java/org/apache/avro/ipc/HttpTransceiver.java?rev=823682&r1=823681&r2=823682&view=diff
==============================================================================
--- hadoop/avro/trunk/src/java/org/apache/avro/ipc/HttpTransceiver.java
(original)
+++ hadoop/avro/trunk/src/java/org/apache/avro/ipc/HttpTransceiver.java Fri Oct
9 20:52:47 2009
@@ -36,6 +36,8 @@
private static final Logger LOG
= LoggerFactory.getLogger(HttpTransceiver.class);
+ static final String CONTENT_TYPE = "avro/binary";
+
private URL url;
private URLConnection connection;
@@ -47,7 +49,7 @@
public synchronized List<ByteBuffer> transceive(List<ByteBuffer> request)
throws IOException {
this.connection = url.openConnection();
- connection.setRequestProperty("Content-Type", "avro/binary");
+ connection.setRequestProperty("Content-Type", CONTENT_TYPE);
connection.setRequestProperty("Content-Length",
Integer.toString(getLength(request)));
connection.setDoOutput(true);
Added: hadoop/avro/trunk/src/java/org/apache/avro/ipc/ResponderServlet.java
URL:
http://svn.apache.org/viewvc/hadoop/avro/trunk/src/java/org/apache/avro/ipc/ResponderServlet.java?rev=823682&view=auto
==============================================================================
--- hadoop/avro/trunk/src/java/org/apache/avro/ipc/ResponderServlet.java (added)
+++ hadoop/avro/trunk/src/java/org/apache/avro/ipc/ResponderServlet.java Fri
Oct 9 20:52:47 2009
@@ -0,0 +1,56 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.avro.ipc;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.util.List;
+
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.avro.AvroRuntimeException;
+
+/** An {...@link HttpServlet} that responds to Avro RPC requests. */
+public class ResponderServlet extends HttpServlet {
+ private Responder responder;
+
+ public ResponderServlet(Responder responder) throws IOException {
+ this.responder = responder;
+ }
+
+ @Override
+ protected void doPost(HttpServletRequest request,
+ HttpServletResponse response)
+ throws IOException, ServletException {
+ response.setContentType(HttpTransceiver.CONTENT_TYPE);
+ List<ByteBuffer> requestBufs =
+ HttpTransceiver.readBuffers(request.getInputStream());
+ try {
+ List<ByteBuffer> responseBufs = responder.respond(requestBufs);
+ response.setContentLength(HttpTransceiver.getLength(responseBufs));
+ HttpTransceiver.writeBuffers(responseBufs, response.getOutputStream());
+ } catch (AvroRuntimeException e) {
+ throw new ServletException(e);
+ }
+ }
+}
+