Example usage for java.io InputStream markSupported

List of usage examples for java.io InputStream markSupported

Introduction

In this page you can find the example usage for java.io InputStream markSupported.

Prototype

public boolean markSupported() 

Source Link

Document

Tests if this input stream supports the mark and reset methods.

Usage

From source file:com.amazon.s3.http.AmazonHttpClient.java

/**
 * Internal method to execute the HTTP method given.
 * /*  w ww .  ja  v  a2  s  . c o  m*/
 * @see AmazonHttpClient#execute(Request, HttpResponseHandler,
 *      HttpResponseHandler)
 * @see AmazonHttpClient#execute(Request, HttpResponseHandler,
 *      HttpResponseHandler, ExecutionContext)
 */
private <T extends Object> T executeHelper(Request<?> request,
        HttpResponseHandler<AmazonWebServiceResponse<T>> responseHandler,
        HttpResponseHandler<AmazonServiceException> errorResponseHandler, ExecutionContext executionContext)
        throws AmazonClientException, AmazonServiceException {

    /*
     * Depending on which response handler we end up choosing to handle the
     * HTTP response, it might require us to leave the underlying HTTP
     * connection open, depending on whether or not it reads the complete
     * HTTP response stream from the HTTP connection, or if delays reading
     * any of the content until after a response is returned to the caller.
     */
    boolean leaveHttpConnectionOpen = false;

    AWSRequestMetrics awsRequestMetrics = executionContext.getAwsRequestMetrics();
    /*
     * add the service endpoint to the logs. You can infer service name from
     * service endpoint
     */
    awsRequestMetrics.addProperty(Field.ServiceName.name(), request.getServiceName());
    awsRequestMetrics.addProperty(Field.ServiceEndpoint.name(), request.getEndpoint());

    // Apply whatever request options we know how to handle, such as
    // user-agent.
    applyRequestData(request);

    int retryCount = 0;
    URI redirectedURI = null;
    HttpEntity entity = null;
    AmazonServiceException exception = null;

    // Make a copy of the original request params and headers so that we can
    // permute it in this loop and start over with the original every time.
    Map<String, String> originalParameters = new HashMap<String, String>();
    originalParameters.putAll(request.getParameters());
    Map<String, String> originalHeaders = new HashMap<String, String>();
    originalHeaders.putAll(request.getHeaders());

    while (true) {
        awsRequestMetrics.setCounter(Field.AttemptCount.name(), retryCount + 1);
        if (retryCount > 0) {
            request.setParameters(originalParameters);
            request.setHeaders(originalHeaders);
        }

        HttpRequestBase httpRequest = null;
        org.apache.http.HttpResponse response = null;

        try {
            // Sign the request if a signer was provided
            if (executionContext.getSigner() != null && executionContext.getCredentials() != null) {
                awsRequestMetrics.startEvent(Field.RequestSigningTime.name());
                executionContext.getSigner().sign(request, executionContext.getCredentials());
                awsRequestMetrics.endEvent(Field.RequestSigningTime.name());
            }

            Log.d(TAG, "Sending Request: " + request.toString());
            httpRequest = httpRequestFactory.createHttpRequest(request, config, entity, executionContext);

            if (httpRequest instanceof HttpEntityEnclosingRequest) {
                entity = ((HttpEntityEnclosingRequest) httpRequest).getEntity();
            }

            if (redirectedURI != null) {
                httpRequest.setURI(redirectedURI);
            }

            if (retryCount > 0) {
                awsRequestMetrics.startEvent(Field.RetryPauseTime.name());
                pauseExponentially(retryCount, exception, executionContext.getCustomBackoffStrategy());
                awsRequestMetrics.endEvent(Field.RetryPauseTime.name());
            }

            if (entity != null) {
                InputStream content = entity.getContent();
                if (retryCount > 0) {
                    if (content.markSupported()) {
                        content.reset();
                        content.mark(-1);
                    }
                } else {
                    if (content.markSupported()) {
                        content.mark(-1);
                    }
                }
            }

            exception = null;

            awsRequestMetrics.startEvent(Field.HttpRequestTime.name());
            response = httpClient.execute(httpRequest);
            awsRequestMetrics.endEvent(Field.HttpRequestTime.name());

            if (isRequestSuccessful(response)) {

                awsRequestMetrics.addProperty(Field.StatusCode.name(),
                        response.getStatusLine().getStatusCode());

                /*
                 * If we get back any 2xx status code, then we know we
                 * should treat the service call as successful.
                 */
                leaveHttpConnectionOpen = responseHandler.needsConnectionLeftOpen();
                return handleResponse(request, responseHandler, httpRequest, response, executionContext);
            } else if (isTemporaryRedirect(response)) {
                /*
                 * S3 sends 307 Temporary Redirects if you try to delete an
                 * EU bucket from the US endpoint. If we get a 307, we'll
                 * point the HTTP method to the redirected location, and let
                 * the next retry deliver the request to the right location.
                 */
                Header[] locationHeaders = response.getHeaders("location");
                String redirectedLocation = locationHeaders[0].getValue();
                Log.d(TAG, "Redirecting to: " + redirectedLocation);
                redirectedURI = URI.create(redirectedLocation);
                httpRequest.setURI(redirectedURI);
                awsRequestMetrics.addProperty(Field.StatusCode.name(),
                        response.getStatusLine().getStatusCode());
                awsRequestMetrics.addProperty(Field.RedirectLocation.name(), redirectedLocation);
                awsRequestMetrics.addProperty(Field.AWSRequestID.name(), null);

            } else {
                leaveHttpConnectionOpen = errorResponseHandler.needsConnectionLeftOpen();
                exception = handleErrorResponse(request, errorResponseHandler, httpRequest, response);
                awsRequestMetrics.addProperty(Field.AWSRequestID.name(), exception.getRequestId());
                awsRequestMetrics.addProperty(Field.AWSErrorCode.name(), exception.getErrorCode());
                awsRequestMetrics.addProperty(Field.StatusCode.name(), exception.getStatusCode());

                if (!shouldRetry(httpRequest, exception, retryCount)) {
                    throw exception;
                }
                resetRequestAfterError(request, exception);
            }
        } catch (IOException ioe) {
            Log.i(TAG, "Unable to execute HTTP request: " + ioe.getMessage(), ioe);
            awsRequestMetrics.addProperty(Field.Exception.name(), ioe.toString());
            awsRequestMetrics.addProperty(Field.AWSRequestID.name(), null);

            if (!shouldRetry(httpRequest, ioe, retryCount)) {
                throw new AmazonClientException("Unable to execute HTTP request: " + ioe.getMessage(), ioe);
            }
            resetRequestAfterError(request, ioe);
        } finally {
            retryCount++;

            /*
             * Some response handlers need to manually manage the HTTP
             * connection and will take care of releasing the connection on
             * their own, but if this response handler doesn't need the
             * connection left open, we go ahead and release the it to free
             * up resources.
             */
            if (!leaveHttpConnectionOpen) {
                try {
                    response.getEntity().getContent().close();
                } catch (Throwable t) {
                }
            }
        }
    } /* end while (true) */
}

