List of usage examples for org.apache.http.entity ContentType TEXT_PLAIN
ContentType TEXT_PLAIN
To view the source code for org.apache.http.entity ContentType TEXT_PLAIN.
Click Source Link
From source file:org.knoxcraft.http.client.ClientMultipartFormPost.java
public static void upload(String url, String playerName, File jsonfile, File sourcefile) throws ClientProtocolException, IOException { CloseableHttpClient httpclient = HttpClients.createDefault(); try {/*from w ww .j av a2 s . c om*/ HttpPost httppost = new HttpPost(url); FileBody json = new FileBody(jsonfile); FileBody source = new FileBody(sourcefile); String jsontext = readFromFile(jsonfile); HttpEntity reqEntity = MultipartEntityBuilder.create() .addPart("playerName", new StringBody(playerName, ContentType.TEXT_PLAIN)) .addPart("jsonfile", json).addPart("sourcefile", source) .addPart("language", new StringBody("java", ContentType.TEXT_PLAIN)) .addPart("jsontext", new StringBody(jsontext, ContentType.TEXT_PLAIN)).addPart("sourcetext", new StringBody("public class Foo {\n int x=5\n}", ContentType.TEXT_PLAIN)) .build(); httppost.setEntity(reqEntity); System.out.println("executing request " + httppost.getRequestLine()); CloseableHttpResponse response = httpclient.execute(httppost); try { //System.out.println("----------------------------------------"); System.out.println(response.getStatusLine()); HttpEntity resEntity = response.getEntity(); if (resEntity != null) { //System.out.println("Response content length: " + resEntity.getContentLength()); Scanner sc = new Scanner(resEntity.getContent()); while (sc.hasNext()) { System.out.println(sc.nextLine()); } sc.close(); EntityUtils.consume(resEntity); } } finally { response.close(); } } finally { httpclient.close(); } }
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:cn.aofeng.demo.httpclient.HttpClientBasic.java
public void sendFile(String filePath) throws UnsupportedOperationException, IOException { CloseableHttpClient client = HttpClients.createDefault(); HttpPost post = new HttpPost(_targetHost + "/file"); File file = new File(filePath); FileEntity entity = new FileEntity(file, ContentType.create(ContentType.TEXT_PLAIN.getMimeType(), _charset)); post.setEntity(entity);//from w w w. j ava2 s.co m CloseableHttpResponse response = client.execute(post); processResponse(response); }
From source file:org.elasticsearch.client.HeapBufferedAsyncResponseConsumerTests.java
public void testResponseProcessing() throws Exception { ContentDecoder contentDecoder = mock(ContentDecoder.class); IOControl ioControl = mock(IOControl.class); HttpContext httpContext = mock(HttpContext.class); HeapBufferedAsyncResponseConsumer consumer = spy(new HeapBufferedAsyncResponseConsumer(TEST_BUFFER_LIMIT)); ProtocolVersion protocolVersion = new ProtocolVersion("HTTP", 1, 1); StatusLine statusLine = new BasicStatusLine(protocolVersion, 200, "OK"); HttpResponse httpResponse = new BasicHttpResponse(statusLine); httpResponse.setEntity(new StringEntity("test", ContentType.TEXT_PLAIN)); //everything goes well consumer.responseReceived(httpResponse); consumer.consumeContent(contentDecoder, ioControl); consumer.responseCompleted(httpContext); verify(consumer).releaseResources(); verify(consumer).buildResult(httpContext); assertTrue(consumer.isDone());//from w w w. java2 s.c om assertSame(httpResponse, consumer.getResult()); consumer.responseCompleted(httpContext); verify(consumer, times(1)).releaseResources(); verify(consumer, times(1)).buildResult(httpContext); }
From source file:com.wattzap.model.social.SelfLoopsAPI.java
public static int uploadActivity(String email, String passWord, String fileName, String note) throws IOException { JSONObject jsonObj = null;//from w w w .j a v a2 s . co m FileInputStream in = null; GZIPOutputStream out = null; CloseableHttpClient httpClient = HttpClients.createDefault(); try { HttpPost httpPost = new HttpPost(url); httpPost.setHeader("enctype", "multipart/mixed"); in = new FileInputStream(fileName); // Create stream to compress data and write it to the to file. ByteArrayOutputStream obj = new ByteArrayOutputStream(); out = new GZIPOutputStream(obj); // Copy bytes from one stream to the other byte[] buffer = new byte[4096]; int bytes_read; while ((bytes_read = in.read(buffer)) != -1) { out.write(buffer, 0, bytes_read); } out.close(); in.close(); ByteArrayBody bin = new ByteArrayBody(obj.toByteArray(), ContentType.create("application/x-gzip"), fileName); HttpEntity reqEntity = MultipartEntityBuilder.create() .addPart("email", new StringBody(email, ContentType.TEXT_PLAIN)) .addPart("pw", new StringBody(passWord, ContentType.TEXT_PLAIN)).addPart("tcxfile", bin) .addPart("note", new StringBody(note, ContentType.TEXT_PLAIN)).build(); httpPost.setEntity(reqEntity); CloseableHttpResponse response = null; try { response = httpClient.execute(httpPost); int code = response.getStatusLine().getStatusCode(); switch (code) { case 200: HttpEntity respEntity = response.getEntity(); if (respEntity != null) { // EntityUtils to get the response content String content = EntityUtils.toString(respEntity); //System.out.println(content); JSONParser jsonParser = new JSONParser(); jsonObj = (JSONObject) jsonParser.parse(content); } break; case 403: throw new RuntimeException( "Authentification failure " + email + " " + response.getStatusLine()); default: throw new RuntimeException("Error " + code + " " + response.getStatusLine()); } } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { if (response != null) { response.close(); } } int activityId = ((Long) jsonObj.get("activity_id")).intValue(); // parse error code int error = ((Long) jsonObj.get("error_code")).intValue(); if (activityId == -1) { String message = (String) jsonObj.get("message"); switch (error) { case 102: throw new RuntimeException("Empty TCX file " + fileName); case 103: throw new RuntimeException("Invalide TCX Format " + fileName); case 104: throw new RuntimeException("TCX Already Present " + fileName); case 105: throw new RuntimeException("Invalid XML " + fileName); case 106: throw new RuntimeException("invalid compression algorithm"); case 107: throw new RuntimeException("Invalid file mime types"); default: throw new RuntimeException(message + " " + error); } } return activityId; } finally { if (in != null) { in.close(); } if (out != null) { out.close(); } httpClient.close(); } }
From source file:rxweb.RxJavaServerTests.java
public void echoCapitalizedStream() throws IOException { server.post("/test", (request,//w w w .ja v a2s .c om response) -> response.content(request.getContent() .map(data -> ByteBuffer.wrap(new String(data.array(), StandardCharsets.UTF_8) .toUpperCase().getBytes(StandardCharsets.UTF_8))))); String content = Request.Post("http://localhost:8080/test") .bodyString("This is a test!", ContentType.TEXT_PLAIN).execute().returnContent().asString(); Assert.assertEquals("THIS IS A TEST!", content); }
From source file:com.graphaware.common.ping.GoogleAnalyticsStatsCollector.java
private void post(final String body) { executor.scheduleAtFixedRate(new Runnable() { @Override//from w ww. ja v a 2 s. c om public void run() { HttpPost httpPost = new HttpPost("http://www.google-analytics.com/collect"); httpPost.setEntity(new StringEntity(body, ContentType.TEXT_PLAIN)); try (CloseableHttpClient httpClient = HttpClients.createDefault()) { httpClient.execute(httpPost); } catch (IOException e1) { LOG.warn("Unable to collect stats", e1); } } }, 5, 5, TimeUnit.MINUTES); }
From source file:com.king.ess.gsa.GSAFeedForm.java
/** * It sends XML with feeds to GSA using proper form: * //from www . ja v a 2s. c o m * @param xmlDocument GSADocumentFormatter containing XML Document information * * @return Was posted Ok? */ public boolean sendForm(GSADocumentFormatter xmlDocument) { boolean bSent = false; HttpClient client = HttpClientBuilder.create().build(); try { HttpPost postPageRequest; postPageRequest = new HttpPost(this.gsaServer); InputStream is = xmlDocument.writeToInputStream(); // Add Form parameters MultipartEntityBuilder builder = MultipartEntityBuilder.create(); builder.addTextBody(FEEDTYPE_PARAM, xmlDocument.getFeedType(), ContentType.TEXT_PLAIN); builder.addTextBody(DATASOURCE_PARAM, xmlDocument.getDatasource(), ContentType.TEXT_PLAIN); builder.addBinaryBody(XMLFILE_PARAM, is); HttpEntity multipartEntity = builder.build(); postPageRequest.setEntity(multipartEntity); HttpResponse postPageResponse = client.execute(postPageRequest); int status = postPageResponse.getStatusLine().getStatusCode(); if (!(bSent = (status == 200))) log.error("GitHub API (" + this.gsaServer + ") returned " + status); else log.info("XML For datasource '" + xmlDocument.getDatasource() + "' and FeedType '" + xmlDocument.getFeedType() + "' was posted successfully to GSA!!"); } catch (Exception e) { log.error("Exception " + e.getMessage() + " in HTTP request " + this.gsaServer); } return bSent; }
From source file:com.movilizer.mds.webservice.models.MovilizerUploadForm.java
private MultipartEntityBuilder getForm(long systemId, String password, String pool, String key, String lang, String suffix, String ack) { MultipartEntityBuilder form = MultipartEntityBuilder.create() .addTextBody("systemId", String.valueOf(systemId), ContentType.TEXT_PLAIN) .addTextBody("password", password, ContentType.TEXT_PLAIN) .addTextBody("pool", pool, ContentType.TEXT_PLAIN).addTextBody("key", key, ContentType.TEXT_PLAIN) .addTextBody("suffix", suffix, ContentType.TEXT_PLAIN); if (lang != null) { form.addTextBody("lang", lang, ContentType.TEXT_PLAIN); }/*from w w w . j a va2s . co m*/ if (ack != null) { form.addTextBody("ack", suffix, ContentType.TEXT_PLAIN); } return form; }
From source file:org.ambraproject.wombat.service.FreemarkerMailServiceImpl.java
@Override public Multipart createContent(Site site, String templateFilename, Model context) throws IOException, MessagingException { Template textTemplate = getEmailTemplate(site, "txt", templateFilename); Template htmlTemplate = getEmailTemplate(site, "html", templateFilename); // Create a "text" Multipart message Multipart mp = createPartForMultipart(textTemplate, context, "alternative", ContentType.TEXT_PLAIN); // Create a "HTML" Multipart message Multipart htmlContent = createPartForMultipart(htmlTemplate, context, "related", ContentType.TEXT_HTML); BodyPart htmlPart = new MimeBodyPart(); htmlPart.setContent(htmlContent);/* ww w . ja v a 2s.com*/ mp.addBodyPart(htmlPart); return mp; }