Example usage for org.apache.http.entity InputStreamEntity setContentType

List of usage examples for org.apache.http.entity InputStreamEntity setContentType

Introduction

In this page you can find the example usage for org.apache.http.entity InputStreamEntity setContentType.

Prototype

public void setContentType(Header header) 

Source Link

Usage

From source file:fr.smile.liferay.EsigatePortlet.java

/**
 * Transform request to IncominqRequest//from   w  ww.ja v a 2s  .  c  o m
 *
 * @param request
 * @param method
 * @return an incoming request
 * @throws IOException
 */
public IncomingRequest create(PortletRequest request, String method) throws IOException {

    HttpServletRequest httpServletRequest = PortalUtil
            .getOriginalServletRequest(PortalUtil.getHttpServletRequest(request));

    StringBuilder uri = new StringBuilder(HTTP_BASE_INCOMING_URL);

    StringBuilder query = new StringBuilder();
    Enumeration<String> parameters = request.getParameterNames();
    String sep = "";
    while (parameters.hasMoreElements()) {
        String name = parameters.nextElement();
        String[] values = request.getParameterValues(name);
        if (!name.equals(ACTION_PARAMETER)) {
            for (String value : values) {
                query.append(sep);
                query.append(name).append("=").append(URLEncoder.encode(value, "UTF-8"));
                sep = "&";
            }
        }
    }

    ProtocolVersion protocolVersion = HttpVersion.HTTP_1_1.forVersion(1, 0);

    if (method.equals("GET")) {
        if (!query.toString().isEmpty()) {
            if (!uri.toString().contains("?")) {
                uri.append("?");
            } else {
                uri.append("&");
            }
            uri.append(query);
        }
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("Creating Incoming request with method " + method + ", URI " + uri + ", protocoleVersion "
                + protocolVersion);
    }
    IncomingRequest.Builder builder = IncomingRequest
            .builder(new BasicRequestLine(method, uri.toString(), protocolVersion));

    if (method.equals("POST")) {
        // create entity
        InputStream inputStream = IOUtils.toInputStream(query.toString());

        if (inputStream != null) {
            // Copy entity-related headers
            InputStreamEntity entity = new InputStreamEntity(inputStream, query.length());
            String contentTypeHeader = httpServletRequest.getContentType();
            if (contentTypeHeader != null) {
                entity.setContentType(contentTypeHeader);
            }
            String contentEncodingHeader = httpServletRequest.getCharacterEncoding();
            if (contentEncodingHeader != null) {
                entity.setContentEncoding(contentEncodingHeader);
            }
            builder.setEntity(entity);
        }
    }

    HttpServletRequestContext context = new HttpServletRequestContext(httpServletRequest, null, null);
    builder.setContext(context);
    builder.setRemoteAddr(httpServletRequest.getRemoteAddr());
    builder.setRemoteUser(request.getRemoteUser());
    HttpSession session = httpServletRequest.getSession(false);
    if (session != null) {
        builder.setSessionId(session.getId());
    }
    builder.setUserPrincipal(request.getUserPrincipal());
    // Copy cookies
    javax.servlet.http.Cookie[] src = request.getCookies();

    if (src != null) {
        LOG.debug("Copying " + src.length + " cookie(s) to response.");
        for (int i = 0; i < src.length; i++) {
            javax.servlet.http.Cookie c = src[i];
            BasicClientCookie dest = new BasicClientCookie(c.getName(), c.getValue());
            dest.setSecure(c.getSecure());
            dest.setDomain(c.getDomain());
            dest.setPath(c.getPath());
            dest.setComment(c.getComment());
            dest.setVersion(c.getVersion());
            builder.addCookie(dest);
        }
    }

    builder.setSession(new HttpServletSession(httpServletRequest));

    IncomingRequest incomingRequest = builder.build();
    return incomingRequest;

}

From source file:org.sonatype.nexus.proxy.storage.remote.httpclient.HttpClientRemoteStorage.java

@Override
public void storeItem(final ProxyRepository repository, final StorageItem item)
        throws UnsupportedStorageOperationException, RemoteStorageException {
    if (!(item instanceof StorageFileItem)) {
        throw new UnsupportedStorageOperationException("Storing of non-files remotely is not supported!");
    }//from w  w  w  .j ava  2  s  . c om

    final StorageFileItem fileItem = (StorageFileItem) item;

    final ResourceStoreRequest request = new ResourceStoreRequest(item);

    final URL remoteUrl = appendQueryString(getAbsoluteUrlFromBase(repository, request), repository);

    final HttpPut method = new HttpPut(remoteUrl.toExternalForm());

    final InputStreamEntity entity;
    try {
        entity = new InputStreamEntity(fileItem.getInputStream(), fileItem.getLength());
    } catch (IOException e) {
        throw new RemoteStorageException(
                e.getMessage() + " [repositoryId=\"" + repository.getId() + "\", requestPath=\""
                        + request.getRequestPath() + "\", remoteUrl=\"" + remoteUrl.toString() + "\"]",
                e);
    }

    entity.setContentType(fileItem.getMimeType());
    method.setEntity(entity);

    final HttpResponse httpResponse = executeRequestAndRelease(repository, request, method);
    final int statusCode = httpResponse.getStatusLine().getStatusCode();

    if (statusCode != HttpStatus.SC_OK && statusCode != HttpStatus.SC_CREATED
            && statusCode != HttpStatus.SC_NO_CONTENT && statusCode != HttpStatus.SC_ACCEPTED) {
        throw new RemoteStorageException("Unexpected response code while executing " + method.getMethod()
                + " method [repositoryId=\"" + repository.getId() + "\", requestPath=\""
                + request.getRequestPath() + "\", remoteUrl=\"" + remoteUrl.toString()
                + "\"]. Expected: \"any success (2xx)\". Received: " + statusCode + " : "
                + httpResponse.getStatusLine().getReasonPhrase());
    }
}

