svn commit: r521060 - in /ofbiz/trunk/framework: webapp/lib/ webapp/src/org/ofbiz/webapp/event/ webtools/webapp/webtools/WEB-INF/

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

svn commit: r521060 - in /ofbiz/trunk/framework: webapp/lib/ webapp/src/org/ofbiz/webapp/event/ webtools/webapp/webtools/WEB-INF/

jaz-3
Author: jaz
Date: Wed Mar 21 17:51:42 2007
New Revision: 521060

URL: http://svn.apache.org/viewvc?view=rev&rev=521060
Log:
implemented XML-RPC to service engine

Added:
    ofbiz/trunk/framework/webapp/lib/ws-commons-java5-1.0.1.jar   (with props)
    ofbiz/trunk/framework/webapp/lib/ws-commons-util-1.0.1.jar   (with props)
    ofbiz/trunk/framework/webapp/lib/xmlrpc-common-3.0.jar   (with props)
    ofbiz/trunk/framework/webapp/lib/xmlrpc-server-3.0.jar   (with props)
    ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/event/XmlRpcEventHandler.java   (with props)
Modified:
    ofbiz/trunk/framework/webtools/webapp/webtools/WEB-INF/controller.xml

Added: ofbiz/trunk/framework/webapp/lib/ws-commons-java5-1.0.1.jar
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/webapp/lib/ws-commons-java5-1.0.1.jar?view=auto&rev=521060
==============================================================================
Binary file - no diff available.

Propchange: ofbiz/trunk/framework/webapp/lib/ws-commons-java5-1.0.1.jar
------------------------------------------------------------------------------
    svn:mime-type = application/octet-stream

Added: ofbiz/trunk/framework/webapp/lib/ws-commons-util-1.0.1.jar
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/webapp/lib/ws-commons-util-1.0.1.jar?view=auto&rev=521060
==============================================================================
Binary file - no diff available.

Propchange: ofbiz/trunk/framework/webapp/lib/ws-commons-util-1.0.1.jar
------------------------------------------------------------------------------
    svn:mime-type = application/octet-stream

Added: ofbiz/trunk/framework/webapp/lib/xmlrpc-common-3.0.jar
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/webapp/lib/xmlrpc-common-3.0.jar?view=auto&rev=521060
==============================================================================
Binary file - no diff available.

Propchange: ofbiz/trunk/framework/webapp/lib/xmlrpc-common-3.0.jar
------------------------------------------------------------------------------
    svn:mime-type = application/octet-stream

Added: ofbiz/trunk/framework/webapp/lib/xmlrpc-server-3.0.jar
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/webapp/lib/xmlrpc-server-3.0.jar?view=auto&rev=521060
==============================================================================
Binary file - no diff available.

Propchange: ofbiz/trunk/framework/webapp/lib/xmlrpc-server-3.0.jar
------------------------------------------------------------------------------
    svn:mime-type = application/octet-stream

