/**
* Copyright (c) 2008 Intalio inc.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http: *
* Contributors:
* Intalio inc. - initial API and implementation
*/
package com.intalio.bpms.gi;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.namespace.QName;
import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author <a href="http:>Intalio Inc.</a>
*/
public class AttachmentServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected static final Logger LOG = LoggerFactory.getLogger(RouterServlet.class);
public AttachmentServlet() {
super();
}
@SuppressWarnings("unchecked")
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String shp = req.getScheme() + ": + req.getServerName() + ":" + req.getServerPort();
PrintWriter out = resp.getWriter();
if (ServletFileUpload.isMultipartContent(req)) {
HashMap<String, FileItem> fieldMap = new HashMap<String, FileItem>();
try {
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setSizeMax(1100000);
List items = upload.parseRequest(req);
for (Object object : items) {
FileItem item = (FileItem) object;
fieldMap.put(item.getFieldName(), item);
}
if (fieldMap.size() > 0) {
OMElement tasResponse = callTasAddAttachment(shp, fieldMap);
if (tasResponse == null) {
LOG.info("null tasResponse");
} else {
out.println(tasResponse);
OMElement tmsResponse = callTmsAddAttachment(shp, fieldMap, tasResponse);
out.println(tmsResponse);
}
}
} catch (FileUploadException ex) {
LOG.error(ex.getMessage());
}
}
out.println("ok");
out.close();
}
protected OMElement callTasAddAttachment(String shp, HashMap<String, FileItem> fieldMap)
throws AxisFault {
FileItem fileItem = fieldMap.get("attachmentFile");
String participantToken = getFieldString(fieldMap, "participantToken");
String users = getFieldString(fieldMap, "authorizedUsers");
String roles = getFieldString(fieldMap, "authorizedRoles");
String type = getFieldString(fieldMap, "attachmentType");
String text = getFieldString(fieldMap, "attachmentText");
String title = getFieldString(fieldMap, "attachmentName");
String mimeType = "text/plain";
String fileName = title;
boolean isText = ("text".equalsIgnoreCase(type));
if (!isText) {
if (fileItem == null) {
LOG.info("null attachmentFile FileItem");
return null;
}
mimeType = fileItem.getContentType();
fileName = fileItem.getName();
}
if (title.equals("")) {
LOG.info("empty title");
return null;
}
if (fileName.equals("")) {
LOG.info("empty fileName");
return null;
}
OMFactory fac = OMAbstractFactory.getOMFactory();
OMNamespace ns = fac.createOMNamespace(
"http:, "tns");
OMElement request = fac.createOMElement("addRequest", ns);
OMElement authCredentials = fac.createOMElement("authCredentials", ns);
addTextElement(fac, ns, authCredentials, "participantToken", participantToken);
if (!users.equals("")) {
OMElement authroizedUsers = fac.createOMElement("authorizedUsers", ns);
addTextElement(fac, ns, authroizedUsers, "user", users);
authCredentials.addChild(authroizedUsers);
}
if (!roles.equals("")) {
OMElement authroizedRoles = fac.createOMElement("authorizedRoles", ns);
addTextElement(fac, ns, authroizedRoles, "role", roles);
authCredentials.addChild(authroizedRoles);
}
request.addChild(authCredentials);
OMElement attachmentMetadata = fac.createOMElement("attachmentMetadata", ns);
addTextElement(fac, ns, attachmentMetadata, "mimeType", mimeType);
addTextElement(fac, ns, attachmentMetadata, "filename", fileName);
request.addChild(attachmentMetadata);
if (isText) {
addTextElement(fac, ns, request, "plaintext", text);
} else {
String payload = new String(Base64.encodeBase64(fileItem.get()));
addTextElement(fac, ns, request, "payload", payload);
}
ServiceClient sc = new ServiceClient();
Options opts = new Options();
opts.setTo(new EndpointReference(shp + "/axis2/services/tas"));
opts.setAction("add");
sc.setOptions(opts);
return sc.sendReceive(request);
}
protected OMElement callTmsAddAttachment(String shp, HashMap<String, FileItem> fieldMap,
OMElement tasResponse) throws AxisFault {
FileItem fileItem = fieldMap.get("attachmentFile");
String taskId = getFieldString(fieldMap, "taskId");
String participantToken = getFieldString(fieldMap, "participantToken");
String type = getFieldString(fieldMap, "attachmentType");
String title = getFieldString(fieldMap, "attachmentName");
String mimeType = "text/plain";
String fileName = title;
boolean isText = ("text".equalsIgnoreCase(type));
if (!isText) {
if (fileItem == null) {
LOG.info("null attachmentFile FileItem");
return null;
}
mimeType = fileItem.getContentType();
fileName = fileItem.getName();
}
QName qname = new QName("http:,
"url", "tas");
OMElement element = tasResponse.getFirstChildWithName(qname);
if (element == null) {
LOG.info("null payloadUrl");
return null;
}
String payloadUrl = element.getText();
String description = "";
String creationDate = "";
OMFactory fac = OMAbstractFactory.getOMFactory();
OMNamespace ns = fac.createOMNamespace(
"http:, "tns");
OMElement request = fac.createOMElement("addAttachmentRequest", ns);
addTextElement(fac, ns, request, "taskId", taskId);
OMElement attachment = fac.createOMElement("attachment", ns);
OMElement attachmentMetadata = fac.createOMElement("attachmentMetadata", ns);
addTextElement(fac, ns, attachmentMetadata, "mimeType", mimeType);
addTextElement(fac, ns, attachmentMetadata, "fileName", fileName);
addTextElement(fac, ns, attachmentMetadata, "title", title);
addTextElement(fac, ns, attachmentMetadata, "description", description);
addTextElement(fac, ns, attachmentMetadata, "creationDate", creationDate);
attachment.addChild(attachmentMetadata);
addTextElement(fac, ns, attachment, "payloadUrl", payloadUrl);
request.addChild(attachment);
addTextElement(fac, ns, request, "participantToken", participantToken);
ServiceClient sc = new ServiceClient();
Options opts = new Options();
opts.setTo(new EndpointReference(shp + "/axis2/services/TaskManagementServices"));
opts.setAction("addAttachment");
sc.setOptions(opts);
return sc.sendReceive(request);
}
protected void addTextElement(OMFactory fac, OMNamespace ns, OMElement parent, String name,
String text) {
OMElement child = fac.createOMElement(name, ns);
child.setText(text);
parent.addChild(child);
}
protected String getFieldString(HashMap<String, FileItem> fieldMap, String field) {
FileItem item = fieldMap.get(field);
if (item == null) return "";
return item.getString();
}
}