From source file:de.mendelson.comm.as2.send.MessageHttpUploader.java

/**Uploads the data, returns the HTTP result code*/
public int performUpload(HttpConnectionParameter connectionParameter, AS2Message message, Partner sender,
        Partner receiver, URL receiptURL) {
    String ediintFeatures = "multiple-attachments, CEM";
    //set the http connection/routing/protocol parameter
    HttpParams httpParams = new BasicHttpParams();
    if (connectionParameter.getConnectionTimeoutMillis() != -1) {
        HttpConnectionParams.setConnectionTimeout(httpParams, connectionParameter.getConnectionTimeoutMillis());
    }/*  www . j av a2  s  . co m*/
    if (connectionParameter.getSoTimeoutMillis() != -1) {
        HttpConnectionParams.setSoTimeout(httpParams, connectionParameter.getSoTimeoutMillis());
    }
    HttpConnectionParams.setStaleCheckingEnabled(httpParams, connectionParameter.isStaleConnectionCheck());
    if (connectionParameter.getHttpProtocolVersion() == null) {
        //default settings: HTTP 1.1
        HttpProtocolParams.setVersion(httpParams, HttpVersion.HTTP_1_1);
    } else if (connectionParameter.getHttpProtocolVersion().equals(HttpConnectionParameter.HTTP_1_0)) {
        HttpProtocolParams.setVersion(httpParams, HttpVersion.HTTP_1_0);
    } else if (connectionParameter.getHttpProtocolVersion().equals(HttpConnectionParameter.HTTP_1_1)) {
        HttpProtocolParams.setVersion(httpParams, HttpVersion.HTTP_1_1);
    }
    HttpProtocolParams.setUseExpectContinue(httpParams, connectionParameter.isUseExpectContinue());
    HttpProtocolParams.setUserAgent(httpParams, connectionParameter.getUserAgent());
    if (connectionParameter.getLocalAddress() != null) {
        ConnRouteParams.setLocalAddress(httpParams, connectionParameter.getLocalAddress());
    }
    int status = -1;
    HttpPost filePost = null;
    DefaultHttpClient httpClient = null;
    try {
        ClientConnectionManager clientConnectionManager = this.createClientConnectionManager(httpParams);
        httpClient = new DefaultHttpClient(clientConnectionManager, httpParams);
        //some ssl implementations have problems with a session/connection reuse
        httpClient.setReuseStrategy(new NoConnectionReuseStrategy());
        //disable SSL hostname verification. Do not confuse this with SSL trust verification!
        SSLSocketFactory sslFactory = (SSLSocketFactory) httpClient.getConnectionManager().getSchemeRegistry()
                .get("https").getSocketFactory();
        sslFactory.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
        //determine the receipt URL if it is not set
        if (receiptURL == null) {
            //async MDN requested?
            if (message.isMDN()) {
                if (this.runtimeConnection == null) {
                    throw new IllegalArgumentException(
                            "MessageHTTPUploader.performUpload(): A MDN receipt URL is not set, unable to determine where to send the MDN");
                }
                MessageAccessDB messageAccess = new MessageAccessDB(this.configConnection,
                        this.runtimeConnection);
                AS2MessageInfo relatedMessageInfo = messageAccess
                        .getLastMessageEntry(((AS2MDNInfo) message.getAS2Info()).getRelatedMessageId());
                receiptURL = new URL(relatedMessageInfo.getAsyncMDNURL());
            } else {
                receiptURL = new URL(receiver.getURL());
            }
        }
        filePost = new HttpPost(receiptURL.toExternalForm());
        filePost.addHeader("as2-version", "1.2");
        filePost.addHeader("ediint-features", ediintFeatures);
        filePost.addHeader("mime-version", "1.0");
        filePost.addHeader("recipient-address", receiptURL.toExternalForm());
        filePost.addHeader("message-id", "<" + message.getAS2Info().getMessageId() + ">");
        filePost.addHeader("as2-from", AS2Message.escapeFromToHeader(sender.getAS2Identification()));
        filePost.addHeader("as2-to", AS2Message.escapeFromToHeader(receiver.getAS2Identification()));
        String originalFilename = null;
        if (message.getPayloads() != null && message.getPayloads().size() > 0) {
            originalFilename = message.getPayloads().get(0).getOriginalFilename();
        }
        if (originalFilename != null) {
            String subject = this.replace(message.getAS2Info().getSubject(), "${filename}", originalFilename);
            filePost.addHeader("subject", subject);
            //update the message infos subject with the actual content
            if (!message.isMDN()) {
                ((AS2MessageInfo) message.getAS2Info()).setSubject(subject);
                //refresh this in the database if it is requested
                if (this.runtimeConnection != null) {
                    MessageAccessDB access = new MessageAccessDB(this.configConnection, this.runtimeConnection);
                    access.updateSubject((AS2MessageInfo) message.getAS2Info());
                }
            }
        } else {
            filePost.addHeader("subject", message.getAS2Info().getSubject());
        }
        filePost.addHeader("from", sender.getEmail());
        filePost.addHeader("connection", "close, TE");
        //the data header must be always in english locale else there would be special
        //french characters (e.g. 13 dc. 2011 16:28:56 CET) which is not allowed after 
        //RFC 4130           
        DateFormat format = new SimpleDateFormat("EE, dd MMM yyyy HH:mm:ss zz", Locale.US);
        filePost.addHeader("date", format.format(new Date()));
        String contentType = null;
        if (message.getAS2Info().getEncryptionType() != AS2Message.ENCRYPTION_NONE) {
            contentType = "application/pkcs7-mime; smime-type=enveloped-data; name=smime.p7m";
        } else {
            contentType = message.getContentType();
        }
        filePost.addHeader("content-type", contentType);
        //MDN header, this is always the way for async MDNs
        if (message.isMDN()) {
            if (this.logger != null) {
                this.logger.log(Level.INFO,
                        this.rb.getResourceString("sending.mdn.async",
                                new Object[] { message.getAS2Info().getMessageId(), receiptURL }),
                        message.getAS2Info());
            }
            filePost.addHeader("server", message.getAS2Info().getUserAgent());
        } else {
            AS2MessageInfo messageInfo = (AS2MessageInfo) message.getAS2Info();
            //outbound AS2/CEM message
            if (messageInfo.requestsSyncMDN()) {
                if (this.logger != null) {
                    if (messageInfo.getMessageType() == AS2Message.MESSAGETYPE_CEM) {
                        this.logger.log(Level.INFO,
                                this.rb.getResourceString("sending.cem.sync",
                                        new Object[] { messageInfo.getMessageId(), receiver.getURL() }),
                                messageInfo);
                    } else if (messageInfo.getMessageType() == AS2Message.MESSAGETYPE_AS2) {
                        this.logger.log(Level.INFO,
                                this.rb.getResourceString("sending.msg.sync",
                                        new Object[] { messageInfo.getMessageId(), receiver.getURL() }),
                                messageInfo);
                    }
                }
            } else {
                //Message with ASYNC MDN request
                if (this.logger != null) {
                    if (messageInfo.getMessageType() == AS2Message.MESSAGETYPE_CEM) {
                        this.logger.log(Level.INFO,
                                this.rb.getResourceString("sending.cem.async", new Object[] {
                                        messageInfo.getMessageId(), receiver.getURL(), sender.getMdnURL() }),
                                messageInfo);
                    } else if (messageInfo.getMessageType() == AS2Message.MESSAGETYPE_AS2) {
                        this.logger.log(Level.INFO,
                                this.rb.getResourceString("sending.msg.async", new Object[] {
                                        messageInfo.getMessageId(), receiver.getURL(), sender.getMdnURL() }),
                                messageInfo);
                    }
                }
                //The following header indicates that this requests an asnc MDN.
                //When the header "receipt-delivery-option" is present,
                //the header "disposition-notification-to" serves as a request
                //for an asynchronous MDN.
                //The header "receipt-delivery-option" must always be accompanied by
                //the header "disposition-notification-to".
                //When the header "receipt-delivery-option" is not present and the header
                //"disposition-notification-to" is present, the header "disposition-notification-to"
                //serves as a request for a synchronous MDN.
                filePost.addHeader("receipt-delivery-option", sender.getMdnURL());
            }
            filePost.addHeader("disposition-notification-to", sender.getMdnURL());
            //request a signed MDN if this is set up in the partner configuration
            if (receiver.isSignedMDN()) {
                filePost.addHeader("disposition-notification-options",
                        messageInfo.getDispositionNotificationOptions().getHeaderValue());
            }
            if (messageInfo.getSignType() != AS2Message.SIGNATURE_NONE) {
                filePost.addHeader("content-disposition", "attachment; filename=\"smime.p7m\"");
            } else if (messageInfo.getSignType() == AS2Message.SIGNATURE_NONE
                    && message.getAS2Info().getSignType() == AS2Message.ENCRYPTION_NONE) {
                filePost.addHeader("content-disposition",
                        "attachment; filename=\"" + message.getPayload(0).getOriginalFilename() + "\"");
            }
        }
        int port = receiptURL.getPort();
        if (port == -1) {
            port = receiptURL.getDefaultPort();
        }
        filePost.addHeader("host", receiptURL.getHost() + ":" + port);
        InputStream rawDataInputStream = message.getRawDataInputStream();
        InputStreamEntity postEntity = new InputStreamEntity(rawDataInputStream, message.getRawDataSize());
        postEntity.setContentType(contentType);
        filePost.setEntity(postEntity);
        if (connectionParameter.getProxy() != null) {
            this.setProxyToConnection(httpClient, message, connectionParameter.getProxy());
        }
        this.setHTTPAuthentication(httpClient, receiver, message.getAS2Info().isMDN());
        this.updateUploadHttpHeader(filePost, receiver);
        HttpHost targetHost = new HttpHost(receiptURL.getHost(), receiptURL.getPort(),
                receiptURL.getProtocol());
        BasicHttpContext localcontext = new BasicHttpContext();
        // Generate BASIC scheme object and stick it to the local
        // execution context. Without this a HTTP authentication will not be sent
        BasicScheme basicAuth = new BasicScheme();
        localcontext.setAttribute("preemptive-auth", basicAuth);
        HttpResponse httpResponse = httpClient.execute(targetHost, filePost, localcontext);
        rawDataInputStream.close();
        this.responseData = this.readEntityData(httpResponse);
        if (httpResponse != null) {
            this.responseStatusLine = httpResponse.getStatusLine();
            status = this.responseStatusLine.getStatusCode();
            this.responseHeader = httpResponse.getAllHeaders();
        }
        for (Header singleHeader : filePost.getAllHeaders()) {
            if (singleHeader.getValue() != null) {
                this.requestHeader.setProperty(singleHeader.getName(), singleHeader.getValue());
            }
        }
        //accept all 2xx answers
        //SC_ACCEPTED Status code (202) indicating that a request was accepted for processing, but was not completed.
        //SC_CREATED  Status code (201) indicating the request succeeded and created a new resource on the server.
        //SC_NO_CONTENT Status code (204) indicating that the request succeeded but that there was no new information to return.
        //SC_NON_AUTHORITATIVE_INFORMATION Status code (203) indicating that the meta information presented by the client did not originate from the server.
        //SC_OK Status code (200) indicating the request succeeded normally.
        //SC_RESET_CONTENT Status code (205) indicating that the agent SHOULD reset the document view which caused the request to be sent.
        //SC_PARTIAL_CONTENT Status code (206) indicating that the server has fulfilled the partial GET request for the resource.
        if (status != HttpServletResponse.SC_OK && status != HttpServletResponse.SC_ACCEPTED
                && status != HttpServletResponse.SC_CREATED && status != HttpServletResponse.SC_NO_CONTENT
                && status != HttpServletResponse.SC_NON_AUTHORITATIVE_INFORMATION
                && status != HttpServletResponse.SC_RESET_CONTENT
                && status != HttpServletResponse.SC_PARTIAL_CONTENT) {
            if (this.logger != null) {
                this.logger
                        .severe(this.rb.getResourceString("error.httpupload",
                                new Object[] { message.getAS2Info().getMessageId(),
                                        URLDecoder.decode(
                                                this.responseStatusLine == null ? ""
                                                        : this.responseStatusLine.getReasonPhrase(),
                                                "UTF-8") }));
            }
        }
    } catch (Exception ex) {
        if (this.logger != null) {
            StringBuilder errorMessage = new StringBuilder(message.getAS2Info().getMessageId());
            errorMessage.append(": MessageHTTPUploader.performUpload: [");
            errorMessage.append(ex.getClass().getSimpleName());
            errorMessage.append("]");
            if (ex.getMessage() != null) {
                errorMessage.append(": ").append(ex.getMessage());
            }
            this.logger.log(Level.SEVERE, errorMessage.toString(), message.getAS2Info());
        }
    } finally {
        if (httpClient != null && httpClient.getConnectionManager() != null) {
            //shutdown the HTTPClient to release the resources
            httpClient.getConnectionManager().shutdown();
        }
    }
    return (status);
}

