Revision: 1978
Author: [email protected]
Date: Fri Jul 23 15:48:15 2010
Log: Process post to addProject REST API as text/xml content type with the DOAP RDF/XML in the request body (fixes Issue 328)
http://code.google.com/p/simal/source/detail?r=1978

Modified:
/trunk/uk.ac.osswatch.simal.rest/src/main/java/uk/ac/osswatch/simal/rest/RESTCommand.java /trunk/uk.ac.osswatch.simal.rest/src/main/java/uk/ac/osswatch/simal/rest/RESTServlet.java

=======================================
--- /trunk/uk.ac.osswatch.simal.rest/src/main/java/uk/ac/osswatch/simal/rest/RESTCommand.java Tue Dec 15 16:42:26 2009 +++ /trunk/uk.ac.osswatch.simal.rest/src/main/java/uk/ac/osswatch/simal/rest/RESTCommand.java Fri Jul 23 15:48:15 2010
@@ -17,11 +17,18 @@
  * under the License.                                                *
  */

+import java.io.BufferedReader;
+import java.io.IOException;
 import java.util.HashMap;
 import java.util.Iterator;
 import java.util.Map;
 import java.util.Map.Entry;

+import javax.servlet.http.HttpServletRequest;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
 import uk.ac.osswatch.simal.SimalProperties;
 import uk.ac.osswatch.simal.rdf.SimalRepositoryException;

@@ -31,6 +38,9 @@
  */
 public final class RESTCommand {

+  private static final Logger LOGGER = LoggerFactory
+      .getLogger(RESTCommand.class);
+
   public static final String ALL_PROJECTS = "/allProjects";
   public static final String GET_PROJECT = "/project";
   public static final String PROJECT_ADD = "/addProject";
@@ -50,6 +60,8 @@
   public static final String TYPE_SIMAL = "simal";
   public static final String TYPE_MYEXPERIMENT = "myExperiment";

+  public static final String CONTENT_TYPE_TEXT_XML = "text/xml";
+
   private static String PARAM_METHOD = "method";
   private static String PARAM_FORMAT = "format";

@@ -264,8 +276,12 @@
    * @return
    * @throws SimalAPIException
    */
- public static RESTCommand createCommand(String cmdString, Map<String, String[]> paramMap) throws SimalAPIException {
+  @SuppressWarnings("unchecked")
+ public static RESTCommand createCommand(HttpServletRequest req) throws SimalAPIException {
+    String cmdString = req.getPathInfo();
+    Map<String, String[]> paramMap = req.getParameterMap();
     RESTCommand cmd = createCommand(cmdString);
+
Iterator<Entry<String, String[]>> entries = paramMap.entrySet().iterator();
     while (entries.hasNext()) {
       Entry<String, String[]> entry = entries.next();
@@ -275,8 +291,32 @@
         cmd.addParameter(key, values[i]);
       }
     }
+
+ if(cmd.isAddProject() && req.getContentType().equals(CONTENT_TYPE_TEXT_XML)) {
+      cmd.addParameter(PARAM_RDF, readRequestBody(req));
+    }
+
     return cmd;
   }
+
+  /**
+   * Extract the body of the HttpServletRequest
+   * @param req
+   * @return
+   */
+  private static String readRequestBody(HttpServletRequest req) {
+    StringBuffer requestBody = new StringBuffer();
+    try {
+      BufferedReader reader = req.getReader();
+      String currentLine = null;
+      while((currentLine = reader.readLine()) != null) {
+        requestBody.append(currentLine);
+      }
+    } catch (IOException e) {
+ LOGGER.warn("Could not read request body of http req: " + e.getMessage(),e);
+    }
+    return requestBody.toString();
+  }

   /**
    * Create a command object that represents a complete REST command string
=======================================
--- /trunk/uk.ac.osswatch.simal.rest/src/main/java/uk/ac/osswatch/simal/rest/RESTServlet.java Fri Jun 11 10:52:00 2010 +++ /trunk/uk.ac.osswatch.simal.rest/src/main/java/uk/ac/osswatch/simal/rest/RESTServlet.java Fri Jul 23 15:48:15 2010
@@ -91,7 +91,6 @@
     }
   }

-  @SuppressWarnings("unchecked")
   public void doPost(HttpServletRequest req, HttpServletResponse res)
       throws ServletException, IOException {
     if (logger.isTraceEnabled()) {
@@ -101,7 +100,7 @@

     RESTCommand cmd;
     try {
- cmd = RESTCommand.createCommand(req.getPathInfo(), req.getParameterMap());
+      cmd = RESTCommand.createCommand(req);
     } catch (SimalAPIException e) {
       logger.warn("Error craeting Simal REST command: " + e.getMessage());
       throw new ServletException("Unable to create Simal REST command", e);

--
You received this message because you are subscribed to the Google Groups "Simal 
Commits" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/simal-commits?hl=en.

Reply via email to