[ofbiz-plugins] branch trunk updated: Improved: Moved service inParams check in service filer and addded more improvements in its handling. Service call cab be made without passing in inParams for services that don't have any IN attribute(OFBIZ-11328)

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

[ofbiz-plugins] branch trunk updated: Improved: Moved service inParams check in service filer and addded more improvements in its handling. Service call cab be made without passing in inParams for services that don't have any IN attribute(OFBIZ-11328)

grv-2
This is an automated email from the ASF dual-hosted git repository.

grv pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/ofbiz-plugins.git


The following commit(s) were added to refs/heads/trunk by this push:
     new 1c25201  Improved: Moved service inParams check in service filer and addded more improvements in its handling. Service call cab be made without passing in inParams for services that don't have any IN attribute(OFBIZ-11328)
1c25201 is described below

commit 1c25201d7e17263b4f2f960dfb2a5ab76c912207
Author: Girish Vasmatkar <[hidden email]>
AuthorDate: Sat Oct 3 08:12:43 2020 +0530

    Improved: Moved service inParams check in service filer and addded more improvements in its handling. Service call cab be made without passing in inParams for services that don't have any IN attribute(OFBIZ-11328)
---
 .../org/apache/ofbiz/ws/rs/ServiceRequestFilter.java   | 15 +++++++++++++--
 .../apache/ofbiz/ws/rs/openapi/OFBizOpenApiReader.java |  3 ++-
 .../ofbiz/ws/rs/resources/OFBizServiceResource.java    |  3 ---
 .../rs/spi/impl/JsonifiedParamConverterProvider.java   | 18 +++++++++++++++++-
 4 files changed, 32 insertions(+), 7 deletions(-)

diff --git a/ofbiz-rest-impl/src/main/java/org/apache/ofbiz/ws/rs/ServiceRequestFilter.java b/ofbiz-rest-impl/src/main/java/org/apache/ofbiz/ws/rs/ServiceRequestFilter.java
index db6d1eb..444b8a0 100644
--- a/ofbiz-rest-impl/src/main/java/org/apache/ofbiz/ws/rs/ServiceRequestFilter.java
+++ b/ofbiz-rest-impl/src/main/java/org/apache/ofbiz/ws/rs/ServiceRequestFilter.java
@@ -23,6 +23,8 @@ import java.io.IOException;
 import javax.annotation.Priority;
 import javax.servlet.ServletContext;
 import javax.servlet.http.HttpServletRequest;
+import javax.ws.rs.BadRequestException;
+import javax.ws.rs.HttpMethod;
 import javax.ws.rs.NotFoundException;
 import javax.ws.rs.Priorities;
 import javax.ws.rs.container.ContainerRequestContext;
