Skip to content

Instantly share code, notes, and snippets.

@johndemic
Created July 13, 2012 12:25
Show Gist options
  • Save johndemic/3104629 to your computer and use it in GitHub Desktop.
Save johndemic/3104629 to your computer and use it in GitHub Desktop.
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.cookie.CookiePolicy;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.multipart.*;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.mule.api.MuleEventContext;
import org.mule.api.MuleMessage;
import org.mule.api.lifecycle.Callable;
import org.mule.util.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.ByteArrayInputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class MultiPartFormDispatcher implements Callable {
protected final transient Logger logger = LoggerFactory.getLogger(getClass());
public Object onCall(MuleEventContext eventContext) throws Exception {
PostMethod postMethod;
HttpClient client;
postMethod = new PostMethod("FOO");
postMethod.getParams().setBooleanParameter(HttpMethodParams.USE_EXPECT_CONTINUE,
true);
List<ByteArrayPartSource> attachments = new ArrayList<ByteArrayPartSource>();
List<String> attachmentContentTypes = new ArrayList<String>();
MuleMessage message = eventContext.getMessage();
for (String attachment : message.getOutboundAttachmentNames()) {
ByteArrayInputStream inputStream = (ByteArrayInputStream) message.getOutboundAttachment(attachment)
.getInputStream();
attachments.add(new ByteArrayPartSource(attachment, IOUtils.toByteArray(inputStream)));
attachmentContentTypes.add(message.getOutboundAttachment(attachment).getContentType());
}
@SuppressWarnings({"unchecked"})
Map<String,String> postParameterMap = (Map<String,String>) message.getPayload();
Part[] parts = new Part[postParameterMap.size() + attachments.size()];
int i = 0;
for (String parameter : postParameterMap.keySet()) {
parts[i] = new StringPart(parameter,postParameterMap.get(parameter));
i++;
}
for (ByteArrayPartSource attachment : attachments) {
parts[i] = new FilePart("inputFile",attachment,attachmentContentTypes.get(i),"UTF-8");
}
postMethod.setRequestEntity(
new MultipartRequestEntity(parts, postMethod.getParams())
);
client = new HttpClient();
client.getHttpConnectionManager().getParams().setConnectionTimeout(5000);
postMethod.getParams().setCookiePolicy(CookiePolicy.IGNORE_COOKIES);
postMethod.setRequestHeader("Cookie", "JSESSIONID=" + message.getSessionProperty("JSESSIONID"));
int status = client.executeMethod(postMethod);
if (status == HttpStatus.SC_OK) {
logger.debug("Upload success");
} else {
throw new RuntimeException("Upload failed: " + HttpStatus.getStatusText(status));
}
message.setPayload(new String(postMethod.getResponseBody()));
return message;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment