List of usage examples for org.apache.http.entity.mime.content StringBody StringBody
public StringBody(final String text, final String mimeType, Charset charset) throws UnsupportedEncodingException
From source file:org.overlord.sramp.governance.workflow.Multipart.java
public void post(HttpClient httpclient, URI uri, Map<String, Object> parameters) throws IOException, WorkflowException { MultipartEntity multiPartEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); for (String key : parameters.keySet()) { ContentBody content = null;/*ww w . j a va2 s.c om*/ Object param = parameters.get(key); if (param instanceof String) { StringBody stringBody = new StringBody((String) param, "text/plain", Charset.forName("UTF-8")); //$NON-NLS-1$ //$NON-NLS-2$ content = stringBody; } else { //turn object into byteArray, or it also supports InputStreamBody or FileBody ByteArrayBody byteBody = new ByteArrayBody(null, key); content = byteBody; } multiPartEntity.addPart(key, content); } HttpPost httpPost = new HttpPost(uri); httpPost.setEntity(multiPartEntity); HttpResponse response = httpclient.execute(httpPost); InputStream is = response.getEntity().getContent(); String responseStr = IOUtils.toString(is); if (response.getStatusLine().getStatusCode() == 200 || response.getStatusLine().getStatusCode() == 201) { logger.debug(responseStr); } else { throw new WorkflowException( "Workflow ERROR - HTTP STATUS CODE " + response.getStatusLine().getStatusCode() + ". " //$NON-NLS-1$ //$NON-NLS-2$ + response.getStatusLine().getReasonPhrase() + ". " + responseStr); //$NON-NLS-1$ } is.close(); }
From source file:at.uni_salzburg.cs.ckgroup.cscpp.utils.HttpQueryUtils.java
public static String[] fileUpload(String uploadUrl, String name, byte[] byteArray) throws IOException { HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost(uploadUrl); String paramName = "vehicle"; String paramValue = name;// www .j ava 2 s .c o m MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); entity.addPart(paramName, new InputStreamBody((new ByteArrayInputStream(byteArray)), "application/zip")); entity.addPart(paramName, new StringBody(paramValue, "text/plain", Charset.forName("UTF-8"))); httppost.setEntity(entity); HttpResponse httpResponse = httpclient.execute(httppost); StatusLine statusLine = httpResponse.getStatusLine(); String reason = statusLine.getReasonPhrase(); int rc = statusLine.getStatusCode(); String response = EntityUtils.toString(httpResponse.getEntity(), "UTF-8"); httpclient.getConnectionManager().shutdown(); return new String[] { String.valueOf(rc), reason, response }; }
From source file:com.revo.deployr.client.call.project.ProjectImportCall.java
/** * Internal use only, to execute call use RClient.execute(). *//* www . ja v a2 s.com*/ public RCoreResult call() { RCoreResultImpl pResult = null; try { HttpPost httpPost = new HttpPost(serverUrl + API); super.httpUriRequest = httpPost; List<NameValuePair> postParams = new ArrayList<NameValuePair>(); postParams.add(new BasicNameValuePair("format", "json")); MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); entity.addPart("file", new InputStreamBody(((InputStream) fileStream), "application/zip")); entity.addPart("descr", new StringBody(descr, "text/plain", Charset.forName("UTF-8"))); entity.addPart("format", new StringBody("json", "text/plain", Charset.forName("UTF-8"))); httpPost.setEntity(entity); // set any custom headers on the request for (Map.Entry<String, String> entry : httpHeaders.entrySet()) { httpPost.addHeader(entry.getKey(), entry.getValue()); } HttpResponse response = httpClient.execute(httpPost); StatusLine statusLine = response.getStatusLine(); HttpEntity responseEntity = response.getEntity(); String markup = EntityUtils.toString(responseEntity); pResult = new RCoreResultImpl(response.getAllHeaders()); pResult.parseMarkup(markup, API, statusLine.getStatusCode(), statusLine.getReasonPhrase()); } catch (UnsupportedEncodingException ueex) { log.warn("ProjectImportCall: unsupported encoding exception.", ueex); } catch (IOException ioex) { log.warn("ProjectImportCall: io exception.", ioex); } return pResult; }
From source file:com.revo.deployr.client.call.project.ProjectWorkspaceUploadCall.java
/** * Internal use only, to execute call use RClient.execute(). *//* w ww.j ava 2 s .co m*/ public RCoreResult call() { RCoreResultImpl pResult = null; try { HttpPost httpPost = new HttpPost(serverUrl + API); super.httpUriRequest = httpPost; List<NameValuePair> postParams = new ArrayList<NameValuePair>(); postParams.add(new BasicNameValuePair("format", "json")); MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); entity.addPart("project", new StringBody(this.project, "text/plain", Charset.forName("UTF-8"))); entity.addPart("name", new StringBody(this.name, "text/plain", Charset.forName("UTF-8"))); entity.addPart("file", new InputStreamBody(((InputStream) fileStream), "application/zip")); entity.addPart("format", new StringBody("json", "text/plain", Charset.forName("UTF-8"))); httpPost.setEntity(entity); // set any custom headers on the request for (Map.Entry<String, String> entry : httpHeaders.entrySet()) { httpPost.addHeader(entry.getKey(), entry.getValue()); } HttpResponse response = httpClient.execute(httpPost); StatusLine statusLine = response.getStatusLine(); HttpEntity responseEntity = response.getEntity(); String markup = EntityUtils.toString(responseEntity); pResult = new RCoreResultImpl(response.getAllHeaders()); pResult.parseMarkup(markup, API, statusLine.getStatusCode(), statusLine.getReasonPhrase()); } catch (UnsupportedEncodingException ueex) { log.warn("ProjectWorkspaceUploadCall: unsupported encoding exception.", ueex); } catch (IOException ioex) { log.warn("ProjectWorkspaceUploadCall: io exception.", ioex); } return pResult; }
From source file:com.revo.deployr.client.call.project.ProjectDirectoryUploadCall.java
/** * Internal use only, to execute call use RClient.execute(). *//*from ww w . j a v a 2s. c om*/ public RCoreResult call() { RCoreResultImpl pResult = null; try { HttpPost httpPost = new HttpPost(serverUrl + API); super.httpUriRequest = httpPost; List<NameValuePair> postParams = new ArrayList<NameValuePair>(); postParams.add(new BasicNameValuePair("format", "json")); MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); entity.addPart("file", new InputStreamBody(((InputStream) fileStream), "application/zip")); entity.addPart("project", new StringBody(project, "text/plain", Charset.forName("UTF-8"))); entity.addPart("filename", new StringBody(options.filename, "text/plain", Charset.forName("UTF-8"))); if (options.descr != null) entity.addPart("descr", new StringBody(options.descr, "text/plain", Charset.forName("UTF-8"))); entity.addPart("overwrite", new StringBody(Boolean.toString(options.overwrite), "text/plain", Charset.forName("UTF-8"))); entity.addPart("format", new StringBody("json", "text/plain", Charset.forName("UTF-8"))); httpPost.setEntity(entity); // set any custom headers on the request for (Map.Entry<String, String> entry : httpHeaders.entrySet()) { httpPost.addHeader(entry.getKey(), entry.getValue()); } HttpResponse response = httpClient.execute(httpPost); StatusLine statusLine = response.getStatusLine(); HttpEntity responseEntity = response.getEntity(); String markup = EntityUtils.toString(responseEntity); pResult = new RCoreResultImpl(response.getAllHeaders()); pResult.parseMarkup(markup, API, statusLine.getStatusCode(), statusLine.getReasonPhrase()); } catch (UnsupportedEncodingException ueex) { log.warn("ProjectDirectoryUploadCall: unsupported encoding exception.", ueex); } catch (IOException ioex) { log.warn("ProjectDirectoryUploadCall: io exception.", ioex); } return pResult; }
From source file:com.revo.deployr.client.call.repository.RepositoryFileUploadCall.java
/** * Internal use only, to execute call use RClient.execute(). *///from w w w.ja v a 2 s . c o m public RCoreResult call() { RCoreResultImpl pResult = null; try { HttpPost httpPost = new HttpPost(serverUrl + API); super.httpUriRequest = httpPost; List<NameValuePair> postParams = new ArrayList<NameValuePair>(); postParams.add(new BasicNameValuePair("format", "json")); MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); entity.addPart("file", new InputStreamBody(((InputStream) fileStream), "application/zip")); if (options.filename != null) entity.addPart("filename", new StringBody(options.filename, "text/plain", Charset.forName("UTF-8"))); if (options.directory != null) entity.addPart("directory", new StringBody(options.directory, "text/plain", Charset.forName("UTF-8"))); if (options.descr != null) entity.addPart("descr", new StringBody(options.descr, "text/plain", Charset.forName("UTF-8"))); entity.addPart("newversion", new StringBody(Boolean.toString(options.newversion), "text/plain", Charset.forName("UTF-8"))); if (options.newversionmsg != null) entity.addPart("newversionmsg", new StringBody(options.newversionmsg, "text/plain", Charset.forName("UTF-8"))); if (options.restricted != null) entity.addPart("restricted", new StringBody(options.restricted, "text/plain", Charset.forName("UTF-8"))); entity.addPart("shared", new StringBody(Boolean.toString(options.shared), "text/plain", Charset.forName("UTF-8"))); entity.addPart("published", new StringBody(Boolean.toString(options.published), "text/plain", Charset.forName("UTF-8"))); if (options.inputs != null) entity.addPart("inputs", new StringBody(options.inputs, "text/plain", Charset.forName("UTF-8"))); if (options.outputs != null) entity.addPart("outputs", new StringBody(options.outputs, "text/plain", Charset.forName("UTF-8"))); entity.addPart("format", new StringBody("json", "text/plain", Charset.forName("UTF-8"))); httpPost.setEntity(entity); // set any custom headers on the request for (Map.Entry<String, String> entry : httpHeaders.entrySet()) { httpPost.addHeader(entry.getKey(), entry.getValue()); } HttpResponse response = httpClient.execute(httpPost); StatusLine statusLine = response.getStatusLine(); HttpEntity responseEntity = response.getEntity(); String markup = EntityUtils.toString(responseEntity); pResult = new RCoreResultImpl(response.getAllHeaders()); pResult.parseMarkup(markup, API, statusLine.getStatusCode(), statusLine.getReasonPhrase()); } catch (UnsupportedEncodingException ueex) { log.warn("RepositoryFileUploadCall: unsupported encoding exception.", ueex); } catch (IOException ioex) { log.warn("RepositoryFileUploadCall: io exception.", ioex); } return pResult; }
From source file:com.revo.deployr.client.call.repository.RepositoryDirectoryUploadCall.java
/** * Internal use only, to execute call use RClient.execute(). *///from w ww . ja v a2s . c om public RCoreResult call() { RCoreResultImpl pResult = null; try { HttpPost httpPost = new HttpPost(serverUrl + API); super.httpUriRequest = httpPost; List<NameValuePair> postParams = new ArrayList<NameValuePair>(); postParams.add(new BasicNameValuePair("format", "json")); MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); entity.addPart("file", new InputStreamBody(((InputStream) zipStream), "application/zip")); if (options.directory != null) entity.addPart("directory", new StringBody(options.directory, "text/plain", Charset.forName("UTF-8"))); if (options.descr != null) entity.addPart("descr", new StringBody(options.descr, "text/plain", Charset.forName("UTF-8"))); entity.addPart("newversion", new StringBody(Boolean.toString(options.newversion), "text/plain", Charset.forName("UTF-8"))); if (options.restricted != null) entity.addPart("restricted", new StringBody(options.restricted, "text/plain", Charset.forName("UTF-8"))); entity.addPart("shared", new StringBody(Boolean.toString(options.shared), "text/plain", Charset.forName("UTF-8"))); entity.addPart("published", new StringBody(Boolean.toString(options.published), "text/plain", Charset.forName("UTF-8"))); if (options.inputs != null) entity.addPart("inputs", new StringBody(options.inputs, "text/plain", Charset.forName("UTF-8"))); if (options.outputs != null) entity.addPart("outputs", new StringBody(options.outputs, "text/plain", Charset.forName("UTF-8"))); entity.addPart("format", new StringBody("json", "text/plain", Charset.forName("UTF-8"))); httpPost.setEntity(entity); // set any custom headers on the request for (Map.Entry<String, String> entry : httpHeaders.entrySet()) { httpPost.addHeader(entry.getKey(), entry.getValue()); } HttpResponse response = httpClient.execute(httpPost); StatusLine statusLine = response.getStatusLine(); HttpEntity responseEntity = response.getEntity(); String markup = EntityUtils.toString(responseEntity); pResult = new RCoreResultImpl(response.getAllHeaders()); pResult.parseMarkup(markup, API, statusLine.getStatusCode(), statusLine.getReasonPhrase()); } catch (UnsupportedEncodingException ueex) { log.warn("RepositoryDirectoryUploadCall: unsupported encoding exception.", ueex); } catch (IOException ioex) { log.warn("RepositoryDirectoryUploadCall: io exception.", ioex); } return pResult; }
From source file:io.undertow.servlet.test.multipart.MultiPartTestCase.java
@Test public void testMultiPartRequestWithNoMultipartConfig() throws IOException { TestHttpClient client = new TestHttpClient(); try {// w w w.j av a2 s . c om String uri = DefaultServer.getDefaultServerURL() + "/servletContext/0"; 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); Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode()); final String response = HttpClientUtils.readResponse(result); Assert.assertEquals("PARAMS:\n", response); } finally { client.getConnectionManager().shutdown(); } }
From source file:com.openmeap.http.FileHandlingHttpRequestExecuterImpl.java
@Override public HttpResponse postData(String url, Hashtable getParams, Hashtable postParams) throws HttpRequestException { // test to determine whether this is a file upload or not. Boolean isFileUpload = false; for (Object o : postParams.values()) { if (o instanceof File) { isFileUpload = true;//from w ww. ja v a2 s .c o m break; } } if (isFileUpload) { try { HttpPost httpPost = new HttpPost(createUrl(url, getParams)); httpPost.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1); MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); for (Object o : postParams.entrySet()) { Map.Entry<String, Object> entry = (Map.Entry<String, Object>) o; if (entry.getValue() instanceof File) { // For File parameters File file = (File) entry.getValue(); FileNameMap fileNameMap = URLConnection.getFileNameMap(); String type = fileNameMap.getContentTypeFor(file.toURL().toString()); entity.addPart(entry.getKey(), new FileBody(((File) entry.getValue()), type)); } else { // For usual String parameters entity.addPart(entry.getKey(), new StringBody(entry.getValue().toString(), "text/plain", Charset.forName(FormConstants.CHAR_ENC_DEFAULT))); } } httpPost.setEntity(entity); return execute(httpPost); } catch (Exception e) { throw new HttpRequestException(e); } } else { return super.postData(url, getParams, postParams); } }
From source file:io.undertow.server.handlers.form.MultipartFormDataParserTestCase.java
@Test public void testFileUpload() throws Exception { DefaultServer.setRootHandler(new BlockingHandler(createHandler())); TestHttpClient client = new TestHttpClient(); try {//w w w . j ava 2 s . co m HttpPost post = new HttpPost(DefaultServer.getDefaultServerURL() + "/path"); //post.setHeader(Headers.CONTENT_TYPE, MultiPartHandler.MULTIPART_FORM_DATA); 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(MultipartFormDataParserTestCase.class.getResource("uploadfile.txt").getFile()))); post.setEntity(entity); HttpResponse result = client.execute(post); Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode()); HttpClientUtils.readResponse(result); } finally { client.getConnectionManager().shutdown(); } }