From source file:pt.lunacloud.http.AmazonHttpClient.java

/**
 * Internal method to execute the HTTP method given.
 *
 * @see AmazonHttpClient#execute(Request, HttpResponseHandler, HttpResponseHandler)
 * @see AmazonHttpClient#execute(Request, HttpResponseHandler, HttpResponseHandler, ExecutionContext)
 *//*from  w w w.ja v a  2s  .co m*/
private <T extends Object> T executeHelper(Request<?> request,
        HttpResponseHandler<AmazonWebServiceResponse<T>> responseHandler,
        HttpResponseHandler<LunacloudServiceException> errorResponseHandler, ExecutionContext executionContext)
        throws LunacloudClientException, LunacloudServiceException {

    /*
     * Depending on which response handler we end up choosing to handle the
     * HTTP response, it might require us to leave the underlying HTTP
     * connection open, depending on whether or not it reads the complete
     * HTTP response stream from the HTTP connection, or if delays reading
     * any of the content until after a response is returned to the caller.
     */
    boolean leaveHttpConnectionOpen = false;

    AWSRequestMetrics awsRequestMetrics = executionContext.getAwsRequestMetrics();
    /* add the service endpoint to the logs. You can infer service name from service endpoint */
    awsRequestMetrics.addProperty(Field.ServiceName.name(), request.getServiceName());
    awsRequestMetrics.addProperty(Field.ServiceEndpoint.name(), request.getEndpoint());

    // Apply whatever request options we know how to handle, such as user-agent.
    applyRequestData(request);

    int retryCount = 0;
    URI redirectedURI = null;
    HttpEntity entity = null;
    LunacloudServiceException exception = null;

    // Make a copy of the original request params and headers so that we can
    // permute it in this loop and start over with the original every time.
    Map<String, String> originalParameters = new HashMap<String, String>();
    originalParameters.putAll(request.getParameters());
    Map<String, String> originalHeaders = new HashMap<String, String>();
    originalHeaders.putAll(request.getHeaders());

    while (true) {
        awsRequestMetrics.setCounter(Field.AttemptCount.name(), retryCount + 1);
        if (retryCount > 0) {
            request.setParameters(originalParameters);
            request.setHeaders(originalHeaders);
        }

        HttpRequestBase httpRequest = null;
        org.apache.http.HttpResponse response = null;

        try {
            // Sign the request if a signer was provided
            if (executionContext.getSigner() != null && executionContext.getCredentials() != null) {
                awsRequestMetrics.startEvent(Field.RequestSigningTime.name());
                executionContext.getSigner().sign(request, executionContext.getCredentials());
                awsRequestMetrics.endEvent(Field.RequestSigningTime.name());
            }

            if (requestLog.isDebugEnabled()) {
                requestLog.debug("Sending Request: " + request.toString());
            }

            httpRequest = httpRequestFactory.createHttpRequest(request, config, entity, executionContext);

            if (httpRequest instanceof HttpEntityEnclosingRequest) {
                entity = ((HttpEntityEnclosingRequest) httpRequest).getEntity();
            }

            if (redirectedURI != null) {
                httpRequest.setURI(redirectedURI);
            }

            if (retryCount > 0) {
                awsRequestMetrics.startEvent(Field.RetryPauseTime.name());
                pauseExponentially(retryCount, exception, executionContext.getCustomBackoffStrategy());
                awsRequestMetrics.endEvent(Field.RetryPauseTime.name());
            }

            if (entity != null) {
                InputStream content = entity.getContent();
                if (retryCount > 0) {
                    if (content.markSupported()) {
                        content.reset();
                        content.mark(-1);
                    }
                } else {
                    if (content.markSupported()) {
                        content.mark(-1);
                    }
                }
            }

            exception = null;

            awsRequestMetrics.startEvent(Field.HttpRequestTime.name());
            response = httpClient.execute(httpRequest);
            awsRequestMetrics.endEvent(Field.HttpRequestTime.name());

            if (isRequestSuccessful(response)) {

                awsRequestMetrics.addProperty(Field.StatusCode.name(),
                        response.getStatusLine().getStatusCode());

                /*
                 * If we get back any 2xx status code, then we know we should
                 * treat the service call as successful.
                 */
                leaveHttpConnectionOpen = responseHandler.needsConnectionLeftOpen();
                return handleResponse(request, responseHandler, httpRequest, response, executionContext);
            } else if (isTemporaryRedirect(response)) {
                /*
                 * S3 sends 307 Temporary Redirects if you try to delete an
                 * EU bucket from the US endpoint. If we get a 307, we'll
                 * point the HTTP method to the redirected location, and let
                 * the next retry deliver the request to the right location.
                 */
                Header[] locationHeaders = response.getHeaders("location");
                String redirectedLocation = locationHeaders[0].getValue();
                log.debug("Redirecting to: " + redirectedLocation);
                redirectedURI = URI.create(redirectedLocation);
                httpRequest.setURI(redirectedURI);
                awsRequestMetrics.addProperty(Field.StatusCode.name(),
                        response.getStatusLine().getStatusCode());
                awsRequestMetrics.addProperty(Field.RedirectLocation.name(), redirectedLocation);
                awsRequestMetrics.addProperty(Field.AWSRequestID.name(), null);

            } else {
                leaveHttpConnectionOpen = errorResponseHandler.needsConnectionLeftOpen();
                exception = handleErrorResponse(request, errorResponseHandler, httpRequest, response);
                awsRequestMetrics.addProperty(Field.AWSRequestID.name(), exception.getRequestId());
                awsRequestMetrics.addProperty(Field.AWSErrorCode.name(), exception.getErrorCode());
                awsRequestMetrics.addProperty(Field.StatusCode.name(), exception.getStatusCode());

                if (!shouldRetry(httpRequest, exception, retryCount)) {
                    throw exception;
                }
                resetRequestAfterError(request, exception);
            }
        } catch (IOException ioe) {
            log.info("Unable to execute HTTP request: " + ioe.getMessage(), ioe);
            awsRequestMetrics.addProperty(Field.Exception.name(), ioe.toString());
            awsRequestMetrics.addProperty(Field.AWSRequestID.name(), null);

            if (!shouldRetry(httpRequest, ioe, retryCount)) {
                throw new LunacloudClientException("Unable to execute HTTP request: " + ioe.getMessage(), ioe);
            }
            resetRequestAfterError(request, ioe);
        } finally {
            retryCount++;

            /*
             * Some response handlers need to manually manage the HTTP
             * connection and will take care of releasing the connection on
             * their own, but if this response handler doesn't need the
             * connection left open, we go ahead and release the it to free
             * up resources.
             */
            if (!leaveHttpConnectionOpen) {
                try {
                    response.getEntity().getContent().close();
                } catch (Throwable t) {
                }
            }
        }
    } /* end while (true) */
}

