Search in sources :

Example 71 with StringBody

use of org.apache.http.entity.mime.content.StringBody in project ninja by ninjaframework.

the class NinjaTestBrowser method uploadFileWithForm.

public String uploadFileWithForm(String url, String paramName, File fileToUpload, Map<String, String> formParameters) {
    String response = null;
    try {
        httpClient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
        HttpPost post = new HttpPost(url);
        MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
        // For File parameters
        entity.addPart(paramName, new FileBody((File) fileToUpload));
        // add form parameters:
        if (formParameters != null) {
            for (Entry<String, String> parameter : formParameters.entrySet()) {
                entity.addPart(parameter.getKey(), new StringBody(parameter.getValue()));
            }
        }
        post.setEntity(entity);
        // Here we go!
        response = EntityUtils.toString(httpClient.execute(post).getEntity(), "UTF-8");
        post.releaseConnection();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    return response;
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) FileBody(org.apache.http.entity.mime.content.FileBody) MultipartEntity(org.apache.http.entity.mime.MultipartEntity) StringBody(org.apache.http.entity.mime.content.StringBody) File(java.io.File) IOException(java.io.IOException)

Example 72 with StringBody

use of org.apache.http.entity.mime.content.StringBody in project mzmine2 by mzmine.

the class LibrarySubmitTask method submitGNPS.

/**
 * Submit json library entry to GNPS webserver
 *
 * @param json
 */
private void submitGNPS(String json) {
    try {
        CloseableHttpClient httpclient = HttpClients.createDefault();
        try {
            MultipartEntity entity = new MultipartEntity();
            // ######################################################
            // NEEDED
            // user pass and json entry
            // 
            entity.addPart("username", new StringBody(USER));
            entity.addPart("password", new StringBody(PASS));
            entity.addPart("spectrum", new StringBody(json));
            // job description is not entry description
            entity.addPart("description", new StringBody(SOURCE_DESCRIPTION));
            HttpPost httppost = new HttpPost(GNPS_LIBRARY_SUBMIT_URL);
            httppost.setEntity(entity);
            log.info("Submitting GNPS library entry " + httppost.getRequestLine());
            CloseableHttpResponse response = httpclient.execute(httppost);
            try {
                writeResults("GNPS submit entry response status: " + response.getStatusLine(), Result.INFO);
                log.info("GNPS submit entry response status: " + response.getStatusLine());
                HttpEntity resEntity = response.getEntity();
                if (resEntity != null) {
                    log.info("GNPS submit entry response content length: " + resEntity.getContentLength());
                    writeResults("GNPS submit entry response content length: " + resEntity.getContentLength(), Result.SUCCED);
                    String body = IOUtils.toString(resEntity.getContent());
                    String url = "https://gnps.ucsd.edu/ProteoSAFe/status.jsp?task=" + body;
                    log.log(Level.INFO, "Submission task: " + url);
                    writeResults(url, Result.SUCCED, true);
                    EntityUtils.consume(resEntity);
                } else {
                    log.warning("Not submitted to GNPS:\n" + json);
                    writeResults("Not submitted to GNPS\n" + json, Result.ERROR);
                }
            } finally {
                response.close();
            }
        } finally {
            httpclient.close();
        }
    } catch (IOException e) {
        log.log(Level.SEVERE, "Error while submitting GNPS job", e);
        throw new MSDKRuntimeException(e);
    }
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpPost(org.apache.http.client.methods.HttpPost) HttpEntity(org.apache.http.HttpEntity) MultipartEntity(org.apache.http.entity.mime.MultipartEntity) StringBody(org.apache.http.entity.mime.content.StringBody) MSDKRuntimeException(io.github.msdk.MSDKRuntimeException) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) IOException(java.io.IOException)

Aggregations

StringBody (org.apache.http.entity.mime.content.StringBody)72 HttpPost (org.apache.http.client.methods.HttpPost)50 MultipartEntity (org.apache.http.entity.mime.MultipartEntity)42 HttpResponse (org.apache.http.HttpResponse)33 FileBody (org.apache.http.entity.mime.content.FileBody)31 File (java.io.File)26 Test (org.junit.Test)25 HttpEntity (org.apache.http.HttpEntity)24 IOException (java.io.IOException)19 MultipartEntityBuilder (org.apache.http.entity.mime.MultipartEntityBuilder)18 TestHttpClient (io.undertow.testutils.TestHttpClient)14 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)8 InputStreamReader (java.io.InputStreamReader)7 HttpClient (org.apache.http.client.HttpClient)7 InputStreamBody (org.apache.http.entity.mime.content.InputStreamBody)7 Header (org.apache.http.Header)6 ClientProtocolException (org.apache.http.client.ClientProtocolException)6 InputStream (java.io.InputStream)5 HashMap (java.util.HashMap)5 Map (java.util.Map)5