[ofbiz-framework] branch trunk updated: Implemented: New service definition attribute "verb" to denote the corresponding HTTP method (OFBIZ-11328) (#214)

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

[ofbiz-framework] branch trunk updated: Implemented: New service definition attribute "verb" to denote the corresponding HTTP method (OFBIZ-11328) (#214)

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-framework.git


The following commit(s) were added to refs/heads/trunk by this push:
     new 04791d5  Implemented: New service definition attribute "verb" to denote the corresponding HTTP method (OFBIZ-11328) (#214)
04791d5 is described below

commit 04791d5821bd4e3173577b55e2607ee490e57e64
Author: girishvasmatkar <[hidden email]>
AuthorDate: Sat Jul 18 08:54:55 2020 +0530

    Implemented: New service definition attribute "verb" to denote the corresponding HTTP method (OFBIZ-11328) (#214)
   
    * Added verb as an attribute to specify the REST method OFBiz service is going to serve on
   
    * Added methods to get service IN and OUT parameters as MAP
   
    * Added TODO for supporing nested parameters
   
    * Improvement : Added Restrictions for verb attribute and documentation for the same
   
    * Fixed checkstyle issues
   
    * Changed attribute name from verb to action for better clarity
---
 applications/product/servicedef/services.xml       |  2 +-
 framework/service/dtd/services.xsd                 | 14 +++++++++
 .../org/apache/ofbiz/service/ModelService.java     | 34 ++++++++++++++++++++--
 .../apache/ofbiz/service/ModelServiceReader.java   |  1 +
 4 files changed, 47 insertions(+), 4 deletions(-)

diff --git a/applications/product/servicedef/services.xml b/applications/product/servicedef/services.xml
index ac7f217..6fe8a0c 100644
--- a/applications/product/servicedef/services.xml
+++ b/applications/product/servicedef/services.xml
@@ -213,7 +213,7 @@ under the License.
         <attribute name="statusId" type="String" mode="IN" optional="false"/>
     </service>
 
-    <service name="findProductById" engine="java"  auth="true" export="true"
+    <service name="findProductById" engine="java"  auth="true" export="true" action="get"
         location="org.apache.ofbiz.product.product.ProductServices" invoke="findProductById">
         <description>Finds productId(s) corresponding to a product reference, productId or a GoodIdentification idValue</description>
         <attribute type="String" mode="IN" name="idToFind" optional="false"/>
diff --git a/framework/service/dtd/services.xsd b/framework/service/dtd/services.xsd
index 359d4f0..5a1450b 100644
--- a/framework/service/dtd/services.xsd
+++ b/framework/service/dtd/services.xsd
@@ -66,6 +66,20 @@ under the License.
         <xs:attribute name="export" type="xs:boolean" default="false"/>
         <xs:attribute name="validate" type="xs:boolean" default="true"/>
         <xs:attribute name="default-entity-name" type="xs:string"/>
+        <xs:attribute name="action">
+         <xs:annotation>
+                <xs:documentation>
+                    Specifies the HTTP method name this service can be called using REST interface. For now only POST and GET are supported.
+                    Services that have export=true and have action attribute defined can be called using REST interface.
+                </xs:documentation>
+            </xs:annotation>
+       <xs:simpleType>
+     <xs:restriction base="xs:string">
+       <xs:enumeration value="POST"/>
+       <xs:enumeration value="GET"/>
+     </xs:restriction>
+   </xs:simpleType>
+        </xs:attribute>
         <xs:attribute name="use-transaction" type="xs:boolean" default="true">
             <xs:annotation>
                 <xs:documentation>
diff --git a/framework/service/src/main/java/org/apache/ofbiz/service/ModelService.java b/framework/service/src/main/java/org/apache/ofbiz/service/ModelService.java
index 9a9df53..444774c 100644
--- a/framework/service/src/main/java/org/apache/ofbiz/service/ModelService.java
+++ b/framework/service/src/main/java/org/apache/ofbiz/service/ModelService.java
@@ -36,6 +36,7 @@ import java.util.NoSuchElementException;
 import java.util.Set;
 import java.util.TimeZone;
 import java.util.TreeSet;
+import java.util.stream.Collectors;
 
 import javax.wsdl.Binding;
 import javax.wsdl.BindingInput;
@@ -129,6 +130,9 @@ public class ModelService extends AbstractMap<String, Object> implements Seriali
     /** The namespace of this service */
     public String nameSpace;
 
+    /** The corresponding REST verb behaviour for this service */
+    public String action;
+
     /** The package name or location of this service */
     public String location;
 
@@ -233,6 +237,7 @@ public class ModelService extends AbstractMap<String, Object> implements Seriali
         this.defaultEntityName = model.defaultEntityName;
         this.auth = model.auth;
         this.export = model.export;
+        this.action = model.action;
         this.validate = model.validate;
         this.useTransaction = model.useTransaction;
         this.requireNewTransaction = model.requireNewTransaction;
@@ -368,6 +373,7 @@ public class ModelService extends AbstractMap<String, Object> implements Seriali
         buf.append(defaultEntityName).append("::");
         buf.append(auth).append("::");
         buf.append(export).append("::");
+        buf.append(action).append("::");
         buf.append(validate).append("::");
         buf.append(useTransaction).append("::");
         buf.append(requireNewTransaction).append("::");
@@ -456,6 +462,28 @@ public class ModelService extends AbstractMap<String, Object> implements Seriali
         }
         return nameList;
     }
+    
+    /**
+     * Creates a map of service IN parameters using Name as key and Type as value.
+     * Skips internal parameters
+     * @return Map of IN parameters
+     */
+    public Map<String, String> getInParamNamesMap() {
+        // TODO : Does not yet support getting nested parameters
+        return getInModelParamList().stream().filter(param -> !param.internal)
+                .collect(Collectors.toMap(ModelParam::getName, param -> param.getType()));
+    }
+
+    /**
+     * Creates a map of service OUT parameters using Name as key and Type as value.
+     * Skips internal parameters
+     * @return Map of OUT parameters
+     */
+    public Map<String, String> getOutParamNamesMap() {
+        // TODO : Does not yet support getting nested parameters
+        return getModelParamList().stream().filter(param -> param.isOut() && !param.internal)
+                .collect(Collectors.toMap(ModelParam::getName, param -> param.getType()));
+    }
 
     // only returns number of defined parameters (not internal)
     public int getDefinedInCount() {
@@ -972,7 +1000,7 @@ public class ModelService extends AbstractMap<String, Object> implements Seriali
         for (Map.Entry<String, ? extends Object> entry: source.entrySet()) {
             String key = entry.getKey();
             if (key.startsWith(param.stringMapPrefix)) {
-                key=key.replace(param.stringMapPrefix,"");
+                key = key.replace(param.stringMapPrefix, "");
                 paramMap.put(key, entry.getValue());
             }
         }
@@ -1034,7 +1062,7 @@ public class ModelService extends AbstractMap<String, Object> implements Seriali
             for (ModelPermGroup group: this.permissionGroups) {
                 if (Debug.verboseOn()) Debug.logVerbose(" Permission : Analyse " + group.toString(), MODULE);
                 Map<String, Object> permResult = group.evalPermissions(dctx, context);
-                if (! ServiceUtil.isSuccess(permResult)) {
+                if (!ServiceUtil.isSuccess(permResult)) {
                     ServiceUtil.addErrors(permGroupErrors, null, permResult);
                 }
             }
@@ -1208,7 +1236,7 @@ public class ModelService extends AbstractMap<String, Object> implements Seriali
     public void informIfDeprecated() {
         if (this.deprecatedUseInstead != null) {
             StringBuilder informMsg = new StringBuilder("DEPRECATED: the service ")
-                    .append(name).append( " has been deprecated and replaced by ").append(deprecatedUseInstead);
+                    .append(name).append(" has been deprecated and replaced by ").append(deprecatedUseInstead);
             if (this.deprecatedSince != null) {
                 informMsg.append(", since ").append(deprecatedSince);
             }
diff --git a/framework/service/src/main/java/org/apache/ofbiz/service/ModelServiceReader.java b/framework/service/src/main/java/org/apache/ofbiz/service/ModelServiceReader.java
index b6cbf46..3e1894b 100644
--- a/framework/service/src/main/java/org/apache/ofbiz/service/ModelServiceReader.java
+++ b/framework/service/src/main/java/org/apache/ofbiz/service/ModelServiceReader.java
@@ -169,6 +169,7 @@ public class ModelServiceReader implements Serializable {
         service.semaphore = UtilXml.checkEmpty(serviceElement.getAttribute("semaphore")).intern();
         service.defaultEntityName = UtilXml.checkEmpty(serviceElement.getAttribute("default-entity-name")).intern();
         service.fromLoader = isFromURL ? readerURL.toExternalForm() : handler.getLoaderName();
+        service.action = UtilXml.checkEmpty(serviceElement.getAttribute("action")).intern();
 
         // these default to true; if anything but true, make false
         service.auth = "true".equalsIgnoreCase(serviceElement.getAttribute("auth"));