List of usage examples for org.apache.http.entity.mime.content ByteArrayBody ByteArrayBody
public ByteArrayBody(final byte[] data, final String mimeType, final String filename)
From source file:com.basistech.rosette.api.HttpRosetteAPI.java
private void setupMultipartRequest(final Request request, final ObjectWriter finalWriter, HttpPost post) throws IOException { MultipartEntityBuilder builder = MultipartEntityBuilder.create(); builder.setMimeSubtype("mixed"); builder.setMode(HttpMultipartMode.STRICT); FormBodyPartBuilder partBuilder = FormBodyPartBuilder.create("request", // Make sure we're not mislead by someone who puts a charset into the mime type. new AbstractContentBody(ContentType.parse(ContentType.APPLICATION_JSON.getMimeType())) { @Override// www. j a v a2s . co m public String getFilename() { return null; } @Override public void writeTo(OutputStream out) throws IOException { finalWriter.writeValue(out, request); } @Override public String getTransferEncoding() { return MIME.ENC_BINARY; } @Override public long getContentLength() { return -1; } }); // Either one of 'name=' or 'Content-ID' would be enough. partBuilder.setField(MIME.CONTENT_DISPOSITION, "inline;name=\"request\""); partBuilder.setField("Content-ID", "request"); builder.addPart(partBuilder.build()); AbstractContentBody insBody; if (request instanceof DocumentRequest) { DocumentRequest docReq = (DocumentRequest) request; insBody = new InputStreamBody(docReq.getContentBytes(), ContentType.parse(docReq.getContentType())); } else if (request instanceof AdmRequest) { //TODO: smile? AdmRequest admReq = (AdmRequest) request; ObjectWriter writer = mapper.writer().without(JsonGenerator.Feature.AUTO_CLOSE_TARGET); byte[] json = writer.writeValueAsBytes(admReq.getText()); insBody = new ByteArrayBody(json, ContentType.parse(AdmRequest.ADM_CONTENT_TYPE), null); } else { throw new UnsupportedOperationException("Unsupported request type for multipart processing"); } partBuilder = FormBodyPartBuilder.create("content", insBody); partBuilder.setField(MIME.CONTENT_DISPOSITION, "inline;name=\"content\""); partBuilder.setField("Content-ID", "content"); builder.addPart(partBuilder.build()); builder.setCharset(StandardCharsets.UTF_8); HttpEntity entity = builder.build(); post.setEntity(entity); }
From source file:org.ow2.proactive_grid_cloud_portal.scheduler.server.SchedulerServiceImpl.java
private MultipartEntity createLoginPasswordSSHKeyMultipart(String login, String pass, String ssh) throws UnsupportedEncodingException { MultipartEntity entity = new MultipartEntity(); entity.addPart("username", new StringBody(login)); entity.addPart("password", new StringBody(pass)); if (ssh != null && !ssh.isEmpty()) { entity.addPart("sshKey", new ByteArrayBody(ssh.getBytes(), MediaType.APPLICATION_OCTET_STREAM, null)); }/*from w w w . jav a2 s . c om*/ return entity; }
From source file:com.bigdata.rdf.sail.webapp.client.RemoteRepository.java
/** * Perform an ACID update/*from w w w .j a va 2 s.co m*/ * <p> * There are two different patterns which are supported: * <dl> * <dt>UPDATE (DELETE statements selected by a QUERY plus INSERT statements * from Request Body using PUT)</dt> * <dd>Where query is a CONSTRUCT or DESCRIBE query. <br> * Note: The QUERY + DELETE operation is ACID. <br> * Note: You MAY specify a CONSTRUCT query with an empty WHERE clause in * order to specify a set of statements to be removed without reference to * statements already existing in the database.</dd> * <dt>UPDATE (POST with Multi-Part Request Body)</dt> * <dd>You can specify two sets of serialized statements - one to be removed * and one to be added..</dd> * </dl> * * @param remove * The RDF data to be removed (either a collection of statements * or a CONSTRUCT or DESCRIBE QUERY identifying the data to be * deleted). * @param add * The RDF data to be added (must be a collection of statements). * * @return The mutation count. * @see http * ://wiki.blazegraph.com/wiki/index.php/NanoSparqlServer#UPDATE_.28D * ELETE_.2B_INSERT.29 */ public long update(final RemoveOp remove, final AddOp add, final UUID uuid) throws Exception { if (remove == null) throw new IllegalArgumentException(); if (add == null) throw new IllegalArgumentException(); remove.prepareForWire(); add.prepareForWire(); final ConnectOptions opts = mgr.newUpdateConnectOptions(sparqlEndpointURL, uuid, tx); if (remove.format != null) { // Code path when caller specifies data to be removed. opts.method = "POST"; opts.addRequestParam("update"); // Note: Multi-part MIME request entity. final MultipartEntity entity = new MultipartEntity(); // The data to be removed. entity.addPart(new FormBodyPart("remove", new ByteArrayBody(remove.data, remove.format.getDefaultMIMEType(), "remove"))); // The data to be added. entity.addPart( new FormBodyPart("add", new ByteArrayBody(add.data, add.format.getDefaultMIMEType(), "add"))); // The multi-part request entity. opts.entity = entity; } else { // Code path when caller specifies CONSTRUCT or DESCRIBE query // identifying the data to be removed. opts.method = "PUT"; // QUERY specifying the data to be removed. opts.addRequestParam("query", remove.query); // The data to be added. final ByteArrayEntity entity = new ByteArrayEntity(add.data); entity.setContentType(add.format.getDefaultMIMEType()); opts.entity = entity; } if (add.context != null) { // set the default context for insert. opts.addRequestParam("context-uri-insert", toStrings(add.context)); } if (remove.context != null) { // set the default context for delete. opts.addRequestParam("context-uri-delete", toStrings(remove.context)); } opts.setAcceptHeader(ConnectOptions.MIME_APPLICATION_XML); JettyResponseListener response = null; boolean ok = false; try { checkResponseCode(response = doConnect(opts)); final MutationResult result = mutationResults(response); ok = true; return result.mutationCount; } finally { if (response != null) { // Abort the http response handling. response.abort(); if (!ok) { try { /* * POST back to the server to cancel the request in case * it is still running on the server. */ cancel(uuid); } catch (Exception ex) { log.warn(ex); } } } } }
From source file:com.todoroo.astrid.actfm.sync.ActFmSyncService.java
public static MultipartEntity buildPictureData(Bitmap bitmap) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); if (bitmap.getWidth() > 512 || bitmap.getHeight() > 512) { float scale = Math.min(512f / bitmap.getWidth(), 512f / bitmap.getHeight()); bitmap = Bitmap.createScaledBitmap(bitmap, (int) (scale * bitmap.getWidth()), (int) (scale * bitmap.getHeight()), false); }/* w w w . j a va 2s.com*/ bitmap.compress(Bitmap.CompressFormat.JPEG, 50, baos); byte[] bytes = baos.toByteArray(); MultipartEntity data = new MultipartEntity(); data.addPart("picture", new ByteArrayBody(bytes, "image/jpg", "image.jpg")); return data; }
From source file:org.ow2.proactive_grid_cloud_portal.rm.server.RMServiceImpl.java
private MultipartEntity createLoginPasswordSSHKeyMultipart(String login, String pass, String ssh) throws UnsupportedEncodingException { MultipartEntity entity = new MultipartEntity(); entity.addPart("username", new StringBody(login)); entity.addPart("password", new StringBody(pass)); if (ssh != null && !ssh.isEmpty()) { entity.addPart("sshKey", new ByteArrayBody(ssh.getBytes(), MediaType.APPLICATION_OCTET_STREAM, null)); }//from w w w . ja v a 2 s . c o m return entity; }