Search in sources :

Example 66 with ByteArrayEntity

use of org.apache.http.entity.ByteArrayEntity in project opennms by OpenNMS.

the class NCSNorthbounder method createEntity.

private HttpEntity createEntity(List<NorthboundAlarm> alarms) {
    ByteArrayOutputStream out = null;
    OutputStreamWriter writer = null;
    try {
        out = new ByteArrayOutputStream();
        writer = new OutputStreamWriter(out);
        // marshall the output
        JaxbUtils.marshal(toServiceAlarms(alarms), writer);
        // verify its matches the expected results
        byte[] utf8 = out.toByteArray();
        ByteArrayEntity entity = new ByteArrayEntity(utf8);
        entity.setContentType("application/xml");
        return entity;
    } catch (Exception e) {
        throw new NorthbounderException("failed to convert alarms to xml", e);
    } finally {
        IOUtils.closeQuietly(writer);
        IOUtils.closeQuietly(out);
    }
}
Also used : ByteArrayEntity(org.apache.http.entity.ByteArrayEntity) NorthbounderException(org.opennms.netmgt.alarmd.api.NorthbounderException) OutputStreamWriter(java.io.OutputStreamWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ClientProtocolException(org.apache.http.client.ClientProtocolException) GeneralSecurityException(java.security.GeneralSecurityException) IOException(java.io.IOException) NorthbounderException(org.opennms.netmgt.alarmd.api.NorthbounderException)

Example 67 with ByteArrayEntity

use of org.apache.http.entity.ByteArrayEntity in project Activiti by Activiti.

the class MuleSendActivitiBehavior method execute.

public void execute(ActivityExecution execution) throws Exception {
    String endpointUrlValue = this.getStringFromField(this.endpointUrl, execution);
    String languageValue = this.getStringFromField(this.language, execution);
    String payloadExpressionValue = this.getStringFromField(this.payloadExpression, execution);
    String resultVariableValue = this.getStringFromField(this.resultVariable, execution);
    String usernameValue = this.getStringFromField(this.username, execution);
    String passwordValue = this.getStringFromField(this.password, execution);
    ScriptingEngines scriptingEngines = Context.getProcessEngineConfiguration().getScriptingEngines();
    Object payload = scriptingEngines.evaluate(payloadExpressionValue, languageValue, execution);
    if (endpointUrlValue.startsWith("vm:")) {
        LocalMuleClient client = this.getMuleContext().getClient();
        MuleMessage message = new DefaultMuleMessage(payload, this.getMuleContext());
        MuleMessage resultMessage = client.send(endpointUrlValue, message);
        Object result = resultMessage.getPayload();
        if (resultVariableValue != null) {
            execution.setVariable(resultVariableValue, result);
        }
    } else {
        HttpClientBuilder clientBuilder = HttpClientBuilder.create();
        if (usernameValue != null && passwordValue != null) {
            CredentialsProvider provider = new BasicCredentialsProvider();
            UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(usernameValue, passwordValue);
            provider.setCredentials(new AuthScope("localhost", -1, "mule-realm"), credentials);
            clientBuilder.setDefaultCredentialsProvider(provider);
        }
        HttpClient client = clientBuilder.build();
        HttpPost request = new HttpPost(endpointUrlValue);
        try {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(baos);
            oos.writeObject(payload);
            oos.flush();
            oos.close();
            request.setEntity(new ByteArrayEntity(baos.toByteArray()));
        } catch (Exception e) {
            throw new ActivitiException("Error setting message payload", e);
        }
        byte[] responseBytes = null;
        try {
            // execute the POST request
            HttpResponse response = client.execute(request);
            responseBytes = IOUtils.toByteArray(response.getEntity().getContent());
        } finally {
            // release any connection resources used by the method
            request.releaseConnection();
        }
        if (responseBytes != null) {
            try {
                ByteArrayInputStream in = new ByteArrayInputStream(responseBytes);
                ObjectInputStream is = new ObjectInputStream(in);
                Object result = is.readObject();
                if (resultVariableValue != null) {
                    execution.setVariable(resultVariableValue, result);
                }
            } catch (Exception e) {
                throw new ActivitiException("Failed to read response value", e);
            }
        }
    }
    this.leave(execution);
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) ActivitiException(org.activiti.engine.ActivitiException) ScriptingEngines(org.activiti.engine.impl.scripting.ScriptingEngines) HttpResponse(org.apache.http.HttpResponse) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) ActivitiException(org.activiti.engine.ActivitiException) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) ByteArrayEntity(org.apache.http.entity.ByteArrayEntity) MuleMessage(org.mule.api.MuleMessage) DefaultMuleMessage(org.mule.DefaultMuleMessage) ByteArrayInputStream(java.io.ByteArrayInputStream) HttpClient(org.apache.http.client.HttpClient) AuthScope(org.apache.http.auth.AuthScope) LocalMuleClient(org.mule.api.client.LocalMuleClient) DefaultMuleMessage(org.mule.DefaultMuleMessage) ObjectInputStream(java.io.ObjectInputStream)

Example 68 with ByteArrayEntity

use of org.apache.http.entity.ByteArrayEntity in project asterixdb by apache.

the class ApplicationDeploymentAPIIntegrationTest method deployApplicationFile.

