001: /*
002: * $Id: ServiceEventHandler.java,v 1.7 2004/02/19 18:52:35 ajzeneski Exp $
003: *
004: * Copyright (c) 2001-2003 The Open For Business Project - www.ofbiz.org
005: *
006: * Permission is hereby granted, free of charge, to any person obtaining a
007: * copy of this software and associated documentation files (the "Software"),
008: * to deal in the Software without restriction, including without limitation
009: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
010: * and/or sell copies of the Software, and to permit persons to whom the
011: * Software is furnished to do so, subject to the following conditions:
012: *
013: * The above copyright notice and this permission notice shall be included
014: * in all copies or substantial portions of the Software.
015: *
016: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
017: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
018: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
019: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
020: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
021: * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
022: * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
023: *
024: */
025: package org.ofbiz.content.webapp.event;
026:
027: import java.util.*;
028:
029: import javax.servlet.http.HttpServletRequest;
030: import javax.servlet.http.HttpServletResponse;
031: import javax.servlet.http.HttpSession;
032:
033: import org.ofbiz.base.util.Debug;
034: import org.ofbiz.base.util.UtilHttp;
035: import org.ofbiz.base.util.UtilProperties;
036: import org.ofbiz.base.util.UtilValidate;
037: import org.ofbiz.entity.GenericValue;
038: import org.ofbiz.service.DispatchContext;
039: import org.ofbiz.service.GenericServiceException;
040: import org.ofbiz.service.LocalDispatcher;
041: import org.ofbiz.service.ModelParam;
042: import org.ofbiz.service.ModelService;
043: import org.ofbiz.service.ServiceUtil;
044: import org.ofbiz.service.ServiceValidationException;
045: import org.ofbiz.service.ServiceAuthException;
046:
047: /**
048: * ServiceEventHandler - Service Event Handler
049: *
050: * @author <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a>
051: * @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
052: * @version $Revision: 1.7 $
053: * @since 2.0
054: */
055: public class ServiceEventHandler implements EventHandler {
056:
057: public static final String module = ServiceEventHandler.class
058: .getName();
059:
060: public static final String SYNC = "sync";
061: public static final String ASYNC = "async";
062:
063: /**
064: * @see org.ofbiz.content.webapp.event.EventHandler#invoke(java.lang.String, java.lang.String, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
065: */
066: public String invoke(String eventPath, String eventMethod,
067: HttpServletRequest request, HttpServletResponse response)
068: throws EventHandlerException {
069: // make sure we have a valid reference to the Service Engine
070: LocalDispatcher dispatcher = (LocalDispatcher) request
071: .getAttribute("dispatcher");
072: if (dispatcher == null) {
073: throw new EventHandlerException(
074: "The local service dispatcher is null");
075: }
076: DispatchContext dctx = dispatcher.getDispatchContext();
077: if (dctx == null) {
078: throw new EventHandlerException(
079: "Dispatch context cannot be found");
080: }
081:
082: // get the details for the service(s) to call
083: String mode = SYNC;
084: String serviceName = null;
085:
086: if (eventPath == null || eventPath.length() == 0) {
087: mode = SYNC;
088: } else {
089: mode = eventPath;
090: }
091:
092: // nake sure we have a defined service to call
093: serviceName = eventMethod;
094: if (serviceName == null) {
095: throw new EventHandlerException(
096: "Service name (eventMethod) cannot be null");
097: }
098: if (Debug.verboseOn())
099: Debug.logVerbose("[Set mode/service]: " + mode + "/"
100: + serviceName, module);
101:
102: // some needed info for when running the service
103: Locale locale = UtilHttp.getLocale(request);
104: HttpSession session = request.getSession();
105: GenericValue userLogin = (GenericValue) session
106: .getAttribute("userLogin");
107:
108: // get the service model to generate context
109: ModelService model = null;
110:
111: try {
112: model = dctx.getModelService(serviceName);
113: } catch (GenericServiceException e) {
114: throw new EventHandlerException(
115: "Problems getting the service model", e);
116: }
117:
118: if (model == null) {
119: throw new EventHandlerException(
120: "Problems getting the service model");
121: }
122:
123: if (Debug.verboseOn())
124: Debug.logVerbose("[Processing]: SERVICE Event", module);
125: if (Debug.verboseOn())
126: Debug.logVerbose("[Using delegator]: "
127: + dispatcher.getDelegator().getDelegatorName(),
128: module);
129:
130: // we have a service and the model; build the context
131: Map serviceContext = new HashMap();
132: Iterator modelParmInIter = model.getInModelParamList()
133: .iterator();
134: while (modelParmInIter.hasNext()) {
135: ModelParam modelParam = (ModelParam) modelParmInIter.next();
136: String name = modelParam.name;
137:
138: // don't include userLogin, that's taken care of below
139: if ("userLogin".equals(name))
140: continue;
141: // don't include locale, that is also taken care of below
142: if ("locale".equals(name))
143: continue;
144:
145: Object value = null;
146: if (modelParam.stringMapPrefix != null
147: && modelParam.stringMapPrefix.length() > 0) {
148: Map paramMap = UtilHttp.makeParamMapWithPrefix(request,
149: modelParam.stringMapPrefix, null);
150: value = paramMap;
151: if (Debug.verboseOn())
152: Debug.log("Set [" + modelParam.name + "]: "
153: + paramMap, module);
154: } else if (modelParam.stringListSuffix != null
155: && modelParam.stringListSuffix.length() > 0) {
156: List paramList = UtilHttp.makeParamListWithSuffix(
157: request, modelParam.stringListSuffix, null);
158: value = paramList;
159: } else {
160: value = request.getParameter(name);
161:
162: // if the parameter wasn't passed and no other value found, don't pass on the null
163: if (value == null) {
164: value = request.getAttribute(name);
165: }
166: if (value == null) {
167: value = request.getSession().getAttribute(name);
168: }
169: if (value == null) {
170: //still null, give up for this one
171: continue;
172: }
173:
174: if (value instanceof String
175: && ((String) value).length() == 0) {
176: // interpreting empty fields as null values for each in back end handling...
177: value = null;
178: }
179: }
180: // set even if null so that values will get nulled in the db later on
181: serviceContext.put(name, value);
182: }
183:
184: // get only the parameters for this service - converted to proper type
185: serviceContext = model.makeValid(serviceContext,
186: ModelService.IN_PARAM);
187:
188: // include the UserLogin value object
189: if (userLogin != null) {
190: serviceContext.put("userLogin", userLogin);
191: }
192:
193: // include the Locale object
194: if (locale != null) {
195: serviceContext.put("locale", locale);
196: }
197:
198: // invoke the service
199: Map result = null;
200:
201: try {
202: if (ASYNC.equalsIgnoreCase(mode)) {
203: dispatcher.runAsync(serviceName, serviceContext);
204: } else {
205: result = dispatcher
206: .runSync(serviceName, serviceContext);
207: }
208: } catch (ServiceAuthException e) {
209: // not logging since the service engine already did
210: request.setAttribute("_ERROR_MESSAGE_", e
211: .getNonNestedMessage());
212: return "error";
213: } catch (ServiceValidationException e) {
214: // not logging since the service engine already did
215: request.setAttribute("serviceValidationException", e);
216: request.setAttribute("_ERROR_MESSAGE_", e
217: .getNonNestedMessage());
218: return "error";
219: } catch (GenericServiceException e) {
220: Debug.logError(e, "Service invocation error", module);
221: throw new EventHandlerException("Service invocation error",
222: e.getNested());
223: }
224:
225: String responseString = null;
226:
227: if (result == null) {
228: responseString = ModelService.RESPOND_SUCCESS;
229: } else {
230:
231: if (!result.containsKey(ModelService.RESPONSE_MESSAGE))
232: responseString = ModelService.RESPOND_SUCCESS;
233: else
234: responseString = (String) result
235: .get(ModelService.RESPONSE_MESSAGE);
236:
237: // Get the messages:
238: String errorPrefixStr = UtilProperties.getMessage(
239: "DefaultMessages", "service.error.prefix", locale);
240: String errorSuffixStr = UtilProperties.getMessage(
241: "DefaultMessages", "service.error.suffix", locale);
242: String successPrefixStr = UtilProperties
243: .getMessage("DefaultMessages",
244: "service.success.prefix", locale);
245: String successSuffixStr = UtilProperties
246: .getMessage("DefaultMessages",
247: "service.success.suffix", locale);
248: String messagePrefixStr = UtilProperties
249: .getMessage("DefaultMessages",
250: "service.message.prefix", locale);
251: String messageSuffixStr = UtilProperties
252: .getMessage("DefaultMessages",
253: "service.message.suffix", locale);
254: String defaultMessageStr = UtilProperties.getMessage(
255: "DefaultMessages", "service.default.message",
256: locale);
257:
258: String errorMessage = ServiceUtil.makeErrorMessage(result,
259: messagePrefixStr, messageSuffixStr, errorPrefixStr,
260: errorSuffixStr);
261:
262: if (UtilValidate.isNotEmpty(errorMessage))
263: request.setAttribute("_ERROR_MESSAGE_", errorMessage);
264:
265: String successMessage = ServiceUtil.makeSuccessMessage(
266: result, messagePrefixStr, messageSuffixStr,
267: successPrefixStr, successSuffixStr);
268:
269: if (UtilValidate.isNotEmpty(successMessage))
270: request.setAttribute("_EVENT_MESSAGE_", successMessage);
271:
272: if (UtilValidate.isEmpty(errorMessage)
273: && UtilValidate.isEmpty(successMessage)
274: && UtilValidate.isNotEmpty(defaultMessageStr))
275: request.setAttribute("_EVENT_MESSAGE_",
276: defaultMessageStr);
277:
278: // set the results in the request
279: Iterator ri = result.keySet().iterator();
280:
281: while (ri.hasNext()) {
282: String resultKey = (String) ri.next();
283: Object resultValue = result.get((Object) resultKey);
284:
285: if (!resultKey.equals(ModelService.RESPONSE_MESSAGE)
286: && !resultKey
287: .equals(ModelService.ERROR_MESSAGE)
288: && !resultKey
289: .equals(ModelService.ERROR_MESSAGE_LIST)
290: && !resultKey
291: .equals(ModelService.ERROR_MESSAGE_MAP)
292: && !resultKey
293: .equals(ModelService.SUCCESS_MESSAGE)
294: && !resultKey
295: .equals(ModelService.SUCCESS_MESSAGE_LIST)) {
296: request.setAttribute(resultKey, resultValue);
297: }
298: }
299: }
300:
301: if (Debug.verboseOn())
302: Debug.logVerbose("[Event Return]: " + responseString,
303: module);
304: return responseString;
305: }
306: }
|