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

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

Introduction

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

Prototype

public Header[] getAllHeaders() 

Source Link

Usage

From source file:org.dasein.cloud.azure.AzureMethod.java

public void tempRedirectInvoke(@Nonnull String tempEndpoint, @Nonnull String method, @Nonnull String account,
        @Nonnull String resource, @Nonnull String body) throws CloudException, InternalException {
    if (logger.isTraceEnabled()) {
        logger.trace("enter - " + AzureMethod.class.getName() + ".post(" + account + "," + resource + ")");
    }/* w w w. j ava 2s  .c  o  m*/
    if (wire.isDebugEnabled()) {
        wire.debug("POST --------------------------------------------------------> " + endpoint + account
                + resource);
        wire.debug("");
    }
    try {
        HttpClient client = getClient();
        String url = tempEndpoint + account + resource;

        HttpRequestBase httpMethod = getMethod(method, url);

        //If it is networking configuration services
        if (httpMethod instanceof HttpPut) {
            if (url.endsWith("/services/networking/media")) {
                httpMethod.addHeader("Content-Type", "text/plain");
            } else {
                httpMethod.addHeader("Content-Type", "application/xml;charset=UTF-8");
            }
        } else {
            httpMethod.addHeader("Content-Type", "application/xml;charset=UTF-8");
        }

        //dmayne version is older for anything to do with images and for disk deletion
        if (url.indexOf("/services/images") > -1
                || (httpMethod instanceof HttpDelete && url.indexOf("/services/disks") > -1)) {
            httpMethod.addHeader("x-ms-version", "2012-08-01");
        } else {
            httpMethod.addHeader("x-ms-version", "2012-03-01");
        }
        if (wire.isDebugEnabled()) {
            wire.debug(httpMethod.getRequestLine().toString());
            for (Header header : httpMethod.getAllHeaders()) {
                wire.debug(header.getName() + ": " + header.getValue());
            }
            wire.debug("");
            if (body != null) {
                wire.debug(body);
                wire.debug("");
            }
        }

        if (httpMethod instanceof HttpEntityEnclosingRequestBase) {

            HttpEntityEnclosingRequestBase entityEnclosingMethod = (HttpEntityEnclosingRequestBase) httpMethod;

            if (body != null) {
                try {
                    entityEnclosingMethod.setEntity(new StringEntity(body, "application/xml", "utf-8"));
                } catch (UnsupportedEncodingException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }

        HttpResponse response;
        StatusLine status;

        try {
            response = client.execute(httpMethod);
            status = response.getStatusLine();
        } catch (IOException e) {
            logger.error("post(): Failed to execute HTTP request due to a cloud I/O error: " + e.getMessage());
            if (logger.isTraceEnabled()) {
                e.printStackTrace();
            }
            throw new CloudException(e);
        }
        if (logger.isDebugEnabled()) {
            logger.debug("post(): HTTP Status " + status);
        }
        Header[] headers = response.getAllHeaders();

        if (wire.isDebugEnabled()) {
            wire.debug(status.toString());
            for (Header h : headers) {
                if (h.getValue() != null) {
                    wire.debug(h.getName() + ": " + h.getValue().trim());
                } else {
                    wire.debug(h.getName() + ":");
                }
            }
            wire.debug("");
        }
        if (status.getStatusCode() != HttpServletResponse.SC_OK
                && status.getStatusCode() != HttpServletResponse.SC_CREATED
                && status.getStatusCode() != HttpServletResponse.SC_ACCEPTED) {
            logger.error("post(): Expected OK for GET request, got " + status.getStatusCode());

            HttpEntity entity = response.getEntity();

            if (entity == null) {
                throw new AzureException(CloudErrorType.GENERAL, status.getStatusCode(),
                        status.getReasonPhrase(), "An error was returned without explanation");
            }
            try {
                body = EntityUtils.toString(entity);
            } catch (IOException e) {
                throw new AzureException(CloudErrorType.GENERAL, status.getStatusCode(),
                        status.getReasonPhrase(), e.getMessage());
            }
            if (wire.isDebugEnabled()) {
                wire.debug(body);
            }
            wire.debug("");
            AzureException.ExceptionItems items = AzureException.parseException(status.getStatusCode(), body);

            if (items == null) {
                throw new CloudException(CloudErrorType.GENERAL, status.getStatusCode(), "Unknown", "Unknown");
            }
            logger.error("post(): [" + status.getStatusCode() + " : " + items.message + "] " + items.details);
            throw new AzureException(items);
        }
    } finally {
        if (logger.isTraceEnabled()) {
            logger.trace("exit - " + AzureMethod.class.getName() + ".post()");
        }
        if (wire.isDebugEnabled()) {
            wire.debug("");
            wire.debug("POST --------------------------------------------------------> " + endpoint + account
                    + resource);
        }
    }
}

From source file:org.dasein.cloud.azure.AzureMethod.java

public String invoke(@Nonnull String method, @Nonnull String account, @Nonnull String resource,
        @Nonnull String body) throws CloudException, InternalException {
    if (logger.isTraceEnabled()) {
        logger.trace("enter - " + AzureMethod.class.getName() + ".post(" + account + "," + resource + ")");
    }//from  w  w w.j a v  a2s. c  o  m
    if (wire.isDebugEnabled()) {
        wire.debug("POST --------------------------------------------------------> " + endpoint + account
                + resource);
        wire.debug("");
    }
    String requestId = null;
    try {
        HttpClient client = getClient();
        String url = endpoint + account + resource;

        HttpRequestBase httpMethod = getMethod(method, url);

        //If it is networking configuration services
        if (httpMethod instanceof HttpPut) {
            if (url.endsWith("/services/networking/media")) {
                httpMethod.addHeader("Content-Type", "text/plain");
            } else {
                httpMethod.addHeader("Content-Type", "application/xml;charset=UTF-8");
            }
        } else {
            httpMethod.addHeader("Content-Type", "application/xml;charset=UTF-8");
        }

        //dmayne version is older for anything to do with images and for disk deletion
        if (url.indexOf("/services/images") > -1
                || (httpMethod instanceof HttpDelete && url.indexOf("/services/disks") > -1)) {
            httpMethod.addHeader("x-ms-version", "2012-08-01");
        } else {
            httpMethod.addHeader("x-ms-version", "2012-03-01");
        }
        if (wire.isDebugEnabled()) {
            wire.debug(httpMethod.getRequestLine().toString());
            for (Header header : httpMethod.getAllHeaders()) {
                wire.debug(header.getName() + ": " + header.getValue());
            }
            wire.debug("");
            if (body != null) {
                wire.debug(body);
                wire.debug("");
            }
        }

        if (httpMethod instanceof HttpEntityEnclosingRequestBase) {

            HttpEntityEnclosingRequestBase entityEnclosingMethod = (HttpEntityEnclosingRequestBase) httpMethod;

            if (body != null) {
                try {
                    entityEnclosingMethod.setEntity(new StringEntity(body, "application/xml", "utf-8"));
                } catch (UnsupportedEncodingException e) {
                    throw new CloudException(e);
                }
            }
        }

        HttpResponse response;
        StatusLine status;

        try {
            response = client.execute(httpMethod);
            status = response.getStatusLine();
        } catch (IOException e) {
            logger.error("post(): Failed to execute HTTP request due to a cloud I/O error: " + e.getMessage());
            if (logger.isTraceEnabled()) {
                e.printStackTrace();
            }
            throw new CloudException(e);
        }
        if (logger.isDebugEnabled()) {
            logger.debug("post(): HTTP Status " + status);
        }
        Header[] headers = response.getAllHeaders();

        if (wire.isDebugEnabled()) {
            wire.debug(status.toString());
            for (Header h : headers) {
                if (h.getValue() != null) {
                    wire.debug(h.getName() + ": " + h.getValue().trim());
                    if (h.getName().equalsIgnoreCase("x-ms-request-id")) {
                        requestId = h.getValue().trim();
                    }
                } else {
                    wire.debug(h.getName() + ":");
                }
            }
            wire.debug("");
        }
        if (status.getStatusCode() == HttpServletResponse.SC_TEMPORARY_REDIRECT) {
            logger.warn("Expected OK, got " + status.getStatusCode());

            String responseBody = "";

            HttpEntity entity = response.getEntity();

            if (entity == null) {
                throw new AzureException(CloudErrorType.GENERAL, status.getStatusCode(),
                        status.getReasonPhrase(), "An error was returned without explanation");
            }
            try {
                responseBody = EntityUtils.toString(entity);
            } catch (IOException e) {
                throw new AzureException(CloudErrorType.GENERAL, status.getStatusCode(),
                        status.getReasonPhrase(), e.getMessage());
            }
            logger.debug(responseBody);
            logger.debug("https: char " + responseBody.indexOf("https://"));
            logger.debug("account number: char " + responseBody.indexOf(account));
            String tempEndpoint = responseBody.substring(responseBody.indexOf("https://"),
                    responseBody.indexOf(account) - responseBody.indexOf("https://"));
            logger.debug("temp redirect location: " + tempEndpoint);
            tempRedirectInvoke(tempEndpoint, method, account, resource, body);
        } else if (status.getStatusCode() != HttpServletResponse.SC_OK
                && status.getStatusCode() != HttpServletResponse.SC_CREATED
                && status.getStatusCode() != HttpServletResponse.SC_ACCEPTED) {
            logger.error("post(): Expected OK for GET request, got " + status.getStatusCode());

            HttpEntity entity = response.getEntity();

            if (entity == null) {
                throw new AzureException(CloudErrorType.GENERAL, status.getStatusCode(),
                        status.getReasonPhrase(), "An error was returned without explanation");
            }
            try {
                body = EntityUtils.toString(entity);
            } catch (IOException e) {
                throw new AzureException(CloudErrorType.GENERAL, status.getStatusCode(),
                        status.getReasonPhrase(), e.getMessage());
            }
            if (wire.isDebugEnabled()) {
                wire.debug(body);
            }
            wire.debug("");
            AzureException.ExceptionItems items = AzureException.parseException(status.getStatusCode(), body);

            if (items == null) {
                throw new CloudException(CloudErrorType.GENERAL, status.getStatusCode(), "Unknown", "Unknown");
            }
            logger.error("post(): [" + status.getStatusCode() + " : " + items.message + "] " + items.details);
            throw new AzureException(items);
        }
    } finally {
        if (logger.isTraceEnabled()) {
            logger.trace("exit - " + AzureMethod.class.getName() + ".post()");
        }
        if (wire.isDebugEnabled()) {
            wire.debug("");
            wire.debug("POST --------------------------------------------------------> " + endpoint + account
                    + resource);
        }
    }
    return requestId;
}

From source file:org.dasein.cloud.azure.AzureStorageMethod.java

private String calculatedSharedKeyLiteSignature(@Nonnull HttpRequestBase method,
        @Nonnull Map<String, String> queryParams) throws CloudException, InternalException {
    fetchKeys();//from www.  ja va2  s  .  c o m

    ProviderContext ctx = provider.getContext();

    if (ctx == null) {
        throw new AzureConfigException("No context was specified for this request");
    }
    Header h = method.getFirstHeader("content-type");
    String contentType = (h == null ? null : h.getValue());

    if (contentType == null) {
        contentType = "";
    }
    StringBuilder stringToSign = new StringBuilder();

    stringToSign.append(method.getMethod().toUpperCase()).append("\n");
    stringToSign.append("\n"); // content-md5
    stringToSign.append(contentType).append("\n");
    stringToSign.append(method.getFirstHeader("date").getValue()).append("\n");

    Header[] headers = method.getAllHeaders();
    TreeSet<String> keys = new TreeSet<String>();

    for (Header header : headers) {
        if (header.getName().startsWith(Header_Prefix_MS)) {
            keys.add(header.getName().toLowerCase());
        }
    }

    for (String key : keys) {
        Header header = method.getFirstHeader(key);

        if (header != null) {
            Header[] all = method.getHeaders(key);

            stringToSign.append(key.toLowerCase().trim()).append(":");
            if (all != null && all.length > 0) {
                for (Header current : all) {
                    String v = (current.getValue() != null ? current.getValue() : "");

                    stringToSign.append(v.trim().replaceAll("\n", " ")).append(",");
                }
            }
            stringToSign.deleteCharAt(stringToSign.lastIndexOf(","));
        } else {
            stringToSign.append(key.toLowerCase().trim()).append(":");
        }
        stringToSign.append("\n");
    }

    stringToSign.append("/").append(getStorageAccount()).append(method.getURI().getPath());

    keys.clear();
    for (String key : queryParams.keySet()) {
        if (key.equalsIgnoreCase("comp")) {
            key = key.toLowerCase();
            keys.add(key);
        }
    }
    if (!keys.isEmpty()) {
        stringToSign.append("?");
        for (String key : keys) {
            String value = queryParams.get(key);

            if (value == null) {
                value = "";
            }
            stringToSign.append(key).append("=").append(value).append("&");
        }
        stringToSign.deleteCharAt(stringToSign.lastIndexOf("&"));
    }
    try {
        if (logger.isDebugEnabled()) {
            logger.debug("BEGIN STRING TO SIGN");
            logger.debug(stringToSign.toString());
            logger.debug("END STRING TO SIGN");
        }
        Mac mac = Mac.getInstance("HmacSHA256");
        mac.init(new SecretKeySpec(Base64.decodeBase64(ctx.getStoragePrivate()), "HmacSHA256"));

        String signature = new String(
                Base64.encodeBase64(mac.doFinal(stringToSign.toString().getBytes("UTF-8"))));

        if (logger.isDebugEnabled()) {
            logger.debug("signature=" + signature);
        }
        return signature;
    } catch (UnsupportedEncodingException e) {
        logger.error("UTF-8 not supported: " + e.getMessage());
        throw new InternalException(e);
    } catch (NoSuchAlgorithmException e) {
        logger.error("No such algorithm: " + e.getMessage());
        throw new InternalException(e);
    } catch (InvalidKeyException e) {
        logger.error("Invalid key: " + e.getMessage());
        throw new InternalException(e);
    }
}

From source file:org.apache.axis2.transport.http.impl.httpclient4.HTTPSenderImpl.java

public void addCustomHeaders(HttpRequestBase method, MessageContext msgContext) {

    boolean isCustomUserAgentSet = false;
    // set the custom headers, if available
    Object httpHeadersObj = msgContext.getProperty(HTTPConstants.HTTP_HEADERS);
    if (httpHeadersObj != null) {
        if (httpHeadersObj instanceof List) {
            List httpHeaders = (List) httpHeadersObj;
            for (int i = 0; i < httpHeaders.size(); i++) {
                NamedValue nv = (NamedValue) httpHeaders.get(i);
                if (nv != null) {
                    Header header = new BasicHeader(nv.getName(), nv.getValue());
                    if (HTTPConstants.HEADER_USER_AGENT.equals(header.getName())) {
                        isCustomUserAgentSet = true;
                    }/*from ww w . ja  v a 2 s .c  om*/
                    method.addHeader(header);
                }
            }

        }
        if (httpHeadersObj instanceof Map) {
            Map httpHeaders = (Map) httpHeadersObj;
            for (Iterator iterator = httpHeaders.entrySet().iterator(); iterator.hasNext();) {
                Map.Entry entry = (Map.Entry) iterator.next();
                String key = (String) entry.getKey();
                String value = (String) entry.getValue();
                if (HTTPConstants.HEADER_USER_AGENT.equals(key)) {
                    isCustomUserAgentSet = true;
                }
                method.addHeader(key, value);
            }
        }
    }

    // we have to consider the TRANSPORT_HEADERS map as well
    Map transportHeaders = (Map) msgContext.getProperty(MessageContext.TRANSPORT_HEADERS);
    if (transportHeaders != null) {
        removeUnwantedHeaders(msgContext);

        Set headerEntries = transportHeaders.entrySet();

        for (Object headerEntry : headerEntries) {
            if (headerEntry instanceof Map.Entry) {
                Header[] headers = method.getAllHeaders();

                boolean headerAdded = false;
                for (Header header : headers) {
                    if (header.getName() != null
                            && header.getName().equals(((Map.Entry) headerEntry).getKey())) {
                        headerAdded = true;
                        break;
                    }
                }

                if (!headerAdded) {
                    method.addHeader(((Map.Entry) headerEntry).getKey().toString(),
                            ((Map.Entry) headerEntry).getValue().toString());
                }
            }
        }
    }

    if (!isCustomUserAgentSet) {
        String userAgentString = getUserAgent(msgContext);
        method.setHeader(HTTPConstants.HEADER_USER_AGENT, userAgentString);
    }

}

From source file:com.groupon.odo.bmp.http.BrowserMobHttpClient.java

private BrowserMobHttpResponse execute(BrowserMobHttpRequest req, int depth) {
    if (depth >= MAX_REDIRECT) {
        throw new IllegalStateException("Max number of redirects (" + MAX_REDIRECT + ") reached");
    }//w  ww .  java2s  .  co m

    RequestCallback callback = req.getRequestCallback();

    HttpRequestBase method = req.getMethod();
    String verificationText = req.getVerificationText();
    String url = method.getURI().toString();

    // save the browser and version if it's not yet been set
    if (har != null && har.getLog().getBrowser() == null) {
        Header[] uaHeaders = method.getHeaders("User-Agent");
        if (uaHeaders != null && uaHeaders.length > 0) {
            String userAgent = uaHeaders[0].getValue();
            try {
                // note: this doesn't work for 'Fandango/4.5.1 CFNetwork/548.1.4 Darwin/11.0.0'
                ReadableUserAgent uai = PARSER.parse(userAgent);
                String browser = uai.getName();
                String version = uai.getVersionNumber().toVersionString();
                har.getLog().setBrowser(new HarNameVersion(browser, version));
            } catch (Exception e) {
                LOG.warn("Failed to parse user agent string", e);
            }
        }
    }

    // process any rewrite requests
    boolean rewrote = false;
    String newUrl = url;
    for (RewriteRule rule : rewriteRules) {
        Matcher matcher = rule.match.matcher(newUrl);
        newUrl = matcher.replaceAll(rule.replace);
        rewrote = true;
    }

    if (rewrote) {
        try {
            method.setURI(new URI(newUrl));
            url = newUrl;
        } catch (URISyntaxException e) {
            LOG.warn("Could not rewrite url to %s", newUrl);
        }
    }

    // handle whitelist and blacklist entries
    int mockResponseCode = -1;
    synchronized (this) {
        // guard against concurrent modification of whitelistEntry
        if (whitelistEntry != null) {
            boolean found = false;
            for (Pattern pattern : whitelistEntry.patterns) {
                if (pattern.matcher(url).matches()) {
                    found = true;
                    break;
                }
            }

            if (!found) {
                mockResponseCode = whitelistEntry.responseCode;
            }
        }
    }

    if (blacklistEntries != null) {
        for (BlacklistEntry blacklistEntry : blacklistEntries) {
            if (blacklistEntry.pattern.matcher(url).matches()) {
                mockResponseCode = blacklistEntry.responseCode;
                break;
            }
        }
    }

    if (!additionalHeaders.isEmpty()) {
        // Set the additional headers
        for (Map.Entry<String, String> entry : additionalHeaders.entrySet()) {
            String key = entry.getKey();
            String value = entry.getValue();
            method.removeHeaders(key);
            method.addHeader(key, value);
        }
    }

    String charSet = "UTF-8";
    String responseBody = null;

    InputStream is = null;
    int statusCode = -998;
    long bytes = 0;
    boolean gzipping = false;
    boolean contentMatched = true;
    OutputStream os = req.getOutputStream();
    if (os == null) {
        os = new CappedByteArrayOutputStream(1024 * 1024); // MOB-216 don't buffer more than 1 MB
    }
    if (verificationText != null) {
        contentMatched = false;
    }

    // link the object up now, before we make the request, so that if we get cut off (ie: favicon.ico request and browser shuts down)
    // we still have the attempt associated, even if we never got a response
    HarEntry entry = new HarEntry(harPageRef);

    // clear out any connection-related information so that it's not stale from previous use of this thread.
    RequestInfo.clear(url, entry);

    entry.setRequest(new HarRequest(method.getMethod(), url, method.getProtocolVersion().getProtocol()));
    entry.setResponse(new HarResponse(-999, "NO RESPONSE", method.getProtocolVersion().getProtocol()));
    if (this.har != null && harPageRef != null) {
        har.getLog().addEntry(entry);
    }

    String query = method.getURI().getRawQuery();
    if (query != null) {
        MultiMap<String> params = new MultiMap<String>();
        UrlEncoded.decodeTo(query, params, "UTF-8");
        for (String k : params.keySet()) {
            for (Object v : params.getValues(k)) {
                entry.getRequest().getQueryString().add(new HarNameValuePair(k, (String) v));
            }
        }
    }

    String errorMessage = null;
    HttpResponse response = null;

    BasicHttpContext ctx = new BasicHttpContext();

    ActiveRequest activeRequest = new ActiveRequest(method, ctx, entry.getStartedDateTime());
    synchronized (activeRequests) {
        activeRequests.add(activeRequest);
    }

    // for dealing with automatic authentication
    if (authType == AuthType.NTLM) {
        // todo: not supported yet
        //ctx.setAttribute("preemptive-auth", new NTLMScheme(new JCIFSEngine()));
    } else if (authType == AuthType.BASIC) {
        ctx.setAttribute("preemptive-auth", new BasicScheme());
    }

    StatusLine statusLine = null;
    try {
        // set the User-Agent if it's not already set
        if (method.getHeaders("User-Agent").length == 0) {
            method.addHeader("User-Agent", "BrowserMob VU/1.0");
        }

        // was the request mocked out?
        if (mockResponseCode != -1) {
            statusCode = mockResponseCode;

            // TODO: HACKY!!
            callback.handleHeaders(new Header[] { new Header() {
                @Override
                public String getName() {
                    return "Content-Type";
                }

                @Override
                public String getValue() {
                    return "text/plain";
                }

                @Override
                public HeaderElement[] getElements() throws ParseException {
                    return new HeaderElement[0];
                }
            } });
            // Make sure we set the status line here too.
            // Use the version number from the request
            ProtocolVersion version = null;
            int reqDotVersion = req.getProxyRequest().getDotVersion();
            if (reqDotVersion == -1) {
                version = new HttpVersion(0, 9);
            } else if (reqDotVersion == 0) {
                version = new HttpVersion(1, 0);
            } else if (reqDotVersion == 1) {
                version = new HttpVersion(1, 1);
            }
            // and if not any of these, trust that a Null version will
            // cause an appropriate error
            callback.handleStatusLine(
                    new BasicStatusLine(version, statusCode, "Status set by browsermob-proxy"));
            // No mechanism to look up the response text by status code,
            // so include a notification that this is a synthetic error code.
        } else {
            response = httpClient.execute(method, ctx);
            statusLine = response.getStatusLine();
            statusCode = statusLine.getStatusCode();

            if (callback != null) {
                callback.handleStatusLine(statusLine);
                callback.handleHeaders(response.getAllHeaders());
            }

            if (response.getEntity() != null) {
                is = response.getEntity().getContent();
            }

            // check for null (resp 204 can cause HttpClient to return null, which is what Google does with http://clients1.google.com/generate_204)
            if (is != null) {
                Header contentEncodingHeader = response.getFirstHeader("Content-Encoding");
                if (contentEncodingHeader != null
                        && "gzip".equalsIgnoreCase(contentEncodingHeader.getValue())) {
                    gzipping = true;
                }

                // deal with GZIP content!
                if (decompress && gzipping) {
                    is = new GZIPInputStream(is);
                }

                if (captureContent) {
                    // todo - something here?
                    os = new ClonedOutputStream(os);
                }

                bytes = copyWithStats(is, os);
            }
        }
    } catch (Exception e) {
        errorMessage = e.toString();

        if (callback != null) {
            callback.reportError(e);
        }

        // only log it if we're not shutdown (otherwise, errors that happen during a shutdown can likely be ignored)
        if (!shutdown) {
            LOG.info(String.format("%s when requesting %s", errorMessage, url));
        }
    } finally {
        // the request is done, get it out of here
        synchronized (activeRequests) {
            activeRequests.remove(activeRequest);
        }

        if (is != null) {
            try {
                is.close();
            } catch (IOException e) {
                // this is OK to ignore
            }
        }
    }

    // record the response as ended
    RequestInfo.get().finish();

    // set the start time and other timings
    entry.setStartedDateTime(RequestInfo.get().getStart());
    entry.setTimings(RequestInfo.get().getTimings());
    entry.setServerIPAddress(RequestInfo.get().getResolvedAddress());
    entry.setTime(RequestInfo.get().getTotalTime());

    // todo: where you store this in HAR?
    // obj.setErrorMessage(errorMessage);
    entry.getResponse().setBodySize(bytes);
    entry.getResponse().getContent().setSize(bytes);
    entry.getResponse().setStatus(statusCode);
    if (statusLine != null) {
        entry.getResponse().setStatusText(statusLine.getReasonPhrase());
    }

    boolean urlEncoded = false;
    if (captureHeaders || captureContent) {
        for (Header header : method.getAllHeaders()) {
            if (header.getValue() != null && header.getValue().startsWith(URLEncodedUtils.CONTENT_TYPE)) {
                urlEncoded = true;
            }

            entry.getRequest().getHeaders().add(new HarNameValuePair(header.getName(), header.getValue()));
        }

        if (response != null) {
            for (Header header : response.getAllHeaders()) {
                entry.getResponse().getHeaders().add(new HarNameValuePair(header.getName(), header.getValue()));
            }
        }
    }

    if (captureContent) {
        // can we understand the POST data at all?
        if (method instanceof HttpEntityEnclosingRequestBase && req.getCopy() != null) {
            HttpEntityEnclosingRequestBase enclosingReq = (HttpEntityEnclosingRequestBase) method;
            HttpEntity entity = enclosingReq.getEntity();

            HarPostData data = new HarPostData();
            data.setMimeType(req.getMethod().getFirstHeader("Content-Type").getValue());
            entry.getRequest().setPostData(data);

            if (urlEncoded || URLEncodedUtils.isEncoded(entity)) {
                try {
                    final String content = new String(req.getCopy().toByteArray(), "UTF-8");
                    if (content != null && content.length() > 0) {
                        List<NameValuePair> result = new ArrayList<NameValuePair>();
                        URLEncodedUtils.parse(result, new Scanner(content), null);

                        ArrayList<HarPostDataParam> params = new ArrayList<HarPostDataParam>();
                        data.setParams(params);

                        for (NameValuePair pair : result) {
                            params.add(new HarPostDataParam(pair.getName(), pair.getValue()));
                        }
                    }
                } catch (Exception e) {
                    LOG.info("Unexpected problem when parsing input copy", e);
                }
            } else {
                // not URL encoded, so let's grab the body of the POST and capture that
                data.setText(new String(req.getCopy().toByteArray()));
            }
        }
    }

    //capture request cookies
    javax.servlet.http.Cookie[] cookies = req.getProxyRequest().getCookies();
    for (javax.servlet.http.Cookie cookie : cookies) {
        HarCookie hc = new HarCookie();
        hc.setName(cookie.getName());
        hc.setValue(cookie.getValue());
        entry.getRequest().getCookies().add(hc);
    }

    String contentType = null;

    if (response != null) {
        try {
            Header contentTypeHdr = response.getFirstHeader("Content-Type");
            if (contentTypeHdr != null) {
                contentType = contentTypeHdr.getValue();
                entry.getResponse().getContent().setMimeType(contentType);

                if (captureContent && os != null && os instanceof ClonedOutputStream) {
                    ByteArrayOutputStream copy = ((ClonedOutputStream) os).getOutput();

                    if (gzipping) {
                        // ok, we need to decompress it before we can put it in the har file
                        try {
                            InputStream temp = new GZIPInputStream(
                                    new ByteArrayInputStream(copy.toByteArray()));
                            copy = new ByteArrayOutputStream();
                            IOUtils.copy(temp, copy);
                        } catch (IOException e) {
                            throw new RuntimeException(e);
                        }
                    }

                    if (hasTextualContent(contentType)) {
                        setTextOfEntry(entry, copy, contentType);
                    } else if (captureBinaryContent) {
                        setBinaryContentOfEntry(entry, copy);
                    }
                }

                NameValuePair nvp = contentTypeHdr.getElements()[0].getParameterByName("charset");

                if (nvp != null) {
                    charSet = nvp.getValue();
                }
            }

            if (os instanceof ByteArrayOutputStream) {
                responseBody = ((ByteArrayOutputStream) os).toString(charSet);

                if (verificationText != null) {
                    contentMatched = responseBody.contains(verificationText);
                }
            }
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    }

    if (contentType != null) {
        entry.getResponse().getContent().setMimeType(contentType);
    }

    // checking to see if the client is being redirected
    boolean isRedirect = false;

    String location = null;
    if (response != null && statusCode >= 300 && statusCode < 400 && statusCode != 304) {
        isRedirect = true;

        // pulling the header for the redirect
        Header locationHeader = response.getLastHeader("location");
        if (locationHeader != null) {
            location = locationHeader.getValue();
        } else if (this.followRedirects) {
            throw new RuntimeException("Invalid redirect - missing location header");
        }
    }

    //
    // Response validation - they only work if we're not following redirects
    //

    int expectedStatusCode = req.getExpectedStatusCode();

    // if we didn't mock out the actual response code and the expected code isn't what we saw, we have a problem
    if (mockResponseCode == -1 && expectedStatusCode > -1) {
        if (this.followRedirects) {
            throw new RuntimeException("Response validation cannot be used while following redirects");
        }
        if (expectedStatusCode != statusCode) {
            if (isRedirect) {
                throw new RuntimeException("Expected status code of " + expectedStatusCode + " but saw "
                        + statusCode + " redirecting to: " + location);
            } else {
                throw new RuntimeException(
                        "Expected status code of " + expectedStatusCode + " but saw " + statusCode);
            }
        }
    }

    // Location header check:
    if (isRedirect && (req.getExpectedLocation() != null)) {
        if (this.followRedirects) {
            throw new RuntimeException("Response validation cannot be used while following redirects");
        }

        if (location.compareTo(req.getExpectedLocation()) != 0) {
            throw new RuntimeException(
                    "Expected a redirect to  " + req.getExpectedLocation() + " but saw " + location);
        }
    }

    // end of validation logic

    // basic tail recursion for redirect handling
    if (isRedirect && this.followRedirects) {
        // updating location:
        try {
            URI redirectUri = new URI(location);
            URI newUri = method.getURI().resolve(redirectUri);
            method.setURI(newUri);

            return execute(req, ++depth);
        } catch (URISyntaxException e) {
            LOG.warn("Could not parse URL", e);
        }
    }

    return new BrowserMobHttpResponse(entry, method, response, contentMatched, verificationText, errorMessage,
            responseBody, contentType, charSet);
}

From source file:org.bigmouth.nvwa.network.http.HttpClientHelper.java

public static void printHttpRequestHeader(HttpRequestBase httpRequestBase) {
    if (null == httpRequestBase)
        return;/*from  w  ww  . j  a  v a 2s  .c  om*/
    org.apache.http.Header[] headers = httpRequestBase.getAllHeaders();
    if (LOGGER.isInfoEnabled())
        LOGGER.info("===============Request Headers===============");
    printHeaders(headers);
}

From source file:org.dasein.cloud.aws.storage.S3Method.java

S3Response invoke(@Nullable String bucket, @Nullable String object, @Nullable String temporaryEndpoint)
        throws S3Exception, CloudException, InternalException {
    if (wire.isDebugEnabled()) {
        wire.debug("");
        wire.debug("----------------------------------------------------------------------------------");
    }/*from www.  ja v  a 2  s.  c  o  m*/
    HttpClient client = null;
    boolean leaveOpen = false;
    try {
        StringBuilder url = new StringBuilder();
        HttpRequestBase method;
        int status;

        // Sanitise the parameters as they may have spaces and who knows what else
        if (bucket != null) {
            bucket = AWSCloud.encode(bucket, false);
        }
        if (object != null && !"?location".equalsIgnoreCase(object) && !"?acl".equalsIgnoreCase(object)
                && !"?tagging".equalsIgnoreCase(object)) {
            object = AWSCloud.encode(object, false);
        }
        if (temporaryEndpoint != null) {
            temporaryEndpoint = AWSCloud.encode(temporaryEndpoint, false);
        }
        if (provider.getEC2Provider().isAWS()) {
            url.append("https://");
            String regionId = provider.getContext().getRegionId();

            if (temporaryEndpoint == null) {
                boolean validDomainName = isValidDomainName(bucket);

                if (bucket != null && validDomainName) {
                    url.append(bucket);
                    if (regionId != null && !regionId.isEmpty() && !"us-east-1".equals(regionId)) {
                        url.append(".s3-");
                        url.append(regionId);
                        url.append(".amazonaws.com/");
                    } else {
                        url.append(".s3.amazonaws.com/");
                    }
                } else {
                    if (regionId != null && !regionId.isEmpty() && !"us-east-1".equals(regionId)) {
                        url.append("s3-");
                        url.append(regionId);
                        url.append(".amazonaws.com/");
                    } else {
                        url.append("s3.amazonaws.com/");
                    }
                }
                if (bucket != null && !validDomainName) {
                    url.append(bucket);
                    url.append("/");
                }
            } else {
                url.append(temporaryEndpoint);
                url.append("/");
            }
        } else if (provider.getEC2Provider().isStorage()
                && "google".equalsIgnoreCase(provider.getProviderName())) {
            url.append("https://");
            if (temporaryEndpoint == null) {
                if (bucket != null) {
                    url.append(bucket);
                    url.append(".");
                }
                url.append("commondatastorage.googleapis.com/");
            } else {
                url.append(temporaryEndpoint);
                url.append("/");
            }
        } else {
            int idx = 0;

            if (!provider.getContext().getEndpoint().startsWith("http")) {
                url.append("https://");
            } else {
                idx = provider.getContext().getEndpoint().indexOf("https://");
                if (idx == -1) {
                    idx = "http://".length();
                    url.append("http://");
                } else {
                    idx = "https://".length();
                    url.append("https://");
                }
            }
            String service = "";
            if (provider.getEC2Provider().isEucalyptus()) {
                service = "Walrus/";
            }

            if (temporaryEndpoint == null) {
                url.append(provider.getContext().getEndpoint().substring(idx));
                if (!provider.getContext().getEndpoint().endsWith("/")) {
                    url.append("/").append(service);
                } else {
                    url.append(service);
                }
            } else {
                url.append(temporaryEndpoint);
                url.append("/");
                url.append(service);
            }
            if (bucket != null) {
                url.append(bucket);
                url.append("/");
            }
        }
        if (object != null) {
            url.append(object);
        } else if (parameters != null) {
            boolean first = true;

            if (object != null && object.indexOf('?') != -1) {
                first = false;
            }
            for (Map.Entry<String, String> entry : parameters.entrySet()) {
                String key = entry.getKey();
                String val = entry.getValue();

                if (first) {
                    url.append("?");
                    first = false;
                } else {
                    url.append("&");
                }
                if (val != null) {
                    url.append(AWSCloud.encode(key, false));
                    url.append("=");
                    url.append(AWSCloud.encode(val, false));
                } else {
                    url.append(AWSCloud.encode(key, false));
                }
            }
        }

        if (provider.getEC2Provider().isStorage() && provider.getProviderName().equalsIgnoreCase("Google")) {
            headers.put(AWSCloud.P_GOOG_DATE, getDate());
        } else {
            headers.put(AWSCloud.P_AWS_DATE, provider.getV4HeaderDate(null));
        }
        if (contentType == null && body != null) {
            contentType = "application/xml";
            headers.put("Content-Type", contentType);
        } else if (contentType != null) {
            headers.put("Content-Type", contentType);
        }

        method = action.getMethod(url.toString());
        String host = method.getURI().getHost();
        headers.put("host", host);

        if (action.equals(S3Action.PUT_BUCKET_TAG))
            try {
                headers.put("Content-MD5", toBase64(computeMD5Hash(body)));
            } catch (NoSuchAlgorithmException e) {
                logger.error(e);
            } catch (IOException e) {
                logger.error(e);
            }

        if (headers != null) {
            for (Map.Entry<String, String> entry : headers.entrySet()) {
                method.addHeader(entry.getKey(), entry.getValue());
            }
        }

        if (body != null) {
            ((HttpEntityEnclosingRequestBase) method).setEntity(new StringEntity(body, APPLICATION_XML));
        } else if (uploadFile != null) {
            ((HttpEntityEnclosingRequestBase) method).setEntity(new FileEntity(uploadFile, contentType));
        }
        try {
            String hash = null;
            if (method instanceof HttpEntityEnclosingRequestBase) {
                try {
                    hash = provider.getRequestBodyHash(
                            EntityUtils.toString(((HttpEntityEnclosingRequestBase) method).getEntity()));
                } catch (IOException e) {
                    throw new InternalException(e);
                }
            } else {
                hash = provider.getRequestBodyHash("");
            }

            String signature;
            if (provider.getEC2Provider().isAWS()) {
                // Sign v4 for AWS
                signature = provider.getV4Authorization(new String(provider.getAccessKey()[0]),
                        new String(provider.getAccessKey()[1]), method.getMethod(), url.toString(), SERVICE_ID,
                        headers, hash);
                if (hash != null) {
                    method.addHeader(AWSCloud.P_AWS_CONTENT_SHA256, hash);
                }
            } else {
                // Eucalyptus et al use v2
                signature = provider.signS3(new String(provider.getAccessKey()[0], "utf-8"),
                        provider.getAccessKey()[1], method.getMethod(), null, contentType, headers, bucket,
                        object);
            }
            method.addHeader(AWSCloud.P_CFAUTH, signature);
        } catch (UnsupportedEncodingException e) {
            logger.error(e);
        }

        if (wire.isDebugEnabled()) {
            wire.debug("[" + url.toString() + "]");
            wire.debug(method.getRequestLine().toString());
            for (Header header : method.getAllHeaders()) {
                wire.debug(header.getName() + ": " + header.getValue());
            }
            wire.debug("");
            if (body != null) {
                try {
                    wire.debug(EntityUtils.toString(((HttpEntityEnclosingRequestBase) method).getEntity()));
                } catch (IOException ignore) {
                }

                wire.debug("");
            } else if (uploadFile != null) {
                wire.debug("-- file upload --");
                wire.debug("");
            }
        }

        attempts++;
        client = provider.getClient(body == null && uploadFile == null);

        S3Response response = new S3Response();
        HttpResponse httpResponse;

        try {
            APITrace.trace(provider, action.toString());
            httpResponse = client.execute(method);
            if (wire.isDebugEnabled()) {
                wire.debug(httpResponse.getStatusLine().toString());
                for (Header header : httpResponse.getAllHeaders()) {
                    wire.debug(header.getName() + ": " + header.getValue());
                }
                wire.debug("");
            }
            status = httpResponse.getStatusLine().getStatusCode();
        } catch (IOException e) {
            logger.error(url + ": " + e.getMessage());
            throw new InternalException(e);
        }
        response.headers = httpResponse.getAllHeaders();

        HttpEntity entity = httpResponse.getEntity();
        InputStream input = null;

        if (entity != null) {
            try {
                input = entity.getContent();
            } catch (IOException e) {
                throw new CloudException(e);
            }
        }
        try {
            if (status == HttpStatus.SC_OK || status == HttpStatus.SC_CREATED
                    || status == HttpStatus.SC_ACCEPTED) {
                Header clen = httpResponse.getFirstHeader("Content-Length");
                long len = -1L;

                if (clen != null) {
                    len = Long.parseLong(clen.getValue());
                }
                if (len != 0L) {
                    try {
                        Header ct = httpResponse.getFirstHeader("Content-Type");

                        if ((ct != null && (ct.getValue().startsWith("application/xml")
                                || ct.getValue().startsWith("text/xml")))
                                || (action.equals(S3Action.GET_BUCKET_TAG) && input != null)) {
                            try {
                                response.document = parseResponse(input);
                                return response;
                            } finally {
                                input.close();
                            }
                        } else if (ct != null && ct.getValue().startsWith("application/octet-stream")
                                && len < 1) {
                            return null;
                        } else {
                            response.contentLength = len;
                            if (ct != null) {
                                response.contentType = ct.getValue();
                            }
                            response.input = input;
                            response.method = method;
                            leaveOpen = true;
                            return response;
                        }
                    } catch (IOException e) {
                        logger.error(e);
                        throw new CloudException(e);
                    }
                } else {
                    return response;
                }
            } else if (status == HttpStatus.SC_NO_CONTENT) {
                return response;
            }
            if (status == HttpStatus.SC_FORBIDDEN) {
                throw new S3Exception(status, "", "AccessForbidden",
                        "Access was denied : " + (url != null ? url.toString() : ""));
            } else if (status == HttpStatus.SC_NOT_FOUND) {
                throw new S3Exception(status, null, null, "Object not found.");
            } else {
                if (status == HttpStatus.SC_SERVICE_UNAVAILABLE
                        || status == HttpStatus.SC_INTERNAL_SERVER_ERROR) {
                    if (attempts >= 5) {
                        String msg;

                        if (status == HttpStatus.SC_SERVICE_UNAVAILABLE) {
                            msg = "Cloud service is currently unavailable.";
                        } else {
                            msg = "The cloud service encountered a server error while processing your request.";
                        }
                        logger.error(msg);
                        throw new CloudException(msg);
                    } else {
                        leaveOpen = true;
                        if (input != null) {
                            try {
                                input.close();
                            } catch (IOException ignore) {
                            }
                        }
                        try {
                            Thread.sleep(5000L);
                        } catch (InterruptedException ignore) {
                        }
                        return invoke(bucket, object);
                    }
                }
                try {
                    Document doc;

                    try {
                        logger.warn("Received error code: " + status);
                        doc = parseResponse(input);
                    } finally {
                        if (input != null) {
                            input.close();
                        }
                    }
                    if (doc != null) {
                        String endpoint = null, code = null, message = null, requestId = null;
                        NodeList blocks = doc.getElementsByTagName("Error");

                        if (blocks.getLength() > 0) {
                            Node error = blocks.item(0);
                            NodeList attrs;

                            attrs = error.getChildNodes();
                            for (int i = 0; i < attrs.getLength(); i++) {
                                Node attr = attrs.item(i);

                                if (attr.getNodeName().equals("Code") && attr.hasChildNodes()) {
                                    code = attr.getFirstChild().getNodeValue().trim();
                                } else if (attr.getNodeName().equals("Message") && attr.hasChildNodes()) {
                                    message = attr.getFirstChild().getNodeValue().trim();
                                } else if (attr.getNodeName().equals("RequestId") && attr.hasChildNodes()) {
                                    requestId = attr.getFirstChild().getNodeValue().trim();
                                } else if (attr.getNodeName().equals("Endpoint") && attr.hasChildNodes()) {
                                    endpoint = attr.getFirstChild().getNodeValue().trim();
                                }
                            }

                        }
                        if (endpoint != null && code.equals("TemporaryRedirect")) {
                            if (temporaryEndpoint != null) {
                                throw new CloudException("Too deep redirect to " + endpoint);
                            } else {
                                return invoke(bucket, object, endpoint);
                            }
                        } else {
                            if (message == null) {
                                throw new CloudException("Unable to identify error condition: " + status + "/"
                                        + requestId + "/" + code);
                            }
                            throw new S3Exception(status, requestId, code, message);
                        }
                    } else {
                        throw new CloudException("Unable to parse error.");
                    }
                } catch (IOException e) {
                    if (status == HttpStatus.SC_FORBIDDEN) {
                        throw new S3Exception(status, "", "AccessForbidden",
                                "Access was denied without explanation.");
                    }
                    throw new CloudException(e);
                } catch (RuntimeException e) {
                    throw new CloudException(e);
                } catch (Error e) {
                    throw new CloudException(e);
                }
            }
        } finally {
            if (!leaveOpen) {
                if (input != null) {
                    try {
                        input.close();
                    } catch (IOException ignore) {
                    }
                }
            }
        }
    } finally {
        if (!leaveOpen && client != null) {
            client.getConnectionManager().shutdown();
        }
        if (wire.isDebugEnabled()) {
            wire.debug("----------------------------------------------------------------------------------");
            wire.debug("");
        }
    }
}