Example usage for org.apache.commons.httpclient.methods.multipart StringPart setContentType

List of usage examples for org.apache.commons.httpclient.methods.multipart StringPart setContentType

Introduction

In this page you can find the example usage for org.apache.commons.httpclient.methods.multipart StringPart setContentType.

Prototype

public void setContentType(String paramString) 

Source Link

Usage

From source file:edu.tsinghua.lumaqq.test.CustomHeadUploader.java

/**
 * /*from   www  . ja  va 2s  .  c om*/
 * 
 * @param filename
 */
public void upload(String filename, QQUser user) {
    HttpClient client = new HttpClient();
    HostConfiguration conf = new HostConfiguration();
    conf.setHost(QQ.QQ_SERVER_UPLOAD_CUSTOM_HEAD);
    client.setHostConfiguration(conf);
    PostMethod method = new PostMethod("/cgi-bin/cface/upload");
    method.addRequestHeader("Accept", "*/*");
    method.addRequestHeader("Pragma", "no-cache");

    StringPart uid = new StringPart("clientuin", String.valueOf(user.getQQ()));
    uid.setContentType(null);
    uid.setTransferEncoding(null);

    //StringPart clientkey = new StringPart("clientkey", "0D649E66B0C1DBBDB522CE9C846754EF6AFA10BBF1A48A532DF6369BBCEF6EE7");
    //StringPart clientkey = new StringPart("clientkey", "3285284145CC19EC0FFB3B25E4F6817FF3818B0E72F1C4E017D9238053BA2040");
    StringPart clientkey = new StringPart("clientkey",
            "2FEEBE858DAEDFE6352870E32E5297ABBFC8C87125F198A5232FA7ADA9EADE67");
    //StringPart clientkey = new StringPart("clientkey", "8FD643A7913C785AB612F126C6CD68A253F459B90EBCFD9375053C159418AF16");
    //      StringPart clientkey = new StringPart("clientkey", Util.convertByteToHexStringWithoutSpace(user.getClientKey()));
    clientkey.setContentType(null);
    clientkey.setTransferEncoding(null);

    //StringPart sign = new StringPart("sign", "2D139466226299A73F8BCDA7EE9AB157E8D9DA0BB38B6F4942A1658A00BD4C1FEE415838810E5AEF40B90E2AA384A875");
    //StringPart sign = new StringPart("sign", "50F479417088F26EFC75B9BCCF945F35346188BB9ADD3BDF82098C9881DA086E3B28D56726D6CB2331909B62459E1E62");
    //StringPart sign = new StringPart("sign", "31A69198C19A7C4BFB60DCE352FE2CC92C9D27E7C7FEADE1CBAAFD988906981ECC0DD1782CBAE88A2B716F84F9E285AA");
    StringPart sign = new StringPart("sign",
            "68FFB636DE63D164D4072D7581213C77EC7B425DDFEB155428768E1E409935AA688AC88910A74C5D2D94D5EF2A3D1764");
    sign.setContentType(null);
    sign.setTransferEncoding(null);

    FilePart file;
    try {
        file = new FilePart("customfacefile", filename, new File(filename));
    } catch (FileNotFoundException e) {
        return;
    }

    Part[] parts = new Part[] { uid, clientkey, sign, file };
    MultipartRequestEntity entity = new MultipartRequestEntity(parts, method.getParams());

    method.setRequestEntity(entity);
    try {
        client.executeMethod(method);
        if (method.getStatusCode() == HttpStatus.SC_OK) {
            Header header = method.getResponseHeader("CFace-Msg");
            System.out.println(header.getValue());
            header = method.getResponseHeader("CFace-RetCode");
            System.out.println(header.getValue());
        }
    } catch (HttpException e) {
        return;
    } catch (IOException e) {
        return;
    } finally {
        method.releaseConnection();
    }
}

From source file:com.benfante.jslideshare.SlideShareConnectorImpl.java

private StringPart createStringPart(String name, String value) {
    StringPart stringPart = new StringPart(name, value);
    stringPart.setContentType(null);
    stringPart.setTransferEncoding(null);
    stringPart.setCharSet("UTF-8");
    return stringPart;
}

From source file:com.intuit.tank.http.multipart.MultiPartRequest.java

protected List<Part> buildParts() {
    List<Part> parts = new ArrayList<Part>();
    for (PartHolder h : parameters) {
        if (h.getFileName() == null) {
            StringPart stringPart = new StringPart(h.getPartName(), new String(h.getBodyAsString()));
            if (h.isContentTypeSet()) {
                stringPart.setContentType(h.getContentType());
            }//from  www  .  ja  va2s. c o m
            parts.add(stringPart);
        } else {
            PartSource partSource = new ByteArrayPartSource(h.getFileName(), h.getBody());
            FilePart p = new FilePart(h.getPartName(), partSource);
            if (h.isContentTypeSet()) {
                p.setContentType(h.getContentType());
            }
            parts.add(p);
        }
    }
    return parts;
}