From source file:jp.mixi.android.sdk.MixiContainerImpl.java

/**
 * HTTP??//from  w ww.ja  va2s. c o m
 * 
 * @param endpointPath ?
 * @param stream ?InputStream
 * @param length InputStream??
 * @return HttpUriRequest
 */
private HttpUriRequest getHttpMethod(String endpointPath, String contentType, HttpMethod method,
        InputStream stream, long length) throws IOException {

    InputStreamEntity entity = new InputStreamEntity(stream, length);
    entity.setContentType(contentType);
    switch (method) {
    case POST:
        HttpPost postMethod = new HttpPost(Constants.GRAPH_BASE_URL + endpointPath);
        postMethod.setEntity(entity);
        return postMethod;
    case PUT:
        HttpPut putMethod = new HttpPut(Constants.GRAPH_BASE_URL + endpointPath);
        putMethod.setEntity(entity);
        return putMethod;

    default:
        Log.e(TAG, "Unsupported http method");
        throw new IllegalArgumentException("Unsupported HttpMethod parameter:" + method);
    }

}

From source file:ch.iterate.openstack.swift.Client.java

/**
 * Store a file on the server, including metadata, with the contents coming from an input stream.  This allows you to
 * not know the entire length of your content when you start to write it.  Nor do you have to hold it entirely in memory
 * at the same time./*from   ww  w .j  a  v a2 s .  c o  m*/
 *
 * @param container   The name of the container
 * @param data        Any object that implements InputStream
 * @param contentType The MIME type of the file
 * @param name        The name of the file on the server
 * @param metadata    A map with the metadata as key names and values as the metadata values
 * @return True if response code is 201
 * @throws GenericException Unexpected response
 */
