List of usage examples for org.apache.http.entity InputStreamEntity InputStreamEntity
public InputStreamEntity(InputStream inputStream, long j, ContentType contentType)
From source file:org.apache.hadoop.gateway.dispatch.PartiallyRepeatableHttpEntityTest.java
@Test public void testIsStreaming() throws Exception { String input = "0123456789"; BasicHttpEntity basic;/* ww w . j a v a 2 s .com*/ InputStreamEntity streaming; PartiallyRepeatableHttpEntity replay; basic = new BasicHttpEntity(); basic.setContent(new ByteArrayInputStream(input.getBytes(UTF8))); replay = new PartiallyRepeatableHttpEntity(basic, 5); assertThat(replay.isStreaming(), is(true)); basic = new BasicHttpEntity(); basic.setContent(null); replay = new PartiallyRepeatableHttpEntity(basic, 5); assertThat(replay.isStreaming(), is(false)); streaming = new InputStreamEntity(new ByteArrayInputStream(input.getBytes(UTF8)), 10, ContentType.TEXT_PLAIN); replay = new PartiallyRepeatableHttpEntity(streaming, 5); assertThat(replay.isStreaming(), is(true)); }
From source file:eu.scape_project.fcrepo.integration.IntellectualEntitiesIT.java
@Test public void testIngestAndFetchVersionList() throws Exception { IntellectualEntity ie1 = TestUtil.createTestEntity("entity-23"); this.postEntity(ie1); HttpPut put = new HttpPut(SCAPE_URL + "/metadata/entity-23/representation-1/file-1/bitstream-1/TECHNICAL"); ByteArrayOutputStream sink = new ByteArrayOutputStream(); this.marshaller.serialize(TestUtil.createMIXRecord(), sink); put.setEntity(new InputStreamEntity(new ByteArrayInputStream(sink.toByteArray()), sink.size(), ContentType.TEXT_XML));/*from w w w . j a va 2 s . com*/ HttpResponse resp = this.client.execute(put); assertEquals(200, resp.getStatusLine().getStatusCode()); put.releaseConnection(); /* fetch the entity and check that the title has been updated */ HttpGet get = new HttpGet(SCAPE_URL + "/entity-version-list/entity-23"); resp = this.client.execute(get); assertEquals(200, resp.getStatusLine().getStatusCode()); VersionList fetched = (VersionList) this.marshaller.deserialize(resp.getEntity().getContent()); assertEquals(2, fetched.getVersionIdentifiers().size()); get.releaseConnection(); }
From source file:com.buffalokiwi.api.API.java
/** * Perform a patch-based request to some endpoint * @param url URL/* www . j a v a 2 s .c o m*/ * @param payload Payload to send * @param contentLength the length of the payload * @param contentType the type of data in payload * @param headers additional headers to send * @return response * @throws APIException */ @Override public IAPIResponse patch(final String url, final InputStream payload, final long contentLength, final ContentType contentType, final Map<String, String> headers) throws APIException { //..Create the new patch request final HttpPatch patch = (HttpPatch) createRequest(HttpMethod.PUT, url, headers); //..Set the patch payload patch.setEntity(new InputStreamEntity(payload, contentLength, contentType)); APILog.trace(LOG, payload); //..Execute the request return executeRequest(patch); }
From source file:eu.scape_project.fcrepo.integration.IntellectualEntitiesIT.java
@Test public void testIngestAndUpdateRepresentationAndFetchOldVersion() throws Exception { IntellectualEntity ie1 = TestUtil.createTestEntity("entity-24"); this.postEntity(ie1); Representation r = new Representation.Builder(ie1.getRepresentations().get(0)).title("title update") .build();/*w ww .java2s.com*/ HttpPut put = new HttpPut(SCAPE_URL + "/representation/entity-24/" + r.getIdentifier().getValue()); ByteArrayOutputStream sink = new ByteArrayOutputStream(); this.marshaller.serialize(r, sink); put.setEntity(new InputStreamEntity(new ByteArrayInputStream(sink.toByteArray()), sink.size(), ContentType.TEXT_XML)); HttpResponse resp = this.client.execute(put); assertEquals(200, resp.getStatusLine().getStatusCode()); put.releaseConnection(); /* fetch the representation and check that the title has been updated */ HttpGet get = new HttpGet(SCAPE_URL + "/representation/entity-24/" + r.getIdentifier().getValue() + "/1"); resp = this.client.execute(get); assertEquals(200, resp.getStatusLine().getStatusCode()); Representation fetched = this.marshaller.deserialize(Representation.class, resp.getEntity().getContent()); assertEquals(r.getIdentifier().getValue(), fetched.getIdentifier().getValue()); assertEquals("Text representation", fetched.getTitle()); get.releaseConnection(); }
From source file:org.dasein.cloud.joyent.JoyentMethod.java
public @Nullable String doPostStream(@Nonnull String endpoint, @Nonnull String resource, @Nullable String md5Hash, @Nullable InputStream stream) throws CloudException, InternalException { if (logger.isTraceEnabled()) { logger.trace("ENTER - " + JoyentMethod.class.getName() + ".doPostStream(" + endpoint + "," + resource + "," + md5Hash + ",PAYLOAD)"); }//from w w w .java 2 s.co m if (wire.isDebugEnabled()) { wire.debug(""); wire.debug(">>> [POST (" + (new Date()) + ")] -> " + endpoint + "/" + resource + " >--------------------------------------------------------------------------------------"); } try { HttpClient client = clientFactory.getClient(endpoint); HttpPost post = new HttpPost(endpoint + resource); httpAuth.addPreemptiveAuth(post); post.addHeader("Content-Type", "application/octet-stream"); post.addHeader("Accept", "application/json"); post.addHeader("X-Api-Version", VERSION); post.setEntity(new InputStreamEntity(stream, -1L, ContentType.APPLICATION_OCTET_STREAM)); if (wire.isDebugEnabled()) { wire.debug(post.getRequestLine().toString()); for (Header header : post.getAllHeaders()) { wire.debug(header.getName() + ": " + header.getValue()); } wire.debug(""); wire.debug("--> BINARY DATA <--"); wire.debug(""); } HttpResponse response; try { response = client.execute(post); if (wire.isDebugEnabled()) { wire.debug(response.getStatusLine().toString()); for (Header header : response.getAllHeaders()) { wire.debug(header.getName() + ": " + header.getValue()); } wire.debug(""); } } catch (IOException e) { logger.error("I/O error from server communications: " + e.getMessage()); throw new InternalException(e); } int code = response.getStatusLine().getStatusCode(); logger.debug("HTTP STATUS: " + code); String responseHash = null; for (Header h : response.getAllHeaders()) { if (h.getName().equals("ETag")) { responseHash = h.getValue(); } } if (responseHash != null && md5Hash != null && !responseHash.equals(md5Hash)) { throw new CloudException("MD5 hash values do not match, probably data corruption"); } if (code != HttpStatus.SC_ACCEPTED && code != HttpStatus.SC_NO_CONTENT && code != HttpStatus.SC_CREATED) { logger.error("Expected ACCEPTED or NO CONTENT for POST request, got " + code); String json = null; try { HttpEntity entity = response.getEntity(); if (entity != null) { json = EntityUtils.toString(entity); if (wire.isDebugEnabled()) { wire.debug(json); wire.debug(""); } } } catch (IOException e) { logger.error("Failed to read response error due to a cloud I/O error: " + e.getMessage()); throw new CloudException(e); } JoyentException.ExceptionItems items = JoyentException.parseException(code, json); if (items == null) { items = new JoyentException.ExceptionItems(); items.code = 404; items.type = CloudErrorType.COMMUNICATION; items.message = "itemNotFound"; items.details = "No such object: " + resource; } logger.error("[" + code + " : " + items.message + "] " + items.details); throw new JoyentException(items); } else { wire.debug(""); if (code == HttpStatus.SC_ACCEPTED || code == HttpStatus.SC_CREATED) { String json = null; try { HttpEntity entity = response.getEntity(); if (entity != null) { json = EntityUtils.toString(entity); if (wire.isDebugEnabled()) { wire.debug(json); wire.debug(""); } } } catch (IOException e) { logger.error("Failed to read response error due to a cloud I/O error: " + e.getMessage()); throw new CloudException(e); } if (json != null && !json.trim().equals("")) { return json; } } return null; } } finally { if (logger.isTraceEnabled()) { logger.trace("EXIT - " + JoyentMethod.class.getName() + ".doPostStream()"); } if (wire.isDebugEnabled()) { wire.debug("<<< [POST (" + (new Date()) + ")] -> " + endpoint + "/" + resource + " <--------------------------------------------------------------------------------------"); wire.debug(""); } } }
From source file:eu.scape_project.fcrepo.integration.IntellectualEntitiesIT.java
@Test public void testIngestAndUpdateFileAndFetchOldVersion() throws Exception { IntellectualEntity ie1 = TestUtil.createTestEntity("entity-25"); this.postEntity(ie1); File f = new File.Builder(ie1.getRepresentations().get(0).getFiles().get(0)) .uri(URI.create(TestUtil.class.getClassLoader().getResource("scape_logo.png").toString())) .filename("wikipedia.png").mimetype("image/png").build(); Representation r = new Representation.Builder(ie1.getRepresentations().get(0)).title("title update") .files(Arrays.asList(f)).build(); HttpPut put = new HttpPut(SCAPE_URL + "/representation/entity-25/" + r.getIdentifier().getValue()); ByteArrayOutputStream sink = new ByteArrayOutputStream(); this.marshaller.serialize(r, sink); put.setEntity(new InputStreamEntity(new ByteArrayInputStream(sink.toByteArray()), sink.size(), ContentType.TEXT_XML));/*from w w w .j ava 2 s .c o m*/ HttpResponse resp = this.client.execute(put); assertEquals(200, resp.getStatusLine().getStatusCode()); put.releaseConnection(); /* fetch the file */ HttpGet get = new HttpGet(SCAPE_URL + "/file/entity-25/" + r.getIdentifier().getValue() + "/" + f.getIdentifier().getValue() + "/1"); resp = this.client.execute(get); assertEquals(200, resp.getStatusLine().getStatusCode()); get.releaseConnection(); }
From source file:eu.scape_project.fcrepo.integration.IntellectualEntitiesIT.java
@Test public void testIngestAndUpdateBitstreamAndFetchOldVersion() throws Exception { IntellectualEntity ie1 = TestUtil.createTestEntity("entity-26"); this.postEntity(ie1); HttpPut put = new HttpPut(SCAPE_URL + "/metadata/entity-26/representation-1/file-1/bitstream-1/TECHNICAL"); ByteArrayOutputStream sink = new ByteArrayOutputStream(); this.marshaller.serialize(TestUtil.createMIXRecord(), sink); put.setEntity(new InputStreamEntity(new ByteArrayInputStream(sink.toByteArray()), sink.size(), ContentType.TEXT_XML));/* ww w. j ava2 s .c o m*/ HttpResponse resp = this.client.execute(put); assertEquals(200, resp.getStatusLine().getStatusCode()); put.releaseConnection(); /* fetch the entity and check that the title has been updated */ HttpGet get = new HttpGet(SCAPE_URL + "/bitstream/entity-26/representation-1/file-1/bitstream-1/1"); resp = this.client.execute(get); assertEquals(200, resp.getStatusLine().getStatusCode()); BitStream fetched = (BitStream) this.marshaller.deserialize(resp.getEntity().getContent()); assertEquals(Fits.class, fetched.getTechnical().getClass()); get.releaseConnection(); }
From source file:org.dasein.cloud.rackspace.AbstractMethod.java
@SuppressWarnings("unused") protected @Nullable String postStream(@Nonnull String authToken, @Nonnull String endpoint, @Nonnull String resource, @Nullable String md5Hash, @Nullable InputStream stream) throws CloudException, InternalException { Logger std = RackspaceCloud.getLogger(RackspaceCloud.class, "std"); Logger wire = RackspaceCloud.getLogger(RackspaceCloud.class, "wire"); if (std.isTraceEnabled()) { std.trace("enter - " + AbstractMethod.class.getName() + ".postStream(" + authToken + "," + endpoint + "," + resource + "," + md5Hash + ",INPUTSTREAM)"); }// www . j ava 2 s. c o m if (wire.isDebugEnabled()) { wire.debug("---------------------------------------------------------------------------------" + endpoint + resource); wire.debug(""); } try { HttpClient client = getClient(); HttpPost post = new HttpPost(endpoint + resource); post.addHeader("Content-Type", "application/octet-stream"); post.addHeader("X-Auth-Token", authToken); post.setEntity(new InputStreamEntity(stream, -1, ContentType.APPLICATION_OCTET_STREAM)); if (wire.isDebugEnabled()) { wire.debug(post.getRequestLine().toString()); for (Header header : post.getAllHeaders()) { wire.debug(header.getName() + ": " + header.getValue()); } wire.debug(""); wire.debug("--> BINARY DATA <--"); } HttpResponse response; try { response = client.execute(post); if (wire.isDebugEnabled()) { wire.debug(response.getStatusLine().toString()); for (Header header : response.getAllHeaders()) { wire.debug(header.getName() + ": " + header.getValue()); } wire.debug(""); } } catch (IOException e) { std.error("I/O error from server communications: " + e.getMessage()); e.printStackTrace(); throw new InternalException(e); } int code = response.getStatusLine().getStatusCode(); std.debug("HTTP STATUS: " + code); String responseHash = null; for (Header h : response.getAllHeaders()) { if (h.getName().equals("ETag")) { responseHash = h.getValue(); } } if (responseHash != null && md5Hash != null && !responseHash.equals(md5Hash)) { throw new CloudException("MD5 hash values do not match, probably data corruption"); } if (code != HttpServletResponse.SC_ACCEPTED && code != HttpServletResponse.SC_NO_CONTENT) { std.error("postStream(): Expected ACCEPTED or NO CONTENT for POST request, got " + code); HttpEntity entity = response.getEntity(); String json = null; if (entity != null) { try { json = EntityUtils.toString(entity); if (wire.isDebugEnabled()) { wire.debug(json); wire.debug(""); } } catch (IOException e) { throw new CloudException(e); } } RackspaceException.ExceptionItems items = (json == null ? null : RackspaceException.parseException(code, json)); if (items == null) { items = new RackspaceException.ExceptionItems(); items.code = 404; items.type = CloudErrorType.COMMUNICATION; items.message = "itemNotFound"; items.details = "No such object: " + resource; } std.error("postStream(): [" + code + " : " + items.message + "] " + items.details); throw new RackspaceException(items); } else { wire.debug(""); if (code == HttpServletResponse.SC_ACCEPTED) { HttpEntity entity = response.getEntity(); String json = null; if (entity != null) { try { json = EntityUtils.toString(entity); if (wire.isDebugEnabled()) { wire.debug(json); wire.debug(""); } } catch (IOException e) { throw new CloudException(e); } } if (json != null && !json.trim().equals("")) { return json; } } return null; } } finally { if (std.isTraceEnabled()) { std.trace("exit - " + RackspaceCloud.class.getName() + ".postStream()"); } if (wire.isDebugEnabled()) { wire.debug(""); wire.debug("---------------------------------------------------------------------------------" + endpoint + resource); } } }
From source file:org.dasein.cloud.atmos.AtmosMethod.java
public @Nonnull Blob upload(@Nonnull String bucket, @Nonnull String name, @Nonnull InputStream input, Storage<?> size) throws CloudException, InternalException { if (logger.isTraceEnabled()) { logger.trace("ENTER - " + AtmosMethod.class.getName() + ".upload(" + bucket + "," + name + ",[CONTENT]," + size + ")"); }//from w w w . j a v a 2 s .c o m if (wire.isDebugEnabled()) { wire.debug(""); wire.debug(">>> [POST/upload binary] -> " + bucket + " / " + name + "--------------------------------------------------------------------------------------"); } try { ProviderContext ctx = provider.getContext(); if (ctx == null) { throw new CloudException("No context was set for this request"); } if (!bucket.endsWith("/")) { bucket = bucket + "/"; } if (!bucket.startsWith("/")) { bucket = "/" + bucket; } long length = size.convertTo(Storage.BYTE).getQuantity().longValue(); String endpoint = getEndpoint(ctx, EndpointType.NAMESPACE, bucket + name); HttpPost post = new HttpPost(endpoint); HttpClient client = getClient(endpoint); authorize(ctx, post, "application/octet-stream", null); post.setEntity(new InputStreamEntity(input, length, ContentType.APPLICATION_OCTET_STREAM)); if (wire.isDebugEnabled()) { wire.debug(post.getRequestLine().toString()); for (Header header : post.getAllHeaders()) { wire.debug(header.getName() + ": " + header.getValue()); } wire.debug(""); } HttpResponse response; try { response = client.execute(post); if (wire.isDebugEnabled()) { wire.debug(response.getStatusLine().toString()); } } catch (IOException e) { logger.error("I/O error from server communications: " + e.getMessage()); e.printStackTrace(); throw new InternalException(e); } int status = response.getStatusLine().getStatusCode(); if (status == HttpStatus.SC_CREATED) { return toBlob(ctx, response, bucket, name, null); } else { throw new AtmosException(response); } } finally { if (logger.isTraceEnabled()) { logger.trace("EXIT - " + AtmosMethod.class.getName() + ".upload()"); } if (wire.isDebugEnabled()) { wire.debug("<<< [POST/upload binary] -> " + bucket + " / " + name + "--------------------------------------------------------------------------------------"); wire.debug(""); } } }
From source file:org.dasein.cloud.joyent.JoyentMethod.java
protected @Nullable String putStream(@Nonnull String authToken, @Nonnull String endpoint, @Nonnull String resource, @Nullable String md5Hash, @Nullable InputStream stream) throws CloudException, InternalException { if (logger.isTraceEnabled()) { logger.trace("ENTER - " + JoyentMethod.class.getName() + ".doPutStream(" + endpoint + "," + resource + "," + md5Hash + ",PAYLOAD)"); }//from ww w .j ava 2 s.c o m if (wire.isDebugEnabled()) { wire.debug(""); wire.debug(">>> [PUT (" + (new Date()) + ")] -> " + endpoint + resource + " >--------------------------------------------------------------------------------------"); } try { HttpClient client = clientFactory.getClient(endpoint); HttpPut put = new HttpPut(endpoint + resource); put.addHeader("Content-Type", "application/octet-stream"); put.addHeader("Accept", "application/json"); put.addHeader("X-Auth-Token", authToken); if (md5Hash != null) { put.addHeader("ETag", md5Hash); } put.setEntity(new InputStreamEntity(stream, -1L, ContentType.APPLICATION_OCTET_STREAM)); if (wire.isDebugEnabled()) { wire.debug(put.getRequestLine().toString()); for (Header header : put.getAllHeaders()) { wire.debug(header.getName() + ": " + header.getValue()); } wire.debug(""); wire.debug("--> BINARY DATA <--"); wire.debug(""); } HttpResponse response; try { response = client.execute(put); if (wire.isDebugEnabled()) { wire.debug(response.getStatusLine().toString()); for (Header header : response.getAllHeaders()) { wire.debug(header.getName() + ": " + header.getValue()); } wire.debug(""); } } catch (IOException e) { logger.error("I/O error from server communications: " + e.getMessage()); throw new InternalException(e); } int code = response.getStatusLine().getStatusCode(); logger.debug("HTTP STATUS: " + code); String responseHash = null; for (Header h : response.getAllHeaders()) { if (h.getName().equals("ETag")) { responseHash = h.getValue(); } } if (responseHash != null && md5Hash != null && !responseHash.equals(md5Hash)) { throw new CloudException("MD5 hash values do not match, probably data corruption"); } if (code != HttpStatus.SC_CREATED && code != HttpStatus.SC_ACCEPTED && code != HttpStatus.SC_NO_CONTENT) { logger.error("Expected CREATED, ACCEPTED, or NO CONTENT for PUT request, got " + code); String json = null; try { HttpEntity entity = response.getEntity(); if (entity != null) { json = EntityUtils.toString(entity); if (wire.isDebugEnabled()) { wire.debug(json); wire.debug(""); } } } catch (IOException e) { logger.error("Failed to read response error due to a cloud I/O error: " + e.getMessage()); throw new CloudException(e); } JoyentException.ExceptionItems items = JoyentException.parseException(code, json); if (items == null) { items = new JoyentException.ExceptionItems(); items.code = 404; items.type = CloudErrorType.COMMUNICATION; items.message = "itemNotFound"; items.details = "No such object: " + resource; } logger.error("[" + code + " : " + items.message + "] " + items.details); throw new JoyentException(items); } else { if (code == HttpStatus.SC_ACCEPTED) { String json = null; try { HttpEntity entity = response.getEntity(); if (entity != null) { json = EntityUtils.toString(entity); if (wire.isDebugEnabled()) { wire.debug(json); wire.debug(""); } } } catch (IOException e) { logger.error("Failed to read response error due to a cloud I/O error: " + e.getMessage()); throw new CloudException(e); } if (json != null && !json.trim().equals("")) { return json; } } return null; } } finally { if (logger.isTraceEnabled()) { logger.trace("EXIT - " + JoyentMethod.class.getName() + ".doPutStream()"); } if (wire.isDebugEnabled()) { wire.debug("<<< [PUT (" + (new Date()) + ")] -> " + endpoint + resource + " <--------------------------------------------------------------------------------------"); wire.debug(""); } } }