+/*\r
+ * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
+\r
package org.argeo.slc.web.mvc.process;\r
\r
import java.io.BufferedReader;\r
+import java.io.ByteArrayInputStream;\r
+import java.io.InputStream;\r
+import java.util.UUID;\r
\r
import javax.servlet.http.HttpServletRequest;\r
import javax.servlet.http.HttpServletResponse;\r
\r
+import org.apache.commons.io.IOUtils;\r
import org.apache.commons.logging.Log;\r
import org.apache.commons.logging.LogFactory;\r
+import org.argeo.slc.core.attachment.Attachment;\r
+import org.argeo.slc.core.attachment.AttachmentsStorage;\r
+import org.argeo.slc.core.attachment.SimpleAttachment;\r
import org.argeo.slc.msg.MsgConstants;\r
+import org.argeo.slc.msg.ObjectList;\r
import org.argeo.slc.process.SlcExecution;\r
+import org.argeo.slc.process.SlcExecutionStep;\r
import org.argeo.slc.runtime.SlcAgent;\r
import org.argeo.slc.runtime.SlcAgentFactory;\r
+import org.argeo.slc.services.SlcExecutionService;\r
import org.argeo.slc.web.mvc.AbstractServiceController;\r
+import org.springframework.oxm.Marshaller;\r
import org.springframework.oxm.Unmarshaller;\r
+import org.springframework.util.Assert;\r
import org.springframework.web.servlet.ModelAndView;\r
+import org.springframework.xml.transform.StringResult;\r
import org.springframework.xml.transform.StringSource;\r
\r
/** Send a new SlcExecution. */\r
\r
private SlcAgentFactory agentFactory;\r
private Unmarshaller unmarshaller;\r
+ private Marshaller marshaller;\r
+ private SlcExecutionService slcExecutionService;\r
+\r
+ private AttachmentsStorage attachmentsStorage;\r
\r
@Override\r
protected void handleServiceRequest(HttpServletRequest request,\r
HttpServletResponse response, ModelAndView modelAndView)\r
throws Exception {\r
\r
+ if (log.isTraceEnabled()) {\r
+ log.debug("Content-Type: " + request.getContentType());\r
+ log.debug("Content-Length: " + request.getContentLength());\r
+ }\r
+\r
String agentId = request\r
.getParameter(MsgConstants.PROPERTY_SLC_AGENT_ID);\r
+ Assert.notNull(agentId, "agent id");\r
\r
String answer = request.getParameter("body");\r
- if (answer == null && "text/xml".equals(request.getContentType())) {\r
+ if (answer == null) {\r
// lets read the message body instead\r
BufferedReader reader = request.getReader();\r
StringBuffer buffer = new StringBuffer();\r
- while (true) {\r
- String line = reader.readLine();\r
- if (line == null) {\r
- break;\r
- }\r
+ String line = null;\r
+ while (((line = reader.readLine()) != null)) {\r
buffer.append(line);\r
- buffer.append("\n");\r
}\r
answer = buffer.toString();\r
}\r
\r
- if (log.isDebugEnabled())\r
+ if (log.isTraceEnabled())\r
log.debug("Received message:\n" + answer);\r
\r
StringSource source = new StringSource(answer);\r
SlcExecution slcExecution = (SlcExecution) unmarshaller\r
.unmarshal(source);\r
\r
+ // Workaround for https://www.argeo.org/bugzilla/show_bug.cgi?id=86\r
+ if (slcExecution.getUuid() == null\r
+ || slcExecution.getUuid().length() < 8)\r
+ slcExecution.setUuid(UUID.randomUUID().toString());\r
+\r
+ slcExecution.setStatus(SlcExecution.STATUS_SCHEDULED);\r
+ slcExecution.getSteps().add(\r
+ new SlcExecutionStep(SlcExecutionStep.TYPE_START,\r
+ "Process started from the Web UI"));\r
+\r
+ // ObjectList ol = new ObjectList(slcExecution.getRealizedFlows());\r
+ // StringResult result = new StringResult();\r
+ // marshaller.marshal(ol, result);\r
+ // slcExecution.setRealizedFlowsXml(result.toString());\r
+ storeRealizedFlows(slcExecution);\r
+\r
+ slcExecutionService.newExecution(slcExecution);\r
+\r
SlcAgent agent = agentFactory.getAgent(agentId);\r
agent.runSlcExecution(slcExecution);\r
}\r
\r
+ protected void storeRealizedFlows(SlcExecution slcExecution) {\r
+ Attachment attachment = realizedFlowsAttachment(UUID.randomUUID()\r
+ .toString(), slcExecution);\r
+ InputStream in = null;\r
+ try {\r
+\r
+ ObjectList ol = new ObjectList(slcExecution.getRealizedFlows());\r
+ StringResult result = new StringResult();\r
+ marshaller.marshal(ol, result);\r
+\r
+ in = new ByteArrayInputStream(result.toString().getBytes());\r
+ attachmentsStorage.storeAttachment(attachment, in);\r
+\r
+ slcExecution.setRealizedFlowsXml(attachment.getUuid());\r
+\r
+ } catch (Exception e) {\r
+ log.error("Could not store realized flows as attachment #"\r
+ + attachment.getUuid(), e);\r
+ } finally {\r
+ IOUtils.closeQuietly(in);\r
+ }\r
+ }\r
+\r
public void setUnmarshaller(Unmarshaller unmarshaller) {\r
this.unmarshaller = unmarshaller;\r
}\r
this.agentFactory = agentFactory;\r
}\r
\r
+ public void setSlcExecutionService(SlcExecutionService slcExecutionService) {\r
+ this.slcExecutionService = slcExecutionService;\r
+ }\r
+\r
+ public void setMarshaller(Marshaller marshaller) {\r
+ this.marshaller = marshaller;\r
+ }\r
+\r
+ public void setAttachmentsStorage(AttachmentsStorage attachmentsStorage) {\r
+ this.attachmentsStorage = attachmentsStorage;\r
+ }\r
+\r
+ /** Unify labelling in the package */\r
+ static Attachment realizedFlowsAttachment(String attachmentUuid,\r
+ SlcExecution slcExecution) {\r
+ return new SimpleAttachment(attachmentUuid,\r
+ "RealizedFlows of SlcExecution #" + slcExecution.getUuid(),\r
+ "text/xml");\r
+ }\r
}\r