List of usage examples for org.apache.commons.httpclient HttpMethod setFollowRedirects
public abstract void setFollowRedirects(boolean paramBoolean);
From source file:nl.nn.adapterframework.http.HttpSender.java
public String sendMessageWithTimeoutGuarded(String correlationID, String message, ParameterResolutionContext prc) throws SenderException, TimeOutException { ParameterValueList pvl = null;//from w ww . j ava 2s . c o m try { if (prc != null && paramList != null) { pvl = prc.getValues(paramList); } } catch (ParameterException e) { throw new SenderException( getLogPrefix() + "Sender [" + getName() + "] caught exception evaluating parameters", e); } URI uri; HttpMethod httpmethod; HostConfiguration hostconfiguration = new HostConfiguration(hostconfigurationBase); try { if (urlParameter != null) { String url = (String) pvl.getParameterValue(getUrlParam()).getValue(); uri = getURI(url); } else { uri = staticUri; } Map<String, String> headersParamsMap = new HashMap<String, String>(); if (headersParams != null) { StringTokenizer st = new StringTokenizer(headersParams, ","); while (st.hasMoreElements()) { headersParamsMap.put(st.nextToken(), null); } } if (!isParamsInUrl()) { httpmethod = getPostMethodWithParamsInBody(uri, message, pvl, headersParamsMap, prc); } else { httpmethod = getMethod(uri, message, pvl, headersParamsMap); if (!"POST".equals(getMethodType()) && !"PUT".equals(getMethodType()) && !"REPORT".equals(getMethodType())) { httpmethod.setFollowRedirects(isFollowRedirects()); } } int port = getPort(uri); if (socketfactory != null && "https".equals(uri.getScheme())) { Protocol authhttps = new Protocol(uri.getScheme(), socketfactory, port); hostconfiguration.setHost(uri.getHost(), port, authhttps); } else { hostconfiguration.setHost(uri.getHost(), port, uri.getScheme()); } log.info(getLogPrefix() + "configured httpclient for host [" + hostconfiguration.getHostURL() + "]"); if (credentials != null) { httpState.setCredentials(null, uri.getHost(), credentials); } } catch (URIException e) { throw new SenderException(e); } String result = null; int statusCode = -1; int count = getMaxExecuteRetries(); String msg = null; while (count-- >= 0 && statusCode == -1) { try { if (log.isDebugEnabled()) log.debug(getLogPrefix() + "executing method"); statusCode = httpclient.executeMethod(hostconfiguration, httpmethod, httpState); if (log.isDebugEnabled()) log.debug(getLogPrefix() + "executed method"); if (statusCode != HttpServletResponse.SC_OK) { StatusLine statusline = httpmethod.getStatusLine(); if (statusline != null) { log.warn(getLogPrefix() + "status [" + statusline.toString() + "]"); } else { log.warn(getLogPrefix() + "no statusline found"); } } else { if (log.isDebugEnabled()) log.debug(getLogPrefix() + "status [" + statusCode + "]"); } HttpServletResponse response = null; if (isStreamResultToServlet()) { response = (HttpServletResponse) prc.getSession().get("restListenerServletResponse"); } String fileName = null; if (StringUtils.isNotEmpty(getStreamResultToFileNameSessionKey())) { fileName = (String) prc.getSession().get(getStreamResultToFileNameSessionKey()); } result = extractResult(httpmethod, prc, response, fileName); if (log.isDebugEnabled()) log.debug(getLogPrefix() + "retrieved result [" + result + "]"); } catch (HttpException e) { Throwable throwable = e.getCause(); String cause = null; if (throwable != null) { cause = throwable.toString(); } msg = e.getMessage(); log.warn(getLogPrefix() + "httpException with message [" + msg + "] and cause [" + cause + "], executeRetries left [" + count + "]"); } catch (IOException e) { httpmethod.abort(); if (e instanceof SocketTimeoutException) { throw new TimeOutException(e); } throw new SenderException(e); } finally { // In case of storeResultAsStreamInSessionKey release connection // is done by ReleaseConnectionAfterReadInputStream. if (StringUtils.isEmpty(getStoreResultAsStreamInSessionKey())) { httpmethod.releaseConnection(); } } } if (statusCode == -1) { if (StringUtils.contains(msg.toUpperCase(), "TIMEOUTEXCEPTION")) { //java.net.SocketTimeoutException: Read timed out throw new TimeOutException("Failed to recover from timeout exception"); } throw new SenderException("Failed to recover from exception"); } if (isXhtml() && StringUtils.isNotEmpty(result)) { result = XmlUtils.skipDocTypeDeclaration(result.trim()); if (result.startsWith("<html>") || result.startsWith("<html ")) { CleanerProperties props = new CleanerProperties(); HtmlCleaner cleaner = new HtmlCleaner(props); TagNode tagNode = cleaner.clean(result); result = new SimpleXmlSerializer(props).getXmlAsString(tagNode); if (transformerPool != null) { log.debug(getLogPrefix() + " transforming result [" + result + "]"); ParameterResolutionContext prc_xslt = new ParameterResolutionContext(result, null, true, true); try { result = transformerPool.transform(prc_xslt.getInputSource(), null); } catch (Exception e) { throw new SenderException("Exception on transforming input", e); } } } } return result; }
From source file:org.abstracthorizon.proximity.storage.remote.CommonsHttpClientRemotePeer.java
/** * Execute method.//from w ww . j av a 2s . co m * * @param method the method * * @return the int */ protected int executeMethod(HttpMethod method) { if (getUserAgentString() != null) { method.setRequestHeader(new Header("user-agent", getUserAgentString())); } method.setRequestHeader(new Header("accept", "*/*")); method.setRequestHeader(new Header("accept-language", "en-us")); method.setRequestHeader(new Header("accept-encoding", "gzip, identity")); method.setRequestHeader(new Header("connection", "Keep-Alive")); method.setRequestHeader(new Header("cache-control", "no-cache")); // TODO: fix for #93 // method.setFollowRedirects(isFollowRedirection()); method.setFollowRedirects(true); method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, httpRetryHandler); method.setQueryString(getQueryString()); int resultCode = 0; try { resultCode = getHttpClient().executeMethod(httpConfiguration, method); } catch (HttpException ex) { logger.error("Protocol error while executing " + method.getName() + " method", ex); } catch (IOException ex) { logger.error("Tranport error while executing " + method.getName() + " method", ex); } return resultCode; }
From source file:org.agnitas.dao.impl.VersionControlDaoImpl.java
private String fetchServerVersion(String currentVersion, String referrer) { HttpClient client = new HttpClient(); client.setConnectionTimeout(CONNECTION_TIMEOUT); HttpMethod method = new GetMethod(URL); method.setRequestHeader("referer", referrer); NameValuePair[] queryParams = new NameValuePair[1]; queryParams[0] = new NameValuePair(VERSION_KEY, currentVersion); method.setQueryString(queryParams);// ww w . ja va 2 s .c o m method.setFollowRedirects(true); String responseBody = null; try { client.executeMethod(method); responseBody = method.getResponseBodyAsString(); } catch (Exception he) { logger.error("HTTP error connecting to '" + URL + "'", he); } //clean up the connection resources method.releaseConnection(); return responseBody; }
From source file:org.alfresco.custom.rest.httpconnector.RemoteClientImpl.java
private org.apache.commons.httpclient.HttpMethod createRequestMethod(HttpMethod requestMethod, URI redirectURL) {/* w w w . ja va 2 s. c o m*/ org.apache.commons.httpclient.HttpMethod method = null; switch (requestMethod) { default: case GET: method = new GetMethod(redirectURL.toString()); break; case PUT: method = new PutMethod(redirectURL.toString()); break; case POST: method = new PostMethod(redirectURL.toString()); break; case DELETE: method = new DeleteMethod(redirectURL.toString()); break; case HEAD: method = new HeadMethod(redirectURL.toString()); break; } // Switch off automatic redirect handling as we want to process them // ourselves and maintain cookies method.setFollowRedirects(false); return method; }
From source file:org.alfresco.httpclient.AbstractHttpClient.java
protected HttpMethod createMethod(Request req) throws IOException { StringBuilder url = new StringBuilder(128); url.append(baseUrl);/* w w w.j a v a2 s .c o m*/ url.append("/service/"); url.append(req.getFullUri()); // construct method HttpMethod httpMethod = null; String method = req.getMethod(); if (method.equalsIgnoreCase("GET")) { GetMethod get = new GetMethod(url.toString()); httpMethod = get; httpMethod.setFollowRedirects(true); } else if (method.equalsIgnoreCase("POST")) { PostMethod post = new PostMethod(url.toString()); httpMethod = post; ByteArrayRequestEntity requestEntity = new ByteArrayRequestEntity(req.getBody(), req.getType()); if (req.getBody().length > DEFAULT_SAVEPOST_BUFFER) { post.getParams().setBooleanParameter(HttpMethodParams.USE_EXPECT_CONTINUE, true); } post.setRequestEntity(requestEntity); // Note: not able to automatically follow redirects for POST, this is handled by sendRemoteRequest } else if (method.equalsIgnoreCase("HEAD")) { HeadMethod head = new HeadMethod(url.toString()); httpMethod = head; httpMethod.setFollowRedirects(true); } else { throw new AlfrescoRuntimeException("Http Method " + method + " not supported"); } if (req.getHeaders() != null) { for (Map.Entry<String, String> header : req.getHeaders().entrySet()) { httpMethod.setRequestHeader(header.getKey(), header.getValue()); } } return httpMethod; }
From source file:org.alfresco.jive.impl.JiveOpenClientImpl.java
/** * Sets default values for a bunch of standard request headers in the given <code>HttpMethod</code>. * Also sets the custom X-AlfrescoJive-UserId header to the encrypted user id. * /* w w w. j a v a2 s.c o m*/ * @param userId The userId to encrypt and add to the header <i>(must not be null, empty or blank)</i>. * @param method The method to add the headers to <i>(may be null, in which case this method won't do anything)</i>. */ private void setCommonHeaders(final String userId, final HttpMethod method) { if (userId == null || userId.trim().length() == 0) { throw new AuthenticationException(); } if (method != null) { final String encryptedUserId = encrypter.encrypt(userId); method.setDoAuthentication(true); method.setFollowRedirects(false); method.setRequestHeader("Accept", MIME_TYPE_JSON); method.setRequestHeader("Accept-Charset", CHARSET_UTF8); // method.setRequestHeader("Accept-Encoding", "gzip"); // Would dearly love to support compressed responses, but difficult to support in HttpClient 3.1 (used in Alfresco 3.4.x) method.setRequestHeader("Connection", "Keep-Alive"); method.setRequestHeader("Keep-Alive", "300"); method.setRequestHeader("User-Agent", USER_AGENT); method.setRequestHeader(HEADER_NAME_USER_ID, encryptedUserId); } }
From source file:org.apache.abdera.protocol.client.util.MethodHelper.java
public static HttpMethod createMethod(String method, String uri, RequestEntity entity, RequestOptions options) { if (method == null) return null; Method m = Method.fromString(method); Method actual = null;//from w w w. ja v a2 s . c om HttpMethod httpMethod = null; if (options.isUsePostOverride()) { if (m.equals(Method.PUT)) { actual = m; } else if (m.equals(Method.DELETE)) { actual = m; } if (actual != null) m = Method.POST; } switch (m) { case GET: httpMethod = new GetMethod(uri); break; case POST: httpMethod = getMethod(new PostMethod(uri), entity); break; case PUT: httpMethod = getMethod(new PutMethod(uri), entity); break; case DELETE: httpMethod = new DeleteMethod(uri); break; case HEAD: httpMethod = new HeadMethod(uri); break; case OPTIONS: httpMethod = new OptionsMethod(uri); break; case TRACE: httpMethod = new TraceMethod(uri); break; default: httpMethod = getMethod(new ExtensionMethod(method, uri), entity); } if (actual != null) { httpMethod.addRequestHeader("X-HTTP-Method-Override", actual.name()); } initHeaders(options, httpMethod); // by default use expect-continue is enabled on the client // only disable if explicitly disabled if (!options.isUseExpectContinue()) httpMethod.getParams().setBooleanParameter(HttpMethodParams.USE_EXPECT_CONTINUE, false); // should we follow redirects, default is true if (!(httpMethod instanceof EntityEnclosingMethod)) httpMethod.setFollowRedirects(options.isFollowRedirects()); return httpMethod; }
From source file:org.apache.cocoon.generation.WebServiceProxyGenerator.java
/** * Forwards the request and returns the response. * /*from ww w. jav a 2 s.com*/ * The rest is probably out of date: * Will use a UrlGetMethod to benefit the cacheing mechanism * and intermediate proxy servers. * It is potentially possible that the size of the request * may grow beyond a certain limit for GET and it will require POST instead. * * @return byte[] XML response */ public byte[] fetch() throws ProcessingException { HttpMethod method = null; // check which method (GET or POST) to use. if (this.configuredHttpMethod.equalsIgnoreCase(METHOD_POST)) { method = new PostMethod(this.source); } else { method = new GetMethod(this.source); } if (this.getLogger().isDebugEnabled()) { this.getLogger().debug("request HTTP method: " + method.getName()); } // this should probably be exposed as a sitemap option method.setFollowRedirects(true); // copy request parameters and merge with URL parameters Request request = ObjectModelHelper.getRequest(objectModel); ArrayList paramList = new ArrayList(); Enumeration enumeration = request.getParameterNames(); while (enumeration.hasMoreElements()) { String pname = (String) enumeration.nextElement(); String[] paramsForName = request.getParameterValues(pname); for (int i = 0; i < paramsForName.length; i++) { NameValuePair pair = new NameValuePair(pname, paramsForName[i]); paramList.add(pair); } } if (paramList.size() > 0) { NameValuePair[] allSubmitParams = new NameValuePair[paramList.size()]; paramList.toArray(allSubmitParams); String urlQryString = method.getQueryString(); // use HttpClient encoding routines method.setQueryString(allSubmitParams); String submitQryString = method.getQueryString(); // set final web service query string // sometimes the querystring is null here... if (null == urlQryString) { method.setQueryString(submitQryString); } else { method.setQueryString(urlQryString + "&" + submitQryString); } } // if there are submit parameters byte[] response = null; try { int httpStatus = httpClient.executeMethod(method); if (httpStatus < 400) { if (this.getLogger().isDebugEnabled()) { this.getLogger().debug("Return code when accessing the remote Url: " + httpStatus); } } else { throw new ProcessingException("The remote returned error " + httpStatus + " when attempting to access remote URL:" + method.getURI()); } } catch (URIException e) { throw new ProcessingException("There is a problem with the URI: " + this.source, e); } catch (IOException e) { try { throw new ProcessingException( "Exception when attempting to access the remote URL: " + method.getURI(), e); } catch (URIException ue) { throw new ProcessingException("There is a problem with the URI: " + this.source, ue); } } finally { /* It is important to always read the entire response and release the * connection regardless of whether the server returned an error or not. * {@link http://jakarta.apache.org/commons/httpclient/tutorial.html} */ response = method.getResponseBody(); method.releaseConnection(); } return response; }
From source file:org.apache.maven.doxia.linkcheck.validation.OnlineHTTPLinkValidator.java
/** * Checks the given link.//www. j a v a 2 s . co m * * @param link the link to check. * @param nbRedirect the number of current redirects. * @return HttpMethod * @throws IOException if something goes wrong. */ private HttpMethod checkLink(String link, int nbRedirect) throws IOException { int max = MAX_NB_REDIRECT; if (this.http.getHttpClientParameters() != null && this.http.getHttpClientParameters().get(HttpClientParams.MAX_REDIRECTS) != null) { try { max = Integer .valueOf(this.http.getHttpClientParameters().get(HttpClientParams.MAX_REDIRECTS).toString()) .intValue(); } catch (NumberFormatException e) { if (LOG.isWarnEnabled()) { LOG.warn("HttpClient parameter '" + HttpClientParams.MAX_REDIRECTS + "' is not a number. Ignoring!"); } } } if (nbRedirect > max) { throw new HttpException("Maximum number of redirections (" + max + ") exceeded"); } HttpMethod hm; if (HEAD_METHOD.equalsIgnoreCase(this.http.getMethod())) { hm = new HeadMethod(link); } else if (GET_METHOD.equalsIgnoreCase(this.http.getMethod())) { hm = new GetMethod(link); } else { if (LOG.isErrorEnabled()) { LOG.error("Unsupported method: " + this.http.getMethod() + ", using 'get'."); } hm = new GetMethod(link); } // Default hm.setFollowRedirects(this.http.isFollowRedirects()); try { URL url = new URL(link); cl.getHostConfiguration().setHost(url.getHost(), url.getPort(), url.getProtocol()); cl.executeMethod(hm); StatusLine sl = hm.getStatusLine(); if (sl == null) { if (LOG.isErrorEnabled()) { LOG.error("Unknown error validating link : " + link); } return null; } if (hm.getStatusCode() == HttpStatus.SC_MOVED_PERMANENTLY || hm.getStatusCode() == HttpStatus.SC_MOVED_TEMPORARILY || hm.getStatusCode() == HttpStatus.SC_TEMPORARY_REDIRECT) { Header locationHeader = hm.getResponseHeader("location"); if (locationHeader == null) { LOG.error("Site sent redirect, but did not set Location header"); return hm; } String newLink = locationHeader.getValue(); // Be careful to absolute/relative links if (!newLink.startsWith("http://") && !newLink.startsWith("https://")) { if (newLink.startsWith("/")) { URL oldUrl = new URL(link); newLink = oldUrl.getProtocol() + "://" + oldUrl.getHost() + (oldUrl.getPort() > 0 ? ":" + oldUrl.getPort() : "") + newLink; } else { newLink = link + newLink; } } HttpMethod oldHm = hm; if (LOG.isDebugEnabled()) { LOG.debug("[" + link + "] is redirected to [" + newLink + "]"); } oldHm.releaseConnection(); hm = checkLink(newLink, nbRedirect + 1); // Restore the hm to "Moved permanently" | "Moved temporarily" | "Temporary redirect" // if the new location is found to allow us to report it if (hm.getStatusCode() == HttpStatus.SC_OK && nbRedirect == 0) { return oldHm; } } } finally { hm.releaseConnection(); } return hm; }
From source file:org.apache.ode.axis2.httpbinding.HttpMethodConverter.java
/** * create and initialize the http method. * Http Headers that may been passed in the params are not set in this method. * Headers will be automatically set by HttpClient. * See usages of HostParams.DEFAULT_HEADERS * See org.apache.commons.httpclient.HttpMethodDirector#executeMethod(org.apache.commons.httpclient.HttpMethod) *//*from w w w. ja v a2 s. co m*/ protected HttpMethod prepareHttpMethod(BindingOperation opBinding, String verb, Map<String, Element> partValues, Map<String, Node> headers, final String rootUri, HttpParams params) throws UnsupportedEncodingException { if (log.isDebugEnabled()) log.debug("Preparing http request..."); // convenience variables... BindingInput bindingInput = opBinding.getBindingInput(); HTTPOperation httpOperation = (HTTPOperation) WsdlUtils.getOperationExtension(opBinding); MIMEContent content = WsdlUtils.getMimeContent(bindingInput.getExtensibilityElements()); String contentType = content == null ? null : content.getType(); boolean useUrlEncoded = WsdlUtils.useUrlEncoded(bindingInput) || PostMethod.FORM_URL_ENCODED_CONTENT_TYPE.equalsIgnoreCase(contentType); boolean useUrlReplacement = WsdlUtils.useUrlReplacement(bindingInput); // the http method to be built and returned HttpMethod method = null; // the 4 elements the http method may be made of String relativeUri = httpOperation.getLocationURI(); String queryPath = null; RequestEntity requestEntity; String encodedParams = null; // ODE supports uri template in both port and operation location. // so assemble the final url *before* replacement String completeUri = rootUri; if (StringUtils.isNotEmpty(relativeUri)) { completeUri = completeUri + (completeUri.endsWith("/") || relativeUri.startsWith("/") ? "" : "/") + relativeUri; } if (useUrlReplacement) { // insert part values in the url completeUri = new UrlReplacementTransformer().transform(completeUri, partValues); } else if (useUrlEncoded) { // encode part values encodedParams = new URLEncodedTransformer().transform(partValues); } // http-client api is not really neat // something similar to the following would save some if/else manipulations. // But we have to deal with it as-is. // // method = new Method(verb); // method.setRequestEnity(..) // etc... if ("GET".equalsIgnoreCase(verb) || "DELETE".equalsIgnoreCase(verb)) { if ("GET".equalsIgnoreCase(verb)) { method = new GetMethod(); } else if ("DELETE".equalsIgnoreCase(verb)) { method = new DeleteMethod(); } method.getParams().setDefaults(params); if (useUrlEncoded) { queryPath = encodedParams; } // Let http-client manage the redirection // see org.apache.commons.httpclient.params.HttpClientParams.MAX_REDIRECTS // default is 100 method.setFollowRedirects(true); } else if ("POST".equalsIgnoreCase(verb) || "PUT".equalsIgnoreCase(verb)) { if ("POST".equalsIgnoreCase(verb)) { method = new PostMethod(); } else if ("PUT".equalsIgnoreCase(verb)) { method = new PutMethod(); } method.getParams().setDefaults(params); // some body-building... final String contentCharset = method.getParams().getContentCharset(); if (log.isDebugEnabled()) log.debug("Content-Type [" + contentType + "] Charset [" + contentCharset + "]"); if (useUrlEncoded) { requestEntity = new StringRequestEntity(encodedParams, PostMethod.FORM_URL_ENCODED_CONTENT_TYPE, contentCharset); } else { // get the part to be put in the body Part part = opBinding.getOperation().getInput().getMessage().getPart(content.getPart()); Element partValue = partValues.get(part.getName()); if (part.getElementName() == null) { String errMsg = "XML Types are not supported. Parts must use elements."; if (log.isErrorEnabled()) log.error(errMsg); throw new RuntimeException(errMsg); } else if (HttpUtils.isXml(contentType)) { if (log.isDebugEnabled()) log.debug("Content-Type [" + contentType + "] equivalent to 'text/xml'"); // stringify the first element String xmlString = DOMUtils.domToString(DOMUtils.getFirstChildElement(partValue)); requestEntity = new StringRequestEntity(xmlString, contentType, contentCharset); } else { if (log.isDebugEnabled()) log.debug("Content-Type [" + contentType + "] NOT equivalent to 'text/xml'. The text content of part value will be sent as text"); // encoding conversion is managed by StringRequestEntity if necessary requestEntity = new StringRequestEntity(DOMUtils.getTextContent(partValue), contentType, contentCharset); } } // cast safely, PUT and POST are subclasses of EntityEnclosingMethod final EntityEnclosingMethod enclosingMethod = (EntityEnclosingMethod) method; enclosingMethod.setRequestEntity(requestEntity); enclosingMethod .setContentChunked(params.getBooleanParameter(Properties.PROP_HTTP_REQUEST_CHUNK, false)); } else { // should not happen because of HttpBindingValidator, but never say never throw new IllegalArgumentException("Unsupported HTTP method: " + verb); } method.setPath(completeUri); // assumes that the path is properly encoded (URL safe). method.setQueryString(queryPath); // set headers setHttpRequestHeaders(method, opBinding, partValues, headers, params); return method; }