From source file:com.android.sdklib.repository.legacy.remote.internal.DownloadCache.java

private InputStream ensureMarkReset(InputStream is) {
    // If the caller requires an InputStream that supports mark/reset, let's
    // make sure we have such a stream.
    if (is != null) {
        if (!is.markSupported()) {
            try {
                // Consume the whole input stream and offer a byte array stream instead.
                // This can only work sanely if the resource is a small file that can
                // fit in memory. It also means the caller has no chance of showing
                // a meaningful download progress.

                InputStream is2 = toByteArrayInputStream(is);
                if (is2 != null) {
                    try {
                        is.close();/*from   ww w .  j a  v  a  2 s.c  om*/
                    } catch (Exception ignore) {
                    }
                    return is2;
                }
            } catch (Exception e3) {
                // Ignore. If this can't work, caller will fail later.
            }
        }
    }
    return is;
}

From source file:cn.ctyun.amazonaws.http.AmazonHttpClient.java

/**
 * Internal method to execute the HTTP method given.
 *
 * @see AmazonHttpClient#execute(Request, HttpResponseHandler, HttpResponseHandler)
 * @see AmazonHttpClient#execute(Request, HttpResponseHandler, HttpResponseHandler, ExecutionContext)
 */// w  w w.j a  va2 s.  c om