public String storeObject(Region region, String container, InputStream data, String contentType, String name,
        Map<String, String> metadata) throws IOException {
    HttpPut method = new HttpPut(region.getStorageUrl(container, name));
    InputStreamEntity entity = new InputStreamEntity(data, -1);
    entity.setChunked(true);
    entity.setContentType(contentType);
    method.setEntity(entity);
    for (Map.Entry<String, String> key : this.renameObjectMetadata(metadata).entrySet()) {
        method.setHeader(key.getKey(), key.getValue());
    }
    Response response = this.execute(method, new DefaultResponseHandler());
    if (response.getStatusCode() == HttpStatus.SC_CREATED) {
        return response.getResponseHeader(HttpHeaders.ETAG).getValue();
    } else {
        throw new GenericException(response);
    }
}

From source file:groovyx.net.http.EncoderRegistry.java

/**
 * Default request encoder for a binary stream.  Acceptable argument
 * types are:/*from  w w  w .j  a  va 2s  .com*/
 * <ul>
 *   <li>InputStream</li>
 *   <li>byte[] / ByteArrayOutputStream</li>
 *   <li>Closure</li>
 * </ul>
 * If a closure is given, it is executed with an OutputStream passed
 * as the single closure argument.  Any data sent to the stream from the
 * body of the closure is used as the request content body.
 * @param data
 * @return an {@link HttpEntity} encapsulating this request data
 * @throws UnsupportedEncodingException
 */
