[ofbiz-plugins] branch trunk updated: Improved: Added support for publishing/unpublishing a resource and conditional auth support 2.Implemented: Added schema file for REST XML DSL

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: Added support for publishing/unpublishing a resource and conditional auth support 2.Implemented: Added schema file for REST XML DSL

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 e31fd64  Improved: Added support for publishing/unpublishing a resource and conditional auth support 2.Implemented: Added schema file for REST XML DSL
e31fd64 is described below

commit e31fd640fd1a341caa843dcb2b85cc5c0c2760a5
Author: Girish Vasmatkar <[hidden email]>
AuthorDate: Thu Sep 17 17:39:07 2020 +0530

    Improved: Added support for publishing/unpublishing a resource and conditional auth support
    2.Implemented: Added schema file for REST XML DSL
---
 ofbiz-rest-impl/dtd/rest-api.xsd                   | 87 ++++++++++++++++++++++
 .../apache/ofbiz/ws/rs/core/OFBizApiConfig.java    | 41 ++++++----
 .../apache/ofbiz/ws/rs/model/ModelApiReader.java   |  6 +-
 .../apache/ofbiz/ws/rs/model/ModelOperation.java   | 24 ++++++
 .../apache/ofbiz/ws/rs/model/ModelResource.java    | 54 ++++++++++----
 .../ws/rs/resources/OFBizServiceResource.java      | 40 +++++-----
 6 files changed, 199 insertions(+), 53 deletions(-)