private <T extends Object> T executeHelper(Request<?> request,
        HttpResponseHandler<AmazonWebServiceResponse<T>> responseHandler,
        HttpResponseHandler<AmazonServiceException> errorResponseHandler, ExecutionContext executionContext)
        throws AmazonClientException, AmazonServiceException {

    /*
     * Depending on which response handler we end up choosing to handle the
     * HTTP response, it might require us to leave the underlying HTTP
     * connection open, depending on whether or not it reads the complete
     * HTTP response stream from the HTTP connection, or if delays reading
     * any of the content until after a response is returned to the caller.
     */
    boolean leaveHttpConnectionOpen = false;

    AWSRequestMetrics awsRequestMetrics = executionContext.getAwsRequestMetrics();
    /* add the service endpoint to the logs. You can infer service name from service endpoint */
    awsRequestMetrics.addProperty(Field.ServiceName.name(), request.getServiceName());
    awsRequestMetrics.addProperty(Field.ServiceEndpoint.name(), request.getEndpoint());

    // Apply whatever request options we know how to handle, such as user-agent.
    setUserAgent(request);

    int retryCount = 0;
    URI redirectedURI = null;
    HttpEntity entity = null;
    AmazonServiceException exception = null;

    // Make a copy of the original request params and headers so that we can
    // permute it in this loop and start over with the original every time.
    Map<String, String> originalParameters = new HashMap<String, String>();
    originalParameters.putAll(request.getParameters());
    Map<String, String> originalHeaders = new HashMap<String, String>();
    originalHeaders.putAll(request.getHeaders());

    while (true) {
        awsRequestMetrics.setCounter(Field.AttemptCount.name(), retryCount + 1);
        if (retryCount > 0) {
            request.setParameters(originalParameters);
            request.setHeaders(originalHeaders);
        }

        HttpRequestBase httpRequest = null;
        org.apache.http.HttpResponse response = null;

        try {
            // Sign the request if a signer was provided
            if (executionContext.getSigner() != null && executionContext.getCredentials() != null) {
                awsRequestMetrics.startEvent(Field.RequestSigningTime.name());
                executionContext.getSigner().sign(request, executionContext.getCredentials());
                awsRequestMetrics.endEvent(Field.RequestSigningTime.name());
            }

            if (requestLog.isDebugEnabled()) {
                requestLog.debug("Sending Request: " + request.toString());
            }

            httpRequest = httpRequestFactory.createHttpRequest(request, config, entity, executionContext);

            if (httpRequest instanceof HttpEntityEnclosingRequest) {
                entity = ((HttpEntityEnclosingRequest) httpRequest).getEntity();
            }

            if (redirectedURI != null) {
                httpRequest.setURI(redirectedURI);
            }

            if (retryCount > 0) {
                awsRequestMetrics.startEvent(Field.RetryPauseTime.name());
                pauseExponentially(retryCount, exception, executionContext.getCustomBackoffStrategy());
                awsRequestMetrics.endEvent(Field.RetryPauseTime.name());
            }

            if (entity != null) {
                InputStream content = entity.getContent();
                if (retryCount > 0) {
                    if (content.markSupported()) {
                        content.reset();
                        content.mark(-1);
                    }
                } else {
                    if (content.markSupported()) {
                        content.mark(-1);
                    }
                }
            }

            exception = null;

            awsRequestMetrics.startEvent(Field.HttpRequestTime.name());
            response = httpClient.execute(httpRequest);
            awsRequestMetrics.endEvent(Field.HttpRequestTime.name());

            if (isRequestSuccessful(response)) {

                awsRequestMetrics.addProperty(Field.StatusCode.name(),
                        response.getStatusLine().getStatusCode());

                /*
                 * If we get back any 2xx status code, then we know we should
                 * treat the service call as successful.
                 */
                leaveHttpConnectionOpen = responseHandler.needsConnectionLeftOpen();
                return handleResponse(request, responseHandler, httpRequest, response, executionContext);
            } else if (isTemporaryRedirect(response)) {
                /*
                 * S3 sends 307 Temporary Redirects if you try to delete an
                 * EU bucket from the US endpoint. If we get a 307, we'll
                 * point the HTTP method to the redirected location, and let
                 * the next retry deliver the request to the right location.
                 */
                Header[] locationHeaders = response.getHeaders("location");
                String redirectedLocation = locationHeaders[0].getValue();
                log.debug("Redirecting to: " + redirectedLocation);
                redirectedURI = URI.create(redirectedLocation);
                httpRequest.setURI(redirectedURI);
                awsRequestMetrics.addProperty(Field.StatusCode.name(),
                        response.getStatusLine().getStatusCode());
                awsRequestMetrics.addProperty(Field.RedirectLocation.name(), redirectedLocation);
                awsRequestMetrics.addProperty(Field.AWSRequestID.name(), null);

            } else {
                leaveHttpConnectionOpen = errorResponseHandler.needsConnectionLeftOpen();
                exception = handleErrorResponse(request, errorResponseHandler, httpRequest, response);
                awsRequestMetrics.addProperty(Field.AWSRequestID.name(), exception.getRequestId());
                awsRequestMetrics.addProperty(Field.AWSErrorCode.name(), exception.getErrorCode());
                awsRequestMetrics.addProperty(Field.StatusCode.name(), exception.getStatusCode());

                if (!shouldRetry(httpRequest, exception, retryCount)) {
                    throw exception;
                }
                resetRequestAfterError(request, exception);
            }
        } catch (IOException ioe) {
            log.info("Unable to execute HTTP request: " + ioe.getMessage(), ioe);
            awsRequestMetrics.addProperty(Field.Exception.name(), ioe.toString());
            awsRequestMetrics.addProperty(Field.AWSRequestID.name(), null);

            if (!shouldRetry(httpRequest, ioe, retryCount)) {
                throw new AmazonClientException("Unable to execute HTTP request: " + ioe.getMessage(), ioe);
            }
            resetRequestAfterError(request, ioe);
        } finally {
            retryCount++;

            /*
             * Some response handlers need to manually manage the HTTP
             * connection and will take care of releasing the connection on
             * their own, but if this response handler doesn't need the
             * connection left open, we go ahead and release the it to free
             * up resources.
             */
            if (!leaveHttpConnectionOpen) {
                try {
                    response.getEntity().getContent().close();
                } catch (Throwable t) {
                }
            }
        }
    } /* end while (true) */
}

From source file:com.aliyun.odps.rest.RestClient.java

/**
 * RESTful API//  w w w  . ja v a 2 s  .  com
 *
 * @param resource
 * @param method
 * @param params
 * @param headers
 * @param body
 *     InputStream, FileInputStream/ByteArrayInputStream
 * @param bodyLen
 *     InputStream?
 * @return
 * @throws OdpsException
 */