public InputStreamEntity encodeStream(Object data, Object contentType) throws UnsupportedEncodingException {
    InputStreamEntity entity = null;

    if (data instanceof ByteArrayInputStream) {
        // special case for ByteArrayIS so that we can set the content length.
        ByteArrayInputStream in = ((ByteArrayInputStream) data);
        entity = new InputStreamEntity(in, in.available());
    } else if (data instanceof InputStream) {
        entity = new InputStreamEntity((InputStream) data, -1);
    } else if (data instanceof byte[]) {
        byte[] out = ((byte[]) data);
        entity = new InputStreamEntity(new ByteArrayInputStream(out), out.length);
    } else if (data instanceof ByteArrayOutputStream) {
        ByteArrayOutputStream out = ((ByteArrayOutputStream) data);
        entity = new InputStreamEntity(new ByteArrayInputStream(out.toByteArray()), out.size());
    } else if (data instanceof Closure) {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        ((Closure) data).call(out); // data is written to out
        entity = new InputStreamEntity(new ByteArrayInputStream(out.toByteArray()), out.size());
    }

    if (entity == null)
        throw new IllegalArgumentException("Don't know how to encode " + data + " as a byte stream");

    if (contentType == null)
        contentType = ContentType.BINARY;
    entity.setContentType(contentType.toString());
    return entity;
}

From source file:ch.iterate.openstack.swift.Client.java

/**
 * @param container          The name of the container
 * @param name               The name of the object
 * @param entity             The name of the request entity (make sure to set the Content-Type
 * @param metadata           The metadata for the object
 * @param md5sum             The 32 character hex encoded MD5 sum of the data
 * @param objectSize         The total size in bytes of the object to be stored
 * @param segmentSize        Optional size in bytes of the object segments to be stored (forces large object support) default 4G
 * @param dynamicLargeObject Optional setting to use dynamic large objects, False/null will use static large objects if required
 * @param segmentContainer   Optional name of container to store file segments, defaults to storing chunks in the same container as the file sill appear
 * @param segmentFolder      Optional name of folder for storing file segments, defaults to ".chunks/"
 * @param leaveSegments      Optional setting to leave segments of large objects in place when the manifest is overwrtten/changed
 * @return The ETAG if the save was successful, null otherwise
 * @throws GenericException There was a protocol level error talking to CloudFiles
 *///w w  w. j a  v  a  2 s.  c o  m
