+++ /dev/null
-/*\r
- * Copyright (C) 2007-2012 Mathieu Baudier\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
-package org.argeo.slc.web.mvc.controllers;\r
-\r
-import java.io.BufferedReader;\r
-import java.text.SimpleDateFormat;\r
-import java.util.Comparator;\r
-import java.util.List;\r
-import java.util.SortedSet;\r
-import java.util.TreeSet;\r
-import java.util.UUID;\r
-\r
-import javax.servlet.http.HttpServletRequest;\r
-import javax.servlet.http.HttpServletResponse;\r
-\r
-import org.apache.commons.logging.Log;\r
-import org.apache.commons.logging.LogFactory;\r
-import org.argeo.slc.SlcException;\r
-import org.argeo.slc.core.attachment.AttachmentsStorage;\r
-import org.argeo.slc.dao.process.SlcExecutionDao;\r
-import org.argeo.slc.execution.ExecutionModuleDescriptor;\r
-import org.argeo.slc.msg.ExecutionAnswer;\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.springframework.oxm.Marshaller;\r
-import org.springframework.oxm.Unmarshaller;\r
-import org.springframework.stereotype.Controller;\r
-import org.springframework.ui.Model;\r
-import org.springframework.util.Assert;\r
-import org.springframework.web.bind.annotation.RequestMapping;\r
-import org.springframework.web.bind.annotation.RequestParam;\r
-import org.springframework.xml.transform.StringSource;\r
-\r
-@Controller\r
-public class ProcessController {\r
-\r
- private final static Log log = LogFactory.getLog(ProcessController.class);\r
-\r
- private SlcExecutionDao slcExecutionDao;\r
- private SlcAgentFactory agentFactory;\r
- private Unmarshaller unmarshaller;\r
- private Marshaller marshaller;\r
- private SlcExecutionService slcExecutionService;\r
- private AttachmentsStorage attachmentsStorage;\r
-\r
- private SlcExecutionManager slcExecutionManager;\r
-\r
- @RequestMapping("/listSlcExecutions.service")\r
- protected ObjectList listSlcExecutions() {\r
- List<SlcExecution> list = slcExecutionDao.listSlcExecutions();\r
- return new ObjectList(list);\r
- }\r
-\r
- @RequestMapping("/getExecutionDescriptor.service")\r
- protected ExecutionModuleDescriptor getExecutionDescriptor(\r
- @RequestParam String agentId, @RequestParam String moduleName,\r
- @RequestParam String version) {\r
-\r
- SlcAgent slcAgent = agentFactory.getAgent(agentId);\r
-\r
- ExecutionModuleDescriptor md = slcAgent.getExecutionModuleDescriptor(\r
- moduleName, version);\r
- return md;\r
- }\r
-\r
- @RequestMapping("/listModulesDescriptors.service")\r
- protected ObjectList listModulesDescriptors(@RequestParam String agentId) {\r
- // TODO: use centralized agentId property (from MsgConstants)?\r
- SlcAgent slcAgent = agentFactory.getAgent(agentId);\r
-\r
- List<ExecutionModuleDescriptor> descriptors = slcAgent\r
- .listExecutionModuleDescriptors();\r
- SortedSet<ExecutionModuleDescriptor> set = new TreeSet<ExecutionModuleDescriptor>(\r
- new Comparator<ExecutionModuleDescriptor>() {\r
-\r
- public int compare(ExecutionModuleDescriptor md1,\r
- ExecutionModuleDescriptor md2) {\r
- String str1 = md1.getLabel() != null ? md1.getLabel()\r
- : md1.getName();\r
- String str2 = md2.getLabel() != null ? md2.getLabel()\r
- : md2.getName();\r
- return str1.compareTo(str2);\r
- }\r
- });\r
- set.addAll(descriptors);\r
- return new ObjectList(set);\r
- }\r
-\r
- @RequestMapping("/getSlcExecution.service")\r
- protected SlcExecution getSlcExecution(@RequestParam String uuid) {\r
- SlcExecution slcExecution = slcExecutionDao.getSlcExecution(uuid);\r
- initializeSEM();\r
- slcExecutionManager.retrieveRealizedFlows(slcExecution);\r
- return slcExecution;\r
- }\r
-\r
- @RequestMapping("/newSlcExecution.service")\r
- protected ExecutionAnswer newSlcExecution(HttpServletRequest request,\r
- Model model) throws Exception {\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) {\r
- // lets read the message body instead\r
- BufferedReader reader = request.getReader();\r
- StringBuffer buffer = new StringBuffer();\r
- String line = null;\r
- while (((line = reader.readLine()) != null)) {\r
- buffer.append(line);\r
- }\r
- answer = buffer.toString();\r
- }\r
-\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.SCHEDULED);\r
- slcExecution.getSteps().add(\r
- new SlcExecutionStep(SlcExecutionStep.START,\r
- "Process started from the Web UI"));\r
-\r
- initializeSEM();\r
- slcExecutionManager.storeRealizedFlows(slcExecution);\r
- slcExecutionService.newExecution(slcExecution);\r
- SlcAgent agent = agentFactory.getAgent(agentId);\r
- //agent.runSlcExecution(slcExecution);\r
-\r
- return ExecutionAnswer.ok("Execution completed properly");\r
- }\r
-\r
- @RequestMapping("/tailSlcExecutionStepsCount.service")\r
- protected ObjectList tailSlcExecutionSteps(@RequestParam String uuid,\r
- @RequestParam Integer stepCount) {\r
- List<SlcExecutionStep> list = slcExecutionDao\r
- .tailSteps(uuid, stepCount);\r
- return new ObjectList(list);\r
- }\r
-\r
- @RequestMapping("/tailSlcExecutionStepsOffset.service")\r
- protected ObjectList tailSlcExecutionSteps(@RequestParam String uuid,\r
- @RequestParam String stepOffsetUuid) {\r
- List<SlcExecutionStep> list = slcExecutionDao.tailSteps(uuid,\r
- stepOffsetUuid);\r
- return new ObjectList(list);\r
- }\r
-\r
- @RequestMapping("/downloadSlcExecution.service")\r
- protected void downloadSlcExecution(@RequestParam String uuid,\r
- @RequestParam String ext, HttpServletResponse response)\r
- throws Exception {\r
- String contentType;\r
- // cf. http://en.wikipedia.org/wikServicei/Internet_media_type\r
- if ("csv".equals(ext))\r
- contentType = "text/csv";\r
- else if ("pdf".equals(ext))\r
- contentType = "application/pdf";\r
- else if ("zip".equals(ext))\r
- contentType = "application/zip";\r
- else if ("html".equals(ext))\r
- contentType = "application/html";\r
- else if ("txt".equals(ext) || "log".equals(ext))\r
- contentType = "text/plain";\r
- else if ("doc".equals(ext) || "docx".equals(ext))\r
- contentType = "application/msword";\r
- else if ("xls".equals(ext) || "xlsx".equals(ext))\r
- contentType = "application/vnd.ms-excel";\r
- else if ("xml".equals(ext))\r
- contentType = "text/xml";\r
- else\r
- contentType = "Content-Type: application/force-download";\r
-\r
- String name = "Process-" + uuid + "." + ext;\r
-\r
- SlcExecution process = slcExecutionDao.getSlcExecution(uuid);\r
-\r
- SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");\r
-\r
- // TODO: put it in a separate view\r
- if ("log".equals(ext)) {\r
- StringBuffer buf = new StringBuffer("");\r
- buf.append("#\n# PROCESS " + process.getUuid() + "\n#\n\n");\r
- buf\r
- .append("Started at " + df.format(process.getStartDate())\r
- + "\n");\r
- buf.append("Ended at " + df.format(process.getEndDate()) + "\n");\r
- buf.append("On host " + process.getHost() + "\n");\r
- buf.append("\n# LOG\n\n");\r
- for (SlcExecutionStep step : process.getSteps()) {\r
- buf.append(df.format(step.getTimestamp()));\r
- buf.append(" ");\r
- for (int i = 0; i < step.getLogLines().size(); i++) {\r
- if (i > 0)\r
- buf.append('\n');\r
- buf.append(step.getLogLines().get(i));\r
- }\r
- buf.append(" - ");\r
- buf.append(step.getType());\r
- buf.append(" - ");\r
- buf.append('[').append(step.getThread()).append(']');\r
- buf.append('\n');\r
- }\r
- prepareDownloadResponse(name, contentType, response);\r
- response.getWriter().print(buf);\r
- } else {\r
- throw new SlcException("Unsupported content type " + contentType);\r
- }\r
- }\r
-\r
- protected void prepareDownloadResponse(String name, String contentType,\r
- HttpServletResponse response) {\r
- response.setHeader("Content-Disposition", "attachment; filename=\""\r
- + name + "\"");\r
- response.setContentType(contentType + ";name=\"" + name + "\"");\r
- response.setHeader("Expires", "0");\r
- response.setHeader("Cache-Control", "no-cache, must-revalidate");\r
- response.setHeader("Pragma", "no-cache");\r
- }\r
-\r
- private void initializeSEM() {\r
- slcExecutionManager = new SlcExecutionManager(unmarshaller, marshaller,\r
- attachmentsStorage);\r
- }\r
-\r
- public void setSlcExecutionDao(SlcExecutionDao slcExecutionDao) {\r
- this.slcExecutionDao = slcExecutionDao;\r
- }\r
-\r
- public void setSlcExecutionService(SlcExecutionService slcExecutionService) {\r
- this.slcExecutionService = slcExecutionService;\r
- }\r
-\r
- public void setUnmarshaller(Unmarshaller unmarshaller) {\r
- this.unmarshaller = unmarshaller;\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
- public void setAgentFactory(SlcAgentFactory agentFactory) {\r
- this.agentFactory = agentFactory;\r
- }\r
-\r
-}\r