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);
}
}
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);
}
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();
}
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;
}
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);
}
Aggregations