public String storeObject(Region region, String container, String name, HttpEntity entity,
        Map<String, String> metadata, String md5sum, Long objectSize, Long segmentSize,
        Boolean dynamicLargeObject, String segmentContainer, String segmentFolder, Boolean leaveSegments)
        throws IOException, InterruptedException {
    /*
     * Default values for large object support. We also use the defaults combined with the inputs
     * to determine whether to store as a large object.
     */

    /*
     * The maximum size of a single object (5GiB).
     */
    long singleObjectSizeLimit = (long) (5 * Math.pow(1024, 3));

    /*
     * The default minimum segment size (1MiB).
     */
    long minSegmentSize = 1024L * 1024L;

    /*
     * Set the segment size.
     *
     * Defaults to 4GiB segments, and will not permit smaller than 1MiB segments.
     */
    long actualSegmentSize = (segmentSize == null) ? (long) (4 * Math.pow(1024, 3))
            : Math.max(segmentSize, minSegmentSize);

    /*
     * Determines if we will store using large objects - we may do this for 3 reasons:
     *
     *  - A segmentSize has been specified and the object size is greater than the minimum segment size
     *  - If an objectSize is provided and is larger than the single object size limit of 5GiB
     *  - A segmentSize has been specified, but no objectSize given (we take this as a request for segmentation)
     *
     * The last case may fail if the user does not provide at least as much data as the minimum segment
     * size configured on the server, and will always produce a large object structure (even if only one
     * small segment is required).
     */
    objectSize = (objectSize == null) ? -1 : objectSize;
    boolean useLargeObject = ((segmentSize != null) && (objectSize > actualSegmentSize))
            || (objectSize > singleObjectSizeLimit) || ((segmentSize != null) && (objectSize == -1));

    if (!useLargeObject) {
        return storeObject(region, container, name, entity, metadata, md5sum);
    } else {
        /*
         * We need to upload a large object as defined by the method
         * parameters. For now this is done sequentially, but a parallel
         * version using appropriate random access to the underlying data
         * may be desirable.
         *
         * We make the assumption that the given file size will not be
         * greater than int.MAX_VALUE * segmentSize
         *
         */
        leaveSegments = (leaveSegments == null) ? Boolean.FALSE : leaveSegments;
        dynamicLargeObject = (dynamicLargeObject == null) ? Boolean.FALSE : dynamicLargeObject;
        segmentFolder = (segmentFolder == null) ? ".file-segments" : segmentFolder;
        segmentContainer = (segmentContainer == null) ? container : segmentContainer;

        Map<String, List<StorageObject>> oldSegmentsToRemove = null;

        /*
         * If we have chosen not to leave existing large object segments in place (default)
         * then we need to collect information about any existing file segments so that we can
         * deal with them after we complete the upload of the new manifest.
         *
         * We should only delete existing segments after a successful upload of a new manifest file
         * because this constitutes an object update and the older file should remain available
         * until the new file can be downloaded.
         */
        if (!leaveSegments) {
            ObjectMetadata existingMetadata;
            String manifestDLO = null;
            Boolean manifestSLO = Boolean.FALSE;

            try {
                existingMetadata = getObjectMetaData(region, container, name);

                if (existingMetadata.getMetaData().containsKey(Constants.MANIFEST_HEADER)) {
                    manifestDLO = existingMetadata.getMetaData().get(Constants.MANIFEST_HEADER);
                } else if (existingMetadata.getMetaData().containsKey(Constants.X_STATIC_LARGE_OBJECT)) {
                    JSONParser parser = new JSONParser();
                    String manifestSLOValue = existingMetadata.getMetaData()
                            .get(Constants.X_STATIC_LARGE_OBJECT);
                    manifestSLO = (Boolean) parser.parse(manifestSLOValue);
                }
            } catch (NotFoundException e) {
                /*
                 * Just means no object exists already, so continue
                 */
            } catch (ParseException e) {
                /*
                 * X_STATIC_LARGE_OBJECT header existed but failed to parse.
                 * If a static large object already exists this must be set to "true".
                 * If we got here then the X_STATIC_LARGE_OBJECT header existed, but failed
                 * to parse as a boolean, so fail upload as a precaution.
                 */
                return null;
            }

            if (manifestDLO != null) {
                /*
                 * We have found an existing dynamic large object, so use the prefix to get a list of
                 * existing objects. If we're putting up a new dlo, make sure the segment prefixes are
                 * different, then we can delete anything that's not in the new list if necessary.
                 */
                String oldContainer = manifestDLO.substring(0, manifestDLO.indexOf('/', 1));
                String oldPath = manifestDLO.substring(manifestDLO.indexOf('/', 1), manifestDLO.length());
                oldSegmentsToRemove = new HashMap<String, List<StorageObject>>();
                oldSegmentsToRemove.put(oldContainer, listObjects(region, oldContainer, oldPath));
            } else if (manifestSLO) {
                /*
                 * We have found an existing static large object, so grab the manifest data that
                 * details the existing segments - delete any later that we don't need any more
                 */

            }
        }

        int segmentNumber = 1;
        long timeStamp = System.currentTimeMillis() / 1000L;
        String segmentBase = String.format("%s/%d/%d", segmentFolder, timeStamp, objectSize);

        /*
         * Create subInputStream from the OutputStream we will pass to the
         * HttpEntity for writing content.
         */
        final PipedInputStream contentInStream = new PipedInputStream(64 * 1024);
        final PipedOutputStream contentOutStream = new PipedOutputStream(contentInStream);
        SubInputStream segmentStream = new SubInputStream(contentInStream, actualSegmentSize, false);

        /*
         * Fork the call to entity.writeTo() that allows us to grab any exceptions raised
         */
        final HttpEntity e = entity;

        final Callable<Boolean> writer = new Callable<Boolean>() {
            public Boolean call() throws Exception {
                e.writeTo(contentOutStream);
                return Boolean.TRUE;
            }
        };

        ExecutorService writeExecutor = Executors.newSingleThreadExecutor();
        final Future<Boolean> future = writeExecutor.submit(writer);
        /*
         * Check the future for exceptions after we've finished uploading segments
         */

        Map<String, List<StorageObject>> newSegmentsAdded = new HashMap<String, List<StorageObject>>();
        List<StorageObject> newSegments = new LinkedList<StorageObject>();
        JSONArray manifestSLO = new JSONArray();
        boolean finished = false;

        /*
         * Upload each segment of the file by reading sections of the content input stream
         * until the entire underlying stream is complete
         */
        while (!finished) {
            String segmentName = String.format("%s/%08d", segmentBase, segmentNumber);

            String etag;
            boolean error = false;
            try {
                etag = storeObject(region, segmentContainer, segmentStream, "application/octet-stream",
                        segmentName, new HashMap<String, String>());
            } catch (IOException ex) {
                // Finished storing the object
                System.out.println("Caught IO Exception: " + ex.getMessage());
                ex.printStackTrace();
                throw ex;
            }
            String segmentPath = segmentContainer + "/" + segmentName;
            long bytesUploaded = segmentStream.getBytesProduced();

            /*
             * Create the appropriate manifest structure if we're making a static large
             * object.
             *
             *   ETAG returned by the simple upload
             *   total size of segment uploaded
             *   path of segment
             */
            if (!dynamicLargeObject) {
                JSONObject segmentJSON = new JSONObject();

                segmentJSON.put("path", segmentPath);
                segmentJSON.put("etag", etag);
                segmentJSON.put("size_bytes", bytesUploaded);
                manifestSLO.add(segmentJSON);

                newSegments.add(new StorageObject(segmentName));
            }

            segmentNumber++;
            if (!finished) {
                finished = segmentStream.endSourceReached();
            }
            newSegmentsAdded.put(segmentContainer, newSegments);
            System.out.println("JSON: " + manifestSLO.toString());
            if (error)
                return "";

            segmentStream.readMoreBytes(actualSegmentSize);
        }

        /*
         * Attempts to retrieve the return value from the write operation
         * Any exceptions raised can then be handled appropriately
         */
        try {
            future.get();
        } catch (InterruptedException ex) {
            /*
             * The write was interrupted... delete the segments?
             */
        } catch (ExecutionException ex) {
            /*
             * This should always be an IOException or a RuntimeException
             * because the call to entity.writeTo() only throws IOException
             */
            Throwable t = ex.getCause();

            if (t instanceof IOException) {
                throw (IOException) t;
            } else {
                throw (RuntimeException) t;
            }
        }

        /*
         * Create an appropriate manifest depending on our DLO/SLO choice
         */
        String manifestEtag = null;
        if (dynamicLargeObject) {
            /*
             * Empty manifest with header detailing the shared prefix of object segments
             */
            long manifestTimeStamp = System.currentTimeMillis() / 1000L;
            metadata.put("X-Object-Manifest", segmentBase);
            metadata.put("x-object-meta-mtime", String.format("%s", manifestTimeStamp));
            manifestEtag = storeObject(region, container, new ByteArrayInputStream(new byte[0]),
                    entity.getContentType().getValue(), name, metadata);
        } else {
            /*
             * Manifest containing json list specifying details of the object segments.
             */
            URIBuilder urlBuild = new URIBuilder(region.getStorageUrl(container, name));
            urlBuild.setParameter("multipart-manifest", "put");
            URI url;
            try {
                url = urlBuild.build();
                String manifestContent = manifestSLO.toString();
                InputStreamEntity manifestEntity = new InputStreamEntity(
                        new ByteArrayInputStream(manifestContent.getBytes()), -1);
                manifestEntity.setChunked(true);
                manifestEntity.setContentType(entity.getContentType());
                HttpPut method = new HttpPut(url);
                method.setEntity(manifestEntity);
                method.setHeader("x-static-large-object", "true");
                Response response = this.execute(method, new DefaultResponseHandler());
                if (response.getStatusCode() == HttpStatus.SC_CREATED) {
                    manifestEtag = response.getResponseHeader(HttpHeaders.ETAG).getValue();
                } else {
                    throw new GenericException(response);
                }
            } catch (URISyntaxException ex) {
                ex.printStackTrace();
            }
        }

        /*
         * Delete stale segments of overwritten large object if requested.
         */
        if (!leaveSegments) {
            /*
             * Before deleting old segments, remove any objects from the delete list
             * that are also part of a new static large object that were updated during the upload.
             */
            if (!(oldSegmentsToRemove == null)) {
                for (String c : oldSegmentsToRemove.keySet()) {
                    List<StorageObject> rmv = oldSegmentsToRemove.get(c);
                    if (newSegmentsAdded.containsKey(c)) {
                        rmv.removeAll(newSegmentsAdded.get(c));
                    }
                    List<String> rmvNames = new LinkedList<String>();
                    for (StorageObject s : rmv) {
                        rmvNames.add(s.getName());
                    }
                    deleteObjects(region, c, rmvNames);
                }
            }
        }

        return manifestEtag;
    }
}

