package no.magge.google; import java.io.*; import org.apache.http.Header; import org.apache.http.HttpEntity; import org.apache.http.message.BasicHeader; import org.apache.http.params.HttpParams; import org.apache.log4j.Logger; // httpclient-3.1 imports import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity; import org.apache.commons.httpclient.methods.multipart.Part; import org.apache.commons.httpclient.params.HttpMethodParams; /** * MultipartEntityWrapper - this implements the HttpClient-4 HttpEntity interface with the functionality from * HttpClient-3.1's MultipartRequestEntity. Should provide functionality to post multipart/form-data with HttpClient-4. * * Followed some instructions written by Roland Webers here: * (http://www.nabble.com/multipart-form-data-in-4.0-td14224819.html) * * Note: * I tried making a class that extended MultipartRequestEntity at first, but there's at least one conflict in return types of * equally named methods in the MultipartRequestEntity class and the HttpEntity interface. Thus we just instantiate a * separate MultipartRequestEntity object and wrap the necessary methods. * * @author Magnus Olstad Hansen */ public class MultipartEntityWrapper implements HttpEntity { private static final Logger log = Logger.getLogger(MultipartEntityWrapper.class); protected MultipartRequestEntity multipartLegacy; /** * Constructs a new MultipartRequestEntity compatible with HttpClient-4 * * @param parts The parts to include in the entity * @param params - WARNING, mapping of new parameters to old parameters is -NOT- done */ public MultipartEntityWrapper(Part[] parts, HttpParams params) { HttpMethodParams targetParams = new HttpMethodParams(); // TODO: map params to httpmethodparams, defaults used here multipartLegacy = new MultipartRequestEntity(parts, targetParams); } /** * @see org.apache.http.HttpEntity#getContentLength() */ @Override public long getContentLength() { return multipartLegacy.getContentLength(); } /** * @see org.apache.http.HttpEntity#getContentType() */ @Override public Header getContentType() { // TODO: Find the constant for Content-Type? :) return new BasicHeader("Content-Type", multipartLegacy.getContentType()); } /** * @see org.apache.http.HttpEntity#isChunked() */ public boolean isChunked() { // Roland Webers word is law; // (http://www.nabble.com/multipart-form-data-in-4.0-td14224819.html) return getContentLength()<0L; } /** * @see org.apache.http.HttpEntity#isRepeatable() */ @Override public boolean isRepeatable() { return multipartLegacy.isRepeatable(); } /** * @see org.apache.http.HttpEntity#writeTo(java.io.OutputStream) */ @Override public void writeTo(OutputStream outstream) throws IOException { multipartLegacy.writeRequest(outstream); } /** * Receiving not supported. */ @Override public void consumeContent() throws IOException { throw new UnsupportedOperationException("MultipartEntityWrapper has no support for receiving"); } /** * Receiving not supported. */ @Override public InputStream getContent() throws IOException, IllegalStateException { throw new UnsupportedOperationException("MultipartEntityWrapper has no support for receiving"); } /** * Receiving not supported. */ @Override public Header getContentEncoding() { return null; } /** * Receiving not supported. */ @Override public boolean isStreaming() { return false; } }