List of usage examples for org.apache.http.entity StringEntity StringEntity
public StringEntity(String str) throws UnsupportedEncodingException
From source file:onl.area51.filesystem.http.server.SaveContentAction.java
@Override public void apply(Request request) throws HttpException, IOException { Path path = request.getAttribute("path"); HttpRequest req = request.getHttpRequest(); if (path != null && req instanceof HttpEntityEnclosingRequest) { HttpEntityEnclosingRequest entityRequest = (HttpEntityEnclosingRequest) req; HttpEntity entity = entityRequest.getEntity(); Files.copy(entity.getContent(), path, StandardCopyOption.REPLACE_EXISTING); request.getHttpResponse().setStatusCode(HttpStatus.SC_OK); request.getHttpResponse().setEntity(new StringEntity("OK")); } else {//from w w w .j ava 2 s. c o m request.getHttpResponse().setStatusCode(HttpStatus.SC_BAD_REQUEST); request.getHttpResponse().setEntity(new StringEntity("BAD REQUEST")); } }
From source file:org.finra.msl.client.MockAPI.java
/** * Register a template that is passed to the web-server where the it is * stored using the ID as the key. This is used later when setting up a mock * response.//from w w w . j a v a2 s . c o m * * @param server * => url of web-server.js running on node * @param port * => port number of web-server.js running on node * @param template * => the template that is to be registered for later use * @param id * => key used to indicate which template is to be used when * mocking a response * @return => returns the response * @throws Exception */ public static String registerTemplate(String server, int port, String template, String id) throws Exception { HttpPost post = null; JSONObject object = new JSONObject(); URIBuilder builder = new URIBuilder(); builder.setScheme("http").setHost(server).setPort(port).setPath("/mock/template"); post = new HttpPost(builder.toString()); object.put("template", template); object.put("id", id); post.setHeader("Content-Type", "application/json"); post.setEntity(new StringEntity(object.toString())); HttpClient client = new DefaultHttpClient(); HttpResponse resp = client.execute(post); if (resp.getStatusLine().getStatusCode() != 200) { throw new Exception("POST failed. Error code: " + resp.getStatusLine().getStatusCode()); } return EntityUtils.toString(resp.getEntity()); }
From source file:org.fcrepo.camel.indexing.triplestore.integration.TestUtils.java
/** * Post data to the provided URL// www . j a v a 2 s. c om */ public static void httpPost(final String url, final String content, final String mimeType) throws Exception { final CloseableHttpClient httpClient = HttpClients.createDefault(); final HttpPost post = new HttpPost(url); post.addHeader(Exchange.CONTENT_TYPE, mimeType); post.setEntity(new StringEntity(content)); httpClient.execute(post); }
From source file:com.linkedin.pinot.controller.api.resources.PinotFileUploadTest.java
@Test public void testUploadBogusData() throws Exception { org.apache.http.client.HttpClient httpClient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost(_controllerRequestURLBuilder.forDataFileUpload()); HttpEntity entity = new StringEntity("blah"); httpPost.setEntity(entity);/*from w w w .ja v a 2s . co m*/ HttpResponse response = httpClient.execute(httpPost); int statusCode = response.getStatusLine().getStatusCode(); Assert.assertTrue(statusCode >= 400 && statusCode < 500, "Status code = " + statusCode); }
From source file:com.dtstack.jlogstash.distributed.http.cilent.HttpClient.java
public static String post(String url, Map<String, Object> bodyData) { String responseBody = null;// www .ja v a 2 s . co m CloseableHttpClient httpClient = null; try { httpClient = getHttpClient(); HttpPost httPost = new HttpPost(url); if (SetTimeOut) { RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(SocketTimeout) .setConnectTimeout(ConnectTimeout).build();// httPost.setConfig(requestConfig); } if (bodyData != null && bodyData.size() > 0) { httPost.setEntity(new StringEntity(objectMapper.writeValueAsString(bodyData))); } //? CloseableHttpResponse response = httpClient.execute(httPost); int status = response.getStatusLine().getStatusCode(); if (status == HttpStatus.SC_OK) { HttpEntity entity = response.getEntity(); //FIXME ?header? responseBody = EntityUtils.toString(entity, Charsets.UTF_8); } else { logger.error("url:" + url + "--->http return status error:" + status); } } catch (Exception e) { logger.error("url:" + url + "--->http request error", e); } finally { try { if (httpClient != null) httpClient.close(); } catch (Exception e) { // TODO Auto-generated catch block logger.error(ExceptionUtil.getErrorMessage(e)); } } return responseBody; }
From source file:org.ojbc.util.TestHttpUtils.java
@Test public void test() throws Exception { final String responseString = "yeppers"; LocalTestServer server = new LocalTestServer(null, null); server.register("/test", new HttpRequestHandler() { @Override//ww w .j ava 2 s .c o m public void handle(HttpRequest request, HttpResponse response, HttpContext context) throws HttpException, IOException { response.setEntity(new StringEntity(responseString)); } }); server.start(); String host = server.getServiceAddress().getHostName(); int port = server.getServiceAddress().getPort(); String response = HttpUtils.post("ok?", "http://" + host + ":" + port + "/test"); assertEquals(responseString, response); }
From source file:org.sharetask.controller.UserControllerIT.java
@Test public void testCreateUser() throws IOException { //given//from w ww .ja v a 2s . c om final HttpPost httpPost = new HttpPost(URL_USER); httpPost.addHeader(new BasicHeader("Content-Type", "application/json")); final StringEntity httpEntity = new StringEntity("{\"username\":\"it@shareta.sk\"," + "\"password\":\"Password\"," + "\"name\":\"Integration\"," + "\"surName\":\"Test\", " + "\"mobilePhone\":\"+420123456789\", " + "\"language\":\"en\"}"); httpPost.setEntity(httpEntity); //when final HttpResponse response = getClient().execute(httpPost); //then Assert.assertEquals(HttpStatus.OK.value(), response.getStatusLine().getStatusCode()); final String responseData = EntityUtils.toString(response.getEntity()); Assert.assertTrue(responseData.contains("\"username\":\"it@shareta.sk\"")); Assert.assertTrue(responseData.contains("\"mobilePhone\":\"+420123456789\"")); }
From source file:tk.jomp16.plugin.cyanogenmod.download.Download.java
public void download(CommandEvent commandEvent) throws IOException { if (commandEvent.getArgs().size() >= 1) { System.out.println(commandEvent.getArgs()); DeviceInfo deviceInfo = Device.getDevices().get(commandEvent.getArgs().get(0)); if (deviceInfo != null) { HttpPost httpPost = new HttpPost(CM_DOWNLOAD_API); StringEntity json = new StringEntity(gson.toJson(new CMApiRequest(deviceInfo.getCodename()))); json.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json")); httpPost.setEntity(json);// w w w.j a v a 2 s. c om CloseableHttpClient httpClient = HttpClients.createDefault(); HttpContext context = new BasicHttpContext(); HttpResponse response = httpClient.execute(httpPost, context); if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) { return; } DownloadInfo downloadInfo; try (BufferedReader reader = new BufferedReader( new InputStreamReader(response.getEntity().getContent()))) { downloadInfo = gson.fromJson(reader, DownloadInfo.class); } if (downloadInfo.result.size() > 0) { DownloadInfo.Result latest = downloadInfo.result.get(0); commandEvent.respond("Latest build (" + latest.channel + ") for " + deviceInfo.getCodename() + ": " + latest.url + " [md5sum: " + latest.md5sum + "]"); } else { commandEvent.respond("No builds for " + deviceInfo.getCodename()); } } } }
From source file:net.openwatch.acluaz.http.OWServiceRequests.java
public static StringEntity jsonToSE(JSONObject json) { StringEntity se = null;//from w ww . jav a2 s. com try { se = new StringEntity(json.toString()); Log.i(TAG, "reportToJSON: " + json.toString()); return se; } catch (UnsupportedEncodingException e1) { Log.e(TAG, "json->stringentity failed"); e1.printStackTrace(); } return se; }
From source file:tech.beshu.ror.integration.FieldLevelSecurityTests.java
private static void insertDoc(String docName, RestClient restClient, String idx, String field) { if (adminClient == null) { adminClient = restClient;/*from w w w . ja v a 2s .c o m*/ } String path = "/" + IDX_PREFIX + idx + "/documents/doc-" + docName + String.valueOf(Math.random()); try { HttpPut request = new HttpPut(restClient.from(path)); request.setHeader("Content-Type", "application/json"); request.setHeader("refresh", "true"); request.setHeader("timeout", "50s"); String body = "{\"" + field + "\": \"" + docName + "\", \"dummy2\": true}"; System.out.println("inserting: " + body); request.setEntity(new StringEntity(body)); System.out.println(body(restClient.execute(request))); } catch (Exception e) { e.printStackTrace(); throw new IllegalStateException("Test problem", e); } // Polling phase.. #TODO is there a better way? try { Thread.sleep(5000); HttpResponse response; do { HttpHead request = new HttpHead(restClient.from(path)); request.setHeader("x-api-key", "p"); response = restClient.execute(request); System.out.println( "polling for " + docName + ".. result: " + response.getStatusLine().getReasonPhrase()); Thread.sleep(200); } while (response.getStatusLine().getStatusCode() != 200); } catch (Exception e) { e.printStackTrace(); throw new IllegalStateException("Cannot configure test case", e); } }