From source file:com.jayway.restassured.internal.http.EncoderRegistry.java

/**
 * Default request encoder for a binary stream.  Acceptable argument
 * types are://from   ww w.java 2s  . c  om
 * <ul>
 * <li>InputStream</li>
 * <li>byte[] / ByteArrayOutputStream</li>
 * <li>Closure</li>
 * </ul>
 * If a closure is given, it is executed with an OutputStream passed
 * as the single closure argument.  Any data sent to the stream from the
 * body of the closure is used as the request content body.
 *
 * @param data
 * @return an {@link HttpEntity} encapsulating this request data
 * @throws UnsupportedEncodingException
 */
public InputStreamEntity encodeStream(Object contentType, Object data) throws UnsupportedEncodingException {
    InputStreamEntity entity = null;

    if (data instanceof ByteArrayInputStream) {
        // special case for ByteArrayIS so that we can set the content length.
        ByteArrayInputStream in = ((ByteArrayInputStream) data);
        entity = new InputStreamEntity(in, in.available());
    } else if (data instanceof InputStream) {
        entity = new InputStreamEntity((InputStream) data, -1);
    } else if (data instanceof File) {
        FileInputStream fileInputStream;
        File file = (File) data;
        try {
            fileInputStream = new FileInputStream(file);
        } catch (FileNotFoundException e) {
            throw new RuntimeException("File " + file.getPath() + " not found", e);
        }
        entity = new InputStreamEntity(fileInputStream, -1);
    } else if (data instanceof byte[]) {
        byte[] out = ((byte[]) data);
        entity = new InputStreamEntity(new ByteArrayInputStream(out), out.length);
    } else if (data instanceof ByteArrayOutputStream) {
        ByteArrayOutputStream out = ((ByteArrayOutputStream) data);
        entity = new InputStreamEntity(new ByteArrayInputStream(out.toByteArray()), out.size());
    } else if (data instanceof Closure) {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        ((Closure) data).call(out); // data is written to out
        entity = new InputStreamEntity(new ByteArrayInputStream(out.toByteArray()), out.size());
    }

    if (entity == null)
        throw new IllegalArgumentException("Don't know how to encode " + data
                + " as a byte stream.\n\nPlease use EncoderConfig (EncoderConfig#encodeContentTypeAs) to specify how to serialize data for this content-type.\n"
                + "For example: \"given().config(RestAssured.config().encoderConfig(encoderConfig().encodeContentTypeAs(\""
                + ContentTypeExtractor.getContentTypeWithoutCharset(contentTypeToString(contentType))
                + "\", ContentType.TEXT))). ..\"");

    entity.setContentType(contentTypeToString(contentType));
    return entity;
}

