Author: jaz
Date: Fri Apr 6 12:30:22 2007 New Revision: 526262 URL: http://svn.apache.org/viewvc?view=rev&rev=526262 Log: implemented simple service stream handler; allows reading the inputstream of a request from inside a service; useful for processing non-standard requests Added: ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/event/ServiceStreamHandler.java (with props) Modified: ofbiz/trunk/framework/common/servicedef/services_test.xml ofbiz/trunk/framework/common/src/org/ofbiz/common/CommonServices.java ofbiz/trunk/framework/service/servicedef/services.xml ofbiz/trunk/framework/webtools/webapp/webtools/WEB-INF/controller.xml Modified: ofbiz/trunk/framework/common/servicedef/services_test.xml URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/common/servicedef/services_test.xml?view=diff&rev=526262&r1=526261&r2=526262 ============================================================================== --- ofbiz/trunk/framework/common/servicedef/services_test.xml (original) +++ ofbiz/trunk/framework/common/servicedef/services_test.xml Fri Apr 6 12:30:22 2007 @@ -171,6 +171,11 @@ <implements service="serviceEcaConditionInterface"/> </service> + <service name="serviceStreamTest" engine="java" auth="false" + location="org.ofbiz.common.CommonServices" invoke="streamTest"> + <implements service="serviceStreamInterface"/> + </service> + <service name="ping" engine="java" export="true" require-new-transaction="true" location="org.ofbiz.common.CommonServices" invoke="ping"> <description>Test Ping Service</description> Modified: ofbiz/trunk/framework/common/src/org/ofbiz/common/CommonServices.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/common/src/org/ofbiz/common/CommonServices.java?view=diff&rev=526262&r1=526261&r2=526262 ============================================================================== --- ofbiz/trunk/framework/common/src/org/ofbiz/common/CommonServices.java (original) +++ ofbiz/trunk/framework/common/src/org/ofbiz/common/CommonServices.java Fri Apr 6 12:30:22 2007 @@ -18,9 +18,7 @@ *******************************************************************************/ package org.ofbiz.common; -import java.io.FileNotFoundException; -import java.io.IOException; -import java.io.RandomAccessFile; +import java.io.*; import java.sql.Timestamp; import java.util.HashMap; import java.util.Iterator; @@ -435,6 +433,35 @@ Debug.logError(e, module); } return ServiceUtil.returnSuccess(); + } + + public static Map streamTest(DispatchContext dctx, Map context) { + InputStream in = (InputStream) context.get("inputStream"); + OutputStream out = (OutputStream) context.get("outputStream"); + + BufferedReader reader = new BufferedReader(new InputStreamReader(in)); + Writer writer = new OutputStreamWriter(out); + String line; + + try { + while ((line = reader.readLine()) != null) { + Debug.log("Read line: " + line, module); + writer.write(line); + } + } catch (IOException e) { + Debug.logError(e, module); + return ServiceUtil.returnError(e.getMessage()); + } finally { + try { + writer.close(); + } catch (Exception e) { + Debug.logError(e, module); + } + } + + Map result = ServiceUtil.returnSuccess(); + result.put("contentType", "text/plain"); + return result; } public static Map ping(DispatchContext dctx, Map context) { Modified: ofbiz/trunk/framework/service/servicedef/services.xml URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/service/servicedef/services.xml?view=diff&rev=526262&r1=526261&r2=526262 ============================================================================== --- ofbiz/trunk/framework/service/servicedef/services.xml (original) +++ ofbiz/trunk/framework/service/servicedef/services.xml Fri Apr 6 12:30:22 2007 @@ -73,6 +73,13 @@ <attribute name="userLogin" type="org.ofbiz.entity.GenericValue" mode="OUT"/> <attribute name="userLoginSession" type="java.util.Map" mode="OUT" optional="true"/> </service> + + <service name="serviceStreamInterface" engine="interface"> + <description>Inteface to describe services call with streams</description> + <attribute name="inputStream" type="java.io.InputStream" mode="IN"/> + <attribute name="outputStream" type="java.io.OutputStream" mode="IN"/> + <attribute name="contentType" type="String" mode="OUT"/> + </service> <service name="serviceEcaConditionInterface" engine="interface"> <description>Interface to describe services which are used as SECA conditions</description> Added: ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/event/ServiceStreamHandler.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/event/ServiceStreamHandler.java?view=auto&rev=526262 ============================================================================== --- ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/event/ServiceStreamHandler.java (added) +++ ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/event/ServiceStreamHandler.java Fri Apr 6 12:30:22 2007 @@ -0,0 +1,97 @@ +/* + 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.ofbiz.entity.GenericDelegator; +import org.ofbiz.service.GenericDispatcher; +import org.ofbiz.service.LocalDispatcher; +import org.ofbiz.service.GenericServiceException; +import org.ofbiz.service.ServiceUtil; +import org.ofbiz.base.util.Debug; + +import javax.servlet.ServletContext; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.*; +import java.util.Map; + +import javolution.util.FastMap; + +/** + * ServiceStreamHandler + */ +public class ServiceStreamHandler implements EventHandler { + + public static final String module = ServiceStreamHandler.class.getName(); + public static final String dispatcherName = "sstream-dispatcher"; + protected LocalDispatcher dispatcher; + protected GenericDelegator delegator; + + public void init(ServletContext context) throws EventHandlerException { + String delegatorName = context.getInitParameter("delegatorName"); + this.delegator = GenericDelegator.getGenericDelegator(delegatorName); + this.dispatcher = GenericDispatcher.getLocalDispatcher(dispatcherName, delegator); + } + + public String invoke(String eventPath, String eventMethod, HttpServletRequest request, HttpServletResponse response) throws EventHandlerException { + InputStream in; + try { + in = request.getInputStream(); + } catch (IOException e) { + throw new EventHandlerException(e.getMessage(), e); + } + OutputStream out; + try { + out = response.getOutputStream(); + } catch (IOException e) { + throw new EventHandlerException(e.getMessage(), e); + } + + Map context = FastMap.newInstance(); + context.put("inputStream", in); + context.put("outputStream", out); + + Debug.log("Running service with context: " + context, module); + Map resp; + try { + resp = dispatcher.runSync(eventMethod, context); + } catch (GenericServiceException e) { + throw new EventHandlerException(e.getMessage(), e); + } + Debug.log("Received respone: " + resp, module); + if (ServiceUtil.isError(resp)) { + throw new EventHandlerException(ServiceUtil.getErrorMessage(resp)); + } + String contentType = (String) resp.get("contentType"); + if (contentType != null) { + response.setContentType(contentType); + } + + if (out != null) { + try { + out.close(); + } catch (IOException e) { + throw new EventHandlerException(ServiceUtil.getErrorMessage(resp)); + } + } + + return null; + } +} Propchange: ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/event/ServiceStreamHandler.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/event/ServiceStreamHandler.java ------------------------------------------------------------------------------ svn:keywords = "Date Rev Author URL Id" Propchange: ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/event/ServiceStreamHandler.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=526262&r1=526261&r2=526262 ============================================================================== --- ofbiz/trunk/framework/webtools/webapp/webtools/WEB-INF/controller.xml (original) +++ ofbiz/trunk/framework/webtools/webapp/webtools/WEB-INF/controller.xml Fri Apr 6 12:30:22 2007 @@ -27,6 +27,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="stream" type="request" class="org.ofbiz.webapp.event.ServiceStreamHandler"/> <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"/> @@ -100,6 +101,11 @@ <event type="service" invoke="testScv" /> <response name="error" type="view" value="error"/> <response name="success" type="view" value="error"/> + </request-map> + <request-map uri="streamTest"> + <event type="stream" invoke="serviceStreamTest"/> + <response name="success" type="none"/> + <response name="error" type="none"/> </request-map> <request-map uri="yahoo"> |
Free forum by Nabble | Edit this page |