Added: ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/event/XmlRpcEventHandler.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/event/XmlRpcEventHandler.java?view=auto&rev=521060
==============================================================================
--- ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/event/XmlRpcEventHandler.java (added)
+++ ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/event/XmlRpcEventHandler.java Wed Mar 21 17:51:42 2007
@@ -0,0 +1,215 @@
+/*
+ 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.apache.xmlrpc.common.ServerStreamConnection;
+import org.apache.xmlrpc.common.XmlRpcHttpRequestConfig;
+import org.apache.xmlrpc.common.XmlRpcHttpRequestConfigImpl;
+import org.apache.xmlrpc.server.*;
+import org.apache.xmlrpc.util.HttpUtil;
+import org.apache.xmlrpc.XmlRpcHandler;
+import org.apache.xmlrpc.XmlRpcException;
+import org.apache.xmlrpc.XmlRpcRequest;
+import org.ofbiz.service.*;
+import org.ofbiz.entity.GenericDelegator;
+import org.ofbiz.base.util.Debug;
+import org.ofbiz.base.util.UtilValidate;
+
+import javax.servlet.ServletContext;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.IOException;
+import java.util.Map;
+
+import javolution.util.FastMap;
+
+/**
+ * XmlRpcEventHandler
+ */
+public class XmlRpcEventHandler extends XmlRpcHttpServer implements EventHandler {
+
+    public static final String module = XmlRpcEventHandler.class.getName();
+    public static final String dispatcherName = "xmlrpc-dispatcher";
+    protected GenericDelegator delegator;
+    protected LocalDispatcher dispatcher;
+            
+    public void init(ServletContext context) throws EventHandlerException {
+        String delegatorName = context.getInitParameter("delegatorName");
+        this.delegator = GenericDelegator.getGenericDelegator(delegatorName);
+        this.dispatcher = GenericDispatcher.getLocalDispatcher(dispatcherName, delegator);
+        this.setHandlerMapping(new ServiceRpcHandler());        
+    }
+
+    public String invoke(String eventPath, String eventMethod, HttpServletRequest request, HttpServletResponse response) throws EventHandlerException {
+         try {
+             this.execute(this.getXmlRpcConfig(request), new HttpStreamConnection(request, response));
+         } catch (XmlRpcException e) {
+             Debug.logError(e, module);
+             throw new EventHandlerException(e.getMessage(), e);
+         }
+
+         return null;
+    }
+
+    protected void setResponseHeader(ServerStreamConnection con, String header, String value) {
+ ((HttpStreamConnection) con).getResponse().setHeader(header, value);
+ }
+
+    protected XmlRpcHttpRequestConfig getXmlRpcConfig(HttpServletRequest req) {
+ XmlRpcHttpRequestConfigImpl result = new XmlRpcHttpRequestConfigImpl();
+ XmlRpcHttpServerConfig serverConfig = (XmlRpcHttpServerConfig) getConfig();
+
+        result.setBasicEncoding(serverConfig.getBasicEncoding());
+ result.setContentLengthOptional(serverConfig.isContentLengthOptional());
+ result.setEnabledForExtensions(serverConfig.isEnabledForExtensions());
+ result.setGzipCompressing(HttpUtil.isUsingGzipEncoding(req.getHeader("Content-Encoding")));
+ result.setGzipRequesting(HttpUtil.isUsingGzipEncoding(req.getHeaders("Accept-Encoding")));
+ result.setEncoding(req.getCharacterEncoding());
+        //result.setEnabledForExceptions(serverConfig.isEnabledForExceptions());
+ HttpUtil.parseAuthorization(result, req.getHeader("Authorization"));
+        
+        return result;
+    }
+
+    class OfbizRpcAuthHandler implements AbstractReflectiveHandlerMapping.AuthenticationHandler {
+
+        public boolean isAuthorized(XmlRpcRequest xmlRpcReq) throws XmlRpcException {
+            XmlRpcHttpRequestConfig config = (XmlRpcHttpRequestConfig) xmlRpcReq.getConfig();
+
+            ModelService model;
+            try {
+                model = dispatcher.getDispatchContext().getModelService(xmlRpcReq.getMethodName());
+            } catch (GenericServiceException e) {
+                throw new XmlRpcException(e.getMessage(), e);
+            }
+
+            if (model != null && model.auth) {
+                String username = config.getBasicUserName();
+                String password = config.getBasicPassword();
+
+                // check the account
+                Map context = FastMap.newInstance();
+                context.put("login.username", username);
+                context.put("login.password", password);
+
+                Map resp;
+                try {
+                    resp = dispatcher.runSync("userLogin", context);
+                } catch (GenericServiceException e) {
+                    throw new XmlRpcException(e.getMessage(), e);
+                }
+
+                if (ServiceUtil.isError(resp)) {
+                    return false;
+                }
+            }
+
+            return true;
+        }
+    }
+
+    class ServiceRpcHandler extends AbstractReflectiveHandlerMapping implements XmlRpcHandler {
+
+        public ServiceRpcHandler() {
+            this.setAuthenticationHandler(new OfbizRpcAuthHandler());
+        }
+        
+        public XmlRpcHandler getHandler(String method) throws XmlRpcNoSuchHandlerException, XmlRpcException {
+            ModelService model = null;
+            try {
+                model = dispatcher.getDispatchContext().getModelService(method);
+            } catch (GenericServiceException e) {
+                Debug.logWarning(e, module);
+            }
+            if (model == null) {
+                throw new XmlRpcNoSuchHandlerException("No such service [" + method + "]");
+            }
+            return this;
+        }
+
+        public Object execute(XmlRpcRequest xmlRpcReq) throws XmlRpcException {
+            String serviceName = xmlRpcReq.getMethodName();
+
+            // prepare the context -- single parameter type struct (map)
+            Map context;
+            if (xmlRpcReq.getParameterCount() == 0) {
+                context = FastMap.newInstance();
+            } else {
+                context = (Map) xmlRpcReq.getParameter(0);
+            }
+
+            // add in auth parameters
+            XmlRpcHttpRequestConfig config = (XmlRpcHttpRequestConfig) xmlRpcReq.getConfig();
+            String username = config.getBasicUserName();
+            String password = config.getBasicPassword();
+            if (UtilValidate.isNotEmpty(username)) {
+                context.put("login.username", username);
+                context.put("login.password", password);
+            }
+
+            // invoke the service
+            Map resp;
+            try {
+                resp = dispatcher.runSync(serviceName, context);
+            } catch (GenericServiceException e) {
+                throw new XmlRpcException(e.getMessage(), e);
+            }
+            if (ServiceUtil.isError(resp)) {
+                throw new XmlRpcException(ServiceUtil.getErrorMessage(resp));
+            }
+
+            return resp;
+        }
+    }
+
+    class HttpStreamConnection implements ServerStreamConnection {
+
+        protected HttpServletRequest request;
+        protected HttpServletResponse response;
+
+        protected HttpStreamConnection(HttpServletRequest req, HttpServletResponse res) {
+            this.request = req;
+            this.response = res;
+        }
+
+        public HttpServletRequest getRequest() {
+            return request;
+        }
+
+        public HttpServletResponse getResponse() {
+            return response;
+        }
+
+        public InputStream newInputStream() throws IOException {
+            return request.getInputStream();
+        }
+
+        public OutputStream newOutputStream() throws IOException {
+            response.setContentType("text/xml");
+            return response.getOutputStream();
+        }
+
+        public void close() throws IOException {
+            response.getOutputStream().close();
+        }
+    }
+}