public Response request(String resource, String method, Map<String, String> params, Map<String, String> headers,
        InputStream body, long bodyLen) throws OdpsException {
    int retryTimes = 0;
    if (method.equalsIgnoreCase(Method.GET.toString()) || method.equalsIgnoreCase(Method.HEAD.toString())) {
        retryTimes = getRetryTimes();
        if (body != null && body.markSupported()) {
            body.mark(0);
        }
    }

    long retryWaitTime = getConnectTimeout() + getReadTimeout();

    long retryCount = 0;

    while (retryCount <= retryTimes) {
        long startTime = System.currentTimeMillis();
        try {
            Response resp = requestWithNoRetry(resource, method, params, headers, body, bodyLen);

            if (resp == null) {
                throw new OdpsException("Response is null.");
            }

            if (resp.getStatus() / 100 == 4) {
                retryTimes = 0;
                // IF THE HTTP CODE IS 4XX,
                // IT SHOULD NOT RETRY IF THE REQUEST NOT CHANGED
            }

            handleErrorResponse(resp);

            uploadDeprecatedLog();

            return resp;

        } catch (OdpsException e) {
            if (retryTimes == retryCount) {
                throw e;
            }

            resetBody(body);
            ++retryCount;

            if (logger != null) {
                logger.onRetryLog(e, retryCount, retryWaitTime);
            }

            try {
                long endTime = System.currentTimeMillis();
                long sleepTime = retryWaitTime * 1000 - (endTime - startTime);
                if (sleepTime > 0) {
                    Thread.sleep(sleepTime);
                }
            } catch (InterruptedException e1) {
            }
            continue;
        } catch (Error e) {
            if (retryTimes == retryCount) {
                throw e;
            }
            resetBody(body);
            ++retryCount;
            if (logger != null) {
                logger.onRetryLog(e, retryCount, retryWaitTime);
            }

            try {
                long endTime = System.currentTimeMillis();
                long sleepTime = retryWaitTime * 1000 - (endTime - startTime);
                if (sleepTime > 0) {
                    Thread.sleep(sleepTime);
                }
            } catch (InterruptedException e1) {
            }
            continue;
        }
    }
    throw new OdpsException("Failed in Connection Retry.");
}

From source file:com.smartitengineering.cms.api.impl.type.ContentTypeLoaderImpl.java

@Override
public Collection<WritableContentType> parseContentTypes(WorkspaceId workspaceId,
        InputStream contentTypeDefinitionStream, MediaType mediaType)
        throws NullPointerException, InvalidReferenceException, IOException {
    TypeValidator validator = SmartContentSPI.getInstance().getTypeValidators().getValidators().get(mediaType);
    ContentTypeDefinitionParser parser = SmartContentSPI.getInstance().getContentTypeDefinitionParsers()
            .getParsers().get(mediaType);
    if (validator == null || parser == null) {
        throw new IOException("Media type " + mediaType.toString() + " is not supported!");
    }/*from ww  w  .jav a2s  .  c  om*/
    if (!contentTypeDefinitionStream.markSupported()) {
        contentTypeDefinitionStream = new BufferedInputStream(contentTypeDefinitionStream);
    }
    try {
        if (!validator.isValid(contentTypeDefinitionStream)) {
            throw new IOException("Content does not meet definition!");
        }
        final Collection<WritableContentType> types = parser.parseStream(workspaceId,
                contentTypeDefinitionStream);
        if (logger.isDebugEnabled()) {
            for (WritableContentType contentType : types) {
                logger.debug("ID " + contentType.getContentTypeID());
            }
        }
        List<ContentTypeImpl> resultingTypes = mergeWithStoredContentTypes(types);
        return Collections.<WritableContentType>unmodifiableCollection(resultingTypes);
    } catch (InvalidReferenceException ex) {
        throw ex;
    } catch (Exception ex) {
        throw new IOException(ex);
    }
}

From source file:core.com.qiniu.http.AmazonHttpClient.java

/**
 * Internal method to execute the HTTP method given.
 *
 * @see AmazonHttpClient#execute(Request, HttpResponseHandler,
 *      HttpResponseHandler)/*from   w w  w .  j  ava  2s  . c o m*/
 * @see AmazonHttpClient#execute(Request, HttpResponseHandler,
 *      HttpResponseHandler, ExecutionContext)
 */
