001: /*
002: * Copyright 2007 Outerthought bvba and Schaubroeck nv
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.outerj.daisy.workflow.serverimpl.httpconnector.handlers;
017:
018: import org.outerj.daisy.httpconnector.spi.HttpUtil;
019: import org.outerj.daisy.httpconnector.spi.RequestHandlerSupport;
020: import org.outerj.daisy.httpconnector.spi.UploadItem;
021: import org.outerj.daisy.repository.Repository;
022: import org.outerj.daisy.workflow.WorkflowManager;
023: import org.outerj.daisy.workflow.WfProcessDefinition;
024: import org.outerj.daisy.workflow.WfListHelper;
025: import org.outerj.daisy.util.Constants;
026: import org.outerj.daisy.util.HttpConstants;
027: import org.apache.xmlbeans.XmlOptions;
028:
029: import javax.servlet.http.HttpServletRequest;
030: import javax.servlet.http.HttpServletResponse;
031: import java.util.Map;
032: import java.util.List;
033: import java.util.Locale;
034: import java.io.InputStream;
035:
036: public class ProcessDefinitionsHandler extends
037: AbstractWorkflowRequestHandler {
038:
039: public void handleRequest(Map matchMap, HttpServletRequest request,
040: HttpServletResponse response, Repository repository,
041: RequestHandlerSupport support) throws Exception {
042: XmlOptions xmlOptions = new XmlOptions()
043: .setSaveSuggestedPrefixes(Constants.SUGGESTED_NAMESPACE_PREFIXES);
044: WorkflowManager workflowManager = (WorkflowManager) repository
045: .getExtension("WorkflowManager");
046: Locale locale = WfHttpUtil.getLocale(request);
047:
048: if (request.getMethod().equals(HttpConstants.GET)) {
049: boolean latestOnly = "true".equals(request
050: .getParameter("latestOnly"));
051: List<WfProcessDefinition> processDefinitions;
052: if (latestOnly) {
053: processDefinitions = workflowManager
054: .getAllLatestProcessDefinitions(locale);
055: } else {
056: processDefinitions = workflowManager
057: .getAllProcessDefinitions(locale);
058: }
059: WfListHelper.getProcessDefinitionsAsXml(processDefinitions)
060: .save(response.getOutputStream(), xmlOptions);
061: } else if (request.getMethod().equals(HttpConstants.POST)) {
062: String action = request.getParameter("action");
063: if ("loadSamples".equals(action)) {
064: workflowManager.loadSampleWorkflows();
065: } else {
066: List<UploadItem> uploadedItems = support
067: .parseMultipartRequest(request, response);
068: UploadItem processDefItem = getItemByName(
069: uploadedItems, "processdefinition");
070:
071: if (processDefItem == null) {
072: HttpUtil
073: .sendCustomError(
074: "The required field named \"processdefinition\" is missing.",
075: HttpConstants._400_Bad_Request,
076: response);
077: return;
078: }
079:
080: InputStream is = null;
081: WfProcessDefinition processDef;
082: try {
083: is = processDefItem.getInputStream();
084: String mimeType = processDefItem.getContentType();
085: // strip character set from content type
086: int semicolonPos = mimeType.indexOf(';');
087: if (semicolonPos != -1)
088: mimeType = mimeType.substring(0, semicolonPos);
089:
090: processDef = workflowManager
091: .deployProcessDefinition(is, mimeType,
092: locale);
093: } finally {
094: if (is != null)
095: is.close();
096: }
097: processDef.getXml().save(response.getOutputStream(),
098: xmlOptions);
099: }
100: } else {
101: response.sendError(HttpConstants._405_Method_Not_Allowed);
102: }
103: }
104:
105: protected UploadItem getItemByName(List<UploadItem> items,
106: String name) {
107: for (UploadItem item : items) {
108: if (item.getFieldName().equals(name))
109: return item;
110: }
111: return null;
112: }
113:
114: public String getPathPattern() {
115: return "/processDefinition";
116: }
117: }
|