From source file:games.strategy.triplea.pbem.AxisAndAlliesForumPoster.java

/**
 * Utility method for creating string parts, since we need to remove transferEncoding and content type to behave like a browser
 * // w ww.ja  v a2s  .c  o  m
 * @param name
 *            the form field name
 * @param value
 *            the for field value
 * @return return the created StringPart
 */
private StringPart createStringPart(final String name, final String value) {
    final StringPart stringPart = new StringPart(name, value);
    stringPart.setTransferEncoding(null);
    stringPart.setContentType(null);
    return stringPart;
}

From source file:com.intuit.tank.httpclient3.TankHttpClient3.java

private List<Part> buildParts(BaseRequest request) {
    List<Part> parts = new ArrayList<Part>();
    for (PartHolder h : TankHttpUtil.getPartsFromBody(request)) {
        if (h.getFileName() == null) {
            StringPart stringPart = new StringPart(h.getPartName(), new String(h.getBodyAsString()));
            if (h.isContentTypeSet()) {
                stringPart.setContentType(h.getContentType());
            }/*from   w  ww.j  a va 2s  .c o  m*/
            parts.add(stringPart);
        } else {
            PartSource partSource = new ByteArrayPartSource(h.getFileName(), h.getBody());
            FilePart p = new FilePart(h.getPartName(), partSource);
            if (h.isContentTypeSet()) {
                p.setContentType(h.getContentType());
            }
            parts.add(p);
        }
    }
    return parts;
}

From source file:com.celamanzi.liferay.portlets.rails286.OnlineClient.java

private StringPart createStringPart(NameValuePair pair) {
    StringPart part = new StringPart(pair.getName(), pair.getValue());

    //HACK: When content type is null, Rack will interpretate as string param, 
    //otherwise this will be treated like a file.
    part.setContentType(null);

    part.setCharSet("UTF-8");
    return part;//from  ww w  .  j  a v  a2 s. c o m
}

From source file:com.tribune.uiautomation.testscripts.Photo.java

private void setParameters(EntityEnclosingMethod method, String newNamespace, String newSlug)
        throws FileNotFoundException {
    StringPart namespacePart = newNamespace == null ? new StringPart(NAMESPACE_PARAM, namespace)
            : new StringPart(NAMESPACE_PARAM, newNamespace);
    StringPart slugPart = newSlug == null ? new StringPart(SLUG_PARAM, slug)
            : new StringPart(SLUG_PARAM, newSlug);
    // next two lines annoying work around for this bug:
    // https://github.com/rack/rack/issues/186
    namespacePart.setContentType(null);
    slugPart.setContentType(null);/*from   w w  w.  java  2  s .  com*/

    if (file != null) {
        File f = new File(file);
        Part[] parts = { namespacePart, slugPart,
                new FilePart(FILE_PARAM, f, IMAGE_CONTENT_TYPE, FilePart.DEFAULT_CHARSET) };
        method.setRequestEntity(new MultipartRequestEntity(parts, method.getParams()));
    } else {
        Part[] parts = { namespacePart, slugPart };
        method.setRequestEntity(new MultipartRequestEntity(parts, method.getParams()));
    }
}

From source file:com.bugclipse.fogbugz.api.client.FogBugzClient.java

private PostMethod postInternal(String formUrl, Map<String, String> changed, final ITaskAttachment attach)
        throws FogBugzClientException, HttpException, IOException, MarshalException, ValidationException {

    WebClientUtil.setupHttpClient(httpClient, proxy, formUrl, null, null);
    if (!authenticated && hasAuthenticationCredentials()) {
        authenticate();//  w w  w  . ja v  a2 s.co  m
    }

    String requestUrl = WebClientUtil.getRequestPath(formUrl + "&token=" + token);

    ArrayList<Part> parts = new ArrayList<Part>();
    if (attach != null) {
        requestUrl += "&nFileCount=1";
        FilePart part = new FilePart("File1", new PartSource() {

            public InputStream createInputStream() throws IOException {
                return attach.createInputStream();
            }

            public String getFileName() {
                return attach.getFilename();
            }

            public long getLength() {
                return attach.getLength();
            }

        });
        part.setTransferEncoding(null);
        parts.add(part);
        parts.add(new StringPart("Content-Type", attach.getContentType()));
    }
    PostMethod postMethod = new PostMethod(requestUrl);
    // postMethod.setRequestHeader("Content-Type",
    // "application/x-www-form-urlencoded; charset="
    // + characterEncoding);

    // postMethod.setRequestBody(formData);
    postMethod.setDoAuthentication(true);

    for (String key : changed.keySet()) {
        StringPart p = new StringPart(key, changed.get(key));
        p.setTransferEncoding(null);
        p.setContentType(null);
        parts.add(p);
    }
    postMethod.setRequestEntity(new MultipartRequestEntity(parts.toArray(new Part[0]), postMethod.getParams()));

    int status = httpClient.executeMethod(postMethod);
    if (status == HttpStatus.SC_OK) {
        return postMethod;
    } else {
        postMethod.getResponseBody();
        postMethod.releaseConnection();
        throw new IOException(
                "Communication error occurred during upload. \n\n" + HttpStatus.getStatusText(status));
    }
}

