List of usage examples for org.apache.http.entity ContentType create
private static ContentType create(HeaderElement headerElement)
From source file:org.metaeffekt.dcc.commons.ant.HttpRequestTask.java
/** * Executes the task.//from w w w. j ava 2 s . co m * * @see org.apache.tools.ant.Task#execute() */ @Override public void execute() { StringBuilder sb = new StringBuilder(); sb.append(serverScheme).append("://").append(serverHostName).append(':').append(serverPort); sb.append("/").append(uri); String url = sb.toString(); BasicCredentialsProvider credentialsProvider = null; if (username != null) { log("User: " + username, Project.MSG_DEBUG); credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(new AuthScope(serverHostName, serverPort), new UsernamePasswordCredentials(username, password)); } HttpClient httpClient = HttpClientBuilder.create().setDefaultCredentialsProvider(credentialsProvider) .build(); try { switch (httpMethod) { case GET: HttpGet get = new HttpGet(url); doRequest(httpClient, get); break; case PUT: HttpPut put = new HttpPut(url); if (body == null) { body = ""; } log("Setting body: " + body, Project.MSG_DEBUG); put.setEntity(new StringEntity(body, ContentType.create(contentType))); doRequest(httpClient, put); break; case POST: HttpPost post = new HttpPost(url); if (body == null) { body = ""; } log("Setting body: " + body, Project.MSG_DEBUG); post.setEntity(new StringEntity(body, ContentType.create(contentType))); doRequest(httpClient, post); break; case DELETE: HttpDelete delete = new HttpDelete(url); doRequest(httpClient, delete); break; default: throw new IllegalArgumentException("HttpMethod " + httpMethod + " not supported!"); } } catch (IOException e) { throw new BuildException(e); } }
From source file:com.serphacker.serposcope.scraper.captcha.solver.AntiCaptchaSolver.java
@Override public boolean solve(Captcha cap) { if (!(cap instanceof CaptchaImage)) { return false; }//w w w. j a v a 2 s .c om captchaCount.incrementAndGet(); CaptchaImage captcha = (CaptchaImage) cap; captcha.setLastSolver(this); captcha.setStatus(Captcha.Status.CREATED); String filename = null; String textMimeType = captcha.getMimes()[0]; String[] mimes = null; if (textMimeType != null) mimes = textMimeType.split("/"); else textMimeType = "application/octet-stream"; if (mimes != null && mimes.length == 2) { if (isValidImageExtension(mimes[1])) { filename = "image." + mimes[1]; } } else { filename = "image.png"; } Map<String, Object> data = new HashMap<>(); data.put("method", "post"); data.put("key", apiKey); data.put("file", new ByteArrayBody(captcha.getImage(), ContentType.create(textMimeType), filename)); long started = System.currentTimeMillis(); captcha.setStatus(Captcha.Status.SUBMITTED); try (ScrapClient http = new ScrapClient()) { String response; int retry = 0; while (true) { http.post(apiUrl + "in.php", data, ScrapClient.PostType.MULTIPART); response = http.getContentAsString(); if (!isRetryable(response)) { break; } if (++retry > maxRetryOnOverload) { break; } try { Long sleep = 5000l * retry; LOG.debug("server is overloaded \"{}\", sleeping {} ms", response, sleep); Thread.sleep(sleep); } catch (InterruptedException ex) { break; } } if (response == null) { captcha.setError(Captcha.Error.NETWORK_ERROR); return false; } if (!response.startsWith("OK|") || response.length() < 4) { switch (response) { case "ERROR_WRONG_USER_KEY": case "ERROR_KEY_DOES_NOT_EXIST": captcha.setError(Captcha.Error.INVALID_CREDENTIALS); return false; case "ERROR_ZERO_BALANCE": captcha.setError(Captcha.Error.OUT_OF_CREDITS); return false; case "ERROR_NO_SLOT_AVAILABLE": captcha.setError(Captcha.Error.SERVICE_OVERLOADED); return false; default: captcha.setError(Captcha.Error.NETWORK_ERROR); return false; } } captcha.setId(response.substring(3)); long timeLimit = System.currentTimeMillis() + timeoutMS; while (System.currentTimeMillis() < timeLimit) { int status = http.get(apiUrl + "res.php?key=" + apiKey + "&action=get" + "&id=" + captcha.getId() + "&random=" + random.nextInt(Integer.MAX_VALUE)); String res = http.getContentAsString(); if (res == null) { captcha.setError(Captcha.Error.NETWORK_ERROR); return false; } if (!"CAPCHA_NOT_READY".equals(apiKey)) { if (res.startsWith("OK|")) { captcha.setResponse(res.substring(3)); captcha.setStatus(Captcha.Status.SOLVED); return true; } captcha.setError(Captcha.Error.NETWORK_ERROR); captcha.setStatus(Captcha.Status.ERROR); } try { Thread.sleep(POLLING_PAUSE_MS); } catch (InterruptedException ex) { break; } } captcha.setError(Captcha.Error.TIMEOUT); captcha.setStatus(Captcha.Status.ERROR); } catch (IOException ex) { LOG.error("io exception", ex); captcha.setError(EXCEPTION); } finally { captcha.setSolveDuration(System.currentTimeMillis() - started); } return false; }
From source file:es.tid.fiware.fiwareconnectors.cygnus.backends.ckan.CKANRequester.java
/** * Common method to perform HTTP request using the CKAN API with payload. * @param method HTTP method//ww w . j a v a2 s .c o m * @param urlPath URL path to be added to the base URL * @param payload Request payload * @return CKANResponse associated to the request * @throws Exception */ public CKANResponse doCKANRequest(String method, String urlPath, String payload) throws Exception { // build the final URL String url = baseURL + urlPath; HttpRequestBase request = null; HttpResponse response = null; try { // do the post if (method.equals("GET")) { request = new HttpGet(url); } else if (method.equals("POST")) { HttpPost r = new HttpPost(url); // payload (optional) if (!payload.equals("")) { logger.debug("request payload: " + payload); r.setEntity(new StringEntity(payload, ContentType.create("application/json"))); } // if request = r; } else { throw new CygnusRuntimeError("HTTP method not supported: " + method); } // if else // headers request.addHeader("Authorization", apiKey); // execute the request logger.debug("CKAN operation: " + request.toString()); } catch (Exception e) { if (e instanceof CygnusRuntimeError || e instanceof CygnusPersistenceError || e instanceof CygnusBadConfiguration) { throw e; } else { throw new CygnusRuntimeError(e.getMessage()); } // if else } // try catch try { response = httpClient.execute(request); } catch (Exception e) { throw new CygnusPersistenceError(e.getMessage()); } // try catch try { BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); String res = reader.readLine(); request.releaseConnection(); long l = response.getEntity().getContentLength(); logger.debug("CKAN response (" + l + " bytes): " + response.getStatusLine().toString()); // get the JSON encapsulated in the response logger.debug("response payload: " + res); JSONParser j = new JSONParser(); JSONObject o = (JSONObject) j.parse(res); // return result return new CKANResponse(o, response.getStatusLine().getStatusCode()); } catch (Exception e) { if (e instanceof CygnusRuntimeError || e instanceof CygnusPersistenceError || e instanceof CygnusBadConfiguration) { throw e; } else { throw new CygnusRuntimeError(e.getMessage()); } // if else } // try catch }
From source file:org.elasticsearch.xpack.ml.integration.MlBasicMultiNodeIT.java
public void testMiniFarequote() throws Exception { String jobId = "mini-farequote-job"; createFarequoteJob(jobId);//from w w w. j a va2 s. c o m Response response = client().performRequest("post", MachineLearning.BASE_PATH + "anomaly_detectors/" + jobId + "/_open"); assertEquals(200, response.getStatusLine().getStatusCode()); assertEquals(Collections.singletonMap("opened", true), responseEntityToMap(response)); String postData = "{\"airline\":\"AAL\",\"responsetime\":\"132.2046\",\"sourcetype\":\"farequote\",\"time\":\"1403481600\"}\n" + "{\"airline\":\"JZA\",\"responsetime\":\"990.4628\",\"sourcetype\":\"farequote\",\"time\":\"1403481700\"}"; response = client().performRequest("post", MachineLearning.BASE_PATH + "anomaly_detectors/" + jobId + "/_data", Collections.emptyMap(), new StringEntity(postData, randomFrom(ContentType.APPLICATION_JSON, ContentType.create("application/x-ndjson")))); assertEquals(202, response.getStatusLine().getStatusCode()); Map<String, Object> responseBody = responseEntityToMap(response); assertEquals(2, responseBody.get("processed_record_count")); assertEquals(4, responseBody.get("processed_field_count")); assertEquals(177, responseBody.get("input_bytes")); assertEquals(6, responseBody.get("input_field_count")); assertEquals(0, responseBody.get("invalid_date_count")); assertEquals(0, responseBody.get("missing_field_count")); assertEquals(0, responseBody.get("out_of_order_timestamp_count")); assertEquals(0, responseBody.get("bucket_count")); assertEquals(1403481600000L, responseBody.get("earliest_record_timestamp")); assertEquals(1403481700000L, responseBody.get("latest_record_timestamp")); response = client().performRequest("post", MachineLearning.BASE_PATH + "anomaly_detectors/" + jobId + "/_flush"); assertEquals(200, response.getStatusLine().getStatusCode()); assertFlushResponse(response, true, 1403481600000L); response = client().performRequest("post", MachineLearning.BASE_PATH + "anomaly_detectors/" + jobId + "/_close", Collections.singletonMap("timeout", "20s")); assertEquals(200, response.getStatusLine().getStatusCode()); assertEquals(Collections.singletonMap("closed", true), responseEntityToMap(response)); response = client().performRequest("get", MachineLearning.BASE_PATH + "anomaly_detectors/" + jobId + "/_stats"); assertEquals(200, response.getStatusLine().getStatusCode()); @SuppressWarnings("unchecked") Map<String, Object> dataCountsDoc = (Map<String, Object>) ((Map) ((List) responseEntityToMap(response) .get("jobs")).get(0)).get("data_counts"); assertEquals(2, dataCountsDoc.get("processed_record_count")); assertEquals(4, dataCountsDoc.get("processed_field_count")); assertEquals(177, dataCountsDoc.get("input_bytes")); assertEquals(6, dataCountsDoc.get("input_field_count")); assertEquals(0, dataCountsDoc.get("invalid_date_count")); assertEquals(0, dataCountsDoc.get("missing_field_count")); assertEquals(0, dataCountsDoc.get("out_of_order_timestamp_count")); assertEquals(0, dataCountsDoc.get("bucket_count")); assertEquals(1403481600000L, dataCountsDoc.get("earliest_record_timestamp")); assertEquals(1403481700000L, dataCountsDoc.get("latest_record_timestamp")); response = client().performRequest("delete", MachineLearning.BASE_PATH + "anomaly_detectors/" + jobId); assertEquals(200, response.getStatusLine().getStatusCode()); }
From source file:org.apache.unomi.itests.BasicIT.java
@Test public void testContextJSON() throws IOException { String sessionId = "aa3b04bd-8f4d-4a07-8e96-d33ffa04d3d9"; ContextRequest contextRequest = new ContextRequest(); // contextRequest.setSource(new EventSource()); // contextRequest.getSource().setId("af6f393a-a537-4586-991b-8521b9c7b05b"); HttpPost request = new HttpPost(URL + "/context.json?sessionId=" + sessionId); request.setEntity(new StringEntity(objectMapper.writeValueAsString(contextRequest), ContentType.create("application/json"))); CloseableHttpResponse response = HttpClientBuilder.create().build().execute(request); try {//w w w . j a v a2s. co m // validate mimeType String mimeType = ContentType.getOrDefault(response.getEntity()).getMimeType(); Assert.assertEquals("Response content type should be " + JSON_MYME_TYPE, JSON_MYME_TYPE, mimeType); // validate context ContextResponse context = TestUtils.retrieveResourceFromResponse(response, ContextResponse.class); Assert.assertNotNull("Context should not be null", context); Assert.assertNotNull("Context profileId should not be null", context.getProfileId()); Assert.assertEquals("Context sessionId should be the same as the sessionId used to request the context", sessionId, context.getSessionId()); } finally { response.close(); } }
From source file:com.joyent.manta.http.ContentTypeLookupTest.java
public void canFindByFilename() { MantaHttpHeaders headers = new MantaHttpHeaders(EXAMPLE_HEADERS); ContentType troff = ContentType.create("application/x-troff"); ContentType jsonStream = ContentType.create("application/x-json-stream"); Assert.assertNull(ContentTypeLookup.findOrDefaultContentType(null, "/tmp/unknown", null)); Assert.assertNull(ContentTypeLookup.findOrDefaultContentType(headers, "/tmp/unknown", null)); Assert.assertEquals(//w w w. j a v a2 s . c o m ContentTypeLookup.findOrDefaultContentType(headers, "/tmp/unknown", troff).getMimeType(), troff.getMimeType()); Assert.assertEquals( ContentTypeLookup.findOrDefaultContentType(headers, "/tmp/foo.jpeg", troff).getMimeType(), "image/jpeg"); headers.put("Content-Type", "application/x-json-stream; type=directory"); Assert.assertEquals( ContentTypeLookup.findOrDefaultContentType(headers, "/tmp/foo.jpeg", troff).getMimeType(), jsonStream.getMimeType()); }
From source file:com.lagrange.LabelApp.java
private static void sendPhotoToTelegramBot() { File file = new File(pathToImage.toUri()); HttpEntity httpEntity = MultipartEntityBuilder.create() .addBinaryBody("photo", file, ContentType.create("image/jpeg"), file.getName()) .addTextBody("chat_id", CHAT_ID).build(); String urlSendPhoto = "https://api.telegram.org/" + BOT_ID + "/sendPhoto"; CloseableHttpClient httpclient = HttpClients.createDefault(); HttpPost httpPost = new HttpPost(urlSendPhoto); httpPost.setEntity(httpEntity);//from w w w .j a va 2 s . c om try { CloseableHttpResponse response = httpclient.execute(httpPost); } catch (IOException e) { e.printStackTrace(); } System.out.println("Done sending photo"); }
From source file:com.serphacker.serposcope.scraper.captcha.solver.DeathByCaptchaSolver.java
@Override public boolean solve(Captcha cap) { if (!(cap instanceof CaptchaImage)) { return false; }//from www .ja va2 s .co m captchaCount.incrementAndGet(); CaptchaImage captcha = (CaptchaImage) cap; captcha.setLastSolver(this); captcha.setStatus(Captcha.Status.CREATED); String filename = null; String textMimeType = captcha.getMimes()[0]; String[] mimes = null; if (textMimeType != null) mimes = textMimeType.split("/"); else textMimeType = "application/octet-stream"; if (mimes != null && mimes.length == 2) { if (isValidImageExtension(mimes[1])) { filename = "image." + mimes[1]; } } else { filename = "image.png"; } Map<String, Object> data = getMapWithCredentials(); data.put("captchafile", new ByteArrayBody(captcha.getImage(), ContentType.create(textMimeType), filename)); long started = System.currentTimeMillis(); captcha.setStatus(Captcha.Status.SUBMITTED); try (ScrapClient http = new ScrapClient()) { int httpStatus = 0; int retry = 0; while (true) { httpStatus = http.post(apiUrl + "captcha", data, ScrapClient.PostType.MULTIPART); if (!isRetryableStatus(httpStatus)) { break; } if (++retry > maxRetryOnOverload) { break; } try { Long sleep = 5000l * retry; LOG.debug("server is overloaded, sleeping {} ms", sleep); Thread.sleep(sleep); } catch (InterruptedException ex) { break; } } if (httpStatus >= 500) { captcha.setError(Captcha.Error.SERVICE_OVERLOADED); return false; } else if (httpStatus == 403) { captcha.setError(Captcha.Error.INVALID_CREDENTIALS); return false; } else if (httpStatus == 400) { captcha.setError(Captcha.Error.INVALID_CREDENTIALS); return false; } else if (httpStatus != 303) { captcha.setError(Captcha.Error.NETWORK_ERROR); return false; } String location = http.getResponseHeader("location"); if (location == null || !location.startsWith(apiUrl)) { LOG.warn("invalid location : {}", location); captcha.setError(Captcha.Error.NETWORK_ERROR); return false; } String captchaId = extractId(location); if (captchaId == null) { captcha.setError(Captcha.Error.NETWORK_ERROR); return false; } captcha.setId(captchaId); long timeLimit = System.currentTimeMillis() + timeoutMS; while (System.currentTimeMillis() < timeLimit) { int status = http.get(location + "?" + random.nextInt(Integer.MAX_VALUE)); if (status == 200) { Map<String, String> answer = parseAnswer(http.getContentAsString()); if (answer.get("text") != null && !answer.get("text").isEmpty()) { captcha.setResponse(answer.get("text")); captcha.setStatus(Captcha.Status.SOLVED); return true; } } try { Thread.sleep(POLLING_PAUSE_MS); } catch (InterruptedException ex) { break; } } captcha.setError(Captcha.Error.TIMEOUT); captcha.setStatus(Captcha.Status.ERROR); } catch (IOException ex) { LOG.error("io exception", ex); captcha.setError(EXCEPTION); } finally { captcha.setSolveDuration(System.currentTimeMillis() - started); } return false; }
From source file:org.jasig.cas.util.http.SimpleHttpClient.java
@Override public boolean sendMessageToEndPoint(final HttpMessage message) { Assert.notNull(this.httpClient); try {/*from w ww . ja va2s . c om*/ final HttpPost request = new HttpPost(message.getUrl().toURI()); request.addHeader("Content-Type", message.getContentType()); final StringEntity entity = new StringEntity(message.getMessage(), ContentType.create(message.getContentType())); request.setEntity(entity); final HttpRequestFutureTask<String> task = this.requestExecutorService.execute(request, HttpClientContext.create(), new BasicResponseHandler()); if (message.isAsynchronous()) { return true; } return StringUtils.isNotBlank(task.get()); } catch (final RejectedExecutionException e) { LOGGER.warn(e.getMessage(), e); return false; } catch (final Exception e) { LOGGER.trace(e.getMessage(), e); return false; } }