List of usage examples for org.apache.commons.httpclient HttpMethod setFollowRedirects
public abstract void setFollowRedirects(boolean paramBoolean);
From source file:com.zenkey.net.prowser.Tab.java
/************************************************************************** * Configures the HttpMethod object before use. * //from ww w . j ava 2 s.com * @param httpMethod * The HTTP method object. * @param request * The request object. * @param ioTimeout * The timeout value to use for I/O operations (like making * connections and socket reads). This value should be 1 second * longer than the actual timeout used for the request, in order to * allow the request to timeout cleanly without having an I/O * exception get in the way. * @param httpMethodRetryHandler * The retry handler object. * @throws ProtocolException * If the HTTP version in the request object is invalid. */ private static void prepareHttpMethod(HttpMethod httpMethod, Request request, HttpMethodRetryHandler httpMethodRetryHandler) throws ProtocolException { httpMethod.setFollowRedirects(false); httpMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, httpMethodRetryHandler); String httpVersion = null; if ((httpVersion = request.getHttpVersion()) != null) httpMethod.getParams().setVersion(HttpVersion.parse("HTTP/" + httpVersion)); String userAgent = null; if ((userAgent = request.getUserAgent()) != null) httpMethod.getParams().setParameter(HttpMethodParams.USER_AGENT, userAgent); }
From source file:com.fluidops.iwb.HTMLProvider.HTMLProvider.java
/** * Due to some complications with the new CKAN RDF integration, this method * will add triples generated from the JSON representation that are missing * in the RDF. Hopefully just a temporary solution - maybe the RDF will be * updated./*from ww w . j av a2 s . c o m*/ * * @throws RepositoryException */ private List<Statement> jsonFallBack(String host, HttpClient client, URI subject) throws RepositoryException { logger.debug("Executing JSON fallback for: " + host); HttpMethod method = new GetMethod(host); method.setFollowRedirects(true); List<Statement> res = new LinkedList<Statement>(); try { int status = client.executeMethod(method); if (status == HttpStatus.OK_200) { InputStream response = method.getResponseBodyAsStream(); String content = GenUtil.readUrl(response); JSONObject ob = (JSONObject) getJson(content); // Resources (Distributions) JSONArray resources = ob.getJSONArray("resources"); for (int i = 0; i < resources.length(); i++) { JSONObject resource = (JSONObject) resources.get(i); // generate a unique timestamp long timestamp = System.currentTimeMillis() + i; // HINT: // the method ProviderUtils.objectAsUri() is a safe // replacement for // the ValueFactory.createUri(). It may, however, return // null. At // this position we're null safe, as subject is a valid URI // and // the timestamp does not break the URI URI distributionUri = ProviderUtils.objectAsUri(subject.toString() + "/" + timestamp); // HINT: // again, ProviderUtils.createStatement() is used to // generate statements // when all the three components are known res.add(ProviderUtils.createStatement(subject, Vocabulary.DCAT.HAS_DISTRIBUTION, distributionUri)); res.add(ProviderUtils.createStatement(distributionUri, RDF.TYPE, Vocabulary.DCAT.DISTRIBUTION)); String accessURL = resource.getString("url"); String format = resource.getString("format"); // HINT: // the method ProviderUtils.createUriStatement() can be used // to create // statements with a URI in object position whenever // subject+predicate // (or only the predicate) are known if (!StringUtil.isNullOrEmpty(accessURL)) res.add(ProviderUtils.createUriStatement(distributionUri, Vocabulary.DCAT.ACCESSURL, accessURL)); // HINT: // the method ProviderUtils.createLiteralStatement() can be // used to create // statements with literal in object position whenever // subject+predicate // (or only the predicate) are known if (!StringUtil.isNullOrEmpty(format)) res.add(ProviderUtils.createLiteralStatement(distributionUri, Vocabulary.DCTERMS.FORMAT, format)); } // tags JSONArray tags = ob.getJSONArray("tags"); for (int i = 0; i < tags.length(); i++) { String tag = tags.getString(i); if (!StringUtil.isNullOrEmpty(tag)) res.add(ProviderUtils.createLiteralStatement(subject, Vocabulary.DCAT.KEYWORD, tag)); // HINT: // below, we use ProviderUtils.objectToURIInNamespace to // create a // URI in some target namespace; this method works for any // object based on // its toString() representation; it is null safe, assuming // the object's // string representation is neither empty nor null if (!(tag.startsWith("lod") || tag.contains("-") || tag.startsWith("rdf"))) res.add(ProviderUtils.createStatement(subject, Vocabulary.DCAT.THEME, ProviderUtils.objectToURIInNamespace(Vocabulary.DCAT.NAMESPACE, tag))); } response.close(); } else { logger.warn("Bad response from server, JSON fallback failed (status " + status + ", Url: " + host + ")"); } } catch (Exception e) { logger.warn(e.getMessage()); res.clear(); // do not return partial result // ignore warning (affects only a single dataset) } finally { method.releaseConnection(); } return res; }
From source file:edu.indiana.d2i.htrc.portal.HTRCPersistenceAPIClient.java
/** * Get Volum details from solr meta data instance. Here it adds '\' character before the ':'. * @param volId// w ww .j a va 2s . co m * @return VolumeDetailsBean * @throws IOException */ public VolumeDetailsBean getVolumeDetails(String volId) throws IOException { String volumeId; if (volId.contains(":")) { volumeId = volId.substring(0, volId.indexOf(":")) + "\\" + volId.substring(volId.indexOf(":")); } else { volumeId = volId; } String volumeDetailsQueryUrl = PlayConfWrapper.solrMetaQueryUrl() + "id:" + URLEncoder.encode(volumeId, "UTF-8") + "&fl=title,author,htrc_genderMale,htrc_genderFemale,htrc_genderUnknown,htrc_pageCount,htrc_wordCount"; VolumeDetailsBean volDetails = new VolumeDetailsBean(); if (log.isDebugEnabled()) { log.debug(volumeDetailsQueryUrl); } HttpClient httpClient = new HttpClient(); HttpMethod method = new GetMethod(volumeDetailsQueryUrl); method.setFollowRedirects(true); try { httpClient.executeMethod(method); volDetails.setVolumeId(volId); if (method.getStatusCode() == 200 && !method.getResponseBodyAsString().contains("<warn>RESPONSE CODE: 400</warn>")) { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder documentBuilder = dbf.newDocumentBuilder(); Document dom = documentBuilder.parse(method.getResponseBodyAsStream()); NodeList result = dom.getElementsByTagName("result"); NodeList arrays = ((org.w3c.dom.Element) result.item(0)).getElementsByTagName("arr"); NodeList integers = ((org.w3c.dom.Element) result.item(0)).getElementsByTagName("int"); NodeList longIntegers = ((org.w3c.dom.Element) result.item(0)).getElementsByTagName("long"); for (int i = 0; i < arrays.getLength(); i++) { org.w3c.dom.Element arr = (org.w3c.dom.Element) arrays.item(i); if (arr.hasAttribute("name") && arr.getAttribute("name").equals("title")) { NodeList strElements = arr.getElementsByTagName("str"); volDetails.setTitle((strElements.item(0)).getTextContent()); } else if (arr.hasAttribute("name") && arr.getAttribute("name").equals("htrc_genderMale")) { NodeList strElements = arr.getElementsByTagName("str"); String maleAuthor = ""; for (int j = 0; j < strElements.getLength(); j++) { org.w3c.dom.Element str = (org.w3c.dom.Element) strElements.item(j); if (j != strElements.getLength() - 1) { maleAuthor += str.getTextContent(); } else { maleAuthor += str.getTextContent(); } } volDetails.setMaleAuthor(maleAuthor); } else if (arr.hasAttribute("name") && arr.getAttribute("name").equals("htrc_genderFemale")) { NodeList strElements = arr.getElementsByTagName("str"); String femaleAuthor = ""; for (int j = 0; j < strElements.getLength(); j++) { org.w3c.dom.Element str = (org.w3c.dom.Element) strElements.item(j); if (j != strElements.getLength() - 1) { femaleAuthor += str.getTextContent(); } else { femaleAuthor += str.getTextContent(); } } volDetails.setFemaleAuthor(femaleAuthor); } else if (arr.hasAttribute("name") && arr.getAttribute("name").equals("htrc_genderUnknown")) { NodeList strElements = arr.getElementsByTagName("str"); String genderUnknownAuthor = ""; for (int j = 0; j < strElements.getLength(); j++) { org.w3c.dom.Element str = (org.w3c.dom.Element) strElements.item(j); if (j != strElements.getLength() - 1) { genderUnknownAuthor += str.getTextContent(); } else { genderUnknownAuthor += str.getTextContent(); } } volDetails.setGenderUnkownAuthor(genderUnknownAuthor); } } for (int i = 0; i < integers.getLength(); i++) { org.w3c.dom.Element integer = (org.w3c.dom.Element) integers.item(i); if (integer.hasAttribute("name") && integer.getAttribute("name").equals("htrc_pageCount")) { String pageCount = integer.getTextContent(); volDetails.setPageCount(pageCount); } } for (int i = 0; i < longIntegers.getLength(); i++) { org.w3c.dom.Element longInteger = (org.w3c.dom.Element) longIntegers.item(0); if (longInteger.hasAttribute("name") && longInteger.getAttribute("name").equals("htrc_wordCount")) { String wordCount = longInteger.getTextContent(); volDetails.setWordCount(wordCount); } } } else { volDetails.setTitle("Cannot retrieve volume details."); log.warn("Cannot retrieve details for volume id: " + volId + " Response body: \n" + method.getResponseBodyAsString()); } } catch (SAXParseException e) { log.error("Error while parsing volume details for volume: " + volId + " query url: " + volumeDetailsQueryUrl + " status code: " + method.getStatusCode(), e); volDetails.setTitle("Cannot parse volume details."); } catch (ParserConfigurationException e) { log.error("Unrecoverable error while parsing volume details.", e); log.error("Error while parsing volume details for volume: " + volId + " query url: " + volumeDetailsQueryUrl + " status code: " + method.getStatusCode(), e); throw new RuntimeException("Unrecoverable error while parsing volume details.", e); } catch (SAXException e) { log.error("Error while parsing volume details for volume: " + volId + " query url: " + volumeDetailsQueryUrl + " status code: " + method.getStatusCode(), e); volDetails.setTitle("Cannot parse volume details."); } return volDetails; }
From source file:arena.httpclient.commons.JakartaCommonsHttpClient.java
public HttpResponse get() throws IOException { NameValuePair params[] = new NameValuePair[0]; if (this.requestParameters != null) { List<NameValuePair> paramList = new ArrayList<NameValuePair>(); for (String key : this.requestParameters.keySet()) { String[] multipleValues = this.requestParameters.get(key); for (String value : multipleValues) { paramList.add(new NameValuePair(key, value)); }/*from w w w . j a va 2s. c om*/ } params = paramList.toArray(new NameValuePair[paramList.size()]); } HttpMethod method = null; if (this.isPost) { method = new PostMethod(this.url); if (this.encoding != null) { method.getParams().setContentCharset(this.encoding); } ((PostMethod) method).setRequestBody(params); } else { String url = this.url; for (int n = 0; n < params.length; n++) { url = URLUtils.addParamToURL(url, params[n].getName(), params[n].getValue(), this.encoding, false); } method = new GetMethod(url); method.setFollowRedirects(true); if (this.encoding != null) { method.getParams().setContentCharset(this.encoding); } } HttpClient client = new HttpClient(this.manager); if (this.connectTimeout != null) { client.getHttpConnectionManager().getParams().setConnectionTimeout(this.connectTimeout.intValue()); } if (this.readTimeout != null) { client.getHttpConnectionManager().getParams().setSoTimeout(this.readTimeout.intValue()); } client.setState(this.state); try { int statusCode = client.executeMethod(method); return new HttpResponse(getResponseBytes(method), statusCode, buildHeaderMap(method.getResponseHeaders())); } finally { if (method != null) { method.releaseConnection(); } } }
From source file:com.wordpress.metaphorm.authProxy.httpClient.impl.OAuthProxyConnectionApacheHttpCommonsClientImpl.java
/** * Executes the {@link HttpMethod} passed in and sends the proxy response * back to the client via the given {@link HttpServletResponse} * @param httpMethodProxyRequest An object representing the proxy request to be made * @param httpServletResponse An object by which we can send the proxied * response back to the client * @throws IOException Can be thrown by the {@link HttpClient}.executeMethod * @throws ServletException Can be thrown to indicate that another error has occurred *///w w w . j a va 2s . co m private void executeProxyRequest(HttpMethod httpMethodProxyRequest) throws IOException, RedirectRequiredException { //Utils.traceRequest(httpServletRequest); // Create a default HttpClient HttpClient httpClient = new HttpClient(); httpMethodProxyRequest.setFollowRedirects(false); _log.debug("Sending request to " + httpMethodProxyRequest.getURI()); for (Header header : httpMethodProxyRequest.getRequestHeaders()) { _log.debug(" Header \"" + header.getName() + "\" = \"" + header.getValue() + "\""); } // Execute the request int intProxyResponseCode = httpClient.executeMethod(httpMethodProxyRequest); // Persist the respose headers this.responseHeaderMap = new HashMap<String, List<String>>(); for (Header header : this.httpMethod.getResponseHeaders()) { responseHeaderMap.put(header.getName(), Arrays.asList(header.getValue())); } // Check if the proxy response is a redirect // The following code is adapted from org.tigris.noodle.filters.CheckForRedirect // Hooray for open source software if (intProxyResponseCode >= HttpServletResponse.SC_MULTIPLE_CHOICES /* 300 */ && intProxyResponseCode < HttpServletResponse.SC_NOT_MODIFIED /* 304 */) { String stringStatusCode = Integer.toString(intProxyResponseCode); String stringLocation = httpMethodProxyRequest.getResponseHeader(HttpConstants.STRING_LOCATION_HEADER) .getValue(); if (stringLocation == null) { throw new IOException("Recieved status code: " + stringStatusCode + " but no " + HttpConstants.STRING_LOCATION_HEADER + " header was found in the response"); } throw new RedirectRequiredException(new URL(stringLocation)); } else if (intProxyResponseCode == HttpServletResponse.SC_NOT_MODIFIED) { // 304 needs special handling. See: // http://www.ics.uci.edu/pub/ietf/http/rfc1945.html#Code304 // We get a 304 whenever passed an 'If-Modified-Since' // header and the data on disk has not changed; server // responds w/ a 304 saying I'm not going to send the // body because the file has not changed. //httpServletResponse.setIntHeader(HttpConstants.STRING_CONTENT_LENGTH_HEADER_NAME, 0); //httpServletResponse.setStatus(HttpServletResponse.SC_NOT_MODIFIED); } // Pass the response code back to the client this.httpStatusCode = intProxyResponseCode; _log.debug("Response code was " + this.httpStatusCode); // Pass response headers back to the client // TODO: Implement support for proxying response headers //Header[] headerArrayResponse = httpMethodProxyRequest.getResponseHeaders(); //for(Header header : headerArrayResponse) { // httpServletResponse.setHeader(header.getName(), header.getValue()); //} // Send the content to the client //InputStream inputStreamProxyResponse = httpMethodProxyRequest.getResponseBodyAsStream(); //BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStreamProxyResponse); //OutputStream outputStreamClientResponse = httpServletResponse.getOutputStream(); //int intNextByte; //while ( ( intNextByte = bufferedInputStream.read() ) != -1 ) { // outputStreamClientResponse.write(intNextByte); //} }
From source file:io.fabric8.gateway.servlet.ProxyServlet.java
/** * Executes the {@link HttpMethod} passed in and sends the proxy response * back to the client via the given {@link javax.servlet.http.HttpServletResponse} * * @param proxyDetails/*from w w w. j a v a2 s . c o m*/ * @param httpMethodProxyRequest An object representing the proxy request to be made * @param httpServletResponse An object by which we can send the proxied * response back to the client * @throws java.io.IOException Can be thrown by the {@link HttpClient}.executeMethod * @throws javax.servlet.ServletException Can be thrown to indicate that another error has occurred */ private void executeProxyRequest(ProxyDetails proxyDetails, HttpMethod httpMethodProxyRequest, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws IOException, ServletException { httpMethodProxyRequest.setDoAuthentication(false); httpMethodProxyRequest.setFollowRedirects(false); // Create a default HttpClient HttpClient httpClient = proxyDetails.createHttpClient(httpMethodProxyRequest); // Execute the request int intProxyResponseCode = httpClient.executeMethod(httpMethodProxyRequest); // Check if the proxy response is a redirect // The following code is adapted from org.tigris.noodle.filters.CheckForRedirect // Hooray for open source software if (intProxyResponseCode >= HttpServletResponse.SC_MULTIPLE_CHOICES /* 300 */ && intProxyResponseCode < HttpServletResponse.SC_NOT_MODIFIED /* 304 */) { String stringStatusCode = Integer.toString(intProxyResponseCode); String stringLocation = httpMethodProxyRequest.getResponseHeader(STRING_LOCATION_HEADER).getValue(); if (stringLocation == null) { throw new ServletException("Received status code: " + stringStatusCode + " but no " + STRING_LOCATION_HEADER + " header was found in the response"); } // Modify the redirect to go to this proxy servlet rather that the proxied host String stringMyHostName = httpServletRequest.getServerName(); if (httpServletRequest.getServerPort() != 80) { stringMyHostName += ":" + httpServletRequest.getServerPort(); } stringMyHostName += httpServletRequest.getContextPath(); httpServletResponse.sendRedirect(stringLocation .replace(proxyDetails.getProxyHostAndPort() + proxyDetails.getProxyPath(), stringMyHostName)); return; } else if (intProxyResponseCode == HttpServletResponse.SC_NOT_MODIFIED) { // 304 needs special handling. See: // http://www.ics.uci.edu/pub/ietf/http/rfc1945.html#Code304 // We get a 304 whenever passed an 'If-Modified-Since' // header and the data on disk has not changed; server // responds w/ a 304 saying I'm not going to send the // body because the file has not changed. httpServletResponse.setIntHeader(STRING_CONTENT_LENGTH_HEADER_NAME, 0); httpServletResponse.setStatus(HttpServletResponse.SC_NOT_MODIFIED); return; } // Pass the response code back to the client httpServletResponse.setStatus(intProxyResponseCode); // Pass response headers back to the client Header[] headerArrayResponse = httpMethodProxyRequest.getResponseHeaders(); for (Header header : headerArrayResponse) { if (!ProxySupport.isHopByHopHeader(header.getName())) { if (ProxySupport.isSetCookieHeader(header)) { HttpProxyRule proxyRule = proxyDetails.getProxyRule(); String setCookie = ProxySupport.replaceCookieAttributes(header.getValue(), proxyRule.getCookiePath(), proxyRule.getCookieDomain()); httpServletResponse.setHeader(header.getName(), setCookie); } else { httpServletResponse.setHeader(header.getName(), header.getValue()); } } } // check if we got data, that is either the Content-Length > 0 // or the response code != 204 int code = httpMethodProxyRequest.getStatusCode(); boolean noData = code == HttpStatus.SC_NO_CONTENT; if (!noData) { String length = httpServletRequest.getHeader(STRING_CONTENT_LENGTH_HEADER_NAME); if (length != null && "0".equals(length.trim())) { noData = true; } } LOG.trace("Response has data? {}", !noData); if (!noData) { // Send the content to the client InputStream inputStreamProxyResponse = httpMethodProxyRequest.getResponseBodyAsStream(); BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStreamProxyResponse); OutputStream outputStreamClientResponse = httpServletResponse.getOutputStream(); int intNextByte; while ((intNextByte = bufferedInputStream.read()) != -1) { outputStreamClientResponse.write(intNextByte); } } }
From source file:com.datos.vfs.provider.webdav.WebdavFileObject.java
/** * Prepares a Method object.// w w w . jav a 2 s . c om * * @param method the HttpMethod. * @throws FileSystemException if an error occurs encoding the uri. * @throws URIException if the URI is in error. */ @Override protected void setupMethod(final HttpMethod method) throws FileSystemException, URIException { final String pathEncoded = ((URLFileName) getName()).getPathQueryEncoded(this.getUrlCharset()); method.setPath(pathEncoded); method.setFollowRedirects(this.getFollowRedirect()); method.setRequestHeader("User-Agent", "Jakarta-Commons-VFS"); method.addRequestHeader("Cache-control", "no-cache"); method.addRequestHeader("Cache-store", "no-store"); method.addRequestHeader("Pragma", "no-cache"); method.addRequestHeader("Expires", "0"); }
From source file:com.twinsoft.convertigo.engine.proxy.translated.HttpClient.java
private int doExecuteMethod(HttpMethod method, HttpConnector connector, String resourceUrl, ParameterShuttle infoShuttle) throws ConnectionException, URIException, MalformedURLException { int statuscode = -1; HostConfiguration hostConfiguration = connector.hostConfiguration; // Tells the method to automatically handle authentication. method.setDoAuthentication(true);/*from www.j av a 2s .c o m*/ // Tells the method to automatically handle redirection. method.setFollowRedirects(false); try { // Display the cookies if (handleCookie) { Cookie[] cookies = httpState.getCookies(); if (Engine.logEngine.isTraceEnabled()) Engine.logEngine.trace("(HttpClient) request cookies:" + Arrays.asList(cookies).toString()); } Engine.logEngine.debug("(HttpClient) executing method..."); statuscode = Engine.theApp.httpClient.executeMethod(hostConfiguration, method, httpState); Engine.logEngine.debug("(HttpClient) end of method successfull"); // Display the cookies if (handleCookie) { Cookie[] cookies = httpState.getCookies(); if (Engine.logEngine.isTraceEnabled()) Engine.logEngine.trace("(HttpClient) response cookies:" + Arrays.asList(cookies).toString()); } } catch (IOException e) { try { Engine.logEngine.warn("(HttpClient) connection error to " + resourceUrl + ": " + e.getMessage() + "; retrying method"); statuscode = Engine.theApp.httpClient.executeMethod(hostConfiguration, method, httpState); Engine.logEngine.debug("(HttpClient) end of method successfull"); } catch (IOException ee) { throw new ConnectionException("Connection error to " + resourceUrl, ee); } } return statuscode; }
From source file:com.itude.mobile.mobbl.server.http.HttpDelegate.java
private void prepareConnection(HttpMethod method, String userAgent, ArrayList<Cookie> cookies, Header[] requestHeaders, boolean acceptXml, boolean followRedirects) { logger.debug("HttpDelegate.prepareConnection()"); if (logger.isDebugEnabled()) HeaderUtil.printRequestHeaders(logger, method); if (acceptXml) method.addRequestHeader("Accept", "application/xml, */*"); if (cookies != null) { for (Cookie cookie : cookies) method.addRequestHeader("Cookie", cookie.getName() + "=" + cookie.getValue()); }/*from w w w . j a va2 s .co m*/ if (userAgent != null) method.addRequestHeader(Constants.USER_AGENT, userAgent); method.setFollowRedirects(followRedirects); if (requestHeaders != null) for (Header h : requestHeaders) method.addRequestHeader(h); }
From source file:com.cerema.cloud2.lib.common.OwnCloudClient.java
/** * Requests the received method./*www. j a v a2 s . c om*/ * * Executes the method through the inherited HttpClient.executedMethod(method). * * @param method HTTP method request. */ @Override public int executeMethod(HttpMethod method) throws IOException { try { // Update User Agent HttpParams params = method.getParams(); String userAgent = OwnCloudClientManagerFactory.getUserAgent(); params.setParameter(HttpMethodParams.USER_AGENT, userAgent); Log_OC.d(TAG + " #" + mInstanceNumber, "REQUEST " + method.getName() + " " + method.getPath()); // logCookiesAtRequest(method.getRequestHeaders(), "before"); // logCookiesAtState("before"); method.setFollowRedirects(false); int status = super.executeMethod(method); if (mFollowRedirects) { status = followRedirection(method).getLastStatus(); } // logCookiesAtRequest(method.getRequestHeaders(), "after"); // logCookiesAtState("after"); // logSetCookiesAtResponse(method.getResponseHeaders()); return status; } catch (IOException e) { //Log_OC.d(TAG + " #" + mInstanceNumber, "Exception occurred", e); throw e; } }