You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pivot.apache.org by gb...@apache.org on 2010/03/05 18:17:54 UTC
svn commit: r919509 -
/pivot/trunk/web-server/src/org/apache/pivot/web/server/QueryServlet.java
Author: gbrown
Date: Fri Mar 5 17:17:54 2010
New Revision: 919509
URL: http://svn.apache.org/viewvc?rev=919509&view=rev
Log:
Eliminate MIME type mapping to serializers. Add path argument to createSerializer() and make public.
Modified:
pivot/trunk/web-server/src/org/apache/pivot/web/server/QueryServlet.java
Modified: pivot/trunk/web-server/src/org/apache/pivot/web/server/QueryServlet.java
URL: http://svn.apache.org/viewvc/pivot/trunk/web-server/src/org/apache/pivot/web/server/QueryServlet.java?rev=919509&r1=919508&r2=919509&view=diff
==============================================================================
--- pivot/trunk/web-server/src/org/apache/pivot/web/server/QueryServlet.java (original)
+++ pivot/trunk/web-server/src/org/apache/pivot/web/server/QueryServlet.java Fri Mar 5 17:17:54 2010
@@ -31,9 +31,6 @@
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
-import org.apache.pivot.collections.HashMap;
-import org.apache.pivot.serialization.CSVSerializer;
-import org.apache.pivot.serialization.JSONSerializer;
import org.apache.pivot.serialization.SerializationException;
import org.apache.pivot.serialization.Serializer;
import org.apache.pivot.web.Query;
@@ -47,8 +44,6 @@
private static final long serialVersionUID = 4881638232902478092L;
private boolean determineContentLength = false;
- private HashMap<String, Class<? extends Serializer<?>>> serializerTypes
- = new HashMap<String, Class<? extends Serializer<?>>>();
private transient ThreadLocal<String> hostname = new ThreadLocal<String>();
private transient ThreadLocal<Integer> port = new ThreadLocal<Integer>();
@@ -68,17 +63,6 @@
public static final String CONTENT_LENGTH_HEADER = "Content-Length";
public static final String LOCATION_HEADER = "Location";
- @Override
- public void init() throws ServletException {
- super.init();
-
- // TODO Read determineContentLength and MIME type-serializer class mapping
- // from init params
-
- serializerTypes.put(JSONSerializer.MIME_TYPE, JSONSerializer.class);
- serializerTypes.put(CSVSerializer.MIME_TYPE, CSVSerializer.class);
- }
-
/**
* Gets the host name that was requested.
*/
@@ -173,14 +157,6 @@
}
/**
- * Allows a servlet to configure a serializer
- *
- * @param serializer
- */
- public void configureSerializer(Serializer<?> serializer, String path) {
- }
-
- /**
* Handles an HTTP GET request. The default implementation throws an HTTP
* 405 query exception.
*
@@ -249,6 +225,13 @@
throw new QueryException(Query.Status.METHOD_NOT_ALLOWED);
}
+ /**
+ * Creates a serializer that will be used to serialize the current request data.
+ *
+ * @param path
+ */
+ public abstract Serializer<?> createSerializer(String path) throws ServletException;
+
@Override
@SuppressWarnings("unchecked")
protected void service(HttpServletRequest request, HttpServletResponse response)
@@ -324,13 +307,11 @@
@SuppressWarnings("unchecked")
protected final void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
- Serializer<Object> serializer = (Serializer<Object>)createSerializer(request.getHeader(CONTENT_TYPE_HEADER));
+ String path = request.getPathInfo();
Object result = null;
try {
- String path = request.getPathInfo();
validate(path);
- configureSerializer(serializer, path);
result = doGet(path);
} catch (QueryException exception) {
response.setStatus(exception.getStatus());
@@ -340,6 +321,8 @@
if (!response.isCommitted()) {
response.setStatus(200);
setResponseHeaders(response);
+
+ Serializer<Object> serializer = (Serializer<Object>)createSerializer(path);
response.setContentType(serializer.getMIMEType(result));
OutputStream responseOutputStream = response.getOutputStream();
@@ -389,23 +372,21 @@
@Override
protected final void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
- Serializer<?> serializer = createSerializer(request.getHeader(CONTENT_TYPE_HEADER));
String action = request.getHeader(ACTION_HEADER);
if (action == null) {
Object value = null;
- try {
- value = serializer.readObject(request.getInputStream());
- } catch (SerializationException exception) {
- throw new ServletException(exception);
- }
+
+ String path = request.getPathInfo();
URL location = null;
try {
- String path = request.getPathInfo();
validate(path);
- configureSerializer(serializer, path);
+ Serializer<?> serializer = createSerializer(path);
+ value = serializer.readObject(request.getInputStream());
location = doPost(path, value);
+ } catch (SerializationException exception) {
+ throw new ServletException(exception);
} catch (QueryException exception) {
response.setStatus(exception.getStatus());
response.flushBuffer();
@@ -418,8 +399,9 @@
response.setContentLength(0);
}
} else {
+ String path = request.getPathInfo();
+
try {
- String path = request.getPathInfo();
validate(path);
doPostAction(path, action);
} catch (QueryException exception) {
@@ -440,20 +422,17 @@
@Override
protected final void doPut(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
- Serializer<?> serializer = createSerializer(request.getHeader(CONTENT_TYPE_HEADER));
-
Object value = null;
- try {
- value = serializer.readObject(request.getInputStream());
- } catch (SerializationException exception) {
- throw new ServletException(exception);
- }
+
+ String path = request.getPathInfo();
try {
- String path = request.getPathInfo();
validate(path);
- configureSerializer(serializer, path);
+ Serializer<?> serializer = createSerializer(path);
+ value = serializer.readObject(request.getInputStream());
doPut(path, value);
+ } catch (SerializationException exception) {
+ throw new ServletException(exception);
} catch (QueryException exception) {
response.setStatus(exception.getStatus());
response.flushBuffer();
@@ -508,33 +487,6 @@
response.flushBuffer();
}
- @SuppressWarnings("unchecked")
- protected Serializer<?> createSerializer(String mimeType)
- throws ServletException {
- if (mimeType == null) {
- mimeType = JSONSerializer.MIME_TYPE;
- } else {
- mimeType = mimeType.substring(0, mimeType.indexOf(';'));
- mimeType = mimeType.trim();
- }
-
- Class<? extends Serializer<?>> serializerType = serializerTypes.get(mimeType);
- if (serializerType == null) {
- throw new ServletException("A serializer for " + mimeType + " not found.");
- }
-
- Serializer<Object> serializer = null;
- try {
- serializer = (Serializer<Object>)serializerType.newInstance();
- } catch (InstantiationException exception) {
- throw new ServletException(exception);
- } catch (IllegalAccessException exception) {
- throw new ServletException(exception);
- }
-
- return serializer;
- }
-
private void setResponseHeaders(HttpServletResponse response) {
QueryDictionary responseHeaderDictionary = responseHeaders.get();