List of usage examples for org.apache.http.entity ByteArrayEntity setContentType
public void setContentType(Header header)
From source file:com.basho.riak.client.util.ClientHelper.java
/** * See/*from ww w. ja v a 2 s.c om*/ * {@link RiakClient#setBucketSchema(String, com.basho.riak.client.http.RiakBucketInfo, RequestMeta)} */ public HttpResponse setBucketSchema(String bucket, JSONObject schema, RequestMeta meta) { if (schema == null) { schema = new JSONObject(); } if (meta == null) { meta = new RequestMeta(); } meta.setHeader(Constants.HDR_ACCEPT, Constants.CTYPE_JSON); HttpPut put = new HttpPut(ClientUtils.makeURI(config, bucket)); ByteArrayEntity entity = new ByteArrayEntity(utf8StringToBytes(schema.toString())); entity.setContentType(Constants.CTYPE_JSON); put.setEntity(entity); return executeMethod(bucket, null, put, meta); }
From source file:org.fcrepo.camel.FcrepoClientTest.java
@Test public void testBadResponseBody() throws IOException, FcrepoOperationFailedException { final int status = 200; final URI uri = create(baseUrl); final ByteArrayEntity entity = new ByteArrayEntity(rdfXml.getBytes()); entity.setContentType(RDF_XML); doSetupMockRequest(RDF_XML, entity, status); when(mockResponse.getEntity()).thenReturn(mockEntity); when(mockEntity.getContent()).thenThrow(new IOException("Expected IO error")); final FcrepoResponse response = testClient.get(uri, RDF_XML, "return=minimal"); assertEquals(response.getUrl(), uri); assertEquals(response.getStatusCode(), status); assertEquals(response.getContentType(), RDF_XML); assertEquals(response.getLocation(), null); assertEquals(response.getBody(), null); }
From source file:tr.com.turkcellteknoloji.turkcellupdater.RestRequest.java
/** * Subclasses may override this method to place different object types to request body * @return contents of request.//w w w.j ava2 s . c o m * @throws JsonConversionException * @throws JSONException */ protected HttpEntity getRequestContents() throws Exception { try { final JSONObject jsonObject; if (inputJsonObject == null) { jsonObject = new JSONObject(); } else { jsonObject = inputJsonObject; } final String string = jsonObject.toString(); final byte[] bytes; bytes = string.getBytes("UTF-8"); final ByteArrayEntity result = new ByteArrayEntity(bytes); result.setContentType("application/json; charset=UTF-8"); return result; } catch (Exception e) { throw new Exception("Couldn't create request contents", e); } }
From source file:eu.esdihumboldt.hale.io.geoserver.rest.AbstractResourceManager.java
/** * @see eu.esdihumboldt.hale.io.geoserver.rest.ResourceManager#create(java.util.Map) *//*from w w w . ja va 2s .co m*/ @Override public URL create(Map<String, String> parameters) { checkResourceSet(); try { URI requestUri = buildRequestUri(getResourceListURL(), parameters); ByteArrayEntity entity = new ByteArrayEntity(resource.asByteArray()); entity.setContentType(resource.contentType().getMimeType()); return executor.execute(Request.Post(requestUri).body(entity)) .handleResponse(new ResponseHandler<URL>() { /** * @see org.apache.http.client.ResponseHandler#handleResponse(org.apache.http.HttpResponse) */ @Override public URL handleResponse(HttpResponse response) throws ClientProtocolException, IOException { StatusLine statusLine = response.getStatusLine(); if (statusLine.getStatusCode() >= 300) { throw new HttpResponseException(statusLine.getStatusCode(), statusLine.getReasonPhrase()); } if (statusLine.getStatusCode() == 201) { Header locationHeader = response.getFirstHeader("Location"); if (locationHeader != null) { return new URL(locationHeader.getValue()); } } return null; } }); } catch (Exception e) { throw new ResourceException(e); } }
From source file:org.opendatakit.http.conn.GaeManagedClientConnection.java
@Override public void receiveResponseEntity(HttpResponse resp) throws HttpException, IOException { if (resp == null) { throw new IllegalArgumentException("HttpResponse cannot be null"); }//from ww w . j a v a 2 s.c o m if (response == null) { throw new IllegalStateException("no response avaliable"); } byte[] byteArray = response.getContent(); if (byteArray != null) { ByteArrayEntity entity = new ByteArrayEntity(response.getContent()); entity.setContentType(resp.getFirstHeader(HTTP.CONTENT_TYPE)); resp.setEntity(entity); } }
From source file:org.obiba.opal.rest.client.magma.OpalJavaClient.java
public HttpResponse post(URI uri, Message msg) throws IOException { HttpPost post = new HttpPost(uri); ByteArrayEntity e = new ByteArrayEntity(asByteArray(msg)); e.setContentType("application/x-protobuf"); post.setEntity(e);/*from w ww . java2s.c om*/ return execute(post); }
From source file:org.obiba.opal.rest.client.magma.OpalJavaClient.java
public HttpResponse post(URI uri, Iterable<? extends Message> msg) throws IOException { HttpPost post = new HttpPost(uri); ByteArrayEntity e = new ByteArrayEntity(asByteArray(msg)); e.setContentType("application/x-protobuf"); post.setEntity(e);/*from w w w.ja va 2s .c om*/ return execute(post); }
From source file:org.esxx.js.protocol.HTTPHandler.java
private void attachObject(Object data, ContentType ct, HttpEntityEnclosingRequest request, Context cx) throws IOException { // FIXME: This may store the data three times in memory -- If // there were a way to turn the Object into an InputStream // instead, we would not have this problem. ByteArrayOutputStream bos = new ByteArrayOutputStream(); ct = ESXX.getInstance().serializeObject(data, ct, bos, true); ByteArrayEntity bae = new ByteArrayEntity(bos.toByteArray()); bae.setContentType(ct.toString()); request.setEntity(bae);/*from w w w.j a v a 2 s .c o m*/ }
From source file:com.twilio.sdk.AppEngineClientConnection.java
public void receiveResponseEntity(HttpResponse response) throws HttpException, IOException { if (this.response == null) { throw new IOException("receiveResponseEntity() called when closed"); }/*w ww. ja v a 2 s .co m*/ ByteArrayEntity bae; try { Method getContentMethod = this.response.getClass().getMethod("getContent", new Class[0]); bae = new ByteArrayEntity((byte[]) getContentMethod.invoke(this.response, new Object[0])); bae.setContentType(response.getFirstHeader("Content-Type")); response.setEntity(bae); response = null; } catch (Exception e) { e.printStackTrace(); throw new HttpException("Error occurred while using Google App Engine URL fetch"); } }