diff --git a/ofbiz-rest-impl/dtd/rest-api.xsd b/ofbiz-rest-impl/dtd/rest-api.xsd
new file mode 100644
index 0000000..972dfcf
--- /dev/null
+++ b/ofbiz-rest-impl/dtd/rest-api.xsd
@@ -0,0 +1,87 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+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.
+-->
+<!-- NOTE: files using this schema are found in the service directory in a component when named *.rest.xml -->
+<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="unqualified">
+    <xs:element name="api">
+        <xs:complexType>
+            <xs:sequence>
+                <xs:element minOccurs="0" maxOccurs="unbounded" ref="resource"/>
+            </xs:sequence>
+            <xs:attribute name="name" type="xs:string" use="required"/>
+            <xs:attribute name="displayName" type="xs:string"/>
+            <xs:attribute name="description" type="xs:string"/>
+        </xs:complexType>
+    </xs:element>
+    <xs:element name="resource">
+        <xs:complexType>
+            <xs:sequence>
+                <xs:element minOccurs="0" maxOccurs="unbounded" ref="operation"/>
+            </xs:sequence>
+            <xs:attribute name="name" type="xs:string" use="required"/>
+            <xs:attribute name="path" type="xs:string" use="required"/>
+            <xs:attribute name="displayName" type="xs:string"/>
+            <xs:attribute name="description" type="xs:string"/>
+            <xs:attribute name="publish" type="xs:boolean" default="true"/>
+            <xs:attribute name="auth" type="xs:boolean" default="true"/>
+        </xs:complexType>
+    </xs:element>
+    <xs:element name="operation">
+        <xs:complexType>
+            <xs:sequence>
+                <xs:element minOccurs="1" maxOccurs="1" ref="service"/>
+            </xs:sequence>
+            <xs:attribute name="verb" use="required">
+                <xs:simpleType>
+                    <xs:restriction base="xs:token">
+                        <xs:enumeration value="get"/>
+                        <xs:enumeration value="patch"/>
+                        <xs:enumeration value="put"/>
+                        <xs:enumeration value="post"/>
+                        <xs:enumeration value="delete"/>
+                    </xs:restriction>
+                </xs:simpleType>
+            </xs:attribute>
+            <xs:attribute name="produces" use="optional">
+                <xs:simpleType>
+                    <xs:restriction base="xs:token">
+                        <xs:enumeration value="application/xml"/>
+                        <xs:enumeration value="application/json"/>
+                    </xs:restriction>
+                </xs:simpleType>
+            </xs:attribute>
+            <xs:attribute name="consumes" use="optional">
+                <xs:simpleType>
+                    <xs:restriction base="xs:token">
+                        <xs:enumeration value="application/xml"/>
+                        <xs:enumeration value="application/json"/>
+                    </xs:restriction>
+                </xs:simpleType>
+            </xs:attribute>
+            <xs:attribute name="path" type="xs:string" use="optional"/>
+            <xs:attribute name="description" type="xs:string"/>
+            <xs:attribute name="auth" type="xs:boolean" default="true"/>
+        </xs:complexType>
+    </xs:element>
+    <xs:element name="service">
+        <xs:complexType>
+            <xs:attribute name="name" type="xs:string" use="required"/>
+        </xs:complexType>
+    </xs:element>
+</xs:schema>
diff --git a/ofbiz-rest-impl/src/main/java/org/apache/ofbiz/ws/rs/core/OFBizApiConfig.java b/ofbiz-rest-impl/src/main/java/org/apache/ofbiz/ws/rs/core/OFBizApiConfig.java
index 5a168fe..b9d02cb 100644
--- a/ofbiz-rest-impl/src/main/java/org/apache/ofbiz/ws/rs/core/OFBizApiConfig.java
+++ b/ofbiz-rest-impl/src/main/java/org/apache/ofbiz/ws/rs/core/OFBizApiConfig.java
@@ -48,11 +48,13 @@ import org.glassfish.jersey.server.model.ResourceMethod;
 public class OFBizApiConfig extends ResourceConfig {
     private static final String MODULE = OFBizApiConfig.class.getName();
     private static final Map<String, ModelApi> MICRO_APIS = new HashMap<>();
+
     public OFBizApiConfig() {
         packages("org.apache.ofbiz.ws.rs.resources");
         packages("org.apache.ofbiz.ws.rs.security.auth");
         packages("org.apache.ofbiz.ws.rs.spi.impl");
-        //packages("io.swagger.v3.jaxrs2.integration.resources"); //commenting it out to generate customized OpenApi Spec
+        // packages("io.swagger.v3.jaxrs2.integration.resources"); //commenting it out
+        // to generate customized OpenApi Spec
         register(JacksonFeature.class);
         register(MultiPartFeature.class);
         if (Debug.verboseOn()) {
@@ -99,22 +101,31 @@ public class OFBizApiConfig extends ResourceConfig {
             Debug.logInfo("Registring Resource Definitions from API - " + k, MODULE);
             List<ModelResource> resources = v.getResources();
             resources.forEach(modelResource -> {
-                Resource.Builder resourceBuilder = Resource.builder(modelResource.getPath()).name(modelResource.getName());
-                for (ModelOperation op : modelResource.getOperations()) {
-                    if (UtilValidate.isEmpty(op.getPath())) { // Add the method to the parent resource
-                        ResourceMethod.Builder methodBuilder = resourceBuilder.addMethod(op.getVerb().toUpperCase());
-                        methodBuilder.produces(MediaType.APPLICATION_JSON).nameBindings(Secured.class);
-                        String serviceName = op.getService();
-                        methodBuilder.handledBy(new ServiceRequestHandler(serviceName));
-                    } else {
-                        Resource.Builder childResourceBuilder = resourceBuilder.addChildResource(op.getPath());
-                        ResourceMethod.Builder childResourceMethodBuilder = childResourceBuilder.addMethod(op.getVerb().toUpperCase());
-                        childResourceMethodBuilder.produces(MediaType.APPLICATION_JSON).nameBindings(Secured.class);
-                        String serviceName = op.getService();
-                        childResourceMethodBuilder.handledBy(new ServiceRequestHandler(serviceName));
+                if (modelResource.isPublish()) {
+                    Resource.Builder resourceBuilder = Resource.builder(modelResource.getPath())
+                            .name(modelResource.getName());
+                    for (ModelOperation op : modelResource.getOperations()) {
+                        if (UtilValidate.isEmpty(op.getPath())) { // Add the method to the parent resource
+                            ResourceMethod.Builder methodBuilder = resourceBuilder.addMethod(op.getVerb().toUpperCase());
+                            methodBuilder.produces(MediaType.APPLICATION_JSON);
+                            if (op.isAuth()) {
+                                methodBuilder.nameBindings(Secured.class);
+                            }
+                            String serviceName = op.getService();
+                            methodBuilder.handledBy(new ServiceRequestHandler(serviceName));
+                        } else {
+                            Resource.Builder childResourceBuilder = resourceBuilder.addChildResource(op.getPath());
+                            ResourceMethod.Builder childResourceMethodBuilder = childResourceBuilder.addMethod(op.getVerb().toUpperCase());
+                            childResourceMethodBuilder.produces(MediaType.APPLICATION_JSON);
+                            if (op.isAuth()) {
+                                childResourceMethodBuilder.nameBindings(Secured.class);
+                            }
+                            String serviceName = op.getService();
+                            childResourceMethodBuilder.handledBy(new ServiceRequestHandler(serviceName));
+                        }
                     }
+                    registerResources(resourceBuilder.build());
                 }
-                registerResources(resourceBuilder.build());
             });
         });
     }
diff --git a/ofbiz-rest-impl/src/main/java/org/apache/ofbiz/ws/rs/model/ModelApiReader.java b/ofbiz-rest-impl/src/main/java/org/apache/ofbiz/ws/rs/model/ModelApiReader.java
index 208e3af..0bfe9da 100644
--- a/ofbiz-rest-impl/src/main/java/org/apache/ofbiz/ws/rs/model/ModelApiReader.java
+++ b/ofbiz-rest-impl/src/main/java/org/apache/ofbiz/ws/rs/model/ModelApiReader.java
@@ -52,7 +52,8 @@ public final class ModelApiReader {
                     .description(UtilXml.checkEmpty(resourceEle.getAttribute("description")).intern())
                     .displayName(UtilXml.checkEmpty(resourceEle.getAttribute("displayName")).intern())
                     .path(UtilXml.checkEmpty(resourceEle.getAttribute("path")).intern())
-                    .enabled(Boolean.parseBoolean(UtilXml.checkEmpty(resourceEle.getAttribute("name")).intern()));
+                    .publish(Boolean.parseBoolean(UtilXml.checkEmpty(resourceEle.getAttribute("publish")).intern()))
+                    .auth(Boolean.parseBoolean(UtilXml.checkEmpty(resourceEle.getAttribute("auth")).intern()));
             createOperations(resourceEle, resource);
             Debug.logInfo(resource.toString(), MODULE);
             api.addResource(resource);
@@ -69,7 +70,8 @@ public final class ModelApiReader {
                     .verb(UtilXml.checkEmpty(operationEle.getAttribute("verb")).intern()).service(serviceName)
                     .produces(UtilXml.checkEmpty(operationEle.getAttribute("produces")).intern())
                     .consumes(UtilXml.checkEmpty(operationEle.getAttribute("consumes")).intern())
-                    .description(UtilXml.checkEmpty(operationEle.getAttribute("description")).intern());
+                    .description(UtilXml.checkEmpty(operationEle.getAttribute("description")).intern())
+                    .auth(Boolean.parseBoolean(UtilXml.checkEmpty(operationEle.getAttribute("auth")).intern()));
             resource.addOperation(op);
         }
     }
diff --git a/ofbiz-rest-impl/src/main/java/org/apache/ofbiz/ws/rs/model/ModelOperation.java b/ofbiz-rest-impl/src/main/java/org/apache/ofbiz/ws/rs/model/ModelOperation.java
index 65ea5ca..b8a32a0 100644
--- a/ofbiz-rest-impl/src/main/java/org/apache/ofbiz/ws/rs/model/ModelOperation.java
+++ b/ofbiz-rest-impl/src/main/java/org/apache/ofbiz/ws/rs/model/ModelOperation.java
@@ -26,6 +26,30 @@ public class ModelOperation {
     private String consumes;
     private String path;
     private String description;
+    private boolean auth;
+
+    /**
+     * @return the auth
+     */
+    public boolean isAuth() {
+        return auth;
+    }
+
+    /**
+     * @param auth the auth to set
+     */
+    public void setAuth(boolean auth) {
+        this.auth = auth;
+    }
+
+    /**
+     * @param value
+     * @return
+     */
+    public ModelOperation auth(boolean auth) {
+        this.auth = auth;
+        return this;
+    }
 
     /**
      * Gets the value of the service property.
diff --git a/ofbiz-rest-impl/src/main/java/org/apache/ofbiz/ws/rs/model/ModelResource.java b/ofbiz-rest-impl/src/main/java/org/apache/ofbiz/ws/rs/model/ModelResource.java
index 0103f6f..a27a0e3 100644
--- a/ofbiz-rest-impl/src/main/java/org/apache/ofbiz/ws/rs/model/ModelResource.java
+++ b/ofbiz-rest-impl/src/main/java/org/apache/ofbiz/ws/rs/model/ModelResource.java
@@ -28,7 +28,38 @@ public class ModelResource {
     private String path;
     private String displayName;
     private String description;
-    private boolean enabled;
+    private boolean publish;
+    private boolean auth;
+
+    /**
+     * @return the auth
+     */
+    public boolean isAuth() {
+        return auth;
+    }
+
+    /**
+     * @param auth the auth to set
+     */
+    public void setAuth(boolean auth) {
+        this.auth = auth;
+    }
+
+    /**
+     * @param auth
+     * @return
+     */
+    public ModelResource auth(boolean auth) {
+        this.auth = auth;
+        return this;
+    }
+
+    /**
+     * @param publish the publish to set
+     */
+    protected void setPublish(boolean publish) {
+        this.publish = publish;
+    }
 
     /**
      * @return the operation
@@ -142,25 +173,18 @@ public class ModelResource {
     }
 
     /**
-     * @return the enabled
-     */
-    public Boolean getEnabled() {
-        return enabled;
-    }
-
-    /**
-     * @param enabled the enabled to set
+     * @return the publish
      */
-    public void setEnabled(Boolean enabled) {
-        this.enabled = enabled;
+    public boolean isPublish() {
+        return publish;
     }
 
     /**
-     * @param enabled
+     * @param publish
      * @return
      */
-    public ModelResource enabled(boolean enabled) {
-        this.enabled = enabled;
+    public ModelResource publish(boolean publish) {
+        this.publish = publish;
         return this;
     }
 
@@ -171,7 +195,7 @@ public class ModelResource {
     public String toString() {
         // TODO Auto-generated method stub
         return "name: " + name + ", path: " + path + ", displayName: " + displayName + ", description: " + description
-                + ", enabled: " + enabled;
+                + ", publish: " + publish;
     }
 
 }
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 9eb0a3b..6b335b6 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
@@ -91,8 +91,8 @@ public class OFBizServiceResource extends OFBizResource {
                 serviceList.add(serviceMap);
             }
         }
-        Success success = new Success(Response.Status.OK.getStatusCode(), Response.Status.OK.getReasonPhrase(), Response.Status.OK.getReasonPhrase(),
-                serviceList);
+        Success success = new Success(Response.Status.OK.getStatusCode(), Response.Status.OK.getReasonPhrase(),
+                Response.Status.OK.getReasonPhrase(), serviceList);
         return Response.status(Response.Status.OK).type(MediaType.APPLICATION_JSON).entity(success).build();
     }
 
@@ -108,14 +108,13 @@ public class OFBizServiceResource extends OFBizResource {
     @Produces(MediaType.APPLICATION_JSON)
     @Secured
     public Response doGet(@QueryParam(value = "inParams") ApiServiceRequest serviceRequest,
-                                       @PathParam(value = "serviceName") String serviceName) throws IOException, GenericServiceException {
+            @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));
+        return processor.process(UtilMisc.toMap("serviceName", serviceName, "httpVerb", HttpMethod.GET, "requestMap",
+                serviceRequest.getInParams(), "dispatcher", getDispatcher(), "request", httpRequest));
     }
 
     /**
@@ -129,15 +128,15 @@ public class OFBizServiceResource extends OFBizResource {
     @POST
     @Path("/{serviceName}")
     @Produces(MediaType.APPLICATION_JSON)
-    public Response doPost(HashMap<String, Object> serviceInParams, @PathParam(value = "serviceName") String serviceName)
+    public Response doPost(HashMap<String, Object> serviceInParams,
+            @PathParam(value = "serviceName") String serviceName)
             throws IOException, GenericEntityException, GenericServiceException {
         if (UtilValidate.isEmpty(serviceInParams)) {
             throw new BadRequestException("The request body is missing.");
         }
         ServiceRequestProcessor processor = new ServiceRequestProcessor();
-        return processor.process(
-                UtilMisc.toMap("serviceName", serviceName, "httpVerb", HttpMethod.POST, "requestMap", serviceInParams, "dispatcher", getDispatcher(),
-                        "request", httpRequest));
+        return processor.process(UtilMisc.toMap("serviceName", serviceName, "httpVerb", HttpMethod.POST, "requestMap",
+                serviceInParams, "dispatcher", getDispatcher(), "request", httpRequest));
     }
 
     /**
@@ -157,9 +156,8 @@ public class OFBizServiceResource extends OFBizResource {
             throw new BadRequestException("The request body is missing.");
         }
         ServiceRequestProcessor processor = new ServiceRequestProcessor();
-        return processor.process(
-                UtilMisc.toMap("serviceName", serviceName, "httpVerb", HttpMethod.PUT, "requestMap", serviceInParams, "dispatcher", getDispatcher(),
-                        "request", httpRequest));
+        return processor.process(UtilMisc.toMap("serviceName", serviceName, "httpVerb", HttpMethod.PUT, "requestMap",
+                serviceInParams, "dispatcher", getDispatcher(), "request", httpRequest));
     }
 
     /**
@@ -173,15 +171,15 @@ public class OFBizServiceResource extends OFBizResource {
     @PATCH
     @Path("/{serviceName}")
     @Produces(MediaType.APPLICATION_JSON)
-    public Response doPatch(HashMap<String, Object> serviceInParams, @PathParam(value = "serviceName") String serviceName)
+    public Response doPatch(HashMap<String, Object> serviceInParams,
+            @PathParam(value = "serviceName") String serviceName)
             throws IOException, GenericEntityException, GenericServiceException {
         if (UtilValidate.isEmpty(serviceInParams)) {
             throw new BadRequestException("The request body is missing.");
         }
         ServiceRequestProcessor processor = new ServiceRequestProcessor();
-        return processor.process(
-                UtilMisc.toMap("serviceName", serviceName, "httpVerb", HttpMethod.PATCH, "requestMap", serviceInParams, "dispatcher", getDispatcher(),
-                        "request", httpRequest));
+        return processor.process(UtilMisc.toMap("serviceName", serviceName, "httpVerb", HttpMethod.PATCH, "requestMap",
+                serviceInParams, "dispatcher", getDispatcher(), "request", httpRequest));
     }
 
     /**
@@ -195,14 +193,14 @@ public class OFBizServiceResource extends OFBizResource {
     @DELETE
     @Path("/{serviceName}")
     @Produces(MediaType.APPLICATION_JSON)
-    public Response doDelete(HashMap<String, Object> serviceInParams, @PathParam(value = "serviceName") String serviceName)
+    public Response doDelete(HashMap<String, Object> serviceInParams,
+            @PathParam(value = "serviceName") String serviceName)
             throws IOException, GenericEntityException, GenericServiceException {
         if (UtilValidate.isEmpty(serviceInParams)) {
             throw new BadRequestException("The request body is missing.");
         }
         ServiceRequestProcessor processor = new ServiceRequestProcessor();
-        return processor.process(
-                UtilMisc.toMap("serviceName", serviceName, "httpVerb", HttpMethod.DELETE, "requestMap", serviceInParams, "dispatcher", getDispatcher(),
-                        "request", httpRequest));
+        return processor.process(UtilMisc.toMap("serviceName", serviceName, "httpVerb", HttpMethod.DELETE, "requestMap",
+                serviceInParams, "dispatcher", getDispatcher(), "request", httpRequest));
     }
 }