svn commit: r1635461 - in /ofbiz/branches/json-integration-refactoring/framework/webapp/src/org/ofbiz/webapp/event: JSONRequestBodyMapHandler.java RequestBodyMapHandler.java RequestBodyMapHandlerFactory.java ServiceEventHandler.java

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

svn commit: r1635461 - in /ofbiz/branches/json-integration-refactoring/framework/webapp/src/org/ofbiz/webapp/event: JSONRequestBodyMapHandler.java RequestBodyMapHandler.java RequestBodyMapHandlerFactory.java ServiceEventHandler.java

jacopoc
Author: jacopoc
Date: Thu Oct 30 10:36:53 2014
New Revision: 1635461

URL: http://svn.apache.org/r1635461
Log:
OFBIZ-5790 Implemented ability to call a service event by passing its input parameters in the request body as JSON data. These classes provide a simple mechanism, based on the request's content type, that can be extended to support other formats (e.g. XML).


Added:
    ofbiz/branches/json-integration-refactoring/framework/webapp/src/org/ofbiz/webapp/event/JSONRequestBodyMapHandler.java   (with props)
    ofbiz/branches/json-integration-refactoring/framework/webapp/src/org/ofbiz/webapp/event/RequestBodyMapHandler.java   (with props)
    ofbiz/branches/json-integration-refactoring/framework/webapp/src/org/ofbiz/webapp/event/RequestBodyMapHandlerFactory.java   (with props)
Modified:
    ofbiz/branches/json-integration-refactoring/framework/webapp/src/org/ofbiz/webapp/event/ServiceEventHandler.java

Added: ofbiz/branches/json-integration-refactoring/framework/webapp/src/org/ofbiz/webapp/event/JSONRequestBodyMapHandler.java
URL: http://svn.apache.org/viewvc/ofbiz/branches/json-integration-refactoring/framework/webapp/src/org/ofbiz/webapp/event/JSONRequestBodyMapHandler.java?rev=1635461&view=auto
==============================================================================
--- ofbiz/branches/json-integration-refactoring/framework/webapp/src/org/ofbiz/webapp/event/JSONRequestBodyMapHandler.java (added)
+++ ofbiz/branches/json-integration-refactoring/framework/webapp/src/org/ofbiz/webapp/event/JSONRequestBodyMapHandler.java Thu Oct 30 10:36:53 2014
@@ -0,0 +1,34 @@
+/*
+ * 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.ofbiz.webapp.event;
+
+import org.ofbiz.base.lang.JSON;
+import org.ofbiz.base.util.UtilGenerics;
+
+import javax.servlet.ServletRequest;
+import java.io.IOException;
+import java.util.Map;
+
+/** An implementation of <code>RequestBodyMapHandler</code> that can extract a <code>Map<String, Object></code> from the JSON data in the request body */
+public class JSONRequestBodyMapHandler implements RequestBodyMapHandler {
+
+    public Map<String, Object> extractMapFromRequestBody(ServletRequest request) throws IOException {
+        return UtilGenerics.<Map<String, Object>>cast(JSON.from(request.getInputStream()).toObject(Map.class));
+    }
+}

Propchange: ofbiz/branches/json-integration-refactoring/framework/webapp/src/org/ofbiz/webapp/event/JSONRequestBodyMapHandler.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: ofbiz/branches/json-integration-refactoring/framework/webapp/src/org/ofbiz/webapp/event/JSONRequestBodyMapHandler.java
------------------------------------------------------------------------------
    svn:keywords = Date Rev Author URL Id

Propchange: ofbiz/branches/json-integration-refactoring/framework/webapp/src/org/ofbiz/webapp/event/JSONRequestBodyMapHandler.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: ofbiz/branches/json-integration-refactoring/framework/webapp/src/org/ofbiz/webapp/event/RequestBodyMapHandler.java
URL: http://svn.apache.org/viewvc/ofbiz/branches/json-integration-refactoring/framework/webapp/src/org/ofbiz/webapp/event/RequestBodyMapHandler.java?rev=1635461&view=auto
==============================================================================
--- ofbiz/branches/json-integration-refactoring/framework/webapp/src/org/ofbiz/webapp/event/RequestBodyMapHandler.java (added)
+++ ofbiz/branches/json-integration-refactoring/framework/webapp/src/org/ofbiz/webapp/event/RequestBodyMapHandler.java Thu Oct 30 10:36:53 2014
@@ -0,0 +1,33 @@
+/*
+ * 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.ofbiz.webapp.event;
+
+import javax.servlet.ServletRequest;
+import java.io.IOException;
+import java.util.Map;
+
+/** An handler that can extract a Map (typically used as a service input map) from the data in the body of a <code>ServletRequest</code>. */
+public interface RequestBodyMapHandler {
+    /** Extracts from the data in the body of the <code>ServletRequest</code> an instance of <code>Map<String, Object></code>.
+     *
+     * @param request the request with the data in its body
+     * @return an instance of <code>Map<String, Object></code> that represents the data in the request body
+     */
+    public Map<String, Object> extractMapFromRequestBody(ServletRequest request) throws IOException;
+}

