You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ofbiz.apache.org by ja...@apache.org on 2015/03/09 10:26:19 UTC

svn commit: r1665162 - in /ofbiz/trunk/framework: minilang/src/org/ofbiz/minilang/method/MethodContext.java webapp/src/org/ofbiz/webapp/control/ContextFilter.java webapp/src/org/ofbiz/webapp/event/ServiceEventHandler.java

Author: jacopoc
Date: Mon Mar  9 09:26:18 2015
New Revision: 1665162

URL: http://svn.apache.org/r1665162
Log:
Fix for issue happening when a service event was executed using JSON (i.e. parameters in the request body) after a getParameter was called (by other framework code) to fetch the parameter from the body; in that case an io error was thrown because the reuest body stream can be read only once.
This fix move the code that reads the JSON parameters from the body into the ContextFilter: the parameters are then stored as attributes that can be later used by events and other artifacts (service events, simple events, scripts events support this and Java events if they use UtilHttp.getCombinedMap(request) to get the map of input parameters. 

Modified:
    ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/MethodContext.java
    ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/control/ContextFilter.java
    ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/event/ServiceEventHandler.java

Modified: ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/MethodContext.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/MethodContext.java?rev=1665162&r1=1665161&r2=1665162&view=diff
==============================================================================
--- ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/MethodContext.java (original)
+++ ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/MethodContext.java Mon Mar  9 09:26:18 2015
@@ -82,7 +82,7 @@ public final class MethodContext {
 
     public MethodContext(HttpServletRequest request, HttpServletResponse response, ClassLoader loader) {
         this.methodType = MethodContext.EVENT;
-        this.parameters = UtilHttp.getParameterMap(request);
+        this.parameters = UtilHttp.getCombinedMap(request);
         this.loader = loader;
         this.request = request;
         this.response = response;

Modified: ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/control/ContextFilter.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/control/ContextFilter.java?rev=1665162&r1=1665161&r2=1665162&view=diff
==============================================================================
--- ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/control/ContextFilter.java (original)
+++ ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/control/ContextFilter.java Mon Mar  9 09:26:18 2015
@@ -24,6 +24,7 @@ import java.io.IOException;
 import java.util.Enumeration;
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
 
 import javax.servlet.Filter;
 import javax.servlet.FilterChain;
@@ -54,6 +55,7 @@ import org.ofbiz.security.SecurityConfig
 import org.ofbiz.security.SecurityFactory;
 import org.ofbiz.service.LocalDispatcher;
 import org.ofbiz.service.ServiceContainer;
+import org.ofbiz.webapp.event.RequestBodyMapHandlerFactory;
 import org.ofbiz.webapp.website.WebSiteWorker;
 
 /**
@@ -317,6 +319,20 @@ public class ContextFilter implements Fi
             }
         }
 
+        // read the body (for JSON requests) and set the parameters as attributes:
+        Map<String, Object> requestBodyMap = null;
+        try {
+            requestBodyMap = RequestBodyMapHandlerFactory.extractMapFromRequestBody(request);
+        } catch (IOException ioe) {
+            Debug.logWarning(ioe, module);
+        }
+        if (requestBodyMap != null) {
+            Set<String> parameterNames = requestBodyMap.keySet();
+            for (String parameterName: parameterNames) {
+                httpRequest.setAttribute(parameterName, requestBodyMap.get(parameterName));
+            }
+        }
+
         // we're done checking; continue on
         chain.doFilter(httpRequest, httpResponse);
     }

Modified: ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/event/ServiceEventHandler.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/event/ServiceEventHandler.java?rev=1665162&r1=1665161&r2=1665162&view=diff
==============================================================================
--- ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/event/ServiceEventHandler.java (original)
+++ ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/event/ServiceEventHandler.java Mon Mar  9 09:26:18 2015
@@ -21,7 +21,6 @@ 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.LinkedList;
@@ -231,17 +230,8 @@ public class ServiceEventHandler impleme
         // store the multi-part map as an attribute so we can access the parameters
         request.setAttribute("multiPartMap", multiPartMap);
 
-        Map<String, Object> rawParametersMap = UtilHttp.getParameterMap(request, null, null);
+        Map<String, Object> rawParametersMap = UtilHttp.getCombinedMap(request);
         Set<String> urlOnlyParameterNames = UtilHttp.getUrlOnlyParameterMap(request).keySet();
-        Map<String, Object> requestBodyMap = null;
-        try {
-            requestBodyMap = RequestBodyMapHandlerFactory.extractMapFromRequestBody(request);
-        } catch (IOException ioe) {
-            Debug.logWarning(ioe, module);
-        }
-        if (requestBodyMap != null) {
-            rawParametersMap.putAll(requestBodyMap);
-        }
 
         // we have a service and the model; build the context
         Map<String, Object> serviceContext = new HashMap<String, Object>();



Re: svn commit: r1665162 - in /ofbiz/trunk/framework: minilang/src/org/ofbiz/minilang/method/MethodContext.java webapp/src/org/ofbiz/webapp/control/ContextFilter.java webapp/src/org/ofbiz/webapp/event/ServiceEventHandler.java

Posted by Jacopo Cappellato <ja...@hotwaxsystems.com>.
Hi Pierre, all,

On Mar 9, 2015, at 11:44 AM, Pierre Smits <pi...@gmail.com> wrote:

> Hi Jacopo,
> 
> This is very interesting and also welcoming, as I am in the middle of
> something related (getting a rest response in json that needs to be
> processed).
> 
> How would I utilise this in a groovy script?

from a groovy event you can access the parameter (even if it comes as JSON from the request body) using the usual parameters.myparameter syntax. Same for Minilang (i.e. simple events).
For Java events it is slightly trickier because the signature of Java events requires the request object; however you can easily get a map containing all the parameters using the method:

Map inputMap = UtilHttp.getCombinedMap(request);

In my opinion we should make this a best practice for Java events and possibly modify existing Java events to follow this.

I hope it helps, regards,

Jacopo

> 
> Best regards,
> 
> Pierre Smits
> 
> *ORRTIZ.COM <http://www.orrtiz.com>*
> Services & Solutions for Cloud-
> Based Manufacturing, Professional
> Services and Retail & Trade
> http://www.orrtiz.com
> 
> On Mon, Mar 9, 2015 at 10:26 AM, <ja...@apache.org> wrote:
> 
>> Author: jacopoc
>> Date: Mon Mar  9 09:26:18 2015
>> New Revision: 1665162
>> 
>> URL: http://svn.apache.org/r1665162
>> Log:
>> Fix for issue happening when a service event was executed using JSON (i.e.
>> parameters in the request body) after a getParameter was called (by other
>> framework code) to fetch the parameter from the body; in that case an io
>> error was thrown because the reuest body stream can be read only once.
>> This fix move the code that reads the JSON parameters from the body into
>> the ContextFilter: the parameters are then stored as attributes that can be
>> later used by events and other artifacts (service events, simple events,
>> scripts events support this and Java events if they use
>> UtilHttp.getCombinedMap(request) to get the map of input parameters.
>> 
>> Modified:
>> 
>> ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/MethodContext.java
>> 
>> ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/control/ContextFilter.java
>> 
>> ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/event/ServiceEventHandler.java
>> 
>> Modified:
>> ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/MethodContext.java
>> URL:
>> http://svn.apache.org/viewvc/ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/MethodContext.java?rev=1665162&r1=1665161&r2=1665162&view=diff
>> 
>> ==============================================================================
>> ---
>> ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/MethodContext.java
>> (original)
>> +++
>> ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/MethodContext.java
>> Mon Mar  9 09:26:18 2015
>> @@ -82,7 +82,7 @@ public final class MethodContext {
>> 
>>     public MethodContext(HttpServletRequest request, HttpServletResponse
>> response, ClassLoader loader) {
>>         this.methodType = MethodContext.EVENT;
>> -        this.parameters = UtilHttp.getParameterMap(request);
>> +        this.parameters = UtilHttp.getCombinedMap(request);
>>         this.loader = loader;
>>         this.request = request;
>>         this.response = response;
>> 
>> Modified:
>> ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/control/ContextFilter.java
>> URL:
>> http://svn.apache.org/viewvc/ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/control/ContextFilter.java?rev=1665162&r1=1665161&r2=1665162&view=diff
>> 
>> ==============================================================================
>> ---
>> ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/control/ContextFilter.java
>> (original)
>> +++
>> ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/control/ContextFilter.java
>> Mon Mar  9 09:26:18 2015
>> @@ -24,6 +24,7 @@ import java.io.IOException;
>> import java.util.Enumeration;
>> import java.util.List;
>> import java.util.Map;
>> +import java.util.Set;
>> 
>> import javax.servlet.Filter;
>> import javax.servlet.FilterChain;
>> @@ -54,6 +55,7 @@ import org.ofbiz.security.SecurityConfig
>> import org.ofbiz.security.SecurityFactory;
>> import org.ofbiz.service.LocalDispatcher;
>> import org.ofbiz.service.ServiceContainer;
>> +import org.ofbiz.webapp.event.RequestBodyMapHandlerFactory;
>> import org.ofbiz.webapp.website.WebSiteWorker;
>> 
>> /**
>> @@ -317,6 +319,20 @@ public class ContextFilter implements Fi
>>             }
>>         }
>> 
>> +        // read the body (for JSON requests) and set the parameters as
>> attributes:
>> +        Map<String, Object> requestBodyMap = null;
>> +        try {
>> +            requestBodyMap =
>> RequestBodyMapHandlerFactory.extractMapFromRequestBody(request);
>> +        } catch (IOException ioe) {
>> +            Debug.logWarning(ioe, module);
>> +        }
>> +        if (requestBodyMap != null) {
>> +            Set<String> parameterNames = requestBodyMap.keySet();
>> +            for (String parameterName: parameterNames) {
>> +                httpRequest.setAttribute(parameterName,
>> requestBodyMap.get(parameterName));
>> +            }
>> +        }
>> +
>>         // we're done checking; continue on
>>         chain.doFilter(httpRequest, httpResponse);
>>     }
>> 
>> Modified:
>> ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/event/ServiceEventHandler.java
>> URL:
>> http://svn.apache.org/viewvc/ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/event/ServiceEventHandler.java?rev=1665162&r1=1665161&r2=1665162&view=diff
>> 
>> ==============================================================================
>> ---
>> ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/event/ServiceEventHandler.java
>> (original)
>> +++
>> ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/event/ServiceEventHandler.java
>> Mon Mar  9 09:26:18 2015
>> @@ -21,7 +21,6 @@ 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.LinkedList;
>> @@ -231,17 +230,8 @@ public class ServiceEventHandler impleme
>>         // store the multi-part map as an attribute so we can access the
>> parameters
>>         request.setAttribute("multiPartMap", multiPartMap);
>> 
>> -        Map<String, Object> rawParametersMap =
>> UtilHttp.getParameterMap(request, null, null);
>> +        Map<String, Object> rawParametersMap =
>> UtilHttp.getCombinedMap(request);
>>         Set<String> urlOnlyParameterNames =
>> UtilHttp.getUrlOnlyParameterMap(request).keySet();
>> -        Map<String, Object> requestBodyMap = null;
>> -        try {
>> -            requestBodyMap =
>> RequestBodyMapHandlerFactory.extractMapFromRequestBody(request);
>> -        } catch (IOException ioe) {
>> -            Debug.logWarning(ioe, module);
>> -        }
>> -        if (requestBodyMap != null) {
>> -            rawParametersMap.putAll(requestBodyMap);
>> -        }
>> 
>>         // we have a service and the model; build the context
>>         Map<String, Object> serviceContext = new HashMap<String,
>> Object>();
>> 
>> 
>> 


Re: svn commit: r1665162 - in /ofbiz/trunk/framework: minilang/src/org/ofbiz/minilang/method/MethodContext.java webapp/src/org/ofbiz/webapp/control/ContextFilter.java webapp/src/org/ofbiz/webapp/event/ServiceEventHandler.java

Posted by Pierre Smits <pi...@gmail.com>.
Hi Jacopo,

This is very interesting and also welcoming, as I am in the middle of
something related (getting a rest response in json that needs to be
processed).

How would I utilise this in a groovy script?

Best regards,

Pierre Smits

*ORRTIZ.COM <http://www.orrtiz.com>*
Services & Solutions for Cloud-
Based Manufacturing, Professional
Services and Retail & Trade
http://www.orrtiz.com

On Mon, Mar 9, 2015 at 10:26 AM, <ja...@apache.org> wrote:

> Author: jacopoc
> Date: Mon Mar  9 09:26:18 2015
> New Revision: 1665162
>
> URL: http://svn.apache.org/r1665162
> Log:
> Fix for issue happening when a service event was executed using JSON (i.e.
> parameters in the request body) after a getParameter was called (by other
> framework code) to fetch the parameter from the body; in that case an io
> error was thrown because the reuest body stream can be read only once.
> This fix move the code that reads the JSON parameters from the body into
> the ContextFilter: the parameters are then stored as attributes that can be
> later used by events and other artifacts (service events, simple events,
> scripts events support this and Java events if they use
> UtilHttp.getCombinedMap(request) to get the map of input parameters.
>
> Modified:
>
> ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/MethodContext.java
>
> ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/control/ContextFilter.java
>
> ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/event/ServiceEventHandler.java
>
> Modified:
> ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/MethodContext.java
> URL:
> http://svn.apache.org/viewvc/ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/MethodContext.java?rev=1665162&r1=1665161&r2=1665162&view=diff
>
> ==============================================================================
> ---
> ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/MethodContext.java
> (original)
> +++
> ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/MethodContext.java
> Mon Mar  9 09:26:18 2015
> @@ -82,7 +82,7 @@ public final class MethodContext {
>
>      public MethodContext(HttpServletRequest request, HttpServletResponse
> response, ClassLoader loader) {
>          this.methodType = MethodContext.EVENT;
> -        this.parameters = UtilHttp.getParameterMap(request);
> +        this.parameters = UtilHttp.getCombinedMap(request);
>          this.loader = loader;
>          this.request = request;
>          this.response = response;
>
> Modified:
> ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/control/ContextFilter.java
> URL:
> http://svn.apache.org/viewvc/ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/control/ContextFilter.java?rev=1665162&r1=1665161&r2=1665162&view=diff
>
> ==============================================================================
> ---
> ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/control/ContextFilter.java
> (original)
> +++
> ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/control/ContextFilter.java
> Mon Mar  9 09:26:18 2015
> @@ -24,6 +24,7 @@ import java.io.IOException;
>  import java.util.Enumeration;
>  import java.util.List;
>  import java.util.Map;
> +import java.util.Set;
>
>  import javax.servlet.Filter;
>  import javax.servlet.FilterChain;
> @@ -54,6 +55,7 @@ import org.ofbiz.security.SecurityConfig
>  import org.ofbiz.security.SecurityFactory;
>  import org.ofbiz.service.LocalDispatcher;
>  import org.ofbiz.service.ServiceContainer;
> +import org.ofbiz.webapp.event.RequestBodyMapHandlerFactory;
>  import org.ofbiz.webapp.website.WebSiteWorker;
>
>  /**
> @@ -317,6 +319,20 @@ public class ContextFilter implements Fi
>              }
>          }
>
> +        // read the body (for JSON requests) and set the parameters as
> attributes:
> +        Map<String, Object> requestBodyMap = null;
> +        try {
> +            requestBodyMap =
> RequestBodyMapHandlerFactory.extractMapFromRequestBody(request);
> +        } catch (IOException ioe) {
> +            Debug.logWarning(ioe, module);
> +        }
> +        if (requestBodyMap != null) {
> +            Set<String> parameterNames = requestBodyMap.keySet();
> +            for (String parameterName: parameterNames) {
> +                httpRequest.setAttribute(parameterName,
> requestBodyMap.get(parameterName));
> +            }
> +        }
> +
>          // we're done checking; continue on
>          chain.doFilter(httpRequest, httpResponse);
>      }
>
> Modified:
> ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/event/ServiceEventHandler.java
> URL:
> http://svn.apache.org/viewvc/ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/event/ServiceEventHandler.java?rev=1665162&r1=1665161&r2=1665162&view=diff
>
> ==============================================================================
> ---
> ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/event/ServiceEventHandler.java
> (original)
> +++
> ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/event/ServiceEventHandler.java
> Mon Mar  9 09:26:18 2015
> @@ -21,7 +21,6 @@ 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.LinkedList;
> @@ -231,17 +230,8 @@ public class ServiceEventHandler impleme
>          // store the multi-part map as an attribute so we can access the
> parameters
>          request.setAttribute("multiPartMap", multiPartMap);
>
> -        Map<String, Object> rawParametersMap =
> UtilHttp.getParameterMap(request, null, null);
> +        Map<String, Object> rawParametersMap =
> UtilHttp.getCombinedMap(request);
>          Set<String> urlOnlyParameterNames =
> UtilHttp.getUrlOnlyParameterMap(request).keySet();
> -        Map<String, Object> requestBodyMap = null;
> -        try {
> -            requestBodyMap =
> RequestBodyMapHandlerFactory.extractMapFromRequestBody(request);
> -        } catch (IOException ioe) {
> -            Debug.logWarning(ioe, module);
> -        }
> -        if (requestBodyMap != null) {
> -            rawParametersMap.putAll(requestBodyMap);
> -        }
>
>          // we have a service and the model; build the context
>          Map<String, Object> serviceContext = new HashMap<String,
> Object>();
>
>
>