@@ -47,6 +49,8 @@ public class ServiceRequestFilter implements ContainerRequestFilter {
 
     private static final String MODULE = ServiceRequestFilter.class.getName();
 
+    private static final String SVC_IN_PARAMS = "inParams";
+
     @Context
     private UriInfo uriInfo;
 
@@ -68,6 +72,7 @@ public class ServiceRequestFilter implements ContainerRequestFilter {
         Debug.logInfo("Service request is going to get validated!", MODULE);
         String service = (String) RestApiUtil.extractParams(uriInfo.getPathParameters()).get("serviceName");
         String method = requestContext.getMethod();
+        String action = null;
         if (UtilValidate.isNotEmpty(service)) {
             ModelService mdService = null;
             try {
@@ -84,13 +89,19 @@ public class ServiceRequestFilter implements ContainerRequestFilter {
                 throw new NotFoundException("Service '" + service + "' is not exportable.");
             }
 
-            if (mdService != null && UtilValidate.isEmpty(mdService.getAction())) {
+            action = mdService.getAction();
+            if (mdService != null && UtilValidate.isEmpty(action)) {
                 throw new NotFoundException("Service '" + service + "' does not have HTTP action defined.");
             }
 
-            if (!mdService.getAction().equalsIgnoreCase(method)) {
+            if (!action.equalsIgnoreCase(method)) {
                 throw new MethodNotAllowedException("HTTP " + method + " is not allowed on service '" + service + "'");
             }
+
+            if (action.equalsIgnoreCase(HttpMethod.GET) && UtilValidate.isNotEmpty(mdService.getInParamNamesMap())
+                    && UtilValidate.isEmpty(httpRequest.getParameter(SVC_IN_PARAMS))) {
+                throw new BadRequestException("Missing Parameter: 'inParams'");
+            }
             // If everything looks good, set the 'requestForService' property in the
             // context. Indicates which service this request is for.
             requestContext.setProperty("requestForService", service);
diff --git a/ofbiz-rest-impl/src/main/java/org/apache/ofbiz/ws/rs/openapi/OFBizOpenApiReader.java b/ofbiz-rest-impl/src/main/java/org/apache/ofbiz/ws/rs/openapi/OFBizOpenApiReader.java
index d86b22d..bec8286 100644
--- a/ofbiz-rest-impl/src/main/java/org/apache/ofbiz/ws/rs/openapi/OFBizOpenApiReader.java
+++ b/ofbiz-rest-impl/src/main/java/org/apache/ofbiz/ws/rs/openapi/OFBizOpenApiReader.java
@@ -174,7 +174,8 @@ public final class OFBizOpenApiReader extends Reader implements OpenApiReader {
                         .operationId(service.getName()).deprecated(false).addSecurityItem(security);
                 PathItem pathItemObject = new PathItem();
                 if (service.getAction().equalsIgnoreCase(HttpMethod.GET)) {
-                    final QueryParameter serviceInParam = (QueryParameter) new QueryParameter().required(true)
+                    final QueryParameter serviceInParam = (QueryParameter) new QueryParameter()
+                            .required(UtilValidate.isNotEmpty(service.getInParamNamesMap()) ? true: false)
                             .description("Service In Parameters in JSON").name("inParams");
                     Schema<?> refSchema = new Schema<>();
                     refSchema.$ref("#/components/schemas/" + "api.request." + service.getName());
diff --git a/ofbiz-rest-impl/src/main/java/org/apache/ofbiz/ws/rs/resources/OFBizServiceResource.java b/ofbiz-rest-impl/src/main/java/org/apache/ofbiz/ws/rs/resources/OFBizServiceResource.java
index 61cb6af..b688666 100644
--- a/ofbiz-rest-impl/src/main/java/org/apache/ofbiz/ws/rs/resources/OFBizServiceResource.java
+++ b/ofbiz-rest-impl/src/main/java/org/apache/ofbiz/ws/rs/resources/OFBizServiceResource.java
@@ -113,9 +113,6 @@ public class OFBizServiceResource extends OFBizResource {
     @Secured
     public Response doGet(@QueryParam(value = "inParams") ApiServiceRequest serviceRequest,
             @PathParam(value = "serviceName") String serviceName) throws IOException, GenericServiceException {
-        if (UtilValidate.isEmpty(serviceRequest) || UtilValidate.isEmpty(serviceRequest.getInParams())) {
-            throw new BadRequestException("Missing Parameter: 'inParams'");
-        }
         ServiceRequestProcessor processor = new ServiceRequestProcessor();
         return processor.process(UtilMisc.toMap("serviceName", serviceName, "httpVerb", HttpMethod.GET, "requestMap",
                 serviceRequest.getInParams(), "dispatcher", getDispatcher(), "request", httpRequest));
diff --git a/ofbiz-rest-impl/src/main/java/org/apache/ofbiz/ws/rs/spi/impl/JsonifiedParamConverterProvider.java b/ofbiz-rest-impl/src/main/java/org/apache/ofbiz/ws/rs/spi/impl/JsonifiedParamConverterProvider.java
index 9e46178..360fd8d 100644
--- a/ofbiz-rest-impl/src/main/java/org/apache/ofbiz/ws/rs/spi/impl/JsonifiedParamConverterProvider.java
+++ b/ofbiz-rest-impl/src/main/java/org/apache/ofbiz/ws/rs/spi/impl/JsonifiedParamConverterProvider.java
@@ -20,12 +20,18 @@ package org.apache.ofbiz.ws.rs.spi.impl;
 
 import java.lang.annotation.Annotation;
 import java.lang.reflect.Type;
+import java.util.HashMap;
 import java.util.Map;
 
+import javax.servlet.http.HttpServletRequest;
+import javax.ws.rs.BadRequestException;
+import javax.ws.rs.core.Context;
 import javax.ws.rs.ext.ParamConverter;
 import javax.ws.rs.ext.ParamConverterProvider;
 import javax.ws.rs.ext.Provider;
 
+import org.apache.ofbiz.base.util.Debug;
+import org.apache.ofbiz.base.util.UtilValidate;
 import org.apache.ofbiz.ws.rs.ApiServiceRequest;
 
 import com.fasterxml.jackson.core.JsonProcessingException;
@@ -34,6 +40,12 @@ import com.fasterxml.jackson.databind.ObjectMapper;
 
 @Provider
 public class JsonifiedParamConverterProvider implements ParamConverterProvider {
+
+    private static final String MODULE = JsonifiedParamConverterProvider.class.getName();
+
+    @Context
+    private HttpServletRequest httpRequest;
+
     private static final ObjectMapper MAPPER = new ObjectMapper();
 
     private static ObjectMapper getMapper() {
@@ -55,12 +67,16 @@ public class JsonifiedParamConverterProvider implements ParamConverterProvider {
                 @SuppressWarnings("unchecked")
                 @Override
                 public T fromString(String value) {
+                    if (UtilValidate.isEmpty(value)) {
+                        return (T) new ApiServiceRequest(new HashMap<String, Object>());
+                    }
                     Map<String, Object> map = null;
                     try {
                         map = getMapper().readValue(value, new TypeReference<Map<String, Object>>() {
                         });
                     } catch (JsonProcessingException e) {
-                        e.printStackTrace();
+                        Debug.logError(e.getMessage(), MODULE);
+                        throw new BadRequestException("Error parsing JSON, malformed JSON.");
                     }
                     return (T) new ApiServiceRequest(map);
                 }