Propchange: ofbiz/branches/json-integration-refactoring/framework/webapp/src/org/ofbiz/webapp/event/RequestBodyMapHandler.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: ofbiz/branches/json-integration-refactoring/framework/webapp/src/org/ofbiz/webapp/event/RequestBodyMapHandler.java
------------------------------------------------------------------------------
    svn:keywords = Date Rev Author URL Id

Propchange: ofbiz/branches/json-integration-refactoring/framework/webapp/src/org/ofbiz/webapp/event/RequestBodyMapHandler.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: ofbiz/branches/json-integration-refactoring/framework/webapp/src/org/ofbiz/webapp/event/RequestBodyMapHandlerFactory.java
URL: http://svn.apache.org/viewvc/ofbiz/branches/json-integration-refactoring/framework/webapp/src/org/ofbiz/webapp/event/RequestBodyMapHandlerFactory.java?rev=1635461&view=auto
==============================================================================
--- ofbiz/branches/json-integration-refactoring/framework/webapp/src/org/ofbiz/webapp/event/RequestBodyMapHandlerFactory.java (added)
+++ ofbiz/branches/json-integration-refactoring/framework/webapp/src/org/ofbiz/webapp/event/RequestBodyMapHandlerFactory.java Thu Oct 30 10:36:53 2014
@@ -0,0 +1,45 @@
+/*******************************************************************************
+ * 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.ofbiz.webapp.event;
+
+import javax.servlet.ServletRequest;
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
+
+/** Factory class that provides the proper <code>RequestBodyMapHandler</code> based on the content type of the <code>ServletRequest</code> */
+public class RequestBodyMapHandlerFactory {
+    private final static Map<String, RequestBodyMapHandler> requestBodyMapHandlers = new HashMap<String, RequestBodyMapHandler>();
+    static {
+        requestBodyMapHandlers.put("application/json", new JSONRequestBodyMapHandler());
+    }
+
+    public static RequestBodyMapHandler getRequestBodyMapHandler(ServletRequest request) {
+        return requestBodyMapHandlers.get(request.getContentType());
+    }
+
+    public static Map<String, Object> extractMapFromRequestBody(ServletRequest request) throws IOException {
+        Map<String, Object> outputMap = null;
+        RequestBodyMapHandler handler = getRequestBodyMapHandler(request);
+        if (handler != null) {
+            outputMap = handler.extractMapFromRequestBody(request);
+        }
+        return outputMap;
+    }
+}

Propchange: ofbiz/branches/json-integration-refactoring/framework/webapp/src/org/ofbiz/webapp/event/RequestBodyMapHandlerFactory.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: ofbiz/branches/json-integration-refactoring/framework/webapp/src/org/ofbiz/webapp/event/RequestBodyMapHandlerFactory.java
------------------------------------------------------------------------------
    svn:keywords = Date Rev Author URL Id

Propchange: ofbiz/branches/json-integration-refactoring/framework/webapp/src/org/ofbiz/webapp/event/RequestBodyMapHandlerFactory.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: ofbiz/branches/json-integration-refactoring/framework/webapp/src/org/ofbiz/webapp/event/ServiceEventHandler.java
URL: http://svn.apache.org/viewvc/ofbiz/branches/json-integration-refactoring/framework/webapp/src/org/ofbiz/webapp/event/ServiceEventHandler.java?rev=1635461&r1=1635460&r2=1635461&view=diff
==============================================================================
--- ofbiz/branches/json-integration-refactoring/framework/webapp/src/org/ofbiz/webapp/event/ServiceEventHandler.java (original)
+++ ofbiz/branches/json-integration-refactoring/framework/webapp/src/org/ofbiz/webapp/event/ServiceEventHandler.java Thu Oct 30 10:36:53 2014
@@ -21,7 +21,9 @@ package org.ofbiz.webapp.event;
 import static org.ofbiz.base.util.UtilGenerics.checkList;
 
 import java.io.File;
+import java.io.IOException;
 import java.nio.ByteBuffer;
+import java.util.HashMap;
 import java.util.List;
 import java.util.Locale;
 import java.util.Map;
@@ -233,6 +235,14 @@ public class ServiceEventHandler impleme
 
         Map<String, Object> rawParametersMap = UtilHttp.getParameterMap(request, null, null);
         Set<String> urlOnlyParameterNames = UtilHttp.getUrlOnlyParameterMap(request).keySet();
+        Map<String, Object> requestBodyMap;
+        try {
+            requestBodyMap = RequestBodyMapHandlerFactory.extractMapFromRequestBody(request);
+        } catch (IOException ioe) {
+            Debug.logWarning(ioe, module);
+            requestBodyMap = new HashMap<String, Object>();
+        }
+        rawParametersMap.putAll(requestBodyMap);
 
         // we have a service and the model; build the context
         Map<String, Object> serviceContext = FastMap.newInstance();