[Mulgara-svn] r1939 - trunk/src/jar/querylang/java/org/mulgara/protocol/http

pag at mulgara.org pag at mulgara.org
Sat Apr 17 16:21:35 UTC 2010


Author: pag
Date: 2010-04-17 09:21:34 -0700 (Sat, 17 Apr 2010)
New Revision: 1939

Modified:
   trunk/src/jar/querylang/java/org/mulgara/protocol/http/ProtocolServlet.java
Log:
Added support for queries executed with a POST rather than a GET (see the POST bindings in http://www.w3.org/TR/rdf-sparql-protocol/#query-bindings-http). Also added infrastructure support for using PATCH via a doPatch method. This method is currently just a default method reporting that PATCH is not supported.

Modified: trunk/src/jar/querylang/java/org/mulgara/protocol/http/ProtocolServlet.java
===================================================================
--- trunk/src/jar/querylang/java/org/mulgara/protocol/http/ProtocolServlet.java	2010-04-17 16:16:03 UTC (rev 1938)
+++ trunk/src/jar/querylang/java/org/mulgara/protocol/http/ProtocolServlet.java	2010-04-17 16:21:34 UTC (rev 1939)
@@ -18,8 +18,11 @@
 
 import java.io.IOException;
 import java.io.OutputStream;
+import java.io.UnsupportedEncodingException;
 import java.net.URI;
 import java.net.URISyntaxException;
+import java.net.URLDecoder;
+import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collections;
 import java.util.EnumMap;
@@ -57,6 +60,7 @@
 import org.mulgara.query.operation.SetAutoCommit;
 import org.mulgara.server.ServerInfo;
 import org.mulgara.server.SessionFactoryProvider;
+import org.mulgara.util.StringUtil;
 import org.mulgara.util.functional.C;
 import org.mulgara.util.functional.Fn1E;
 import org.mulgara.util.functional.Fn2;
@@ -100,6 +104,9 @@
    */
   private static final String METHOD_PATCH = "PATCH";
 
+  /** Local access to the name of the HTTP POST method. */
+  private static final String METHOD_POST = "POST";
+
   /** The parameter identifying the query. */
   private static final String QUERY_ARG = "query";
 
@@ -549,6 +556,13 @@
    */
   protected void handleUpdateQuery(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
     String queryStr = req.getParameter(QUERY_ARG);
+    // check to see if this was a large query that couldn't fit into a GET
+    if (queryStr == null) {
+      // a GET-style query that put the query into the body
+      doGet(req, resp);
+      return;
+    }
+
     List<Command> cmds = getCommand(queryStr, req);
 
     boolean tx = cmds.size() > 1;
@@ -899,6 +913,9 @@
    */
   static class RestParams {
 
+    /** Character encoding to use. */
+    private static final String UTF8 = "UTF-8";
+
     /** The default graph URIs for use in the SPARQL protocol. */
     List<URI> defaultGraphUris = null;
 
@@ -919,7 +936,12 @@
 
     @SuppressWarnings("unchecked")
     public RestParams(HttpServletRequest req) throws ServletException {
-      Map<String,String[]> params = req.getParameterMap();
+      Map<String,String[]> params;
+      if (METHOD_POST.equals(req.getMethod())) {
+        params = bodyParamParse(req);
+      } else {
+        params = req.getParameterMap();
+      }
 
       defaultGraphUris = getUriParamList(DEFAULT_GRAPH_ARG, params);
       namedGraphUris = getUriParamList(NAMED_GRAPH_ARG, params);
@@ -1087,6 +1109,40 @@
 
 
     /**
+     * Reads parameters out of the body of a query. This is only expected from a POST, not a GET.
+     * @param data The data from the body of the request.
+     * @return A map of parameters to an array of the strings containing the values.
+     * @throws ServletException If there was bad data in the body.
+     */
+    private static Map<String,String[]> bodyParamParse(HttpServletRequest req) throws ServletException {
+      Map<String,List<String>> params = new HashMap<String,List<String>>();
+      try {
+        String bodyData = StringUtil.toString(req.getReader());
+        for (String entry: bodyData.split("&")) {
+          String[] parts = entry.split("=");
+          if (parts.length != 2) throw new BadRequestException("parameters can only have a single = parts");
+          List<String> values = params.get(parts[0]);
+          if (values == null) {
+            values = new ArrayList<String>();
+            params.put(parts[0], values);
+          }
+          values.add(URLDecoder.decode(parts[1], UTF8));
+        }
+      } catch (UnsupportedEncodingException e) {
+        throw new InternalErrorException("Bad data encoding in " + UTF8 + ": " + e.getMessage());
+      } catch (IOException e) {
+        throw new InternalErrorException("Unable to read request: " + e.getMessage());
+      }
+      Map<String,String[]> paramMap = new HashMap<String,String[]>();
+      for (Map.Entry<String,List<String>> p: params.entrySet()) {
+        List<String> vals = p.getValue();
+        paramMap.put(p.getKey(), vals.toArray(new String[vals.size()]));
+      }
+      return paramMap;
+    }
+
+
+    /**
      * Resources being referenced by an HTTP method.
      */
     enum ResourceType {




More information about the Mulgara-svn mailing list