<T> Response<T> executeHelper(Request<?> request,
        HttpResponseHandler<AmazonWebServiceResponse<T>> responseHandler,
        HttpResponseHandler<AmazonServiceException> errorResponseHandler, ExecutionContext executionContext)
        throws AmazonClientException, AmazonServiceException {
    /*
     * Depending on which response handler we end up choosing to handle the
     * HTTP response, it might require us to leave the underlying HTTP
     * connection open, depending on whether or not it reads the complete
     * HTTP response stream from the HTTP connection, or if delays reading
     * any of the content until after a response is returned to the caller.
     */
    boolean leaveHttpConnectionOpen = false;
    AWSRequestMetrics awsRequestMetrics = executionContext.getAwsRequestMetrics();
    /*
     * add the service endpoint to the logs. You can infer service name from
     * service endpoint
     */
    awsRequestMetrics.addProperty(AWSRequestMetrics.Field.ServiceName, request.getServiceName());
    awsRequestMetrics.addProperty(AWSRequestMetrics.Field.ServiceEndpoint, request.getEndpoint());

    // Apply whatever request options we know how to handle, such as
    // user-agent.
    setUserAgent(request);
    request.addHeader(HEADER_SDK_TRANSACTION_ID, UUID.randomUUID().toString());
    int requestCount = 0;
    long lastBackoffDelay = 0;
    URI redirectedURI = null;
    AmazonClientException retriedException = null;

    // Make a copy of the original request params and headers so that we can
    // permute it in this loop and start over with the original every time.
    Map<String, String> originalParameters = new LinkedHashMap<String, String>(request.getParameters());
    Map<String, String> originalHeaders = new HashMap<String, String>(request.getHeaders());
    // mark input stream if supported
    InputStream originalContent = request.getContent();
    if (originalContent != null && originalContent.markSupported()) {
        originalContent.mark(-1);
    }

    final AWSCredentials credentials = executionContext.getCredentials();
    Signer signer = null;
    HttpResponse httpResponse = null;
    HttpRequest httpRequest = null;

    while (true) {
        ++requestCount;
        awsRequestMetrics.setCounter(AWSRequestMetrics.Field.RequestCount, requestCount);
        if (requestCount > 1) { // retry
            request.setParameters(originalParameters);
            request.setHeaders(originalHeaders);
            request.setContent(originalContent);
        }
        if (redirectedURI != null) {
            request.setEndpoint(URI.create(redirectedURI.getScheme() + "://" + redirectedURI.getAuthority()));
            request.setResourcePath(redirectedURI.getPath());
        }

        try {
            if (requestCount > 1) { // retry
                awsRequestMetrics.startEvent(AWSRequestMetrics.Field.RetryPauseTime);
                try {
                    lastBackoffDelay = pauseBeforeNextRetry(request.getOriginalRequest(), retriedException,
                            requestCount, config.getRetryPolicy());
                } finally {
                    awsRequestMetrics.endEvent(AWSRequestMetrics.Field.RetryPauseTime);
                }
                InputStream content = request.getContent();
                if (content != null && content.markSupported()) {
                    content.reset();
                }
            }
            request.addHeader(HEADER_SDK_RETRY_INFO, (requestCount - 1) + "/" + lastBackoffDelay);

            // Sign the request if a signer was provided
            if (signer == null)
                signer = executionContext.getSignerByURI(request.getEndpoint());
            if (signer != null && credentials != null) {
                awsRequestMetrics.startEvent(AWSRequestMetrics.Field.RequestSigningTime);
                try {
                    signer.sign(request, credentials);
                } finally {
                    awsRequestMetrics.endEvent(AWSRequestMetrics.Field.RequestSigningTime);
                }
            }

            if (requestLog.isDebugEnabled()) {
                requestLog.debug("Sending Request: " + request.toString());
            }

            httpRequest = requestFactory.createHttpRequest(request, config, executionContext);

            retriedException = null;
            awsRequestMetrics.startEvent(AWSRequestMetrics.Field.HttpRequestTime);
            try {
                httpResponse = httpClient.execute(httpRequest);
            } finally {
                awsRequestMetrics.endEvent(AWSRequestMetrics.Field.HttpRequestTime);
            }

            if (isRequestSuccessful(httpResponse)) {
                awsRequestMetrics.addProperty(AWSRequestMetrics.Field.StatusCode, httpResponse.getStatusCode());
                /*
                 * If we get back any 2xx status code, then we know we
                 * should treat the service call as successful.
                 */
                leaveHttpConnectionOpen = responseHandler.needsConnectionLeftOpen();
                T response = handleResponse(request, responseHandler, httpResponse, executionContext);
                return new Response<T>(response, httpResponse);
            } else if (isTemporaryRedirect(httpResponse)) {
                /*
                 * S3 sends 307 Temporary Redirects if you try to delete an
                 * EU bucket from the US endpoint. If we get a 307, we'll
                 * point the HTTP method to the redirected location, and let
                 * the next retry deliver the request to the right location.
                 */
                String redirectedLocation = httpResponse.getHeaders().get("Location");
                log.debug("Redirecting to: " + redirectedLocation);
                // set redirect uri and retry
                redirectedURI = URI.create(redirectedLocation);
                awsRequestMetrics.addProperty(AWSRequestMetrics.Field.StatusCode, httpResponse.getStatusCode());
                awsRequestMetrics.addProperty(AWSRequestMetrics.Field.RedirectLocation, redirectedLocation);
                awsRequestMetrics.addProperty(AWSRequestMetrics.Field.AWSRequestID, null);
            } else {
                leaveHttpConnectionOpen = errorResponseHandler.needsConnectionLeftOpen();
                AmazonServiceException ase = handleErrorResponse(request, errorResponseHandler, httpResponse);
                awsRequestMetrics.addProperty(AWSRequestMetrics.Field.AWSRequestID, ase.getRequestId());
                awsRequestMetrics.addProperty(AWSRequestMetrics.Field.AWSErrorCode, ase.getErrorCode());
                awsRequestMetrics.addProperty(AWSRequestMetrics.Field.StatusCode, ase.getStatusCode());

                if (!shouldRetry(request.getOriginalRequest(), httpRequest.getContent(), ase, requestCount,
                        config.getRetryPolicy())) {
                    throw ase;
                }

                // Cache the retryable exception
                retriedException = ase;
                /*
                 * Checking for clock skew error again because we don't want
                 * to set the global time offset for every service
                 * exception.
                 */
                if (RetryUtils.isClockSkewError(ase)) {
                    int timeOffset = parseClockSkewOffset(httpResponse, ase);
                    SDKGlobalConfiguration.setGlobalTimeOffset(timeOffset);
                }
                resetRequestAfterError(request, ase);
            }
        } catch (IOException ioe) {
            if (log.isDebugEnabled()) {
                log.debug("Unable to execute HTTP request: " + ioe.getMessage(), ioe);
            }
            awsRequestMetrics.incrementCounter(AWSRequestMetrics.Field.Exception);
            awsRequestMetrics.addProperty(AWSRequestMetrics.Field.Exception, ioe);
            awsRequestMetrics.addProperty(AWSRequestMetrics.Field.AWSRequestID, null);

            AmazonClientException ace = new AmazonClientException(
                    "Unable to execute HTTP request: " + ioe.getMessage(), ioe);
            if (!shouldRetry(request.getOriginalRequest(), httpRequest.getContent(), ace, requestCount,
                    config.getRetryPolicy())) {
                throw ace;
            }

            // Cache the retryable exception
            retriedException = ace;
            resetRequestAfterError(request, ioe);
        } catch (RuntimeException e) {
            throw handleUnexpectedFailure(e, awsRequestMetrics);
        } catch (Error e) {
            throw handleUnexpectedFailure(e, awsRequestMetrics);
        } finally {
            /*
             * Some response handlers need to manually manage the HTTP
             * connection and will take care of releasing the connection on
             * their own, but if this response handler doesn't need the
             * connection left open, we go ahead and release the it to free
             * up resources.
             */
            if (!leaveHttpConnectionOpen && httpResponse != null) {
                try {
                    if (httpResponse.getRawContent() != null) {
                        httpResponse.getRawContent().close();
                    }
                } catch (IOException e) {
                    log.warn("Cannot close the response content.", e);
                }
            }
        }
    } /* end while (true) */
}