protected void deployApplicationFile(int dataSize, String fileName) throws URISyntaxException, IOException {
    final String deployid = "testApp";
    String path = "/applications/" + deployid + "&" + fileName;
    URI uri = uri(path);
    byte[] data = new byte[dataSize];
    for (int i = 0; i < data.length; ++i) {
        data[i] = (byte) i;
    }
    HttpClient client = HttpClients.createMinimal();
    // Put the data
    HttpPut put = new HttpPut(uri);
    HttpEntity entity = new ByteArrayEntity(data, ContentType.APPLICATION_OCTET_STREAM);
    put.setEntity(entity);
    client.execute(put);
    // Get it back
    HttpGet get = new HttpGet(uri);
    HttpResponse response = client.execute(get);
    HttpEntity respEntity = response.getEntity();
    Header contentType = respEntity.getContentType();
    // compare results
    Assert.assertEquals(ContentType.APPLICATION_OCTET_STREAM.getMimeType(), contentType.getValue());
    InputStream is = respEntity.getContent();
    for (int i = 0; i < dataSize; ++i) {
        Assert.assertEquals(data[i], (byte) is.read());
    }
    Assert.assertEquals(-1, is.read());
    is.close();
}
Also used : HttpEntity(org.apache.http.HttpEntity) ByteArrayEntity(org.apache.http.entity.ByteArrayEntity) Header(org.apache.http.Header) InputStream(java.io.InputStream) HttpClient(org.apache.http.client.HttpClient) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) URI(java.net.URI) HttpPut(org.apache.http.client.methods.HttpPut)

Example 69 with ByteArrayEntity

use of org.apache.http.entity.ByteArrayEntity in project platform_external_apache-http by android.

the class AndroidHttpClient method getCompressedEntity.

/**
     * Compress data to send to server.
     * Creates a Http Entity holding the gzipped data.
     * The data will not be compressed if it is too short.
     * @param data The bytes to compress
     * @return Entity holding the data
     */
public static AbstractHttpEntity getCompressedEntity(byte[] data, ContentResolver resolver) throws IOException {
    AbstractHttpEntity entity;
    if (data.length < getMinGzipSize(resolver)) {
        entity = new ByteArrayEntity(data);
    } else {
        ByteArrayOutputStream arr = new ByteArrayOutputStream();
        OutputStream zipper = new GZIPOutputStream(arr);
        zipper.write(data);
        zipper.close();
        entity = new ByteArrayEntity(arr.toByteArray());
        entity.setContentEncoding("gzip");
    }
    return entity;
}
Also used : ByteArrayEntity(org.apache.http.entity.ByteArrayEntity) GZIPOutputStream(java.util.zip.GZIPOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) AbstractHttpEntity(org.apache.http.entity.AbstractHttpEntity)

Example 70 with ByteArrayEntity

use of org.apache.http.entity.ByteArrayEntity in project platform_external_apache-http by android.

the class HttpService method handleException.

protected void handleException(final HttpException ex, final HttpResponse response) {
    if (ex instanceof MethodNotSupportedException) {
        response.setStatusCode(HttpStatus.SC_NOT_IMPLEMENTED);
    } else if (ex instanceof UnsupportedHttpVersionException) {
        response.setStatusCode(HttpStatus.SC_HTTP_VERSION_NOT_SUPPORTED);
    } else if (ex instanceof ProtocolException) {
        response.setStatusCode(HttpStatus.SC_BAD_REQUEST);
    } else {
        response.setStatusCode(HttpStatus.SC_INTERNAL_SERVER_ERROR);
    }
    byte[] msg = EncodingUtils.getAsciiBytes(ex.getMessage());
    ByteArrayEntity entity = new ByteArrayEntity(msg);
    entity.setContentType("text/plain; charset=US-ASCII");
    response.setEntity(entity);
}
Also used : ProtocolException(org.apache.http.ProtocolException) ByteArrayEntity(org.apache.http.entity.ByteArrayEntity) MethodNotSupportedException(org.apache.http.MethodNotSupportedException) UnsupportedHttpVersionException(org.apache.http.UnsupportedHttpVersionException)

Aggregations

ByteArrayEntity (org.apache.http.entity.ByteArrayEntity)78 HttpEntity (org.apache.http.HttpEntity)28 HttpPost (org.apache.http.client.methods.HttpPost)24 HttpResponse (org.apache.http.HttpResponse)20 IOException (java.io.IOException)17 ByteArrayOutputStream (java.io.ByteArrayOutputStream)16 JSONObject (org.json.JSONObject)11 Test (org.junit.Test)10 HttpClient (org.apache.http.client.HttpClient)9 InputStream (java.io.InputStream)7 JSONArray (org.json.JSONArray)7 URISyntaxException (java.net.URISyntaxException)5 Header (org.apache.http.Header)5 AbstractHttpEntity (org.apache.http.entity.AbstractHttpEntity)5 ContentType (org.apache.http.entity.ContentType)5 BytesRef (org.apache.lucene.util.BytesRef)5 DocWriteRequest (org.elasticsearch.action.DocWriteRequest)5 BulkRequest (org.elasticsearch.action.bulk.BulkRequest)5 DeleteRequest (org.elasticsearch.action.delete.DeleteRequest)5 GetRequest (org.elasticsearch.action.get.GetRequest)5