List of usage examples for org.apache.http.entity ByteArrayEntity ByteArrayEntity
public ByteArrayEntity(byte[] bArr)
From source file:com.selene.volley.stack.HttpClientStack.java
/** * Creates the appropriate subclass of HttpUriRequest for passed in request. *//*from www .java2 s .c om*/ @SuppressWarnings("deprecation") /* protected */ static HttpUriRequest createHttpRequest(Request<?> request, Map<String, String> additionalHeaders) throws AuthFailureError { switch (request.getMethod()) { case Method.DEPRECATED_GET_OR_POST: // This is the deprecated way that needs to be handled for backwards // compatibility. // If the request's post body is null, then the assumption is that // the request is // GET. Otherwise, it is assumed that the request is a POST. byte[] postBody = request.getPostBody(); if (postBody != null) { HttpPost postRequest = new HttpPost(request.getUrl()); postRequest.addHeader(HEADER_CONTENT_TYPE, request.getPostBodyContentType()); HttpEntity entity; entity = new ByteArrayEntity(postBody); postRequest.setEntity(entity); return postRequest; } else { return new HttpGet(request.getUrl()); } case Method.GET: return new HttpGet(request.getUrl()); case Method.DELETE: return new HttpDelete(request.getUrl()); case Method.POST: { HttpPost postRequest = new HttpPost(request.getUrl()); postRequest.addHeader(HTTP.CONTENT_TYPE, request.getBodyContentType()); setEntityIfNonEmptyBody(postRequest, request); return postRequest; } case Method.PUT: { HttpPut putRequest = new HttpPut(request.getUrl()); putRequest.addHeader(HTTP.CONTENT_TYPE, request.getBodyContentType()); setEntityIfNonEmptyBody(putRequest, request); return putRequest; } case Method.HEAD: return new HttpHead(request.getUrl()); case Method.OPTIONS: return new HttpOptions(request.getUrl()); case Method.TRACE: return new HttpTrace(request.getUrl()); case Method.PATCH: { HttpPatch patchRequest = new HttpPatch(request.getUrl()); patchRequest.addHeader(HTTP.CONTENT_TYPE, request.getBodyContentType()); setEntityIfNonEmptyBody(patchRequest, request); return patchRequest; } default: throw new IllegalStateException("Unknown request method."); } }
From source file:com.spotworld.spotapp.widget.utils.volley.toolbox.HttpClientStack.java
/** * Creates the appropriate subclass of HttpUriRequest for passed in request. *//*ww w . j a va2 s. c o m*/ @SuppressWarnings("deprecation") /* protected */static HttpUriRequest createHttpRequest(Request<?> request, Map<String, String> additionalHeaders) throws AuthFailureError { switch (request.getMethod()) { case Method.DEPRECATED_GET_OR_POST: { // This is the deprecated way that needs to be handled for backwards compatibility. // If the request's post body is null, then the assumption is that the request is // GET. Otherwise, it is assumed that the request is a POST. byte[] postBody = request.getPostBody(); if (postBody != null) { HttpPost postRequest = new HttpPost(request.getUrl()); postRequest.addHeader(HEADER_CONTENT_TYPE, request.getPostBodyContentType()); HttpEntity entity; entity = new ByteArrayEntity(postBody); postRequest.setEntity(entity); return postRequest; } else { return new HttpGet(request.getUrl()); } } case Method.GET: return new HttpGet(request.getUrl()); case Method.DELETE: return new HttpDelete(request.getUrl()); case Method.POST: { HttpPost postRequest = new HttpPost(request.getUrl()); postRequest.addHeader(HEADER_CONTENT_TYPE, request.getBodyContentType()); setEntityIfNonEmptyBody(postRequest, request); return postRequest; } case Method.PUT: { HttpPut putRequest = new HttpPut(request.getUrl()); putRequest.addHeader(HEADER_CONTENT_TYPE, request.getBodyContentType()); setEntityIfNonEmptyBody(putRequest, request); return putRequest; } case Method.HEAD: return new HttpHead(request.getUrl()); case Method.OPTIONS: return new HttpOptions(request.getUrl()); case Method.TRACE: return new HttpTrace(request.getUrl()); case Method.PATCH: { HttpPatch patchRequest = new HttpPatch(request.getUrl()); patchRequest.addHeader(HEADER_CONTENT_TYPE, request.getBodyContentType()); setEntityIfNonEmptyBody(patchRequest, request); return patchRequest; } default: throw new IllegalStateException("Unknown request method."); } }
From source file:com.ge.research.semtk.services.client.RestClient.java
/** * Make the service call. // www.ja v a 2s .c om * @return an Object that can be cast to a JSONObject. Subclasses may override and return a more useful Object. */ public Object execute() throws ConnectException, Exception { // TODO can we do this before calling execute()? buildParametersJSON(); // set all parameters available upon instantiation System.out.println("EXECUTE ON " + this.conf.getServiceURL()); if (parametersJSON == null) { throw new Exception("Service parameters not set"); } DefaultHttpClient httpclient = new DefaultHttpClient(); // immediate line below removed to perform htmml encoding in stream // HttpEntity entity = new ByteArrayEntity(parametersJSON.toJSONString().getBytes("UTF-8")); // js version: return String(str).replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"').replace(/%/g, "%") HttpEntity entity = new ByteArrayEntity(parametersJSON.toString().getBytes("UTF-8")); HttpPost httppost = new HttpPost(this.conf.getServiceURL()); httppost.setEntity(entity); httppost.setHeader(HttpHeaders.CONTENT_TYPE, "application/json"); // execute HttpHost targetHost = new HttpHost(this.conf.getServiceServer(), this.conf.getServicePort(), this.conf.getServiceProtocol()); HttpResponse httpresponse = httpclient.execute(targetHost, httppost); // handle the output String responseTxt = EntityUtils.toString(httpresponse.getEntity(), "UTF-8"); httpclient.close(); if (responseTxt == null) { throw new Exception("Received null response text"); } if (responseTxt.trim().isEmpty()) { handleEmptyResponse(); // implementation-specific behavior } if (responseTxt.length() < 500) { System.err.println("RestClient received: " + responseTxt); } else { System.err.println("RestClient received: " + responseTxt.substring(0, 200) + "... (" + responseTxt.length() + " chars)"); } JSONObject responseParsed = (JSONObject) JSONValue.parse(responseTxt); if (responseParsed == null) { System.err.println("The response could not be transformed into json"); if (responseTxt.contains("Error")) { throw new Exception(responseTxt); } } return responseParsed; }
From source file:com.aiven.seafox.controller.http.volley.toolbox.HttpClientStack.java
/** * Creates the appropriate subclass of HttpUriRequest for passed in request. *//*from w w w. j ava2 s. c o m*/ @SuppressWarnings("deprecation") /* protected */ static HttpUriRequest createHttpRequest(Request<?> request, Map<String, String> additionalHeaders) throws AuthFailureError { switch (request.getMethod()) { case Request.Method.DEPRECATED_GET_OR_POST: { // This is the deprecated way that needs to be handled for backwards compatibility. // If the request's post body is null, then the assumption is that the request is // GET. Otherwise, it is assumed that the request is a POST. byte[] postBody = request.getPostBody(); if (postBody != null) { HttpPost postRequest = new HttpPost(request.getUrl()); postRequest.addHeader(HEADER_CONTENT_TYPE, request.getPostBodyContentType()); HttpEntity entity; entity = new ByteArrayEntity(postBody); postRequest.setEntity(entity); return postRequest; } else { return new HttpGet(request.getUrl()); } } case Request.Method.GET: return new HttpGet(request.getUrl()); case Request.Method.DELETE: return new HttpDelete(request.getUrl()); case Request.Method.POST: { HttpPost postRequest = new HttpPost(request.getUrl()); postRequest.addHeader(HEADER_CONTENT_TYPE, request.getBodyContentType()); setEntityIfNonEmptyBody(postRequest, request); return postRequest; } case Request.Method.PUT: { HttpPut putRequest = new HttpPut(request.getUrl()); putRequest.addHeader(HEADER_CONTENT_TYPE, request.getBodyContentType()); setEntityIfNonEmptyBody(putRequest, request); return putRequest; } case Method.HEAD: return new HttpHead(request.getUrl()); case Method.OPTIONS: return new HttpOptions(request.getUrl()); case Method.TRACE: return new HttpTrace(request.getUrl()); case Request.Method.PATCH: { HttpPatch patchRequest = new HttpPatch(request.getUrl()); patchRequest.addHeader(HEADER_CONTENT_TYPE, request.getBodyContentType()); setEntityIfNonEmptyBody(patchRequest, request); return patchRequest; } default: throw new IllegalStateException("Unknown request method."); } }
From source file:org.fcrepo.camel.FcrepoClientTest.java
@Test(expected = FcrepoOperationFailedException.class) public void testGetError() throws Exception { final int status = 400; final URI uri = create(baseUrl); final ByteArrayEntity entity = new ByteArrayEntity(rdfXml.getBytes()); entity.setContentType(RDF_XML);/*from w w w.ja v a2 s . c o m*/ doSetupMockRequest(RDF_XML, entity, status); testClient.get(uri, RDF_XML, "return=representation"); }
From source file:org.talend.dataprep.api.service.command.transformation.SuggestDataSetActions.java
/** * Retrieve the dataset metadata and look for the possible actions. * //from w ww . ja va 2s .co m * @return the dataset possible actions. */ private HttpRequestBase onExecute() { try { // retrieve dataset metadata DataSetMetadata metadata = getInput(); // queries its possible actions final HttpPost post = new HttpPost(transformationServiceUrl + "/suggest/dataset"); post.setHeader(new BasicHeader("Content-Type", MediaType.APPLICATION_JSON_VALUE)); byte[] dataSetMetadataJSON = objectMapper.writer().writeValueAsBytes(metadata); post.setEntity(new ByteArrayEntity(dataSetMetadataJSON)); return post; } catch (JsonProcessingException e) { throw new TDPException(CommonErrorCodes.UNEXPECTED_EXCEPTION, e); } }
From source file:ca.uhn.fhir.rest.client.apache.ApacheHttpClient.java
@Override public IHttpRequest createByteRequest(FhirContext theContext, String theContents, String theContentType, EncodingEnum theEncoding) {//w ww . j av a2s. c o m /* * We aren't using a StringEntity here because the constructors * supported by Android aren't available in non-Android, and vice versa. * Since we add the content type header manually, it makes no difference * which one we use anyhow. */ ByteArrayEntity entity = new ByteArrayEntity(theContents.getBytes(Constants.CHARSET_UTF8)); ApacheHttpRequest retVal = createHttpRequest(entity); addHeadersToRequest(retVal, theEncoding, theContext); retVal.addHeader(Constants.HEADER_CONTENT_TYPE, theContentType + Constants.HEADER_SUFFIX_CT_UTF_8); return retVal; }
From source file:com.ibm.connectors.splunklog.SplunkHttpConnection.java
public Properties sendLogEvent(SplunkConnectionData connData, byte[] thePayload, Properties properties) throws ConnectorException { HttpClient myhClient = HttpClients.custom().setConnectionManager(this.cm).build(); HttpClientContext myhContext = HttpClientContext.create(); CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT), new UsernamePasswordCredentials(connData.getUser(), connData.getPass())); AuthCache authCache = new BasicAuthCache(); authCache.put(new HttpHost(connData.getHost(), Integer.parseInt(connData.getPort()), connData.getScheme()), new BasicScheme()); myhContext.setCredentialsProvider(credsProvider); myhContext.setAuthCache(authCache);/* ww w . ja v a2 s.co m*/ HttpPost myhPost = new HttpPost(connData.getSplunkURI()); ByteArrayEntity payload = new ByteArrayEntity(thePayload); try { myhPost.setEntity(payload); HttpResponse response = myhClient.execute(myhPost, myhContext); Integer statusCode = response.getStatusLine().getStatusCode(); if (statusCode != 200 && !connData.getIgnoreSplunkErrors()) { throw new ConnectorException( "Error posting log event to Splunk: " + response.getStatusLine().toString()); } System.out.println(response.getStatusLine().toString()); properties.setProperty("status", String.valueOf(statusCode)); Integer leasedConns = Integer .valueOf(((PoolingHttpClientConnectionManager) this.cm).getTotalStats().getLeased()); properties.setProperty("conns_leased", leasedConns.toString()); Integer availConns = Integer .valueOf(((PoolingHttpClientConnectionManager) this.cm).getTotalStats().getAvailable()); properties.setProperty("conns_available", availConns.toString()); } catch (IOException e) { e.fillInStackTrace(); throw new ConnectorException(e.toString()); } return properties; }
From source file:th.ac.kmutt.chart.rest.application.Main.java
private static void systemConfig(SystemM param) { SystemM sys = param;/*from ww w . j a v a2 s.c o m*/ String url = "system"; HttpPost httppost = new HttpPost("http://localhost:8081/ChartServices/rest/" + url); XStream xstream = new XStream(new Dom4JDriver()); Class c = null; try { c = Class.forName(sys.getClass().getName()); } catch (ClassNotFoundException e2) { e2.printStackTrace(); } xstream.processAnnotations(c); ByteArrayEntity entity = null; String xString = xstream.toXML(sys); try { entity = new ByteArrayEntity(xString.getBytes("UTF-8")); } catch (UnsupportedEncodingException e1) { e1.printStackTrace(); } httppost.setEntity(entity); CloseableHttpClient httpclient = HttpClientBuilder.create().build(); HttpResponse response = null; HttpEntity resEntity = null; try { response = httpclient.execute(httppost); resEntity = response.getEntity(); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } //httpclient.getConnectionManager().shutdown(); try { httpclient.close(); } catch (IOException e) { e.printStackTrace(); } }
From source file:gmusic.api.api.comm.ApacheConnector.java
@Override public final synchronized String dispatchPost(final URI address, final FormBuilder form) throws IOException, URISyntaxException { final HttpPost request = new HttpPost(); request.setEntity(new ByteArrayEntity(form.getBytes())); if (!Strings.isNullOrEmpty(form.getContentType())) { request.setHeader("Content-Type", form.getContentType()); }//from w w w . ja v a2 s .com final String response = EntityUtils.toString(execute(address, request).getEntity()); if (!isStartup) { return response; } return setupAuthentication(response); }