From source file:org.globus.gsi.X509Credential.java

protected void loadCertificate(InputStream input) throws CredentialException {

    if (input == null) {
        throw new IllegalArgumentException("Input stream to load X509Credential is null");
    }/*from  w w  w.  j a va  2s.c o  m*/

    X509Certificate cert;
    Vector<X509Certificate> chain = new Vector<X509Certificate>();

    String line;
    BufferedReader reader = null;
    try {
        if (input.markSupported()) {
            input.reset();
        }
        reader = new BufferedReader(new InputStreamReader(input));

        while ((line = reader.readLine()) != null) {

            if (line.indexOf("BEGIN CERTIFICATE") != -1) {
                byte[] data = getDecodedPEMObject(reader);
                cert = CertificateLoadUtil.loadCertificate(new ByteArrayInputStream(data));
                chain.addElement(cert);
            }
        }

    } catch (IOException e) {
        throw new CredentialException(e);
    } catch (GeneralSecurityException e) {
        throw new CredentialException(e);
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException e) {
                logger.debug("error closing reader", e);
                // This is ok
            }
        }
    }

    int size = chain.size();
    if (size > 0) {
        this.certChain = new X509Certificate[size];
        chain.copyInto(this.certChain);
    }

}

From source file:com.smartsheet.api.internal.http.DefaultHttpClient.java

/**
 * Make an HTTP request and return the response.
 *
 * @param smartsheetRequest the smartsheet request
 * @return the HTTP response/*from w  w  w .j a va 2  s  .com*/
 * @throws HttpClientException the HTTP client exception
 */
