List of usage examples for org.apache.commons.httpclient.methods StringRequestEntity StringRequestEntity
public StringRequestEntity(String paramString1, String paramString2, String paramString3) throws UnsupportedEncodingException
From source file:org.apache.hadoop.fs.swift.http.SwiftRestClient.java
/** * Convert the (JSON) data to a string request as UTF-8 * @param data data/*from w w w .j a v a2 s . c o m*/ * @return the data * @throws SwiftException if for some very unexpected reason it's impossible * to convert the data to UTF-8. */ private static StringRequestEntity toJsonEntity(String data) throws SwiftException { StringRequestEntity entity; try { entity = new StringRequestEntity(data, "application/json", "UTF-8"); } catch (UnsupportedEncodingException e) { throw new SwiftException("Could not encode data as UTF-8", e); } return entity; }
From source file:org.apache.hadoop.metrics2.sink.timeline.AbstractTimelineMetricsSink.java
protected void emitMetrics(TimelineMetrics metrics) throws IOException { String connectUrl = getCollectorUri(); try {/*www. j ava2 s. co m*/ String jsonData = mapper.writeValueAsString(metrics); StringRequestEntity requestEntity = new StringRequestEntity(jsonData, "application/json", "UTF-8"); PostMethod postMethod = new PostMethod(connectUrl); postMethod.setRequestEntity(requestEntity); int statusCode = httpClient.executeMethod(postMethod); if (statusCode != 200) { LOG.info("Unable to POST metrics to collector, " + connectUrl); } else { LOG.debug("Metrics posted to Collector " + connectUrl); } } catch (ConnectException e) { throw new UnableToConnectException(e).setConnectUrl(connectUrl); } }
From source file:org.apache.hadoop.metrics2.sink.timeline.TimelineMetricsSink.java
private void emitMetrics(TimelineMetrics metrics) throws IOException { String jsonData = mapper.writeValueAsString(metrics); SocketAddress socketAddress = getServerSocketAddress(); if (socketAddress != null) { StringRequestEntity requestEntity = new StringRequestEntity(jsonData, "application/json", "UTF-8"); PostMethod postMethod = new PostMethod(getCollectorUri()); postMethod.setRequestEntity(requestEntity); int statusCode = httpClient.executeMethod(postMethod); if (statusCode != 200) { LOG.info("Unable to POST metrics to collector, " + getCollectorUri()); }//from www.j a va 2s .c o m } }
From source file:org.apache.hadoop.yarn.server.applicationhistoryservice.metrics.timeline.InfluxDBMetric.java
public static void main(String[] args) throws IOException { ObjectMapper mapper = new ObjectMapper(); AnnotationIntrospector introspector = new JaxbAnnotationIntrospector(); mapper.setAnnotationIntrospector(introspector); mapper.getSerializationConfig().setSerializationInclusion(JsonSerialize.Inclusion.NON_NULL); TimelineMetric metric = new TimelineMetric(); metric.setMetricName("cpu"); metric.setHostName("h1"); metric.setAppId("datanode"); metric.getMetricValues().putAll(Collections.singletonMap(10L, 2.0d)); InfluxDBMetric influxDBMetric = InfluxDBMetric.fromTimelineMetric(metric); List<InfluxDBMetric> list = Collections.singletonList(influxDBMetric); String jsonData = ""; try {/*ww w.ja va 2 s.c o m*/ jsonData = mapper.writeValueAsString(list); System.out.println(jsonData); } catch (IOException e) { e.printStackTrace(); } String uri = "http://162.216.151.95:8086/db/metrics/series"; Credentials defaultcreds = new UsernamePasswordCredentials("admin", "admin"); HttpClient httpClient = new HttpClient(); httpClient.getState().setCredentials(AuthScope.ANY, defaultcreds); StringRequestEntity requestEntity = new StringRequestEntity(jsonData, "application/json", "UTF-8"); PostMethod postMethod = new PostMethod(uri); postMethod.setRequestEntity(requestEntity); int statusCode = httpClient.executeMethod(postMethod); System.out.println(statusCode); }
From source file:org.apache.hadoop.yarn.server.applicationhistoryservice.metrics.timeline.InfluxDBWriter.java
public void emitMetricsToInfluxDB(TimelineMetrics metrics) throws IOException { String uri = "http://metrics-100-a-1.c.pramod-thangali.internal:8086" + "/db/metrics/series"; Credentials defaultcreds = new UsernamePasswordCredentials("admin", "admin"); HttpClient httpClient = new HttpClient(); httpClient.getState().setCredentials(AuthScope.ANY, defaultcreds); List<InfluxDBMetric> metricList = new ArrayList<InfluxDBMetric>(); for (TimelineMetric metric : metrics.getMetrics()) { InfluxDBMetric influxDBMetric = InfluxDBMetric.fromTimelineMetric(metric); metricList.add(influxDBMetric);//from w w w . jav a 2 s. c o m } String jsonData = mapper.writeValueAsString(metricList); StringRequestEntity requestEntity = new StringRequestEntity(jsonData, "application/json", "UTF-8"); PostMethod postMethod = new PostMethod(uri); postMethod.setRequestEntity(requestEntity); try { int statusCode = httpClient.executeMethod(postMethod); if (statusCode != 200) { LOG.info("Unable to POST metrics to collector, " + uri); } } finally { postMethod.releaseConnection(); } }
From source file:org.apache.hcatalog.templeton.TestWebHCatE2e.java
/** * Does a basic HTTP GET and returns Http Status code + response body * Will add the dummy user query string//from w w w . j a va 2 s . c o m */ private static MethodCallRetVal doHttpCall(String uri, HTTP_METHOD_TYPE type, Map<String, Object> data, NameValuePair[] params) throws IOException { HttpClient client = new HttpClient(); HttpMethod method; switch (type) { case GET: method = new GetMethod(uri); break; case DELETE: method = new DeleteMethod(uri); break; case PUT: method = new PutMethod(uri); if (data == null) { break; } String msgBody = JsonBuilder.mapToJson(data); LOG.info("Msg Body: " + msgBody); StringRequestEntity sre = new StringRequestEntity(msgBody, "application/json", charSet); ((PutMethod) method).setRequestEntity(sre); break; default: throw new IllegalArgumentException("Unsupported method type: " + type); } if (params == null) { method.setQueryString(new NameValuePair[] { new NameValuePair("user.name", username) }); } else { NameValuePair[] newParams = new NameValuePair[params.length + 1]; System.arraycopy(params, 0, newParams, 1, params.length); newParams[0] = new NameValuePair("user.name", username); method.setQueryString(newParams); } String actualUri = "no URI"; try { actualUri = method.getURI().toString();//should this be escaped string? LOG.debug(type + ": " + method.getURI().getEscapedURI()); int httpStatus = client.executeMethod(method); LOG.debug("Http Status Code=" + httpStatus); String resp = method.getResponseBodyAsString(); LOG.debug("response: " + resp); return new MethodCallRetVal(httpStatus, resp, actualUri, method.getName()); } catch (IOException ex) { LOG.error("doHttpCall() failed", ex); } finally { method.releaseConnection(); } return new MethodCallRetVal(-1, "Http " + type + " failed; see log file for details", actualUri, method.getName()); }
From source file:org.apache.jackrabbit.webdav.client.methods.XmlRequestEntity.java
public XmlRequestEntity(Document xmlDocument) throws IOException { super();//from ww w . j av a 2 s . c o m ByteArrayOutputStream out = new ByteArrayOutputStream(); try { TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); transformer.setOutputProperty(OutputKeys.INDENT, "no"); transformer.transform(new DOMSource(xmlDocument), new StreamResult(out)); } catch (TransformerException e) { log.error("XML serialization failed", e); IOException exception = new IOException("XML serialization failed"); exception.initCause(e); throw exception; } delegatee = new StringRequestEntity(out.toString(), "text/xml", "UTF-8"); }
From source file:org.apache.jackrabbit.webdav.server.BindTest.java
public void testResourceId() throws HttpException, IOException, DavException, URISyntaxException { String testcol = this.root + "testResourceId/"; String testuri1 = testcol + "bindtest1"; String testuri2 = testcol + "bindtest2"; int status;//from w ww. j ava 2s .c o m try { MkColMethod mkcol = new MkColMethod(testcol); status = this.client.executeMethod(mkcol); assertEquals(201, status); PutMethod put = new PutMethod(testuri1); put.setRequestEntity(new StringRequestEntity("foo", "text/plain", "UTF-8")); status = this.client.executeMethod(put); assertEquals(201, status); // enabling version control always makes the resource referenceable VersionControlMethod versioncontrol = new VersionControlMethod(testuri1); status = this.client.executeMethod(versioncontrol); assertTrue("status: " + status, status == 200 || status == 201); URI resourceId = getResourceId(testuri1); MoveMethod move = new MoveMethod(testuri1, testuri2, true); status = this.client.executeMethod(move); move.getResponseBodyAsString(); assertEquals(201, status); URI resourceId2 = getResourceId(testuri2); assertEquals(resourceId, resourceId2); } finally { DeleteMethod delete = new DeleteMethod(testcol); status = this.client.executeMethod(delete); assertTrue("status: " + status, status == 200 || status == 204); } }
From source file:org.apache.jackrabbit.webdav.server.BindTest.java
public void testSimpleBind() throws Exception { String testcol = this.root + "testSimpleBind/"; String subcol1 = testcol + "bindtest1/"; String testres1 = subcol1 + "res1"; String subcol2 = testcol + "bindtest2/"; String testres2 = subcol2 + "res2"; int status;/*w w w . ja va 2 s . c o m*/ try { MkColMethod mkcol = new MkColMethod(testcol); status = this.client.executeMethod(mkcol); assertEquals(201, status); mkcol = new MkColMethod(subcol1); status = this.client.executeMethod(mkcol); assertEquals(201, status); mkcol = new MkColMethod(subcol2); status = this.client.executeMethod(mkcol); assertEquals(201, status); //create new resource R with path bindtest1/res1 PutMethod put = new PutMethod(testres1); put.setRequestEntity(new StringRequestEntity("foo", "text/plain", "UTF-8")); status = this.client.executeMethod(put); assertEquals(201, status); //create new binding of R with path bindtest2/res2 DavMethodBase bind = new BindMethod(subcol2, new BindInfo(testres1, "res2")); status = this.client.executeMethod(bind); assertEquals(201, status); //check if both bindings report the same DAV:resource-id assertEquals(this.getResourceId(testres1), this.getResourceId(testres2)); //compare representations retrieved with both paths GetMethod get = new GetMethod(testres1); status = this.client.executeMethod(get); assertEquals(200, status); assertEquals("foo", get.getResponseBodyAsString()); get = new GetMethod(testres2); status = this.client.executeMethod(get); assertEquals(200, status); assertEquals("foo", get.getResponseBodyAsString()); //modify R using the new path put = new PutMethod(testres2); put.setRequestEntity(new StringRequestEntity("bar", "text/plain", "UTF-8")); status = this.client.executeMethod(put); assertTrue("status: " + status, status == 200 || status == 204); //compare representations retrieved with both paths get = new GetMethod(testres1); status = this.client.executeMethod(get); assertEquals(200, status); assertEquals("bar", get.getResponseBodyAsString()); get = new GetMethod(testres2); status = this.client.executeMethod(get); assertEquals(200, status); assertEquals("bar", get.getResponseBodyAsString()); } finally { DeleteMethod delete = new DeleteMethod(testcol); status = this.client.executeMethod(delete); assertTrue("status: " + status, status == 200 || status == 204); } }
From source file:org.apache.jackrabbit.webdav.server.BindTest.java
public void testRebind() throws Exception { String testcol = this.root + "testRebind/"; String subcol1 = testcol + "bindtest1/"; String testres1 = subcol1 + "res1"; String subcol2 = testcol + "bindtest2/"; String testres2 = subcol2 + "res2"; int status;/*w ww . j a va 2 s . c o m*/ try { MkColMethod mkcol = new MkColMethod(testcol); status = this.client.executeMethod(mkcol); assertEquals(201, status); mkcol = new MkColMethod(subcol1); status = this.client.executeMethod(mkcol); assertEquals(201, status); mkcol = new MkColMethod(subcol2); status = this.client.executeMethod(mkcol); assertEquals(201, status); //create new resource R with path bindtest1/res1 PutMethod put = new PutMethod(testres1); put.setRequestEntity(new StringRequestEntity("foo", "text/plain", "UTF-8")); status = this.client.executeMethod(put); assertEquals(201, status); // enabling version control always makes the resource referenceable VersionControlMethod versioncontrol = new VersionControlMethod(testres1); status = this.client.executeMethod(versioncontrol); assertTrue("status: " + status, status == 200 || status == 201); URI r1 = this.getResourceId(testres1); GetMethod get = new GetMethod(testres1); status = this.client.executeMethod(get); assertEquals(200, status); assertEquals("foo", get.getResponseBodyAsString()); //rebind R with path bindtest2/res2 DavMethodBase rebind = new RebindMethod(subcol2, new RebindInfo(testres1, "res2")); status = this.client.executeMethod(rebind); assertEquals(201, status); URI r2 = this.getResourceId(testres2); get = new GetMethod(testres2); status = this.client.executeMethod(get); assertEquals(200, status); assertEquals("foo", get.getResponseBodyAsString()); //make sure that rebind did not change the resource-id assertEquals(r1, r2); //verify that the initial binding is gone HeadMethod head = new HeadMethod(testres1); status = this.client.executeMethod(head); assertEquals(404, status); } finally { DeleteMethod delete = new DeleteMethod(testcol); status = this.client.executeMethod(delete); assertTrue("status: " + status, status == 200 || status == 204); } }