001: /*
002: * Copyright (c) 2002-2003 by OpenSymphony
003: * All rights reserved.
004: */
005: package com.opensymphony.workflow.config;
006:
007: import com.opensymphony.workflow.FactoryException;
008: import com.opensymphony.workflow.loader.*;
009:
010: import java.io.ByteArrayInputStream;
011: import java.io.IOException;
012:
013: import java.net.MalformedURLException;
014: import java.net.URL;
015:
016: import javax.servlet.ServletException;
017: import javax.servlet.http.HttpServlet;
018: import javax.servlet.http.HttpServletRequest;
019: import javax.servlet.http.HttpServletResponse;
020:
021: /**
022: * @author Hani Suleiman Date: Dec 17, 2004 Time: 12:21:44 PM
023: */
024: public class WorkflowFactoryServlet extends HttpServlet {
025: //~ Instance fields ////////////////////////////////////////////////////////
026:
027: private WorkflowFactory factory;
028:
029: //~ Methods ////////////////////////////////////////////////////////////////
030:
031: public void init() throws ServletException {
032: DefaultConfiguration config = new DefaultConfiguration();
033: URL url = null;
034: String configFile = getInitParameter("config");
035:
036: if (configFile != null) {
037: try {
038: url = new URL(configFile);
039: } catch (MalformedURLException e) {
040: try {
041: url = getServletContext().getResource(configFile);
042: } catch (MalformedURLException e1) {
043: e1.printStackTrace();
044: }
045: }
046: }
047:
048: try {
049: config.load(url);
050: } catch (FactoryException e) {
051: throw new ServletException(
052: "Unable to create workflow factory", e);
053: }
054:
055: this .factory = config.getFactory();
056: }
057:
058: protected void doGet(HttpServletRequest req,
059: HttpServletResponse resp) throws ServletException,
060: IOException {
061: String command = req.getParameter("command");
062: String docId = req.getParameter("docId");
063:
064: if ("layout".equals(command)) {
065: Object layout = factory.getLayout(docId);
066:
067: if (layout != null) {
068: resp.setContentType("text/plain");
069: resp.getWriter().write(layout.toString());
070: }
071: } else if ("workflow".equals(command)) {
072: try {
073: WorkflowDescriptor descriptor = factory
074: .getWorkflow(docId);
075: resp.setContentType("text/xml");
076: descriptor.writeXML(resp.getWriter(), 0);
077: } catch (FactoryException e) {
078: e.printStackTrace(resp.getWriter());
079: resp
080: .setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
081: }
082: }
083: }
084:
085: protected void doPost(HttpServletRequest req,
086: HttpServletResponse resp) throws ServletException,
087: IOException {
088: String command = req.getParameter("command");
089: String docId = req.getParameter("docId");
090: String data = req.getParameter("data");
091:
092: if ("layout".equals(command)) {
093: factory.setLayout(docId, data.toString());
094: } else if ("workflow".equals(command)) {
095: boolean replace = "true"
096: .equals(req.getParameter("replace"));
097:
098: try {
099: WorkflowDescriptor descriptor = WorkflowLoader.load(
100: new ByteArrayInputStream(data.getBytes()),
101: false);
102: factory.saveWorkflow(docId, descriptor, replace);
103: } catch (Exception e) {
104: e.printStackTrace(resp.getWriter());
105: resp
106: .setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
107: }
108: }
109: }
110: }
|