001: package org.bpmscript.web;
002:
003: import java.beans.PropertyEditor;
004: import java.util.HashMap;
005: import java.util.Iterator;
006: import java.util.List;
007: import java.util.Map;
008: import java.util.Set;
009:
010: import javax.jbi.messaging.InOut;
011: import javax.jbi.messaging.NormalizedMessage;
012: import javax.servlet.http.HttpServletRequest;
013: import javax.servlet.http.HttpServletResponse;
014: import javax.xml.namespace.QName;
015:
016: import org.apache.commons.lang.StringEscapeUtils;
017: import org.apache.servicemix.client.DefaultServiceMixClient;
018: import org.apache.servicemix.jbi.jaxp.SourceTransformer;
019: import org.apache.servicemix.jbi.jaxp.StringSource;
020: import org.apache.servicemix.jbi.resolver.ServiceNameEndpointResolver;
021: import org.bpmscript.IProcess;
022: import org.bpmscript.remote.IRemoteProcessManager;
023: import org.springframework.beans.factory.InitializingBean;
024: import org.springframework.beans.propertyeditors.CustomNumberEditor;
025: import org.springframework.validation.BindException;
026: import org.springframework.web.servlet.ModelAndView;
027: import org.springframework.web.servlet.mvc.SimpleFormController;
028:
029: public class SendMessageController extends SimpleFormController
030: implements InitializingBean {
031:
032: private final org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory
033: .getLog(SendMessageController.class);
034:
035: private DefaultServiceMixClient client;
036: private IRemoteProcessManager remoteProcessManager;
037: private String namespace;
038: private Map<String, PropertyEditor> propertyEditors = new HashMap<String, PropertyEditor>();
039:
040: @Override
041: protected Map<String, Object> referenceData(
042: HttpServletRequest request) throws Exception {
043: Map<String, Object> result = new HashMap<String, Object>();
044: List<IProcess> primaryProcesses = remoteProcessManager
045: .getPrimaryProcesses();
046: result.put("processes", primaryProcesses);
047: result.put("types", propertyEditors.keySet());
048: return result;
049: }
050:
051: @Override
052: protected ModelAndView onSubmit(HttpServletRequest request,
053: HttpServletResponse response, Object command,
054: BindException errors) throws Exception {
055: SendMessage message = (SendMessage) command;
056: InOut inOut = client
057: .createInOutExchange(new ServiceNameEndpointResolver(
058: new QName(namespace, message.getProcessName())));
059: NormalizedMessage in = inOut.getInMessage();
060: in.setContent(new StringSource(message.getMessage()));
061: if (message.getOperation() != null
062: && message.getOperation().trim().length() > 0) {
063: in.setProperty("operation", message.getOperation().trim());
064: }
065: if (message.getPropertyKey() != null
066: && message.getPropertyValue() != null
067: && message.getPropertyType() != null
068: && message.getPropertyKey().length == message
069: .getPropertyValue().length
070: && message.getPropertyKey().length == message
071: .getPropertyType().length) {
072: for (int i = 0; i < message.getPropertyKey().length; i++) {
073: String key = message.getPropertyKey()[i];
074: String value = message.getPropertyValue()[i];
075: String type = message.getPropertyType()[i];
076: PropertyEditor editor = propertyEditors.get(type);
077: Object valueObject = null;
078: if (editor != null) {
079: synchronized (editor) {
080: editor.setAsText(value);
081: valueObject = editor.getValue();
082: }
083: } else {
084: valueObject = value;
085: }
086: in.setProperty(key, valueObject);
087: }
088: }
089: Map<String, Object> map = referenceData(request);
090: if (message.isAsync()) {
091: client.send(inOut);
092: log.debug("sent async message");
093: return new ModelAndView(getFormView(), map);
094: } else {
095: if (message.getTimeout() != null
096: && message.getTimeout() > 0) {
097: client.send(inOut);
098: return new ModelAndView(getFormView(), map);
099: } else {
100: boolean b = client.sendSync(inOut);
101: if (b) {
102: NormalizedMessage outMessage = inOut
103: .getOutMessage();
104: if (outMessage == null) {
105: map.put("result", "no out message");
106: return new ModelAndView(getFormView(), map);
107: } else {
108: log.debug("sent sync message and got result");
109: String result = new SourceTransformer()
110: .contentToString(outMessage);
111: Map<String, Object> properties = new HashMap<String, Object>();
112: Set propertyNames = outMessage
113: .getPropertyNames();
114: for (Iterator propertyNameIterator = propertyNames
115: .iterator(); propertyNameIterator
116: .hasNext();) {
117: String propertyName = (String) propertyNameIterator
118: .next();
119: Object propertyValue = outMessage
120: .getProperty(propertyName);
121: properties
122: .put(
123: propertyName,
124: propertyValue == null ? "null"
125: : StringEscapeUtils
126: .escapeHtml(propertyValue
127: .toString()));
128: }
129: log.debug(result);
130: map.put("properties", properties);
131: map.put("result", StringEscapeUtils
132: .escapeHtml(result));
133: return new ModelAndView(getFormView(), map);
134: }
135: } else {
136: log.debug("sent sync message and timed out");
137: map.put("result", "timed out");
138: return new ModelAndView(getFormView(), map);
139: }
140: }
141: }
142: }
143:
144: public void setClient(DefaultServiceMixClient client) {
145: this .client = client;
146: }
147:
148: public void setRemoteProcessManager(
149: IRemoteProcessManager remoteProcessManager) {
150: this .remoteProcessManager = remoteProcessManager;
151: }
152:
153: public void setNamespace(String namespace) {
154: this .namespace = namespace;
155: }
156:
157: public void afterPropertiesSet() throws Exception {
158: if (propertyEditors.size() == 0) {
159: propertyEditors.put("String", null);
160: propertyEditors.put("Integer", new CustomNumberEditor(
161: Integer.class, true));
162: propertyEditors.put("Double", new CustomNumberEditor(
163: Double.class, true));
164: }
165: }
166:
167: }
|