Propchange: ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/event/XmlRpcEventHandler.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/event/XmlRpcEventHandler.java
------------------------------------------------------------------------------
    svn:keywords = "Date Rev Author URL Id"

Propchange: ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/event/XmlRpcEventHandler.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: ofbiz/trunk/framework/webtools/webapp/webtools/WEB-INF/controller.xml
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/webtools/webapp/webtools/WEB-INF/controller.xml?view=diff&rev=521060&r1=521059&r2=521060
==============================================================================
--- ofbiz/trunk/framework/webtools/webapp/webtools/WEB-INF/controller.xml (original)
+++ ofbiz/trunk/framework/webtools/webapp/webtools/WEB-INF/controller.xml Wed Mar 21 17:51:42 2007
@@ -26,6 +26,7 @@
 
     <handler name="java" type="request" class="org.ofbiz.webapp.event.JavaEventHandler"/>
     <handler name="soap" type="request" class="org.ofbiz.webapp.event.SOAPEventHandler"/>
+    <handler name="xmlrpc" type="request" class="org.ofbiz.webapp.event.XmlRpcEventHandler"/>
     <handler name="service" type="request" class="org.ofbiz.webapp.event.ServiceEventHandler"/>
     <handler name="service-multi" type="request" class="org.ofbiz.webapp.event.ServiceMultiEventHandler"/>
     <handler name="simple" type="request" class="org.ofbiz.webapp.event.SimpleEventHandler"/>
@@ -80,6 +81,12 @@
 
     <request-map uri="SOAPService">
         <event type="soap"/>
+        <response name="error" type="none"/>
+        <response name="success" type="none"/>
+    </request-map>
+
+    <request-map uri="xmlrpc">
+        <event type="xmlrpc"/>
         <response name="error" type="none"/>
         <response name="success" type="none"/>
     </request-map>