public HttpResponse request(HttpRequest smartsheetRequest) throws HttpClientException {
    Util.throwIfNull(smartsheetRequest);
    if (smartsheetRequest.getUri() == null) {
        throw new IllegalArgumentException("A Request URI is required.");
    }

    int attempt = 0;
    long start = System.currentTimeMillis();

    HttpRequestBase apacheHttpRequest;
    HttpResponse smartsheetResponse;

    InputStream bodyStream = null;
    if (smartsheetRequest.getEntity() != null && smartsheetRequest.getEntity().getContent() != null) {
        bodyStream = smartsheetRequest.getEntity().getContent();
    }
    // the retry logic will consume the body stream so we make sure it supports mark/reset and mark it
    boolean canRetryRequest = bodyStream == null || bodyStream.markSupported();
    if (!canRetryRequest) {
        try {
            // attempt to wrap the body stream in a input-stream that does support mark/reset
            bodyStream = new ByteArrayInputStream(StreamUtil.readBytesFromStream(bodyStream));
            // close the old stream (just to be tidy) and then replace it with a reset-able stream
            smartsheetRequest.getEntity().getContent().close();
            smartsheetRequest.getEntity().setContent(bodyStream);
            canRetryRequest = true;
        } catch (IOException ignore) {
        }
    }

    // the retry loop
    while (true) {

        apacheHttpRequest = createApacheRequest(smartsheetRequest);

        // Set HTTP headers
        if (smartsheetRequest.getHeaders() != null) {
            for (Map.Entry<String, String> header : smartsheetRequest.getHeaders().entrySet()) {
                apacheHttpRequest.addHeader(header.getKey(), header.getValue());
            }
        }

        HttpEntitySnapshot requestEntityCopy = null;
        HttpEntitySnapshot responseEntityCopy = null;
        // Set HTTP entity
        final HttpEntity entity = smartsheetRequest.getEntity();
        if (apacheHttpRequest instanceof HttpEntityEnclosingRequestBase && entity != null
                && entity.getContent() != null) {
            try {
                // we need access to the original request stream so we can log it (in the event of errors and/or tracing)
                requestEntityCopy = new HttpEntitySnapshot(entity);
            } catch (IOException iox) {
                logger.error("failed to make copy of original request entity - {}", iox);
            }

            InputStreamEntity streamEntity = new InputStreamEntity(entity.getContent(),
                    entity.getContentLength());
            streamEntity.setChunked(false); // why?  not supported by library?
            ((HttpEntityEnclosingRequestBase) apacheHttpRequest).setEntity(streamEntity);
        }

        // mark the body so we can reset on retry
        if (canRetryRequest && bodyStream != null) {
            bodyStream.mark((int) smartsheetRequest.getEntity().getContentLength());
        }

        // Make the HTTP request
        smartsheetResponse = new HttpResponse();
        HttpContext context = new BasicHttpContext();
        try {
            long startTime = System.currentTimeMillis();
            apacheHttpResponse = this.httpClient.execute(apacheHttpRequest, context);
            long endTime = System.currentTimeMillis();

            // Set request headers to values ACTUALLY SENT (not just created by us), this would include:
            // 'Connection', 'Accept-Encoding', etc. However, if a proxy is used, this may be the proxy's CONNECT
            // request, hence the test for HTTP method first
            Object httpRequest = context.getAttribute("http.request");
            if (httpRequest != null && HttpRequestWrapper.class.isAssignableFrom(httpRequest.getClass())) {
                HttpRequestWrapper actualRequest = (HttpRequestWrapper) httpRequest;
                switch (HttpMethod.valueOf(actualRequest.getMethod())) {
                case GET:
                case POST:
                case PUT:
                case DELETE:
                    apacheHttpRequest.setHeaders(((HttpRequestWrapper) httpRequest).getAllHeaders());
                    break;
                }
            }

            // Set returned headers
            smartsheetResponse.setHeaders(new HashMap<String, String>());
            for (Header header : apacheHttpResponse.getAllHeaders()) {
                smartsheetResponse.getHeaders().put(header.getName(), header.getValue());
            }
            smartsheetResponse.setStatus(apacheHttpResponse.getStatusLine().getStatusCode(),
                    apacheHttpResponse.getStatusLine().toString());

            // Set returned entities
            if (apacheHttpResponse.getEntity() != null) {
                HttpEntity httpEntity = new HttpEntity();
                httpEntity.setContentType(apacheHttpResponse.getEntity().getContentType().getValue());
                httpEntity.setContentLength(apacheHttpResponse.getEntity().getContentLength());
                httpEntity.setContent(apacheHttpResponse.getEntity().getContent());
                smartsheetResponse.setEntity(httpEntity);
                responseEntityCopy = new HttpEntitySnapshot(httpEntity);
            }

            long responseTime = endTime - startTime;
            logRequest(apacheHttpRequest, requestEntityCopy, smartsheetResponse, responseEntityCopy,
                    responseTime);

            if (traces.size() > 0) { // trace-logging of request and response (if so configured)
                RequestAndResponseData requestAndResponseData = RequestAndResponseData.of(apacheHttpRequest,
                        requestEntityCopy, smartsheetResponse, responseEntityCopy, traces);
                TRACE_WRITER.println(requestAndResponseData.toString(tracePrettyPrint));
            }

            if (smartsheetResponse.getStatusCode() == 200) {
                // call successful, exit the retry loop
                break;
            }

            // the retry logic might consume the content stream so we make sure it supports mark/reset and mark it
            InputStream contentStream = smartsheetResponse.getEntity().getContent();
            if (!contentStream.markSupported()) {
                // wrap the response stream in a input-stream that does support mark/reset
                contentStream = new ByteArrayInputStream(StreamUtil.readBytesFromStream(contentStream));
                // close the old stream (just to be tidy) and then replace it with a reset-able stream
                smartsheetResponse.getEntity().getContent().close();
                smartsheetResponse.getEntity().setContent(contentStream);
            }
            try {
                contentStream.mark((int) smartsheetResponse.getEntity().getContentLength());
                long timeSpent = System.currentTimeMillis() - start;
                if (!shouldRetry(++attempt, timeSpent, smartsheetResponse)) {
                    // should not retry, or retry time exceeded, exit the retry loop
                    break;
                }
            } finally {
                if (bodyStream != null) {
                    bodyStream.reset();
                }
                contentStream.reset();
            }
            // moving this to finally causes issues because socket is closed (which means response stream is closed)
            this.releaseConnection();

        } catch (ClientProtocolException e) {
            try {
                logger.warn("ClientProtocolException " + e.getMessage());
                logger.warn("{}", RequestAndResponseData.of(apacheHttpRequest, requestEntityCopy,
                        smartsheetResponse, responseEntityCopy, REQUEST_RESPONSE_SUMMARY));
                // if this is a PUT and was retried by the http client, the body content stream is at the
                // end and is a NonRepeatableRequest. If we marked the body content stream prior to execute,
                // reset and retry
                if (canRetryRequest && e.getCause() instanceof NonRepeatableRequestException) {
                    if (smartsheetRequest.getEntity() != null) {
                        smartsheetRequest.getEntity().getContent().reset();
                    }
                    continue;
                }
            } catch (IOException ignore) {
            }
            throw new HttpClientException("Error occurred.", e);
        } catch (NoHttpResponseException e) {
            try {
                logger.warn("NoHttpResponseException " + e.getMessage());
                logger.warn("{}", RequestAndResponseData.of(apacheHttpRequest, requestEntityCopy,
                        smartsheetResponse, responseEntityCopy, REQUEST_RESPONSE_SUMMARY));
                // check to see if the response was empty and this was a POST. All other HTTP methods
                // will be automatically retried by the http client.
                // (POST is non-idempotent and is not retried automatically, but is safe for us to retry)
                if (canRetryRequest && smartsheetRequest.getMethod() == HttpMethod.POST) {
                    if (smartsheetRequest.getEntity() != null) {
                        smartsheetRequest.getEntity().getContent().reset();
                    }
                    continue;
                }
            } catch (IOException ignore) {
            }
            throw new HttpClientException("Error occurred.", e);
        } catch (IOException e) {
            try {
                logger.warn("{}", RequestAndResponseData.of(apacheHttpRequest, requestEntityCopy,
                        smartsheetResponse, responseEntityCopy, REQUEST_RESPONSE_SUMMARY));
            } catch (IOException ignore) {
            }
            throw new HttpClientException("Error occurred.", e);
        }
    }
    return smartsheetResponse;
}