List of usage examples for org.apache.http.client.methods HttpRequestBase removeHeaders
public void removeHeaders(String str)
From source file:org.apache.marmotta.platform.core.util.http.HttpRequestUtil.java
/** * Set the <code>Cache-Control</code>-header for the provided request. * //from www . j ava 2 s . co m * @param request the request to modify * @param cacheControl the cache-control directive * * @see HeaderConstants#CACHE_CONTROL_NO_CACHE * @see HeaderConstants#CACHE_CONTROL_NO_STORE * @see HeaderConstants#CACHE_CONTROL_MAX_AGE * @see HeaderConstants#CACHE_CONTROL_MAX_STALE * @see HeaderConstants#CACHE_CONTROL_MUST_REVALIDATE * @see <a * href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9">RFC2616#Cache-Control</a> * */ public static void setCacheControl(HttpRequestBase request, String cacheControl) { if (cacheControl != null) { request.setHeader(HttpHeaders.CACHE_CONTROL, cacheControl); } else { request.removeHeaders(HttpHeaders.CACHE_CONTROL); } }
From source file:com.eviware.soapui.impl.rest.actions.oauth.OltuOAuth2ClientFacade.java
private void appendAccessTokenToHeader(HttpRequestBase request, OAuthBearerClientRequest oAuthClientRequest) throws OAuthSystemException { Map<String, String> oAuthHeaders = oAuthClientRequest.buildHeaderMessage().getHeaders(); request.removeHeaders(OAuth.HeaderType.AUTHORIZATION); request.addHeader(OAuth.HeaderType.AUTHORIZATION, oAuthHeaders.get(OAuth.HeaderType.AUTHORIZATION)); }
From source file:com.android.exchange.EasSyncServiceTests.java
public void testAddHeaders() { HttpRequestBase method = new HttpPost(); EasSyncService svc = new EasSyncService(); svc.mAuthString = "auth"; svc.mProtocolVersion = "12.1"; svc.mDeviceType = "android"; svc.mAccount = null;//from www . j av a 2 s . co m // With second argument false, there should be no header svc.setHeaders(method, false); Header[] headers = method.getHeaders("X-MS-PolicyKey"); assertEquals(0, headers.length); // With second argument true, there should always be a header // The value will be "0" without an account method.removeHeaders("X-MS-PolicyKey"); svc.setHeaders(method, true); headers = method.getHeaders("X-MS-PolicyKey"); assertEquals(1, headers.length); assertEquals("0", headers[0].getValue()); // With an account, but null security key, the header's value should be "0" Account account = new Account(); account.mSecuritySyncKey = null; svc.mAccount = account; method.removeHeaders("X-MS-PolicyKey"); svc.setHeaders(method, true); headers = method.getHeaders("X-MS-PolicyKey"); assertEquals(1, headers.length); assertEquals("0", headers[0].getValue()); // With an account and security key, the header's value should be the security key account.mSecuritySyncKey = "key"; svc.mAccount = account; method.removeHeaders("X-MS-PolicyKey"); svc.setHeaders(method, true); headers = method.getHeaders("X-MS-PolicyKey"); assertEquals(1, headers.length); assertEquals("key", headers[0].getValue()); }
From source file:groovyx.net.http.HTTPBuilder.java
/** * All <code>request</code> methods delegate to this method. *//*from w w w. j av a2s.com*/ protected Object doRequest(final RequestConfigDelegate delegate) throws ClientProtocolException, IOException { final HttpRequestBase reqMethod = delegate.getRequest(); Object contentType = delegate.getContentType(); if (this.autoAcceptHeader) { String acceptContentTypes = contentType.toString(); if (contentType instanceof ContentType) acceptContentTypes = ((ContentType) contentType).getAcceptHeader(); reqMethod.setHeader("Accept", acceptContentTypes); } reqMethod.setURI(delegate.getUri().toURI()); if (reqMethod.getURI() == null) throw new IllegalStateException("Request URI cannot be null"); log.debug(reqMethod.getMethod() + " " + reqMethod.getURI()); // set any request headers from the delegate Map<?, ?> headers = delegate.getHeaders(); for (Object key : headers.keySet()) { Object val = headers.get(key); if (key == null) continue; if (val == null) reqMethod.removeHeaders(key.toString()); else reqMethod.setHeader(key.toString(), val.toString()); } HttpResponseDecorator resp = new HttpResponseDecorator(client.execute(reqMethod, delegate.getContext()), delegate.getContext(), null); try { int status = resp.getStatusLine().getStatusCode(); Closure responseClosure = delegate.findResponseHandler(status); log.debug("Response code: " + status + "; found handler: " + responseClosure); Object[] closureArgs = null; switch (responseClosure.getMaximumNumberOfParameters()) { case 1: closureArgs = new Object[] { resp }; break; case 2: // parse the response entity if the response handler expects it: HttpEntity entity = resp.getEntity(); try { if (entity == null || entity.getContentLength() == 0) closureArgs = new Object[] { resp, null }; else closureArgs = new Object[] { resp, parseResponse(resp, contentType) }; } catch (Exception ex) { Header h = entity.getContentType(); String respContentType = h != null ? h.getValue() : null; log.warn("Error parsing '" + respContentType + "' response", ex); throw new ResponseParseException(resp, ex); } break; default: throw new IllegalArgumentException("Response closure must accept one or two parameters"); } Object returnVal = responseClosure.call(closureArgs); log.trace("response handler result: " + returnVal); return returnVal; } finally { HttpEntity entity = resp.getEntity(); if (entity != null) entity.consumeContent(); } }
From source file:org.apache.solr.servlet.HttpSolrCall.java
private void remoteQuery(String coreUrl, HttpServletResponse resp) throws IOException { HttpRequestBase method = null;/*from w w w . java 2 s . co m*/ HttpEntity httpEntity = null; try { String urlstr = coreUrl + queryParams.toQueryString(); boolean isPostOrPutRequest = "POST".equals(req.getMethod()) || "PUT".equals(req.getMethod()); if ("GET".equals(req.getMethod())) { method = new HttpGet(urlstr); } else if ("HEAD".equals(req.getMethod())) { method = new HttpHead(urlstr); } else if (isPostOrPutRequest) { HttpEntityEnclosingRequestBase entityRequest = "POST".equals(req.getMethod()) ? new HttpPost(urlstr) : new HttpPut(urlstr); InputStream in = new CloseShieldInputStream(req.getInputStream()); // Prevent close of container streams HttpEntity entity = new InputStreamEntity(in, req.getContentLength()); entityRequest.setEntity(entity); method = entityRequest; } else if ("DELETE".equals(req.getMethod())) { method = new HttpDelete(urlstr); } else { throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Unexpected method type: " + req.getMethod()); } for (Enumeration<String> e = req.getHeaderNames(); e.hasMoreElements();) { String headerName = e.nextElement(); if (!"host".equalsIgnoreCase(headerName) && !"authorization".equalsIgnoreCase(headerName) && !"accept".equalsIgnoreCase(headerName)) { method.addHeader(headerName, req.getHeader(headerName)); } } // These headers not supported for HttpEntityEnclosingRequests if (method instanceof HttpEntityEnclosingRequest) { method.removeHeaders(TRANSFER_ENCODING_HEADER); method.removeHeaders(CONTENT_LENGTH_HEADER); } final HttpResponse response = solrDispatchFilter.httpClient.execute(method, HttpClientUtil.createNewHttpClientRequestContext()); int httpStatus = response.getStatusLine().getStatusCode(); httpEntity = response.getEntity(); resp.setStatus(httpStatus); for (HeaderIterator responseHeaders = response.headerIterator(); responseHeaders.hasNext();) { Header header = responseHeaders.nextHeader(); // We pull out these two headers below because they can cause chunked // encoding issues with Tomcat if (header != null && !header.getName().equalsIgnoreCase(TRANSFER_ENCODING_HEADER) && !header.getName().equalsIgnoreCase(CONNECTION_HEADER)) { resp.addHeader(header.getName(), header.getValue()); } } if (httpEntity != null) { if (httpEntity.getContentEncoding() != null) resp.setCharacterEncoding(httpEntity.getContentEncoding().getValue()); if (httpEntity.getContentType() != null) resp.setContentType(httpEntity.getContentType().getValue()); InputStream is = httpEntity.getContent(); OutputStream os = resp.getOutputStream(); IOUtils.copyLarge(is, os); } } catch (IOException e) { sendError(new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Error trying to proxy request for url: " + coreUrl, e)); } finally { Utils.consumeFully(httpEntity); } }
From source file:com.tremolosecurity.proxy.filter.PostProcess.java
protected void setHeadersCookies(HttpFilterRequest req, UrlHolder holder, HttpRequestBase method, String finalURL) throws Exception { Iterator<String> names; names = req.getHeaderNames();/*from w w w.j ava2 s .c o m*/ String cookieName = null; URL url = new URL(finalURL); while (names.hasNext()) { String name = names.next(); if (name.equalsIgnoreCase("Cookie")) { cookieName = name; continue; } if (logger.isDebugEnabled()) { logger.debug("Header : " + name); } Attribute attrib = req.getHeader(name); Iterator<String> attrVals = attrib.getValues().iterator(); while (attrVals.hasNext()) { String val = attrVals.next(); if (name.equalsIgnoreCase("Content-Type")) { continue; } else if (name.equalsIgnoreCase("If-Range")) { continue; } else if (name.equalsIgnoreCase("Range")) { continue; } else if (name.equalsIgnoreCase("If-None-Match")) { continue; } if (name.equalsIgnoreCase("HOST")) { if (holder.isOverrideHost()) { if (logger.isDebugEnabled()) { logger.debug("Final URL : '" + finalURL + "'"); } val = url.getHost(); if (url.getPort() != -1) { StringBuffer b = new StringBuffer(); b.append(val).append(":").append(url.getPort()); val = b.toString(); } } } else if (name.equalsIgnoreCase("Referer")) { if (holder.isOverrideReferer()) { URL origRef = new URL(val); StringBuffer newRef = new StringBuffer(); newRef.append(url.getProtocol()).append("://").append(url.getHost()); if (url.getPort() != -1) { newRef.append(':').append(url.getPort()); } newRef.append(origRef.getPath()); if (origRef.getQuery() != null) { newRef.append('?').append(origRef.getQuery()); } if (logger.isDebugEnabled()) { logger.debug("Final Ref : '" + newRef.toString() + "'"); } val = newRef.toString(); } } if (this.addHeader(name)) { if (logger.isDebugEnabled()) { logger.debug("Header Added - '" + name + "'='" + val + "'"); } method.addHeader(new BasicHeader(attrib.getName(), val)); } } } HashMap<String, Attribute> fromResults = (HashMap<String, Attribute>) req .getAttribute(AzSys.AUTO_IDM_HTTP_HEADERS); if (fromResults != null) { names = fromResults.keySet().iterator(); while (names.hasNext()) { String name = names.next(); method.removeHeaders(name); Attribute attrib = fromResults.get(name); Iterator<String> attrVals = attrib.getValues().iterator(); while (attrVals.hasNext()) { String val = attrVals.next(); if (logger.isDebugEnabled()) { logger.debug("Header Added - '" + name + "'='" + val + "'"); } method.addHeader(new BasicHeader(name, val)); } } } String sessionCookieName = ""; if (holder.getApp().getCookieConfig() != null) { sessionCookieName = holder.getApp().getCookieConfig().getSessionCookieName(); } HashSet<String> toRemove = new HashSet<String>(); toRemove.add(sessionCookieName); toRemove.add("autoIdmSessionCookieName"); toRemove.add("autoIdmAppName"); toRemove.add("JSESSIONID"); names = req.getCookieNames().iterator(); StringBuffer cookieHeader = new StringBuffer(); boolean isFirst = true; while (names.hasNext()) { String name = names.next(); if (toRemove.contains(name)) { continue; } ArrayList<Cookie> cookies = req.getCookies(name); Iterator<Cookie> itc = cookies.iterator(); while (itc.hasNext()) { Cookie cookie = itc.next(); String cookieFinalName; if (cookie.getName().startsWith("JSESSIONID")) { String host = cookie.getName().substring(cookie.getName().indexOf('-') + 1); host = host.replaceAll("[|]", " "); if (!holder.getApp().getName().equalsIgnoreCase(host)) { continue; } cookieFinalName = "JSESSIONID"; } else { cookieFinalName = cookie.getName(); } String val = cookie.getValue(); if (logger.isDebugEnabled()) { logger.debug("Cookie Added - '" + name + "'='" + val + "'"); } cookieHeader.append(cookieFinalName).append('=').append(val).append("; "); } } if (cookieHeader.length() > 0) { if (cookieName == null) { cookieName = "Cookie"; } method.addHeader(new BasicHeader(cookieName, cookieHeader.toString())); } }
From source file:de.betterform.connector.http.AbstractHTTPConnector.java
protected void execute(HttpRequestBase httpRequestBase) throws Exception { // (new HttpClient()).executeMethod(httpMethod); //HttpClient client = new HttpClient(); HttpParams httpParams = new BasicHttpParams(); DefaultHttpClient client = ConnectorFactory.getFactory().getHttpClient(httpParams); if (!getContext().containsKey(AbstractHTTPConnector.SSL_CUSTOM_SCHEME)) { LOGGER.debug("SSL_CUSTOM_SCHEME"); LOGGER.debug("SSL_CUSTOM_SCHEME: Factory: " + Config.getInstance().getProperty(AbstractHTTPConnector.HTTPCLIENT_SSL_CONTEXT)); String contextPath = Config.getInstance().getProperty(AbstractHTTPConnector.HTTPCLIENT_SSL_CONTEXT); if (contextPath != null) { initSSLScheme(contextPath);//from w ww . ja v a 2s . com } } if (LOGGER.isDebugEnabled()) { LOGGER.debug("context params>>>"); Map map = getContext(); Iterator keys = map.keySet().iterator(); while (keys.hasNext()) { String key = keys.next().toString(); Object value = map.get(key); if (value != null) LOGGER.debug(key + "=" + value.toString()); } LOGGER.debug("<<<end params"); } String username = null; String password = null; String realm = null; //add custom header to signal XFormsFilter to not process this internal request //httpMethod.setRequestHeader(BetterFORMConstants.BETTERFORM_INTERNAL,"true"); httpRequestBase.addHeader(BetterFORMConstants.BETTERFORM_INTERNAL, "true"); /// *** copy all keys in map HTTP_REQUEST_HEADERS as http-submissionHeaders if (getContext().containsKey(HTTP_REQUEST_HEADERS)) { RequestHeaders httpRequestHeaders = (RequestHeaders) getContext().get(HTTP_REQUEST_HEADERS); // Iterator it = Map headersToAdd = new HashMap(); for (RequestHeader header : httpRequestHeaders.getAllHeaders()) { String headername = header.getName(); String headervalue = header.getValue(); if (headername.equals("username")) { username = headervalue; } else if (headername.equals("password")) { password = headervalue; } else if (headername.equals("realm")) { realm = headervalue; } else { if (headersToAdd.containsKey(headername)) { String formerValue = (String) headersToAdd.get(headername); headersToAdd.put(headername, formerValue + "," + headervalue); } else { if (headername.equals("accept-encoding")) { // do nothing LOGGER.debug("do not add accept-encoding:" + headervalue + " for request"); } else { headersToAdd.put(headername, headervalue); if (LOGGER.isDebugEnabled()) { LOGGER.debug("setting header: " + headername + " value: " + headervalue); } } } } } Iterator keyIterator = headersToAdd.keySet().iterator(); while (keyIterator.hasNext()) { String key = (String) keyIterator.next(); //httpMethod.setRequestHeader(new Header(key,(String) headersToAdd.get(key))); httpRequestBase.setHeader(key, (String) headersToAdd.get(key)); //httpRequestBase.addHeader(key, (String) headersToAdd.get(key)); } } if (httpRequestBase.containsHeader("Content-Length")) { //remove content-length if present httpclient will recalucalte the value. httpRequestBase.removeHeaders("Content-Length"); } if (username != null && password != null) { URI targetURI = null; //targetURI = httpMethod.getURI(); targetURI = httpRequestBase.getURI(); //client.getParams().setAuthenticationPreemptive(true); Credentials defaultcreds = new UsernamePasswordCredentials(username, password); if (realm == null) { realm = AuthScope.ANY_REALM; } //client.getState().setCredentials(new AuthScope(targetURI.getHost(), targetURI.getPort(), realm), defaultcreds); client.getCredentialsProvider() .setCredentials(new AuthScope(targetURI.getHost(), targetURI.getPort(), realm), defaultcreds); AuthCache authCache = new BasicAuthCache(); BasicScheme basicAuth = new BasicScheme(); authCache.put(new HttpHost(targetURI.getHost()), basicAuth); BasicHttpContext localContext = new BasicHttpContext(); localContext.setAttribute(ClientContext.AUTH_CACHE, authCache); //Needed? httpMethod.setDoAuthentication(true); } //alternative method for non-tomcat servers if (getContext().containsKey(REQUEST_COOKIE)) { //HttpState state = client.getState(); HttpParams state = client.getParams(); //state.setCookiePolicy(CookiePolicy.COMPATIBILITY); state.setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY); if (getContext().get(REQUEST_COOKIE) instanceof Cookie[]) { Cookie[] cookiesIn = (Cookie[]) getContext().get(REQUEST_COOKIE); if (cookiesIn[0] != null) { for (int i = 0; i < cookiesIn.length; i++) { Cookie cookie = cookiesIn[i]; //state.addCookie(cookie); client.getCookieStore().addCookie(cookie); } /* Cookie[] cookies = state.getCookies(); Header cookieOut = new CookieSpecBase().formatCookieHeader(cookies); httpMethod.setRequestHeader(cookieOut); client.setState(state); */ List<Cookie> cookies = client.getCookieStore().getCookies(); List<Header> cookieHeaders = new BrowserCompatSpec().formatCookies(cookies); Header[] headers = cookieHeaders.toArray(new Header[0]); for (int i = 0; i < headers.length; i++) { httpRequestBase.addHeader(headers[i]); } client.setParams(state); } } else { throw new MalformedCookieException( "Cookies must be passed as org.apache.commons.httpclient.Cookie objects."); } } if (getContext().containsKey(AbstractHTTPConnector.SSL_CUSTOM_SCHEME)) { LOGGER.debug("Using customSSL-Protocol-Handler"); Iterator<Scheme> schemes = ((Vector<Scheme>) getContext().get(AbstractHTTPConnector.SSL_CUSTOM_SCHEME)) .iterator(); while (schemes.hasNext()) { client.getConnectionManager().getSchemeRegistry().register(schemes.next()); } } if (httpRequestBase.getURI().isAbsolute()) { httpRequestBase.setHeader("host", httpRequestBase.getURI().getHost()); } HttpResponse httpResponse = client.execute(httpRequestBase); statusCode = httpResponse.getStatusLine().getStatusCode(); reasonPhrase = httpResponse.getStatusLine().getReasonPhrase(); try { if (statusCode >= 300) { // Allow 302 only if (statusCode != 302) { throw new XFormsInternalSubmitException(statusCode, reasonPhrase, EntityUtils.toString(httpResponse.getEntity()), XFormsConstants.RESOURCE_ERROR); } } this.handleHttpMethod(httpResponse); } catch (Exception e) { LOGGER.trace("AbstractHTTPConnector Exception: ", e); try { throw new XFormsInternalSubmitException(httpResponse.getStatusLine().getStatusCode(), httpResponse.getStatusLine().getReasonPhrase(), EntityUtils.toString(httpResponse.getEntity()), XFormsConstants.RESOURCE_ERROR); } catch (IOException e1) { throw new XFormsInternalSubmitException(httpResponse.getStatusLine().getStatusCode(), httpResponse.getStatusLine().getReasonPhrase(), XFormsConstants.RESOURCE_ERROR); } } }
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"); }/*from ww w. java 2s .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); }