Search in sources :

Example 16 with StringBody

use of org.apache.http.entity.mime.content.StringBody in project perun by CESNET.

the class MUStrategy method getHttpRequest.

@Override
public HttpUriRequest getHttpRequest(String uco, int yearSince, int yearTill, PublicationSystem ps) {
    // prepare request body
    MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create();
    try {
        entityBuilder.addPart("typ", new StringBody("xml", ContentType.create("text/plain", Consts.UTF_8)));
        entityBuilder.addPart("kodovani", new StringBody(StandardCharsets.UTF_8.toString(), ContentType.create("text/plain", Consts.UTF_8)));
        entityBuilder.addPart("keyfile", new ByteArrayBody(buildRequestKeyfile(Integer.parseInt(uco), yearSince, yearTill).getBytes(), "template.xml"));
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    // prepare post request
    HttpPost post = new HttpPost(ps.getUrl());
    UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(ps.getUsername(), ps.getPassword());
    // cred, enc, proxy
    post.addHeader(BasicScheme.authenticate(credentials, StandardCharsets.UTF_8.toString(), false));
    post.setEntity(entityBuilder.build());
    return post;
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) MultipartEntityBuilder(org.apache.http.entity.mime.MultipartEntityBuilder) StringBody(org.apache.http.entity.mime.content.StringBody) ByteArrayBody(org.apache.http.entity.mime.content.ByteArrayBody) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) XPathExpressionException(javax.xml.xpath.XPathExpressionException) CabinetException(cz.metacentrum.perun.cabinet.bl.CabinetException) IOException(java.io.IOException) SAXParseException(org.xml.sax.SAXParseException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials)

Example 17 with StringBody

use of org.apache.http.entity.mime.content.StringBody in project streamsx.topology by IBMStreams.

the class StreamingAnalyticsServiceV2 method submitBuildArtifact.

/**
 * Submit the job from the built artifact.
 */
@Override
protected JsonObject submitBuildArtifact(CloseableHttpClient httpclient, JsonObject jobConfigOverlays, String submitUrl) throws IOException {
    HttpPost postArtifact = new HttpPost(submitUrl);
    postArtifact.addHeader("Authorization", getAuthorization());
    StringBody paramsBody = new StringBody(jobConfigOverlays.toString(), ContentType.APPLICATION_JSON);
    HttpEntity reqEntity = MultipartEntityBuilder.create().addPart("job_options", paramsBody).build();
    postArtifact.setEntity(reqEntity);
    JsonObject jso = StreamsRestUtils.getGsonResponse(httpclient, postArtifact);
    TRACE.info("Streaming Analytics service (" + getName() + "): submit job response: " + jso.toString());
    return jso;
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) HttpEntity(org.apache.http.HttpEntity) StringBody(org.apache.http.entity.mime.content.StringBody) JsonObject(com.google.gson.JsonObject)

Example 18 with StringBody

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

the class Utils method addPart.

/**
 * @param paramName
 * @param value
 * @param resolver
 * @throws RepositoryException
 */
static void addPart(String paramName, QValue value, NamePathResolver resolver, List<FormBodyPart> parts, List<QValue> binaries) throws RepositoryException {
    FormBodyPartBuilder builder = FormBodyPartBuilder.create().setName(paramName);
    ContentType ctype = ContentType.create(JcrValueType.contentTypeFromType(value.getType()), DEFAULT_CHARSET);
    FormBodyPart part;
    switch(value.getType()) {
        case PropertyType.BINARY:
            binaries.add(value);
            // server detects binaries based on presence of filename parameters (JCR-4154)
            part = builder.setBody(new InputStreamBody(value.getStream(), ctype, paramName)).build();
            break;
        case PropertyType.NAME:
            part = builder.setBody(new StringBody(resolver.getJCRName(value.getName()), ctype)).build();
            break;
        case PropertyType.PATH:
            part = builder.setBody(new StringBody(resolver.getJCRPath(value.getPath()), ctype)).build();
            break;
        default:
            part = builder.setBody(new StringBody(value.getString(), ctype)).build();
    }
    parts.add(part);
}
Also used : FormBodyPart(org.apache.http.entity.mime.FormBodyPart) FormBodyPartBuilder(org.apache.http.entity.mime.FormBodyPartBuilder) ContentType(org.apache.http.entity.ContentType) StringBody(org.apache.http.entity.mime.content.StringBody) InputStreamBody(org.apache.http.entity.mime.content.InputStreamBody)