From source file:api.Client.java

private InputStreamEntity createInputStreamEntity(InputStream body) {
    String contentType = body instanceof HasContentType ? ((HasContentType) body).getContentType()
            : "application/octet-stream";
    long contentLength = body instanceof HasContentLength ? ((HasContentLength) body).getContentLength() : -1;
    InputStreamEntity ise = new InputStreamEntity(body, contentLength);
    if (contentType != null)
        ise.setContentType(contentType);
    return ise;//from w  ww .  j av a 2s  . c o  m
}

From source file:com.rackspacecloud.client.cloudfiles.FilesClient.java

/**
 *  //from  w w  w.j a  v  a2s . c o m
 * 
 * @param container
 *             
 * @param data
 *             
 * @param contentType
 *             MIME
 * @param name
 *             
 * @param metadata
 *             
 * @param callback
 *             NULL
 * 
 * @throws IOException
 *              IO
 * @throws HttpException
 *              Http
 * @throws FilesExcepiton
 *              
 * 
 */
public String storeStreamedObject(String container, InputStream data, String contentType, String name,
        Map<String, String> metadata) throws IOException, HttpException, FilesException {
    if (this.isLoggedin()) {
        String objName = name;
        if (isValidContainerName(container) && isValidObjectName(objName)) {
            HttpPut method = new HttpPut(
                    storageURL + "/" + sanitizeForURI(container) + "/" + sanitizeForURI(objName));
            method.getParams().setIntParameter("http.socket.timeout", connectionTimeOut);
            method.setHeader(FilesConstants.X_AUTH_TOKEN, authToken);
            InputStreamEntity entity = new InputStreamEntity(data, -1);
            entity.setChunked(true);
            entity.setContentType(contentType);
            method.setEntity(entity);
            for (String key : metadata.keySet()) {
                // logger.warn("Key:" + key + ":" +
                // sanitizeForURI(metadata.get(key)));
                method.setHeader(FilesConstants.X_OBJECT_META + key, sanitizeForURI(metadata.get(key)));
            }
            method.removeHeaders("Content-Length");

            try {
                FilesResponse response = new FilesResponse(client.execute(method));

                if (response.getStatusCode() == HttpStatus.SC_CREATED) {
                    return response.getResponseHeader(FilesConstants.E_TAG).getValue();
                } else {
                    logger.error(response.getStatusLine());
                    throw new FilesException("Unexpected result", response.getResponseHeaders(),
                            response.getStatusLine());
                }
            } finally {
                method.abort();
            }
        } else {
            if (!isValidObjectName(objName)) {
                throw new FilesInvalidNameException(objName);
            } else {
                throw new FilesInvalidNameException(container);
            }
        }
    } else {
        throw new FilesAuthorizationException("You must be logged in", null, null);
    }
}