List of usage examples for org.apache.http.entity InputStreamEntity InputStreamEntity
public InputStreamEntity(InputStream inputStream, long j, ContentType contentType)
From source file:com.joyent.manta.client.MantaClient.java
/** * Puts an object into Manta.//from w ww. j a v a 2 s. co m * * @param rawPath The path to the Manta object. * @param source {@link InputStream} to copy object data from * @param contentLength the total length of the stream (-1 if unknown) * @param headers optional HTTP headers to include when copying the object * @param metadata optional user-supplied metadata for object * @return Manta response object * @throws IOException If an IO exception has occurred. * @throws MantaClientHttpResponseException If a http status code {@literal > 300} is returned. */ public MantaObjectResponse put(final String rawPath, final InputStream source, final long contentLength, final MantaHttpHeaders headers, final MantaMetadata metadata) throws IOException { Validate.notBlank(rawPath, "rawPath must not be blank"); Validate.notNull(source, "Input stream must not be null"); final String path = formatPath(rawPath); final ContentType contentType = ContentTypeLookup.findOrDefaultContentType(headers, ContentType.APPLICATION_OCTET_STREAM); final int preLoadSize = config.getUploadBufferSize(); final HttpEntity entity; /* We don't know how big the stream is, so we read N bytes from it and * see if it ends. If it ended, then we just convert that buffer into * an entity and pass it. If it didn't end, then we create new stream * that concatenates the bytes read with the source stream. * Unfortunately, this will put us in a chunked transfer encoding and * it will affect performance. */ if (contentLength < 0) { // If our stream is a FileInputStream, then we can pull the size off of it if (source.getClass().equals(FileInputStream.class)) { FileInputStream fsin = (FileInputStream) source; entity = new InputStreamEntity(fsin, fsin.getChannel().size(), contentType); } else { byte[] preLoad = new byte[preLoadSize]; int read = IOUtils.read(source, preLoad); // The total amount of bytes read was less than the preload size, // so we can just return a in-memory non-streaming entity if (read < preLoadSize) { entity = new ExposedByteArrayEntity(preLoad, 0, read, contentType); } else { ByteArrayInputStream bin = new ByteArrayInputStream(preLoad); SequenceInputStream sin = new SequenceInputStream(bin, source); entity = new InputStreamEntity(sin, contentType); } } /* We know how big the stream is, so we can decide if it is within our * preload threshold and load it into memory or if it isn't within the * threshold, we can pass it on as a streamed entity in non-chunked mode. */ } else { if (contentLength <= preLoadSize && contentLength <= Integer.MAX_VALUE) { byte[] preLoad = new byte[(int) contentLength]; IOUtils.read(source, preLoad); entity = new ExposedByteArrayEntity(preLoad, contentType); } else { entity = new InputStreamEntity(source, contentLength, contentType); } } return httpHelper.httpPut(path, headers, entity, metadata); }
From source file:org.dasein.cloud.rackspace.AbstractMethod.java
protected @Nullable String putStream(@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() + ".putStream(" + authToken + "," + endpoint + "," + resource + "," + md5Hash + ",INPUTSTREAM)"); }/* w w w .j ava 2s .c o m*/ if (wire.isDebugEnabled()) { wire.debug("---------------------------------------------------------------------------------" + endpoint + resource); wire.debug(""); } try { HttpClient client = getClient(); HttpPut put = new HttpPut(endpoint + resource); put.addHeader("Content-Type", "application/octet-stream"); put.addHeader("X-Auth-Token", authToken); if (md5Hash != null) { put.addHeader("ETag", md5Hash); } put.setEntity(new InputStreamEntity(stream, -1, 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 <--"); } 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) { 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_CREATED && code != HttpServletResponse.SC_ACCEPTED && code != HttpServletResponse.SC_NO_CONTENT) { std.error("putStream(): Expected CREATED, ACCEPTED, or NO CONTENT for PUT 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("putStream(): [" + code + " : " + items.message + "] " + items.details); throw new RackspaceException(items); } else { 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() + ".putStream()"); } if (wire.isDebugEnabled()) { wire.debug(""); wire.debug("---------------------------------------------------------------------------------" + endpoint + resource); } } }
From source file:org.dasein.cloud.openstack.nova.os.AbstractMethod.java
protected @Nullable String putStream(@Nonnull String authToken, @Nonnull String endpoint, @Nonnull String resource, @Nullable String md5Hash, @Nonnull InputStream stream) throws CloudException, InternalException { Logger std = NovaOpenStack.getLogger(NovaOpenStack.class, "std"); Logger wire = NovaOpenStack.getLogger(NovaOpenStack.class, "wire"); if (std.isTraceEnabled()) { std.trace("enter - " + AbstractMethod.class.getName() + ".putStream(" + authToken + "," + endpoint + "," + resource + "," + md5Hash + ",INPUTSTREAM)"); }// ww w .j a va 2 s.co m if (wire.isDebugEnabled()) { wire.debug("---------------------------------------------------------------------------------" + endpoint + resource); wire.debug(""); } HttpClient client = null; try { client = getClient(); HttpPut put = new HttpPut(endpoint + resource); put.addHeader("Content-Type", "application/octet-stream"); put.addHeader("X-Auth-Token", authToken); if (md5Hash != null) { put.addHeader("ETag", md5Hash); } if (wire.isDebugEnabled()) { wire.debug(put.getRequestLine().toString()); for (Header header : put.getAllHeaders()) { wire.debug(header.getName() + ": " + header.getValue()); } wire.debug(""); } put.setEntity(new InputStreamEntity(stream, -1, ContentType.APPLICATION_OCTET_STREAM)); wire.debug(" ---- BINARY DATA ---- "); wire.debug(""); HttpResponse response; try { APITrace.trace(provider, "PUT " + toAPIResource(resource)); 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) { 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 : put.getAllHeaders()) { if (h.getName().equalsIgnoreCase("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) { std.error("putStream(): Expected CREATED, ACCEPTED, or NO CONTENT for PUT request, got " + code); String data = null; try { HttpEntity entity = response.getEntity(); if (entity != null) { data = EntityUtils.toString(entity); if (wire.isDebugEnabled()) { wire.debug(data); wire.debug(""); } } } catch (IOException e) { std.error("Failed to read response error due to a cloud I/O error: " + e.getMessage()); e.printStackTrace(); throw new CloudException(e); } NovaException.ExceptionItems items = NovaException.parseException(code, data); if (items == null) { items = new NovaException.ExceptionItems(); items.code = 404; items.type = CloudErrorType.COMMUNICATION; items.message = "itemNotFound"; items.details = "No such object: " + resource; } std.error("putStream(): [" + code + " : " + items.message + "] " + items.details); throw new NovaException(items); } else { if (code == HttpStatus.SC_ACCEPTED) { String data = null; try { HttpEntity entity = response.getEntity(); if (entity != null) { data = EntityUtils.toString(entity); if (wire.isDebugEnabled()) { wire.debug(data); wire.debug(""); } } } catch (IOException e) { std.error("Failed to read response error due to a cloud I/O error: " + e.getMessage()); e.printStackTrace(); throw new CloudException(e); } if (data != null && !data.trim().equals("")) { return data; } } return null; } } finally { if (client != null) { client.getConnectionManager().shutdown(); } if (std.isTraceEnabled()) { std.trace("exit - " + NovaOpenStack.class.getName() + ".putStream()"); } if (wire.isDebugEnabled()) { wire.debug(""); wire.debug("---------------------------------------------------------------------------------" + endpoint + resource); } } }
From source file:com.infinities.skyport.openstack.nova.os.SkyportNovaMethod.java
@Override protected @Nullable String putStream(@Nonnull String authToken, @Nonnull String endpoint, @Nonnull String resource, @Nullable String md5Hash, @Nonnull InputStream stream) throws CloudException, InternalException { Logger std = NovaOpenStack.getLogger(NovaOpenStack.class, "std"); Logger wire = NovaOpenStack.getLogger(NovaOpenStack.class, "wire"); if (std.isTraceEnabled()) { std.trace("enter - " + AbstractMethod.class.getName() + ".putStream(" + authToken + "," + endpoint + "," + resource + "," + md5Hash + ",INPUTSTREAM)"); }//from w ww . j a va2s.c o m if (wire.isDebugEnabled()) { wire.debug("---------------------------------------------------------------------------------" + endpoint + resource); wire.debug(""); } HttpClient client = null; try { client = getClient(); HttpPut put = new HttpPut(endpoint + resource); put.addHeader("Content-Type", "application/octet-stream"); put.addHeader("X-Auth-Token", authToken); if (md5Hash != null) { put.addHeader("ETag", md5Hash); } if (wire.isDebugEnabled()) { wire.debug(put.getRequestLine().toString()); for (Header header : put.getAllHeaders()) { wire.debug(header.getName() + ": " + header.getValue()); } wire.debug(""); } put.setEntity(new InputStreamEntity(stream, -1, ContentType.APPLICATION_OCTET_STREAM)); wire.debug(" ---- BINARY DATA ---- "); wire.debug(""); HttpResponse response; try { APITrace.trace(provider, "PUT " + toAPIResource(resource)); 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) { 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 : put.getAllHeaders()) { if (h.getName().equalsIgnoreCase("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) { std.error("putStream(): Expected CREATED, ACCEPTED, or NO CONTENT for PUT request, got " + code); String data = null; try { HttpEntity entity = response.getEntity(); if (entity != null) { data = EntityUtils.toString(entity); if (wire.isDebugEnabled()) { wire.debug(data); wire.debug(""); } } } catch (IOException e) { std.error("Failed to read response error due to a cloud I/O error: " + e.getMessage()); e.printStackTrace(); throw new CloudException(e); } NovaException.ExceptionItems items = NovaException.parseException(code, data); if (items == null) { items = new NovaException.ExceptionItems(); items.code = 404; items.type = CloudErrorType.COMMUNICATION; items.message = "itemNotFound"; items.details = "No such object: " + resource; } std.error("putStream(): [" + code + " : " + items.message + "] " + items.details); throw new NovaException(items); } else { if (code == HttpStatus.SC_ACCEPTED) { String data = null; try { HttpEntity entity = response.getEntity(); if (entity != null) { data = EntityUtils.toString(entity); if (wire.isDebugEnabled()) { wire.debug(data); wire.debug(""); } } } catch (IOException e) { std.error("Failed to read response error due to a cloud I/O error: " + e.getMessage()); e.printStackTrace(); throw new CloudException(e); } if (data != null && !data.trim().equals("")) { return data; } } return null; } } finally { if (client != null) { client.getConnectionManager().shutdown(); } if (std.isTraceEnabled()) { std.trace("exit - " + NovaOpenStack.class.getName() + ".putStream()"); } if (wire.isDebugEnabled()) { wire.debug(""); wire.debug("---------------------------------------------------------------------------------" + endpoint + resource); } } }