Example usage for org.apache.http.client.methods HttpUriRequest getAllHeaders

List of usage examples for org.apache.http.client.methods HttpUriRequest getAllHeaders

Introduction

In this page you can find the example usage for org.apache.http.client.methods HttpUriRequest getAllHeaders.

Prototype

Header[] getAllHeaders();

Source Link

Usage

From source file:org.dasein.cloud.terremark.TerremarkMethod.java

public Document invoke(boolean debug) throws TerremarkException, CloudException, InternalException {
    if (logger.isTraceEnabled()) {
        logger.trace("ENTER - " + TerremarkMethod.class.getName() + ".invoke(" + debug + ")");
    }//  w  w w .  j  av  a2 s .  co  m
    try {
        if (logger.isDebugEnabled()) {
            logger.debug("Talking to server at " + url);
        }

        if (parameters != null) {
            URIBuilder uri = null;
            try {
                uri = new URIBuilder(url);
            } catch (URISyntaxException e) {
                e.printStackTrace();
            }
            for (NameValuePair parameter : parameters) {
                uri.addParameter(parameter.getName(), parameter.getValue());
            }
            url = uri.toString();
        }

        HttpUriRequest method = null;
        if (methodType.equals(HttpMethodName.GET)) {
            method = new HttpGet(url);
        } else if (methodType.equals(HttpMethodName.POST)) {
            method = new HttpPost(url);
        } else if (methodType.equals(HttpMethodName.DELETE)) {
            method = new HttpDelete(url);
        } else if (methodType.equals(HttpMethodName.PUT)) {
            method = new HttpPut(url);
        } else if (methodType.equals(HttpMethodName.HEAD)) {
            method = new HttpHead(url);
        } else {
            method = new HttpGet(url);
        }
        HttpResponse status = null;
        try {
            HttpClient client = new DefaultHttpClient();
            HttpParams params = new BasicHttpParams();

            HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
            HttpProtocolParams.setContentCharset(params, "UTF-8");
            HttpProtocolParams.setUserAgent(params, "Dasein Cloud");

            attempts++;

            String proxyHost = provider.getProxyHost();
            if (proxyHost != null) {
                int proxyPort = provider.getProxyPort();
                boolean ssl = url.startsWith("https");
                params.setParameter(ConnRoutePNames.DEFAULT_PROXY,
                        new HttpHost(proxyHost, proxyPort, ssl ? "https" : "http"));
            }
            for (Map.Entry<String, String> entry : headers.entrySet()) {
                method.addHeader(entry.getKey(), entry.getValue());
            }
            if (body != null && body != ""
                    && (methodType.equals(HttpMethodName.PUT) || methodType.equals(HttpMethodName.POST))) {
                try {
                    HttpEntity entity = new StringEntity(body, "UTF-8");
                    ((HttpEntityEnclosingRequestBase) method).setEntity(entity);
                } catch (UnsupportedEncodingException e) {
                    logger.warn(e);
                }
            }
            if (wire.isDebugEnabled()) {

                wire.debug(methodType.name() + " " + method.getURI());
                for (Header header : method.getAllHeaders()) {
                    wire.debug(header.getName() + ": " + header.getValue());
                }
                if (body != null) {
                    wire.debug(body);
                }
            }
            try {
                status = client.execute(method);
                if (wire.isDebugEnabled()) {
                    wire.debug("HTTP STATUS: " + status);
                }
            } catch (IOException e) {
                logger.error("I/O error from server communications: " + e.getMessage());
                e.printStackTrace();
                throw new InternalException(e);
            }
            int statusCode = status.getStatusLine().getStatusCode();
            if (statusCode == HttpStatus.SC_OK || statusCode == HttpStatus.SC_CREATED
                    || statusCode == HttpStatus.SC_ACCEPTED) {
                try {
                    InputStream input = status.getEntity().getContent();

                    try {
                        return parseResponse(input);
                    } finally {
                        input.close();
                    }
                } catch (IOException e) {
                    logger.error("Error parsing response from Teremark: " + e.getMessage());
                    e.printStackTrace();
                    throw new CloudException(CloudErrorType.COMMUNICATION, statusCode, null, e.getMessage());
                }
            } else if (statusCode == HttpStatus.SC_NO_CONTENT) {
                logger.debug("Recieved no content in response. Creating an empty doc.");
                DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
                DocumentBuilder docBuilder = null;
                try {
                    docBuilder = dbfac.newDocumentBuilder();
                } catch (ParserConfigurationException e) {
                    e.printStackTrace();
                }
                return docBuilder.newDocument();
            } else if (statusCode == HttpStatus.SC_FORBIDDEN) {
                String msg = "OperationNotAllowed ";
                try {
                    msg += parseResponseToString(status.getEntity().getContent());
                } catch (IllegalStateException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                wire.error(msg);
                throw new TerremarkException(statusCode, "OperationNotAllowed", msg);
            } else {
                String response = "Failed to parse response.";
                ParsedError parsedError = null;
                try {
                    response = parseResponseToString(status.getEntity().getContent());
                    parsedError = parseErrorResponse(response);
                } catch (IllegalStateException e1) {
                    e1.printStackTrace();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
                if (logger.isDebugEnabled()) {
                    logger.debug("Received " + status + " from " + url);
                }
                if (statusCode == HttpStatus.SC_SERVICE_UNAVAILABLE
                        || statusCode == HttpStatus.SC_INTERNAL_SERVER_ERROR) {
                    if (attempts >= 5) {
                        String msg;
                        wire.warn(response);
                        if (statusCode == HttpStatus.SC_SERVICE_UNAVAILABLE) {
                            msg = "Cloud service is currently unavailable.";
                        } else {
                            msg = "The cloud service encountered a server error while processing your request.";
                            try {
                                msg = msg + "Response from server was:\n" + response;
                            } catch (RuntimeException runException) {
                                logger.warn(runException);
                            } catch (Error error) {
                                logger.warn(error);
                            }
                        }
                        wire.error(response);
                        logger.error(msg);
                        if (parsedError != null) {
                            throw new TerremarkException(parsedError);
                        } else {
                            throw new CloudException("HTTP Status " + statusCode + msg);
                        }
                    } else {
                        try {
                            Thread.sleep(5000L);
                        } catch (InterruptedException e) {
                            /* ignore */ }
                        return invoke();
                    }
                }
                wire.error(response);
                if (parsedError != null) {
                    throw new TerremarkException(parsedError);
                } else {
                    String msg = "\nResponse from server was:\n" + response;
                    logger.error(msg);
                    throw new CloudException("HTTP Status " + statusCode + msg);
                }
            }
        } finally {
            try {
                if (status != null) {
                    EntityUtils.consume(status.getEntity());
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    } finally {
        if (logger.isTraceEnabled()) {
            logger.trace("EXIT - " + TerremarkMethod.class.getName() + ".invoke()");
        }
    }
}

From source file:com.fujitsu.dc.core.rs.box.DcEngineSvcCollectionResource.java

/**
 * relay??./*from  w  w w . ja v  a2s.c  o  m*/
 * @param method 
 * @param uriInfo URI
 * @param path ??
 * @param headers 
 * @param is 
 * @return JAX-RS Response
 */
public Response relaycommon(String method, UriInfo uriInfo, String path, HttpHeaders headers, InputStream is) {

    String cellName = this.davRsCmp.getCell().getName();
    String boxName = this.davRsCmp.getBox().getName();
    String requestUrl = String.format("http://%s:%s/%s/%s/%s/service/%s", DcCoreConfig.getEngineHost(),
            DcCoreConfig.getEnginePort(), DcCoreConfig.getEnginePath(), cellName, boxName, path);

    // baseUrl?
    String baseUrl = uriInfo.getBaseUri().toString();

    // ???
    HttpClient client = new DefaultHttpClient();
    HttpUriRequest req = null;
    if (method.equals(HttpMethod.POST)) {
        HttpPost post = new HttpPost(requestUrl);
        InputStreamEntity ise = new InputStreamEntity(is, -1);
        ise.setChunked(true);
        post.setEntity(ise);
        req = post;
    } else if (method.equals(HttpMethod.PUT)) {
        HttpPut put = new HttpPut(requestUrl);
        InputStreamEntity ise = new InputStreamEntity(is, -1);
        ise.setChunked(true);
        put.setEntity(ise);
        req = put;
    } else if (method.equals(HttpMethod.DELETE)) {
        HttpDelete delete = new HttpDelete(requestUrl);
        req = delete;
    } else {
        HttpGet get = new HttpGet(requestUrl);
        req = get;
    }

    req.addHeader("X-Baseurl", baseUrl);
    req.addHeader("X-Request-Uri", uriInfo.getRequestUri().toString());
    // ?INDEXIDTYPE?
    if (davCmp instanceof DavCmpEsImpl) {
        DavCmpEsImpl test = (DavCmpEsImpl) davCmp;
        req.addHeader("X-Dc-Es-Index", test.getEsColType().getIndex().getName());
        req.addHeader("X-Dc-Es-Id", test.getNodeId());
        req.addHeader("X-Dc-Es-Type", test.getEsColType().getType());
        req.addHeader("X-Dc-Es-Routing-Id", this.davRsCmp.getCell().getId());
        req.addHeader("X-Dc-Box-Schema", this.davRsCmp.getBox().getSchema());
    }

    // ???
    MultivaluedMap<String, String> multivalueHeaders = headers.getRequestHeaders();
    for (Iterator<Entry<String, List<String>>> it = multivalueHeaders.entrySet().iterator(); it.hasNext();) {
        Entry<String, List<String>> entry = it.next();
        String key = (String) entry.getKey();
        if (key.equalsIgnoreCase(HttpHeaders.CONTENT_LENGTH)) {
            continue;
        }
        List<String> valueList = (List<String>) entry.getValue();
        for (Iterator<String> i = valueList.iterator(); i.hasNext();) {
            String value = (String) i.next();
            req.setHeader(key, value);
        }
    }

    if (log.isDebugEnabled()) {
        log.debug("?EngineRelay " + req.getMethod() + "  " + req.getURI());
        Header[] reqHeaders = req.getAllHeaders();
        for (int i = 0; i < reqHeaders.length; i++) {
            log.debug("RelayHeader[" + reqHeaders[i].getName() + "] : " + reqHeaders[i].getValue());
        }
    }

    // Engine??
    HttpResponse objResponse = null;
    try {
        objResponse = client.execute(req);
    } catch (ClientProtocolException e) {
        throw DcCoreException.ServiceCollection.SC_INVALID_HTTP_RESPONSE_ERROR;
    } catch (Exception ioe) {
        throw DcCoreException.ServiceCollection.SC_ENGINE_CONNECTION_ERROR.reason(ioe);
    }

    // 
    ResponseBuilder res = Response.status(objResponse.getStatusLine().getStatusCode());
    Header[] headersResEngine = objResponse.getAllHeaders();
    // ?
    for (int i = 0; i < headersResEngine.length; i++) {
        // Engine????Transfer-Encoding????
        // ?MW????????Content-Length???Transfer-Encoding????
        // 2??????????????????????
        if ("Transfer-Encoding".equalsIgnoreCase(headersResEngine[i].getName())) {
            continue;
        }
        // Engine????Date????
        // Web??MW?Jetty???2?????????
        if (HttpHeaders.DATE.equalsIgnoreCase(headersResEngine[i].getName())) {
            continue;
        }
        res.header(headersResEngine[i].getName(), headersResEngine[i].getValue());
    }

    InputStream isResBody = null;

    // ?
    HttpEntity entity = objResponse.getEntity();
    if (entity != null) {
        try {
            isResBody = entity.getContent();
        } catch (IllegalStateException e) {
            throw DcCoreException.ServiceCollection.SC_UNKNOWN_ERROR.reason(e);
        } catch (IOException e) {
            throw DcCoreException.ServiceCollection.SC_ENGINE_CONNECTION_ERROR.reason(e);
        }
        final InputStream isInvariable = isResBody;
        // ??
        StreamingOutput strOutput = new StreamingOutput() {
            @Override
            public void write(final OutputStream os) throws IOException {
                int chr;
                try {
                    while ((chr = isInvariable.read()) != -1) {
                        os.write(chr);
                    }
                } finally {
                    isInvariable.close();
                }
            }
        };
        res.entity(strOutput);
    }

    // ??
    return res.build();
}

From source file:cn.com.loopj.android.http.AsyncHttpClient.java

/**
 * Puts a new request in queue as a new thread in pool to be executed
 *
 * @param client          HttpClient to be used for request, can differ in single requests
 * @param contentType     MIME body type, for POST and PUT requests, may be null
 * @param context         Context of Android application, to hold the reference of request
 * @param httpContext     HttpContext in which the request will be executed
 * @param responseHandler ResponseHandler or its subclass to put the response into
 * @param uriRequest      instance of HttpUriRequest, which means it must be of HttpDelete,
 *                        HttpPost, HttpGet, HttpPut, etc.
 * @return RequestHandle of future request process
 *///  w  ww. java 2 s  .  c  o m
protected RequestHandle sendRequest(DefaultHttpClient client, HttpContext httpContext,
        HttpUriRequest uriRequest, String contentType, ResponseHandlerInterface responseHandler,
        Context context) {
    if (uriRequest == null) {
        throw new IllegalArgumentException("HttpUriRequest must not be null");
    }

    if (responseHandler == null) {
        throw new IllegalArgumentException("ResponseHandler must not be null");
    }

    if (responseHandler.getUseSynchronousMode() && !responseHandler.getUsePoolThread()) {
        throw new IllegalArgumentException(
                "Synchronous ResponseHandler used in AsyncHttpClient. You should create your response handler in a looper thread or use SyncHttpClient instead.");
    }

    if (contentType != null) {
        if (uriRequest instanceof HttpEntityEnclosingRequestBase
                && ((HttpEntityEnclosingRequestBase) uriRequest).getEntity() != null
                && uriRequest.containsHeader(HEADER_CONTENT_TYPE)) {
            log.w(LOG_TAG, "Passed contentType will be ignored because HttpEntity sets content type");
        } else {
            uriRequest.setHeader(HEADER_CONTENT_TYPE, contentType);
        }
    }

    responseHandler.setRequestHeaders(uriRequest.getAllHeaders());
    responseHandler.setRequestURI(uriRequest.getURI());

    AsyncHttpRequest request = newAsyncHttpRequest(client, httpContext, uriRequest, contentType,
            responseHandler, context);
    threadPool.submit(request);
    RequestHandle requestHandle = new RequestHandle(request);

    if (context != null) {
        List<RequestHandle> requestList;
        // Add request to request map
        synchronized (requestMap) {
            requestList = requestMap.get(context);
            if (requestList == null) {
                requestList = Collections.synchronizedList(new LinkedList<RequestHandle>());
                requestMap.put(context, requestList);
            }
        }

        requestList.add(requestHandle);

        Iterator<RequestHandle> iterator = requestList.iterator();
        while (iterator.hasNext()) {
            if (iterator.next().shouldBeGarbageCollected()) {
                iterator.remove();
            }
        }
    }

    return requestHandle;
}

From source file:org.eweb4j.spiderman.plugin.util.PageFetcherImpl.java

/**
 * // www  .  j a va  2 s . c  om
 * @date 2013-1-7 ?11:08:54
 * @param toFetchURL
 * @return
 */
public FetchResult request(FetchRequest req) throws Exception {
    FetchResult fetchResult = new FetchResult();
    HttpUriRequest request = null;
    HttpEntity entity = null;
    String toFetchURL = req.getUrl();
    boolean isPost = false;
    try {
        if (Http.Method.GET.equalsIgnoreCase(req.getHttpMethod()))
            request = new HttpGet(toFetchURL);
        else if (Http.Method.POST.equalsIgnoreCase(req.getHttpMethod())) {
            request = new HttpPost(toFetchURL);
            isPost = true;
        } else if (Http.Method.PUT.equalsIgnoreCase(req.getHttpMethod()))
            request = new HttpPut(toFetchURL);
        else if (Http.Method.HEAD.equalsIgnoreCase(req.getHttpMethod()))
            request = new HttpHead(toFetchURL);
        else if (Http.Method.OPTIONS.equalsIgnoreCase(req.getHttpMethod()))
            request = new HttpOptions(toFetchURL);
        else if (Http.Method.DELETE.equalsIgnoreCase(req.getHttpMethod()))
            request = new HttpDelete(toFetchURL);
        else
            throw new Exception("Unknown http method name");

        //???,??
        // TODO ?delay?
        synchronized (mutex) {
            //??
            long now = (new Date()).getTime();
            //?Host??
            if (now - lastFetchTime < config.getPolitenessDelay())
                Thread.sleep(config.getPolitenessDelay() - (now - lastFetchTime));
            //????HOST??URL
            lastFetchTime = (new Date()).getTime();
        }

        //GZIP???GZIP?
        request.addHeader("Accept-Encoding", "gzip");
        for (Iterator<Entry<String, String>> it = headers.entrySet().iterator(); it.hasNext();) {
            Entry<String, String> entry = it.next();
            request.addHeader(entry.getKey(), entry.getValue());
        }

        //?
        Header[] headers = request.getAllHeaders();
        for (Header h : headers) {
            Map<String, List<String>> hs = req.getHeaders();
            String key = h.getName();
            List<String> val = hs.get(key);
            if (val == null)
                val = new ArrayList<String>();
            val.add(h.getValue());

            hs.put(key, val);
        }
        req.getCookies().putAll(this.cookies);
        fetchResult.setReq(req);

        HttpEntity reqEntity = null;
        if (Http.Method.POST.equalsIgnoreCase(req.getHttpMethod())
                || Http.Method.PUT.equalsIgnoreCase(req.getHttpMethod())) {
            if (!req.getFiles().isEmpty()) {
                reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
                for (Iterator<Entry<String, List<File>>> it = req.getFiles().entrySet().iterator(); it
                        .hasNext();) {
                    Entry<String, List<File>> e = it.next();
                    String paramName = e.getKey();
                    for (File file : e.getValue()) {
                        // For File parameters
                        ((MultipartEntity) reqEntity).addPart(paramName, new FileBody(file));
                    }
                }

                for (Iterator<Entry<String, List<Object>>> it = req.getParams().entrySet().iterator(); it
                        .hasNext();) {
                    Entry<String, List<Object>> e = it.next();
                    String paramName = e.getKey();
                    for (Object paramValue : e.getValue()) {
                        // For usual String parameters
                        ((MultipartEntity) reqEntity).addPart(paramName, new StringBody(
                                String.valueOf(paramValue), "text/plain", Charset.forName("UTF-8")));
                    }
                }
            } else {
                List<NameValuePair> params = new ArrayList<NameValuePair>(req.getParams().size());
                for (Iterator<Entry<String, List<Object>>> it = req.getParams().entrySet().iterator(); it
                        .hasNext();) {
                    Entry<String, List<Object>> e = it.next();
                    String paramName = e.getKey();
                    for (Object paramValue : e.getValue()) {
                        params.add(new BasicNameValuePair(paramName, String.valueOf(paramValue)));
                    }
                }
                reqEntity = new UrlEncodedFormEntity(params, HTTP.UTF_8);
            }

            if (isPost)
                ((HttpPost) request).setEntity(reqEntity);
            else
                ((HttpPut) request).setEntity(reqEntity);
        }

        //??
        HttpResponse response = httpClient.execute(request);
        headers = response.getAllHeaders();
        for (Header h : headers) {
            Map<String, List<String>> hs = fetchResult.getHeaders();
            String key = h.getName();
            List<String> val = hs.get(key);
            if (val == null)
                val = new ArrayList<String>();
            val.add(h.getValue());

            hs.put(key, val);
        }
        //URL
        fetchResult.setFetchedUrl(toFetchURL);
        String uri = request.getURI().toString();
        if (!uri.equals(toFetchURL))
            if (!URLCanonicalizer.getCanonicalURL(uri).equals(toFetchURL))
                fetchResult.setFetchedUrl(uri);

        entity = response.getEntity();
        //???
        int statusCode = response.getStatusLine().getStatusCode();
        if (statusCode != HttpStatus.SC_OK) {
            if (statusCode != HttpStatus.SC_NOT_FOUND) {
                Header locationHeader = response.getFirstHeader("Location");
                //301?302?URL??
                if (locationHeader != null && (statusCode == HttpStatus.SC_MOVED_PERMANENTLY
                        || statusCode == HttpStatus.SC_MOVED_TEMPORARILY))
                    fetchResult.setMovedToUrl(
                            URLCanonicalizer.getCanonicalURL(locationHeader.getValue(), toFetchURL));
            }
            //???OKURLstatusCode??
            //???
            if (this.site.getSkipStatusCode() != null && this.site.getSkipStatusCode().trim().length() > 0) {
                String[] scs = this.site.getSkipStatusCode().split(",");
                for (String code : scs) {
                    int c = CommonUtil.toInt(code);
                    //????entity
                    if (statusCode == c) {
                        assemPage(fetchResult, entity);
                        break;
                    }
                }
            }
            fetchResult.setStatusCode(statusCode);
            return fetchResult;
        }

        //??
        if (entity != null) {
            fetchResult.setStatusCode(statusCode);
            assemPage(fetchResult, entity);
            return fetchResult;
        }
    } catch (Throwable e) {
        fetchResult.setFetchedUrl(e.toString());
        fetchResult.setStatusCode(Status.INTERNAL_SERVER_ERROR.ordinal());
        return fetchResult;
    } finally {
        try {
            if (entity == null && request != null)
                request.abort();
        } catch (Exception e) {
            throw e;
        }
    }

    fetchResult.setStatusCode(Status.UNSPECIFIED_ERROR.ordinal());
    return fetchResult;
}

From source file:com.amytech.android.library.utils.asynchttp.AsyncHttpClient.java

/**
 * Puts a new request in queue as a new thread in pool to be executed
 *
 * @param client//from w  w w  .  j av  a  2s  .  co  m
 *            HttpClient to be used for request, can differ in single
 *            requests
 * @param contentType
 *            MIME body type, for POST and PUT requests, may be null
 * @param context
 *            Context of Android application, to hold the reference of
 *            request
 * @param httpContext
 *            HttpContext in which the request will be executed
 * @param responseHandler
 *            ResponseHandler or its subclass to put the response into
 * @param uriRequest
 *            instance of HttpUriRequest, which means it must be of
 *            HttpDelete, HttpPost, HttpGet, HttpPut, etc.
 * @return RequestHandle of future request process
 */
protected RequestHandle sendRequest(DefaultHttpClient client, HttpContext httpContext,
        HttpUriRequest uriRequest, String contentType, ResponseHandlerInterface responseHandler,
        Context context) {
    if (uriRequest == null) {
        throw new IllegalArgumentException("HttpUriRequest must not be null");
    }

    if (responseHandler == null) {
        throw new IllegalArgumentException("ResponseHandler must not be null");
    }

    if (responseHandler.getUseSynchronousMode() && !responseHandler.getUsePoolThread()) {
        throw new IllegalArgumentException(
                "Synchronous ResponseHandler used in AsyncHttpClient. You should create your response handler in a looper thread or use SyncHttpClient instead.");
    }

    if (contentType != null) {
        if (uriRequest instanceof HttpEntityEnclosingRequestBase
                && ((HttpEntityEnclosingRequestBase) uriRequest).getEntity() != null
                && uriRequest.containsHeader(HEADER_CONTENT_TYPE)) {
            Log.w(LOG_TAG, "Passed contentType will be ignored because HttpEntity sets content type");
        } else {
            uriRequest.setHeader(HEADER_CONTENT_TYPE, contentType);
        }
    }

    responseHandler.setRequestHeaders(uriRequest.getAllHeaders());
    responseHandler.setRequestURI(uriRequest.getURI());

    AsyncHttpRequest request = newAsyncHttpRequest(client, httpContext, uriRequest, contentType,
            responseHandler, context);
    threadPool.submit(request);
    RequestHandle requestHandle = new RequestHandle(request);

    if (context != null) {
        List<RequestHandle> requestList;
        // Add request to request map
        synchronized (requestMap) {
            requestList = requestMap.get(context);
            if (requestList == null) {
                requestList = Collections.synchronizedList(new LinkedList<RequestHandle>());
                requestMap.put(context, requestList);
            }
        }

        requestList.add(requestHandle);

        Iterator<RequestHandle> iterator = requestList.iterator();
        while (iterator.hasNext()) {
            if (iterator.next().shouldBeGarbageCollected()) {
                iterator.remove();
            }
        }
    }

    return requestHandle;
}

From source file:org.jets3t.service.impl.rest.httpclient.RestStorageService.java

/**
 * Performs an HTTP/S request by invoking the provided HttpMethod object. If the HTTP
 * response code doesn't match the expected value, an exception is thrown.
 *
 * @param httpMethod//from   w w  w  .  j ava  2  s .  c o m
 *        the object containing a request target and all other information necessary to perform the
 *        request
 * @param expectedResponseCodes
 *        the HTTP response code(s) that indicates a successful request. If the response code received
 *        does not match this value an error must have occurred, so an exception is thrown.
 * @param context
 *        An HttpContext to facilitate information sharing in the HTTP chain
 * @throws ServiceException
 *        all exceptions are wrapped in an ServiceException. Depending on the kind of error that
 *        occurred, this exception may contain additional error information available from an XML
 *        error response document.
 */
protected HttpResponse performRequest(HttpUriRequest httpMethod, int[] expectedResponseCodes,
        HttpContext context) throws ServiceException {
    HttpResponse response = null;
    InterfaceLogBean reqBean = new InterfaceLogBean(httpMethod.getURI().toString(), "", "");
    try {
        if (log.isDebugEnabled()) {
            log.debug("Performing " + httpMethod.getMethod() + " request for '" + httpMethod.getURI().toString()
                    + "', expecting response codes: " + "[" + ServiceUtils.join(expectedResponseCodes, ",")
                    + "]");
            log.debug("Headers: " + Arrays.asList(httpMethod.getAllHeaders()));
        }
        log.debug("Endpoint: " + getEndpoint());

        // Variables to manage S3 Internal Server 500 or 503 Service Unavailable errors.
        boolean completedWithoutRecoverableError = true;
        int internalErrorCount = 0;
        int requestTimeoutErrorCount = 0;
        int redirectCount = 0;
        int authFailureCount = 0;
        boolean wasRecentlyRedirected = false;

        // Perform the request, sleeping and retrying when errors are encountered.
        int responseCode = -1;
        do {
            // Build the authorization string for the method (Unless we have just been redirected).
            if (!wasRecentlyRedirected) {
                authorizeHttpRequest(httpMethod, context);
            } else {
                // Reset redirection flag
                wasRecentlyRedirected = false;
            }

            response = httpClient.execute(httpMethod, context);
            responseCode = response.getStatusLine().getStatusCode();
            reqBean.setRespParams("[responseCode: " + responseCode + "][x-amz-request-id: "
                    + response.getFirstHeader("x-amz-request-id").getValue() + "]");
            if (responseCode == 307) {
                // Retry on Temporary Redirects, using new URI from location header
                authorizeHttpRequest(httpMethod, context); // Re-authorize *before* we change the URI
                Header locationHeader = response.getFirstHeader("location");

                // deal with implementations of HttpUriRequest
                if (httpMethod instanceof HttpRequestBase) {
                    ((HttpRequestBase) httpMethod).setURI(new URI(locationHeader.getValue()));
                } else if (httpMethod instanceof RequestWrapper) {
                    ((RequestWrapper) httpMethod).setURI(new URI(locationHeader.getValue()));
                }

                completedWithoutRecoverableError = false;
                redirectCount++;
                wasRecentlyRedirected = true;

                if (redirectCount > 5) {
                    reqBean.setResponseInfo("Exceeded 307 redirect limit (5).", "-1");
                    throw new ServiceException("Exceeded 307 redirect limit (5).");
                }
            } else if (responseCode == 500 || responseCode == 503) {
                // Retry on S3 Internal Server 500 or 503 Service Unavailable errors.
                completedWithoutRecoverableError = false;
                reqBean.setResponseInfo("Internal Server error(s).", "-1");
                ilog.error(reqBean);
                sleepOnInternalError(++internalErrorCount);
            } else {
                completedWithoutRecoverableError = true;
            }

            String contentType = "";
            if (response.getFirstHeader("Content-Type") != null) {
                contentType = response.getFirstHeader("Content-Type").getValue();
            }
            if (log.isDebugEnabled()) {
                log.debug("Response for '" + httpMethod.getMethod() + "'. Content-Type: " + contentType
                        + ", Headers: " + Arrays.asList(response.getAllHeaders()));
                log.debug("Response entity: " + response.getEntity());
                if (response.getEntity() != null) {
                    log.debug("Entity length: " + response.getEntity().getContentLength());
                }
            }

            // Check we received the expected result code.
            boolean didReceiveExpectedResponseCode = false;
            for (int i = 0; i < expectedResponseCodes.length && !didReceiveExpectedResponseCode; i++) {
                if (responseCode == expectedResponseCodes[i]) {
                    didReceiveExpectedResponseCode = true;
                }
            }
            if (log.isDebugEnabled()) {
                log.debug("Received expected response code: " + didReceiveExpectedResponseCode);
                log.debug("  expected code(s): " + Arrays.toString(expectedResponseCodes) + ".");
            }

            if (!didReceiveExpectedResponseCode) {
                if (log.isDebugEnabled()) {
                    log.debug("Response xml: " + isXmlContentType(contentType));
                    log.debug("Response entity: " + response.getEntity());
                    log.debug("Response entity length: " + (response.getEntity() == null ? "??"
                            : "" + response.getEntity().getContentLength()));
                }

                if (response.getEntity() != null && response.getEntity().getContentLength() != 0) {
                    if (log.isDebugEnabled()) {
                        log.debug("Response '" + httpMethod.getURI().getRawPath()
                                + "' - Received error response with XML message");
                    }

                    StringBuilder sb = new StringBuilder();
                    BufferedReader reader = null;
                    try {
                        reader = new BufferedReader(
                                new InputStreamReader(new HttpMethodReleaseInputStream(response)));
                        String line = null;
                        while ((line = reader.readLine()) != null) {
                            sb.append(line).append("\n");
                        }
                    } finally {
                        if (reader != null) {
                            reader.close();
                        }
                    }

                    EntityUtils.consume(response.getEntity());

                    // Throw exception containing the XML message document.
                    ServiceException exception = new ServiceException("S3 Error Message.", sb.toString());

                    exception.setResponseCode(responseCode);
                    exception.setResponseHeaders(RestUtils.convertHeadersToMap(response.getAllHeaders()));
                    reqBean.setResponseInfo("http status: " + responseCode, exception.getErrorCode());
                    ilog.error(reqBean);
                    if ("RequestTimeout".equals(exception.getErrorCode())) {
                        int retryMaxCount = jets3tProperties.getIntProperty("httpclient.retry-max", 5);

                        if (requestTimeoutErrorCount < retryMaxCount) {
                            requestTimeoutErrorCount++;
                            if (log.isWarnEnabled()) {
                                log.warn("Retrying connection that failed with RequestTimeout error"
                                        + ", attempt number " + requestTimeoutErrorCount + " of "
                                        + retryMaxCount);
                            }
                            completedWithoutRecoverableError = false;
                        } else {
                            if (log.isErrorEnabled()) {
                                log.error("Exceeded maximum number of retries for RequestTimeout errors: "
                                        + retryMaxCount);
                            }
                            throw exception;
                        }
                    } else if ("RequestTimeTooSkewed".equals(exception.getErrorCode())) {
                        //                            this.timeOffset = RestUtils.getAWSTimeAdjustment();
                        if (log.isWarnEnabled()) {
                            log.warn("Adjusted time offset in response to RequestTimeTooSkewed error. "
                                    + "Local machine and S3 server disagree on the time by approximately "
                                    + (this.timeOffset / 1000) + " seconds. Retrying connection.");
                        }
                        completedWithoutRecoverableError = false;
                        throw new ServiceException("S3 Error Message.", sb.toString());
                    } else if (responseCode == 500 || responseCode == 503) {
                        // Retrying after 500 or 503 error, don't throw exception.
                    } else if (responseCode == 307) {
                        // Retrying after Temporary Redirect 307, don't throw exception.
                        if (log.isDebugEnabled()) {
                            log.debug("Following Temporary Redirect to: " + httpMethod.getURI().toString());
                        }
                    }

                    // Special handling for S3 object PUT failures causing NoSuchKey errors - Issue #85
                    else if (responseCode == 404 && "PUT".equalsIgnoreCase(httpMethod.getMethod())
                            && "NoSuchKey".equals(exception.getErrorCode())
                            // If PUT operation is trying to copy an existing source object, don't ignore 404
                            && httpMethod.getFirstHeader(getRestHeaderPrefix() + "copy-source") == null) {
                        // Retrying after mysterious PUT NoSuchKey error caused by S3, don't throw exception.
                        if (log.isDebugEnabled()) {
                            log.debug("Ignoring NoSuchKey/404 error on PUT to: "
                                    + httpMethod.getURI().toString());
                        }
                        completedWithoutRecoverableError = false;
                    }

                    else if ((responseCode == 403 || responseCode == 401)
                            && this.isRecoverable403(httpMethod, exception)) {
                        completedWithoutRecoverableError = false;
                        authFailureCount++;

                        if (authFailureCount > 1) {
                            throw new ServiceException("Exceeded 403 retry limit (1).");
                        }

                        if (log.isDebugEnabled()) {
                            log.debug("Retrying after 403 Forbidden");
                        }
                    }

                    else {
                        throw exception;
                    }
                } else {
                    reqBean.setResponseInfo("http status:" + responseCode, "-1");
                    ilog.error(reqBean);
                    // Consume response content and release connection.
                    String responseText = null;
                    byte[] responseBody = null;
                    if (response.getEntity() != null) {
                        responseBody = EntityUtils.toByteArray(response.getEntity());
                    }
                    if (responseBody != null && responseBody.length > 0) {
                        responseText = new String(responseBody);
                    }

                    if (log.isDebugEnabled()) {
                        log.debug("Releasing error response without XML content");
                    }
                    EntityUtils.consume(response.getEntity());

                    if (responseCode == 500 || responseCode == 503) {
                        // Retrying after InternalError 500, don't throw exception.
                    } else {
                        // Throw exception containing the HTTP error fields.
                        HttpException httpException = new HttpException(responseCode,
                                response.getStatusLine().getReasonPhrase());
                        ServiceException exception = new ServiceException(
                                "Request Error" + (responseText != null ? " [" + responseText + "]." : "."),
                                httpException);
                        reqBean.setResponseInfo(
                                "Request Error" + (responseText != null ? " [" + responseText + "]." : "."),
                                "-1");
                        ilog.error(reqBean);
                        exception.setResponseHeaders(RestUtils.convertHeadersToMap(response.getAllHeaders()));
                        throw exception;
                    }
                }

                // Print warning message if a non-fatal error occurred (we only reach this
                // point in the code if an exception isn't thrown above)
                if (log.isWarnEnabled()) {
                    String requestDescription = httpMethod.getMethod() + " '" + httpMethod.getURI().getPath()
                            + (httpMethod.getURI().getQuery() != null
                                    && httpMethod.getURI().getQuery().length() > 0
                                            ? "?" + httpMethod.getURI().getQuery()
                                            : "")
                            + "'" + " -- ResponseCode: " + responseCode + ", ResponseStatus: "
                            + response.getStatusLine().getReasonPhrase() + ", Request Headers: ["
                            + ServiceUtils.join(httpMethod.getAllHeaders(), ", ") + "]"
                            + ", Response Headers: [" + ServiceUtils.join(response.getAllHeaders(), ", ") + "]";
                    requestDescription = requestDescription.replaceAll("[\\n\\r\\f]", ""); // Remove any newlines.
                    log.warn("Error Response: " + requestDescription);
                }
            }
        } while (!completedWithoutRecoverableError);
    } catch (Throwable t) {
        if (log.isDebugEnabled()) {
            String msg = "Rethrowing as a ServiceException error in performRequest: " + t;
            if (t.getCause() != null) {
                msg += ", with cause: " + t.getCause();
            }
            if (log.isTraceEnabled()) {
                log.trace(msg, t);
            } else {
                log.debug(msg);
            }
        }
        if (log.isDebugEnabled() && !shuttingDown) {
            log.debug("Releasing HttpClient connection after error: " + t.getMessage());
        }
        httpMethod.abort();

        ServiceException serviceException;
        if (t instanceof ServiceException) {
            serviceException = (ServiceException) t;
        } else {
            MxDelegate.getInstance().registerS3ServiceExceptionEvent();
            serviceException = new ServiceException("Request Error: " + t, t);
        }

        // Add S3 request and host IDs from HTTP headers to exception, if they are available
        // and have not already been populated by parsing an XML error response.
        if (!serviceException.isParsedFromXmlMessage() && response != null
                && response.getFirstHeader(Constants.AMZ_REQUEST_ID_1) != null
                && response.getFirstHeader(Constants.AMZ_REQUEST_ID_2) != null) {
            serviceException.setRequestAndHostIds(
                    response.getFirstHeader(Constants.AMZ_REQUEST_ID_1).getValue(),
                    response.getFirstHeader(Constants.AMZ_REQUEST_ID_2).getValue());
            serviceException.setResponseHeaders(RestUtils.convertHeadersToMap(response.getAllHeaders()));
        }
        if (response != null) {
            try {
                serviceException.setResponseCode(response.getStatusLine().getStatusCode());
                serviceException.setResponseStatus(response.getStatusLine().getReasonPhrase());
            } catch (NullPointerException e) {
                // If no network connection is available, status info is not available
            }
        }
        if (httpMethod.getFirstHeader("Host") != null) {
            serviceException.setRequestHost(httpMethod.getFirstHeader("Host").getValue());
        }
        if (response != null && response.getFirstHeader("Date") != null) {
            serviceException.setResponseDate(response.getFirstHeader("Date").getValue());
        }
        reqBean.setResponseInfo(serviceException.getErrorMessage(), serviceException.getErrorCode());
        throw serviceException;
    }
    reqBean.setRespTime(new Date());
    reqBean.setTargetAddr(getEndpoint());
    reqBean.setResultCode("0");
    ilog.info(reqBean);
    return response;
}

From source file:com.fujitsu.dc.client.http.DcRequestBuilder.java

/**
 * This method is used to generate a HttpUriRequest object by setting the parameters in request header.
 * @return HttpUriRequest object that is generated
 * @throws DaoException Exception thrown
 *///from  w  w  w  .j  a va2 s  . c o  m
public HttpUriRequest build() throws DaoException {
    HttpUriRequest req = null;
    if (HttpMethods.PUT.equals(this.methodValue)) {
        req = new HttpPut(this.urlValue);
    } else if (HttpMethods.POST.equals(this.methodValue)) {
        req = new HttpPost(this.urlValue);
    } else if (HttpMethods.DELETE.equals(this.methodValue)) {
        req = new HttpDelete(this.urlValue);
    } else if (HttpMethods.ACL.equals(this.methodValue)) {
        req = new HttpAclMethod(this.urlValue);
    } else if (HttpMethods.MKCOL.equals(this.methodValue)) {
        req = new HttpMkColMethod(this.urlValue);
    } else if (HttpMethods.PROPPATCH.equals(this.methodValue)) {
        req = new HttpPropPatchMethod(this.urlValue);
    } else if (HttpMethods.PROPFIND.equals(this.methodValue)) {
        req = new HttpPropfindMethod(this.urlValue);
    } else if (HttpMethods.GET.equals(this.methodValue)) {
        req = new HttpGet(this.urlValue);
    } else if (HttpMethods.MERGE.equals(this.methodValue)) {
        req = new HttpMergeMethod(this.urlValue);
    }

    if (this.tokenValue != null) {
        req.addHeader(HttpHeaders.AUTHORIZATION, "Bearer " + this.tokenValue);
    }

    /** include header parameters if any. */
    for (String key : headers.keySet()) {
        String value = headers.get(key);
        req.addHeader(key, value);
    }

    // ????????
    /** If Default header is set, configure them. */
    // ?????????????????????
    /**
     * The reason you do not want to set for the first time, since the request header, would have been more than one
     * registration is the same name header
     */
    if (this.defaultHeaders != null) {
        for (String key : this.defaultHeaders.keySet()) {
            String val = this.defaultHeaders.get(key);
            Header[] headerItems = req.getHeaders(key);
            if (headerItems.length == 0) {
                req.addHeader(key, val);
            }
        }
    }
    if (this.bodyValue != null) {
        HttpEntity body = null;
        try {
            if (this.getContentType() != "" && RestAdapter.CONTENT_TYPE_JSON.equals(this.getContentType())) {
                String bodyStr = toUniversalCharacterNames(this.bodyValue);
                body = new StringEntity(bodyStr);
            } else {
                body = new StringEntity(this.bodyValue, RestAdapter.ENCODE);
            }
        } catch (UnsupportedEncodingException e) {
            throw DaoException.create("error while request body encoding : " + e.getMessage(), 0);
        }
        ((HttpEntityEnclosingRequest) req).setEntity(body);
    }
    if (this.bodyStream != null) {
        InputStreamEntity body = new InputStreamEntity(this.bodyStream, -1);
        body.setChunked(true);
        this.bodyValue = "[stream]";
        ((HttpEntityEnclosingRequest) req).setEntity(body);
    }
    if (req != null) {
        log.debug("");
        log.debug("?Request " + req.getMethod() + "  " + req.getURI());
        Header[] allheaders = req.getAllHeaders();
        for (int i = 0; i < allheaders.length; i++) {
            log.debug("RequestHeader[" + allheaders[i].getName() + "] : " + allheaders[i].getValue());
        }
        log.debug("RequestBody : " + bodyValue);
    }
    return req;
}

From source file:org.eclipse.om2m.binding.http.RestHttpClient.java

/**
 * Converts a protocol-independent {@link RequestPrimitive} object into a standard HTTP request and sends a standard HTTP request.
 * Converts the received standard HTTP request into {@link ResponsePrimitive} object and returns it back.
 * @param requestPrimitive - protocol independent request.
 * @return protocol independent response.
 *///from  www.j  av a  2s  .com
public ResponsePrimitive sendRequest(RequestPrimitive requestPrimitive) {
    LOGGER.info("Sending request: " + requestPrimitive);
    CloseableHttpClient httpClient = HttpClients.createDefault();
    ResponsePrimitive responsePrimitive = new ResponsePrimitive(requestPrimitive);
    HttpUriRequest method = null;

    // Retrieve the url
    String url = requestPrimitive.getTo();
    if (!url.startsWith(protocol + "://")) {
        if (url.startsWith("://")) {
            url = protocol + url;
        } else if (url.startsWith("//")) {
            url = protocol + ":" + url;
        } else {
            url = protocol + "://" + url;
        }
    }

    Map<String, List<String>> parameters = getParameters(requestPrimitive);
    parameters.putAll(requestPrimitive.getQueryStrings());
    if (!parameters.isEmpty()) {
        String queryString = "";
        for (String parameter : parameters.keySet()) {
            for (String value : parameters.get(parameter)) {
                queryString += "&" + parameter + "=" + value;
            }
        }
        queryString = queryString.replaceFirst("&", "?");
        LOGGER.info("Query string generated: " + queryString);
        url += queryString;
    }

    try {
        // Set the operation
        BigInteger operation = requestPrimitive.getOperation();
        if (operation != null) {
            if (operation.equals(Operation.CREATE)) {
                method = new HttpPost(url);
                if (requestPrimitive.getContent() != null) {
                    ((HttpPost) method).setEntity(new StringEntity((String) requestPrimitive.getContent()));
                }
            } else if (operation.equals(Operation.RETRIEVE)) {
                method = new HttpGet(url);
            } else if (operation.equals(Operation.UPDATE)) {
                method = new HttpPut(url);
                if (requestPrimitive.getContent() != null) {
                    ((HttpPut) method).setEntity(new StringEntity((String) requestPrimitive.getContent()));
                }
            } else if (operation.equals(Operation.DELETE)) {
                method = new HttpDelete(url);
            } else if (operation.equals(Operation.NOTIFY)) {
                method = new HttpPost(url);
                if (requestPrimitive.getContent() != null) {
                    ((HttpPost) method).setEntity(new StringEntity((String) requestPrimitive.getContent()));
                }
            }
        } else {
            return null;
        }

        // Set the return content type
        method.addHeader(HttpHeaders.ACCEPT, requestPrimitive.getReturnContentType());

        // Set the request content type
        String contentTypeHeader = requestPrimitive.getRequestContentType();

        // Set the request identifier header
        if (requestPrimitive.getRequestIdentifier() != null) {
            method.addHeader(HttpHeaders.REQUEST_IDENTIFIER, requestPrimitive.getRequestIdentifier());
        }

        // Set the originator header
        if (requestPrimitive.getFrom() != null) {
            method.addHeader(HttpHeaders.ORIGINATOR, requestPrimitive.getFrom());
        }

        // Add the content type header with the resource type for create operation
        if (requestPrimitive.getResourceType() != null) {
            contentTypeHeader += ";ty=" + requestPrimitive.getResourceType().toString();
        }
        method.addHeader(HttpHeaders.CONTENT_TYPE, contentTypeHeader);

        // Add the notification URI in the case of non-blocking request
        if (requestPrimitive.getResponseTypeInfo() != null) {
            String uris = "";
            for (String notifUri : requestPrimitive.getResponseTypeInfo().getNotificationURI()) {
                uris += "&" + notifUri;
            }
            uris = uris.replaceFirst("&", "");
            method.addHeader(HttpHeaders.RESPONSE_TYPE, uris);
        }

        if (requestPrimitive.getName() != null) {
            method.addHeader(HttpHeaders.NAME, requestPrimitive.getName());
        }

        LOGGER.info("Request to be send: " + method.toString());
        String headers = "";
        for (Header h : method.getAllHeaders()) {
            headers += h.toString() + "\n";
        }
        LOGGER.info("Headers:\n" + headers);

        HttpResponse httpResponse = httpClient.execute(method);
        int statusCode = httpResponse.getStatusLine().getStatusCode();
        if (httpResponse.getFirstHeader(HttpHeaders.RESPONSE_STATUS_CODE) != null) {
            responsePrimitive.setResponseStatusCode(
                    new BigInteger(httpResponse.getFirstHeader(HttpHeaders.RESPONSE_STATUS_CODE).getValue()));
        } else {
            responsePrimitive.setResponseStatusCode(getResponseStatusCode(httpResponse, statusCode));
        }
        if (statusCode != 204) {
            if (httpResponse.getEntity() != null) {
                responsePrimitive.setContent(Util.convertStreamToString(httpResponse.getEntity().getContent()));
                if (httpResponse.getFirstHeader(HttpHeaders.CONTENT_TYPE) != null) {
                    responsePrimitive
                            .setContentType(httpResponse.getFirstHeader(HttpHeaders.CONTENT_TYPE).getValue());
                }
            }
        }
        if (statusCode == 201) {
            String contentHeader = "";
            for (Header header : httpResponse.getHeaders(HttpHeaders.CONTENT_LOCATION)) {
                contentHeader += header.getValue();
            }
            responsePrimitive.setLocation(contentHeader);
        }
        LOGGER.info("Http Client response: " + responsePrimitive);
        httpClient.close();
    } catch (HttpHostConnectException e) {
        LOGGER.info("Target is not reachable: " + requestPrimitive.getTo());
        responsePrimitive.setResponseStatusCode(ResponseStatusCode.TARGET_NOT_REACHABLE);
        responsePrimitive.setContent("Target is not reachable: " + requestPrimitive.getTo());
        responsePrimitive.setContentType(MimeMediaType.TEXT_PLAIN);
    } catch (IOException e) {
        LOGGER.error(url + " not found", e);
        responsePrimitive.setResponseStatusCode(ResponseStatusCode.TARGET_NOT_REACHABLE);
    }
    return responsePrimitive;
}

From source file:org.jets3t.service.impl.rest.httpclient.RestStorageService.java

/**
 * Authorizes an HTTP/S request by signing it with an HMAC signature compatible with
 * the S3 service and Google Storage (legacy) authorization techniques.
 *
 * The signature is added to the request as an Authorization header.
 *
 * @param httpMethod// w w  w.  j  a  va  2  s.c o  m
 * the request object
 * @throws ServiceException
 */
public void authorizeHttpRequest(HttpUriRequest httpMethod, HttpContext context) throws ServiceException {
    if (getProviderCredentials() != null) {
        if (log.isDebugEnabled()) {
            log.debug("Adding authorization for Access Key '" + getProviderCredentials().getAccessKey() + "'.");
        }
    } else {
        if (log.isDebugEnabled()) {
            log.debug("Service has no Credential and is un-authenticated, skipping authorization");
        }
        return;
    }

    URI uri = httpMethod.getURI();
    String hostname = uri.getHost();

    /*
     * Determine the complete URL for the S3 resource, including any S3-specific parameters.
     */
    // Use raw-path, otherwise escaped characters are unescaped and a wrong
    // signature is produced
    String xfullUrl = uri.getPath();
    String fullUrl = uri.getRawPath();

    // If we are using an alternative hostname, include the hostname/bucketname in the resource path.
    String s3Endpoint = this.getEndpoint();
    if (hostname != null && !s3Endpoint.equals(hostname)) {
        int subdomainOffset = hostname.lastIndexOf("." + s3Endpoint);
        if (subdomainOffset > 0) {
            // Hostname represents an S3 sub-domain, so the bucket's name is the CNAME portion
            fullUrl = "/" + hostname.substring(0, subdomainOffset) + fullUrl;
        } else {
            // Hostname represents a virtual host, so the bucket's name is identical to hostname
            fullUrl = "/" + hostname + fullUrl;
        }
    }

    String queryString = uri.getRawQuery();
    if (queryString != null && queryString.length() > 0) {
        fullUrl += "?" + queryString;
    }

    // Set/update the date timestamp to the current time
    // Note that this will be over-ridden if an "x-amz-date" or
    // "x-goog-date" header is present.
    httpMethod.setHeader("Date", ServiceUtils.formatRfc822Date(getCurrentTimeWithOffset()));

    if (log.isDebugEnabled()) {
        log.debug("For creating canonical string, using uri: " + fullUrl);
    }

    // Generate a canonical string representing the operation.
    String canonicalString = null;
    try {
        canonicalString = RestUtils.makeServiceCanonicalString(httpMethod.getMethod(), fullUrl,
                convertHeadersToMap(httpMethod.getAllHeaders()), null, getRestHeaderPrefix(),
                getResourceParameterNames());
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException(e.getMessage(), e);
    }
    if (log.isDebugEnabled()) {
        log.debug("Canonical string ('|' is a newline): " + canonicalString.replace('\n', '|'));
    }

    // Sign the canonical string.
    String signedCanonical = ServiceUtils.signWithHmacSha1(getProviderCredentials().getSecretKey(),
            canonicalString);

    // Add encoded authorization to connection as HTTP Authorization header.
    String authorizationString = getSignatureIdentifier() + " " + getProviderCredentials().getAccessKey() + ":"
            + signedCanonical;
    httpMethod.setHeader("Authorization", authorizationString);
}