From source file:com.bugclipse.fogbugz.api.client.FogBugzClient.java

private PostMethod postInternal(String formUrl, Map<String, String> changed, final ITaskAttachment attach,
        IProgressMonitor monitor)/*from w w w  .j  a v a2s  .  co m*/
        throws FogBugzClientException, HttpException, IOException, MarshalException, ValidationException {

    WebClientUtil.setupHttpClient(httpClient, proxy, formUrl, null, null);
    if (!authenticated && hasAuthenticationCredentials()) {
        monitor.subTask("Authenticating request");
        authenticate();
        if (checkMonitor(monitor))
            return null;
    }

    String requestUrl = WebClientUtil.getRequestPath(formUrl + "&token=" + token);

    ArrayList<Part> parts = new ArrayList<Part>();
    if (attach != null) {
        requestUrl += "&nFileCount=1";
        FilePart part = new FilePart("File1", new PartSource() {

            public InputStream createInputStream() throws IOException {
                return attach.createInputStream();
            }

            public String getFileName() {
                return attach.getFilename();
            }

            public long getLength() {
                return attach.getLength();
            }

        });
        part.setTransferEncoding(null);
        parts.add(part);
        parts.add(new StringPart("Content-Type", attach.getContentType()));
    }
    PostMethod postMethod = new PostMethod(requestUrl);
    // postMethod.setRequestHeader("Content-Type",
    // "application/x-www-form-urlencoded; charset="
    // + characterEncoding);

    // postMethod.setRequestBody(formData);
    postMethod.setDoAuthentication(true);

    for (String key : changed.keySet()) {
        StringPart p = new StringPart(key, changed.get(key));
        p.setTransferEncoding(null);
        p.setContentType(null);
        parts.add(p);
    }
    postMethod.setRequestEntity(new MultipartRequestEntity(parts.toArray(new Part[0]), postMethod.getParams()));

    monitor.subTask("Sending request");
    int status = httpClient.executeMethod(postMethod);
    if (status == HttpStatus.SC_OK) {
        return postMethod;
    } else {
        postMethod.getResponseBody();
        postMethod.releaseConnection();
        throw new IOException(
                "Communication error occurred during upload. \n\n" + HttpStatus.getStatusText(status));
    }
}

From source file:JiraWebClient.java

public void attachFile(final JiraIssue issue, final String comment, final FilePart filePart,
        final String contentType, IProgressMonitor monitor) throws JiraException {
    doInSession(monitor, new JiraWebSessionCallback() {
        @Override// w w  w  .j  ava  2s .  co m
        public void run(JiraClient server, String baseUrl, IProgressMonitor monitor) throws JiraException {
            StringBuilder attachFileURLBuffer = new StringBuilder(baseUrl);
            attachFileURLBuffer.append("/secure/AttachFile.jspa"); //$NON-NLS-1$

            PostMethod post = new PostMethod(attachFileURLBuffer.toString());
            prepareSecurityToken(post);

            List<PartBase> parts = new ArrayList<PartBase>();

            StringPart idPart = new StringPart("id", issue.getId()); //$NON-NLS-1$
            StringPart commentLevelPart = new StringPart("commentLevel", ""); //$NON-NLS-1$ //$NON-NLS-2$

            // The transfer encodings have to be removed for some reason
            // There is no need to send the content types for the strings,
            // as they should be in the correct format
            idPart.setTransferEncoding(null);
            idPart.setContentType(null);

            if (comment != null) {
                StringPart commentPart = new StringPart("comment", comment); //$NON-NLS-1$
                commentPart.setTransferEncoding(null);
                commentPart.setContentType(null);
                commentPart.setCharSet(client.getCharacterEncoding(monitor));
                parts.add(commentPart);
            }

            commentLevelPart.setTransferEncoding(null);
            commentLevelPart.setContentType(null);

            filePart.setTransferEncoding(null);
            if (contentType != null) {
                filePart.setContentType(contentType);
            }
            parts.add(filePart);
            parts.add(idPart);
            parts.add(commentLevelPart);

            post.setRequestEntity(
                    new MultipartRequestEntity(parts.toArray(new Part[parts.size()]), post.getParams()));

            try {
                execute(post);
                if (!expectRedirect(post, "/secure/ManageAttachments.jspa?id=" + issue.getId())) { //$NON-NLS-1$
                    handleErrorMessage(post);
                }
            } finally {
                post.releaseConnection();
            }
        }
    });
}