Example 19 with StringBody

use of org.apache.http.entity.mime.content.StringBody in project undertow by undertow-io.

the class MultiPartTestCase method testMultiPartIndividualFileToLarge.

@Test
public void testMultiPartIndividualFileToLarge() throws IOException {
    TestHttpClient client = new TestHttpClient();
    try {
        String uri = DefaultServer.getDefaultServerURL() + "/servletContext/3";
        HttpPost post = new HttpPost(uri);
        MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
        entity.addPart("formValue", new StringBody("myValue", "text/plain", StandardCharsets.UTF_8));
        entity.addPart("file", new FileBody(new File(MultiPartTestCase.class.getResource("uploadfile.txt").getFile())));
        post.setEntity(entity);
        HttpResponse result = client.execute(post);
        final String response = HttpClientUtils.readResponse(result);
        Assert.assertEquals("EXCEPTION: class java.lang.IllegalStateException", response);
    } finally {
        client.getConnectionManager().shutdown();
    }
}
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) HttpResponse(org.apache.http.HttpResponse) File(java.io.File) TestHttpClient(io.undertow.testutils.TestHttpClient) Test(org.junit.Test)

Example 20 with StringBody

use of org.apache.http.entity.mime.content.StringBody in project undertow by undertow-io.

the class MultiPartTestCase method testMultiPartRequestUtf8CharsetInPart.

@Test
public void testMultiPartRequestUtf8CharsetInPart() throws IOException {
    TestHttpClient client = new TestHttpClient();
    try {
        String uri = DefaultServer.getDefaultServerURL() + "/servletContext/1";
        HttpPost post = new HttpPost(uri);
        MultipartEntity entity = new MultipartEntity();
        entity.addPart("formValue", new StringBody("myValue\u00E5", ContentType.create("text/plain", StandardCharsets.UTF_8)));
        post.setEntity(entity);
        HttpResponse result = client.execute(post);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        final String response = HttpClientUtils.readResponse(result);
        Assert.assertEquals("PARAMS:\r\n" + "parameter count: 1\r\n" + "parameter name count: 1\r\n" + "name: formValue\r\n" + "filename: null\r\n" + "content-type: text/plain; charset=UTF-8\r\n" + "Content-Disposition: form-data; name=\"formValue\"\r\n" + "Content-Transfer-Encoding: 8bit\r\n" + "Content-Type: text/plain; charset=UTF-8\r\n" + "size: 9\r\n" + "content: myValue\u00E5\r\n", response);
    } finally {
        client.getConnectionManager().shutdown();
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) MultipartEntity(org.apache.http.entity.mime.MultipartEntity) StringBody(org.apache.http.entity.mime.content.StringBody) HttpResponse(org.apache.http.HttpResponse) TestHttpClient(io.undertow.testutils.TestHttpClient) Test(org.junit.Test)

Aggregations

StringBody (org.apache.http.entity.mime.content.StringBody)65 HttpPost (org.apache.http.client.methods.HttpPost)44 MultipartEntity (org.apache.http.entity.mime.MultipartEntity)40 FileBody (org.apache.http.entity.mime.content.FileBody)30 HttpResponse (org.apache.http.HttpResponse)29 File (java.io.File)25 Test (org.junit.Test)25 HttpEntity (org.apache.http.HttpEntity)21 IOException (java.io.IOException)15 MultipartEntityBuilder (org.apache.http.entity.mime.MultipartEntityBuilder)15 TestHttpClient (io.undertow.testutils.TestHttpClient)14 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)8 Header (org.apache.http.Header)6 HttpClient (org.apache.http.client.HttpClient)6 FormBodyPart (org.apache.http.entity.mime.FormBodyPart)6 HashMap (java.util.HashMap)5 Map (java.util.Map)5 InputStreamBody (org.apache.http.entity.mime.content.InputStreamBody)5 BasicNameValuePair (org.apache.http.message.BasicNameValuePair)5 RequestBuilder (org.apache.sling.testing.tools.http.RequestBuilder)5