List of usage examples for org.apache.http.entity.mime MultipartEntityBuilder addPart
public MultipartEntityBuilder addPart(final String name, final ContentBody contentBody)
From source file:org.wso2.ml.client.MLClient.java
public CloseableHttpResponse createdDataSet(JSONObject datasetConf) throws IOException { CloseableHttpClient httpClient = HttpClients.createDefault(); HttpPost httpPost = new HttpPost(mlHost + "/api/datasets/"); httpPost.setHeader(MLConstants.AUTHORIZATION_HEADER, getBasicAuthKey()); MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create(); multipartEntityBuilder.addPart("description", new StringBody(datasetConf.get("description").toString(), ContentType.TEXT_PLAIN)); multipartEntityBuilder.addPart("sourceType", new StringBody(datasetConf.get("sourceType").toString(), ContentType.TEXT_PLAIN)); multipartEntityBuilder.addPart("destination", new StringBody(datasetConf.get("destination").toString(), ContentType.TEXT_PLAIN)); multipartEntityBuilder.addPart("dataFormat", new StringBody(datasetConf.get("dataFormat").toString(), ContentType.TEXT_PLAIN)); multipartEntityBuilder.addPart("containsHeader", new StringBody(datasetConf.get("containsHeader").toString(), ContentType.TEXT_PLAIN)); multipartEntityBuilder.addPart("datasetName", new StringBody(datasetConf.get("datasetName").toString(), ContentType.TEXT_PLAIN)); multipartEntityBuilder.addPart("version", new StringBody(datasetConf.get("version").toString(), ContentType.TEXT_PLAIN)); File file = new File(mlDatasetPath); multipartEntityBuilder.addBinaryBody("file", file, ContentType.APPLICATION_OCTET_STREAM, datasetConf.get("file").toString()); httpPost.setEntity(multipartEntityBuilder.build()); return httpClient.execute(httpPost); }
From source file:com.diversityarrays.dalclient.httpimpl.DalHttpFactoryImpl.java
@Override public DalRequest createForUpload(String url, List<Pair<String, String>> pairs, String rand_num, String namesInOrder, String signature, File fileForUpload) { MultipartEntityBuilder builder = MultipartEntityBuilder.create(); for (Pair<String, String> pair : pairs) { builder.addTextBody(pair.a, pair.b); }//from www . j ava 2 s .com builder.addPart("uploadfile", new FileBody(fileForUpload)).addTextBody("rand_num", rand_num) .addTextBody("url", url); HttpEntity entity = builder.addTextBody("param_order", namesInOrder).addTextBody("signature", signature) .build(); HttpPost post = new HttpPost(url); post.setEntity(entity); return new DalRequestImpl(post); }
From source file:ch.ralscha.extdirectspring_itest.FileUploadControllerTest.java
@Test public void testUpload() throws IOException { CloseableHttpClient client = HttpClientBuilder.create().build(); InputStream is = null;/*from www . j a va 2 s. c o m*/ CloseableHttpResponse response = null; try { HttpPost post = new HttpPost("http://localhost:9998/controller/router"); is = getClass().getResourceAsStream("/UploadTestFile.txt"); MultipartEntityBuilder builder = MultipartEntityBuilder.create(); ContentBody cbFile = new InputStreamBody(is, ContentType.create("text/plain"), "UploadTestFile.txt"); builder.addPart("fileUpload", cbFile); builder.addPart("extTID", new StringBody("2", ContentType.DEFAULT_TEXT)); builder.addPart("extAction", new StringBody("fileUploadController", ContentType.DEFAULT_TEXT)); builder.addPart("extMethod", new StringBody("uploadTest", ContentType.DEFAULT_TEXT)); builder.addPart("extType", new StringBody("rpc", ContentType.DEFAULT_TEXT)); builder.addPart("extUpload", new StringBody("true", ContentType.DEFAULT_TEXT)); builder.addPart("name", new StringBody("Jim", ContentType.create("text/plain", Charset.forName("UTF-8")))); builder.addPart("firstName", new StringBody("Ralph", ContentType.DEFAULT_TEXT)); builder.addPart("age", new StringBody("25", ContentType.DEFAULT_TEXT)); builder.addPart("email", new StringBody("test@test.ch", ContentType.DEFAULT_TEXT)); post.setEntity(builder.build()); response = client.execute(post); HttpEntity resEntity = response.getEntity(); assertThat(resEntity).isNotNull(); String responseString = EntityUtils.toString(resEntity); String prefix = "<html><body><textarea>"; String postfix = "</textarea></body></html>"; assertThat(responseString).startsWith(prefix); assertThat(responseString).endsWith(postfix); String json = responseString.substring(prefix.length(), responseString.length() - postfix.length()); ObjectMapper mapper = new ObjectMapper(); Map<String, Object> rootAsMap = mapper.readValue(json, Map.class); assertThat(rootAsMap).hasSize(5); assertThat(rootAsMap.get("method")).isEqualTo("uploadTest"); assertThat(rootAsMap.get("type")).isEqualTo("rpc"); assertThat(rootAsMap.get("action")).isEqualTo("fileUploadController"); assertThat(rootAsMap.get("tid")).isEqualTo(2); @SuppressWarnings("unchecked") Map<String, Object> result = (Map<String, Object>) rootAsMap.get("result"); assertThat(result).hasSize(7); assertThat(result.get("name")).isEqualTo("Jim"); assertThat(result.get("firstName")).isEqualTo("Ralph"); assertThat(result.get("age")).isEqualTo(25); assertThat(result.get("email")).isEqualTo("test@test.ch"); assertThat(result.get("fileName")).isEqualTo("UploadTestFile.txt"); assertThat(result.get("fileContents")).isEqualTo("contents of upload file"); assertThat(result.get("success")).isEqualTo(Boolean.TRUE); EntityUtils.consume(resEntity); } finally { IOUtils.closeQuietly(response); IOUtils.closeQuietly(is); IOUtils.closeQuietly(client); } }
From source file:ch.ralscha.extdirectspring_itest.FileUploadServiceTest.java
@Test public void testUpload() throws IOException { CloseableHttpClient client = HttpClientBuilder.create().build(); CloseableHttpResponse response = null; try {// w ww. j av a2 s . co m HttpPost post = new HttpPost("http://localhost:9998/controller/router"); InputStream is = getClass().getResourceAsStream("/UploadTestFile.txt"); MultipartEntityBuilder builder = MultipartEntityBuilder.create(); ContentBody cbFile = new InputStreamBody(is, ContentType.create("text/plain"), "UploadTestFile.txt"); builder.addPart("fileUpload", cbFile); builder.addPart("extTID", new StringBody("2", ContentType.DEFAULT_TEXT)); builder.addPart("extAction", new StringBody("fileUploadService", ContentType.DEFAULT_TEXT)); builder.addPart("extMethod", new StringBody("uploadTest", ContentType.DEFAULT_TEXT)); builder.addPart("extType", new StringBody("rpc", ContentType.DEFAULT_TEXT)); builder.addPart("extUpload", new StringBody("true", ContentType.DEFAULT_TEXT)); builder.addPart("name", new StringBody("Jim", ContentType.create("text/plain", Charset.forName("UTF-8")))); builder.addPart("firstName", new StringBody("Ralph", ContentType.DEFAULT_TEXT)); builder.addPart("age", new StringBody("25", ContentType.DEFAULT_TEXT)); builder.addPart("email", new StringBody("test@test.ch", ContentType.DEFAULT_TEXT)); post.setEntity(builder.build()); response = client.execute(post); HttpEntity resEntity = response.getEntity(); assertThat(resEntity).isNotNull(); String responseString = EntityUtils.toString(resEntity); String prefix = "<html><body><textarea>"; String postfix = "</textarea></body></html>"; assertThat(responseString).startsWith(prefix); assertThat(responseString).endsWith(postfix); String json = responseString.substring(prefix.length(), responseString.length() - postfix.length()); ObjectMapper mapper = new ObjectMapper(); Map<String, Object> rootAsMap = mapper.readValue(json, Map.class); assertThat(rootAsMap).hasSize(5); assertThat(rootAsMap.get("method")).isEqualTo("uploadTest"); assertThat(rootAsMap.get("type")).isEqualTo("rpc"); assertThat(rootAsMap.get("action")).isEqualTo("fileUploadService"); assertThat(rootAsMap.get("tid")).isEqualTo(2); @SuppressWarnings("unchecked") Map<String, Object> result = (Map<String, Object>) rootAsMap.get("result"); assertThat(result).hasSize(7); assertThat(result.get("name")).isEqualTo("Jim"); assertThat(result.get("firstName")).isEqualTo("Ralph"); assertThat(result.get("age")).isEqualTo(25); assertThat(result.get("e-mail")).isEqualTo("test@test.ch"); assertThat(result.get("fileName")).isEqualTo("UploadTestFile.txt"); assertThat(result.get("fileContents")).isEqualTo("contents of upload file"); assertThat(result.get("success")).isEqualTo(Boolean.TRUE); EntityUtils.consume(resEntity); is.close(); } finally { IOUtils.closeQuietly(response); IOUtils.closeQuietly(client); } }
From source file:com.codedx.burp.ExportActionListener.java
private HttpResponse sendData(File data, String urlStr) throws IOException { CloseableHttpClient client = burpExtender.getHttpClient(); if (client == null) return null; HttpPost post = new HttpPost(urlStr); post.setHeader("API-Key", burpExtender.getApiKey()); MultipartEntityBuilder builder = MultipartEntityBuilder.create(); builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE); builder.addPart("file", new FileBody(data)); HttpEntity entity = builder.build(); post.setEntity(entity);/* w w w. j av a2s. c o m*/ HttpResponse response = client.execute(post); HttpEntity resEntity = response.getEntity(); if (resEntity != null) { EntityUtils.consume(resEntity); } client.close(); return response; }
From source file:org.retrostore.data.BlobstoreWrapperImpl.java
@Override public void addScreenshot(String appId, byte[] data, Responder.ContentType contentType, String cookie) { Preconditions.checkArgument(!Strings.isNullOrEmpty(appId), "'appId' missing"); Preconditions.checkArgument(data != null && data.length > 0, "'data' is empty"); Preconditions.checkNotNull(contentType, "'contentType' missing"); Preconditions.checkArgument(!Strings.isNullOrEmpty(cookie), "'cookie' missing"); LOG.info(String.format("About to add a screenshot blob of size %d with type %s.", data.length, contentType.str));/*from ww w . j av a 2 s .c om*/ final String PATH_UPLOAD = "/screenshotUpload"; String forwardTo = PATH_UPLOAD + "?appId=" + appId; LOG.info("Forward to: " + forwardTo); String uploadUrl = createUploadUrl(forwardTo); LOG.info("UploadUrl: " + uploadUrl); // It is important that we set the cookie so that we're authenticated. We do not allow // anonymous requests to upload screenshots. HttpPost post = new HttpPost(uploadUrl); post.setHeader("Cookie", cookie); MultipartEntityBuilder builder = MultipartEntityBuilder.create(); builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE); // Note we need to use the deprecated constructor so we can use our content type. builder.addPart("file", new ByteArrayBody(data, contentType.str, "screenshot")); HttpEntity entity = builder.build(); post.setEntity(entity); HttpClient client = HttpClientBuilder.create().build(); try { LOG.info("POST constructed. About to make request!"); HttpResponse response = client.execute(post); LOG.info("Request succeeded!"); ByteArrayOutputStream out = new ByteArrayOutputStream(); response.getEntity().writeTo(out); LOG.info(new String(out.toByteArray(), "UTF-8")); } catch (IOException e) { LOG.log(Level.SEVERE, "Cannot make POST request.", e); } }
From source file:be.samey.io.ServerConn.java
private HttpEntity makeEntity(String baits, String[] names, Path[] filepaths, double poscutoff, double negcutoff, String[] orthNames, Path[] orthPaths) throws UnsupportedEncodingException { MultipartEntityBuilder mpeb = MultipartEntityBuilder.create(); //make hidden form fields, to the server knows to use the api mpeb.addPart("__controller", new StringBody("api")); mpeb.addPart("__action", new StringBody("execute_job")); //make the bait part StringBody baitspart = new StringBody(baits, ContentType.TEXT_PLAIN); mpeb.addPart("baits", baitspart); //make the species file upload parts for (int i = 0; i < CyModel.MAX_SPECIES_COUNT; i++) { if (i < names.length && i < filepaths.length) { mpeb.addBinaryBody("matrix[]", filepaths[i].toFile(), ContentType.TEXT_PLAIN, names[i]); }/*from w w w .ja v a 2s . co m*/ } //make the cutoff parts StringBody poscpart = new StringBody(Double.toString(poscutoff)); mpeb.addPart("positive_correlation", poscpart); StringBody negcpart = new StringBody(Double.toString(negcutoff)); mpeb.addPart("negative_correlation", negcpart); //make the orthgroup file upload parts for (int i = 0; i < CyModel.MAX_ORTHGROUP_COUNT; i++) { if (cyModel.getOrthGroupPaths() != null && i < orthNames.length && i < orthPaths.length) { mpeb.addBinaryBody("orthologs[]", orthPaths[i].toFile(), ContentType.TEXT_PLAIN, orthNames[i]); } } return mpeb.build(); }
From source file:org.wso2.carbon.appmanager.integration.ui.Util.Bean.AbstractMultiPartRequest.java
public HttpEntity generateMulipartEnitity() { MultipartEntityBuilder reqBuilder = MultipartEntityBuilder.create(); parameterMap.clear();//from w w w . j av a 2 s .com init(); Iterator<String> irt = parameterMap.keySet().iterator(); String key; ContentBody value; while (irt.hasNext()) { key = irt.next(); value = parameterMap.get(key); reqBuilder.addPart(key, value); } return reqBuilder.build(); }
From source file:org.wisdom.test.http.MultipartBody.java
/** * Computes the request payload.//from ww w. j av a 2 s . com * * @return the payload containing the declared fields and files. */ public HttpEntity getEntity() { if (hasFile) { MultipartEntityBuilder builder = MultipartEntityBuilder.create(); for (Entry<String, Object> part : parameters.entrySet()) { if (part.getValue() instanceof File) { hasFile = true; builder.addPart(part.getKey(), new FileBody((File) part.getValue())); } else { builder.addPart(part.getKey(), new StringBody(part.getValue().toString(), ContentType.APPLICATION_FORM_URLENCODED)); } } return builder.build(); } else { try { return new UrlEncodedFormEntity(getList(parameters), UTF_8); } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); } } }