List of usage examples for org.apache.commons.httpclient.params HttpMethodParams setParameter
public void setParameter(String paramString, Object paramObject)
From source file:com.zimbra.common.soap.SoapHttpTransport.java
public Element invoke(Element document, boolean raw, boolean noSession, String requestedAccountId, String changeToken, String tokenType, ResponseHandler respHandler) throws IOException, HttpException, ServiceException { PostMethod method = null;//from ww w. j a v a2 s . c o m try { // Assemble post method. Append document name, so that the request // type is written to the access log. String uri, query; int i = mUri.indexOf('?'); if (i >= 0) { uri = mUri.substring(0, i); query = mUri.substring(i); } else { uri = mUri; query = ""; } if (!uri.endsWith("/")) uri += '/'; uri += getDocumentName(document); method = new PostMethod(uri + query); // Set user agent if it's specified. String agentName = getUserAgentName(); if (agentName != null) { String agentVersion = getUserAgentVersion(); if (agentVersion != null) agentName += " " + agentVersion; method.setRequestHeader(new Header("User-Agent", agentName)); } // the content-type charset will determine encoding used // when we set the request body method.setRequestHeader("Content-Type", getRequestProtocol().getContentType()); if (getClientIp() != null) { method.setRequestHeader(RemoteIP.X_ORIGINATING_IP_HEADER, getClientIp()); if (ZimbraLog.misc.isDebugEnabled()) { ZimbraLog.misc.debug("set remote IP header [%s] to [%s]", RemoteIP.X_ORIGINATING_IP_HEADER, getClientIp()); } } Element soapReq = generateSoapMessage(document, raw, noSession, requestedAccountId, changeToken, tokenType); String soapMessage = SoapProtocol.toString(soapReq, getPrettyPrint()); HttpMethodParams params = method.getParams(); method.setRequestEntity(new StringRequestEntity(soapMessage, null, "UTF-8")); if (getRequestProtocol().hasSOAPActionHeader()) method.setRequestHeader("SOAPAction", mUri); if (mCustomHeaders != null) { for (Map.Entry<String, String> entry : mCustomHeaders.entrySet()) method.setRequestHeader(entry.getKey(), entry.getValue()); } String host = method.getURI().getHost(); HttpState state = HttpClientUtil.newHttpState(getAuthToken(), host, this.isAdmin()); String trustedToken = getTrustedToken(); if (trustedToken != null) { state.addCookie( new Cookie(host, ZimbraCookie.COOKIE_ZM_TRUST_TOKEN, trustedToken, "/", null, false)); } params.setCookiePolicy(state.getCookies().length == 0 ? CookiePolicy.IGNORE_COOKIES : CookiePolicy.BROWSER_COMPATIBILITY); params.setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(mRetryCount - 1, true)); params.setSoTimeout(mTimeout); params.setVersion(HttpVersion.HTTP_1_1); method.setRequestHeader("Connection", mKeepAlive ? "Keep-alive" : "Close"); if (mHostConfig != null && mHostConfig.getUsername() != null && mHostConfig.getPassword() != null) { state.setProxyCredentials(new AuthScope(null, -1), new UsernamePasswordCredentials(mHostConfig.getUsername(), mHostConfig.getPassword())); } if (mHttpDebugListener != null) { mHttpDebugListener.sendSoapMessage(method, soapReq, state); } int responseCode = mClient.executeMethod(mHostConfig, method, state); // SOAP allows for "200" on success and "500" on failure; // real server issues will probably be "503" or "404" if (responseCode != HttpServletResponse.SC_OK && responseCode != HttpServletResponse.SC_INTERNAL_SERVER_ERROR) throw ServiceException.PROXY_ERROR(method.getStatusLine().toString(), uri); // Read the response body. Use the stream API instead of the byte[] // version to avoid HTTPClient whining about a large response. InputStreamReader reader = new InputStreamReader(method.getResponseBodyAsStream(), SoapProtocol.getCharset()); String responseStr = ""; try { if (respHandler != null) { respHandler.process(reader); return null; } else { responseStr = ByteUtil.getContent(reader, (int) method.getResponseContentLength(), false); Element soapResp = parseSoapResponse(responseStr, raw); if (mHttpDebugListener != null) { mHttpDebugListener.receiveSoapMessage(method, soapResp); } return soapResp; } } catch (SoapFaultException x) { // attach request/response to the exception and rethrow x.setFaultRequest(soapMessage); x.setFaultResponse(responseStr.substring(0, Math.min(10240, responseStr.length()))); throw x; } } finally { // Release the connection to the connection manager if (method != null) method.releaseConnection(); // really not necessary if running in the server because the reaper thread // of our connection manager will take care it. // if called from CLI, all connections will be closed when the CLI // exits. Leave it here anyway. if (!mKeepAlive) mClient.getHttpConnectionManager().closeIdleConnections(0); } }
From source file:com.silverwrist.venice.std.TrackbackManager.java
/** * Loads the HTTP content at the specified URL, scans it for RDF description blocks, and adds those blocks * as {@link com.silverwrist.venice.std.TrackbackItem TrackbackItem}s to our internal cache. Uses modification * detection to keep from reloading a page unless necessary. * * @param url The URL of the resource to be loaded. * @param attrs The attributes of the specified page; if this is <code>null</code>, we'll check the page * cache for the right attributes. * @return <code>true</code> if the page data was loaded and scanned for trackback items; <code>false</code> * if no data was loaded (because it was not modified since the last time we loaded it, for instance). * @exception com.silverwrist.venice.except.TrackbackException If there was an error loading or interpreting * the page data.// w w w .ja v a 2s . c o m */ private synchronized boolean load(URL url, PageAttributes attrs) throws TrackbackException { if (attrs == null) attrs = (PageAttributes) (m_page_cache.get(url)); // Create the GET method and set its headers. String s = url.toString(); int x = s.lastIndexOf('#'); if (x >= 0) s = s.substring(0, x); GetMethod getter = new GetMethod(s); HttpMethodParams params = getter.getParams(); getter.setDoAuthentication(false); getter.setFollowRedirects(true); getter.setRequestHeader("User-Agent", USER_AGENT); getter.setRequestHeader("Accept", "text/*"); getter.setRequestHeader("Accept-Encoding", "identity"); params.setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler()); getter.setParams(params); boolean get_resp = false; PageAttributes newattrs = null; ContentType ctype = null; byte[] rawdata = null; try { // set the Last-Modified date as an If-Modified-Since header on the request java.util.Date lmod = null; if (attrs != null) lmod = attrs.getLastModified(); if (lmod != null) getter.setRequestHeader("If-Modified-Since", s_httpdate_format.format(lmod)); // execute the Get method! int rc = m_http_client.executeMethod(getter); get_resp = true; if ((lmod != null) && (rc == HttpStatus.SC_NOT_MODIFIED)) return false; // we were not modified if (rc == HttpStatus.SC_NO_CONTENT) return false; // there's no content there if (rc != HttpStatus.SC_OK) // this is farked! throw new TrackbackException("GET of " + url + " returned " + rc); // Get the new page attributes and save them off. newattrs = new PageAttributes(getter); m_page_cache.put(url, newattrs); // Get the Content-Type header and see if it's valid. Header hdr = getter.getResponseHeader("Content-Type"); if (hdr != null) s = hdr.getValue(); else s = "text/plain"; // necessary assumption ctype = new ContentType(s); if (!(ctype.getPrimaryType().equals("text"))) throw new TrackbackException("URL " + url + " does not point to a text-based resource"); // Load the resource in as byte data; we will determine the right character set for it later. rawdata = getter.getResponseBody(); get_resp = false; } // end try catch (IOException e) { // IO error getting the page throw new TrackbackException("I/O error retrieving " + url + ": " + e.getMessage(), e); } // end catch catch (javax.mail.internet.ParseException e) { // translate into TrackbackException throw new TrackbackException("invalid Content-Type received for URL " + url, e); } // end catch finally { // release the connection if possible try { // need to get the message body if (get_resp) getter.getResponseBody(); } // end try catch (IOException e) { // ignore these } // end catch getter.releaseConnection(); } // end finally // make a first guess at the charset from the HTTP header Content-Type String cset = ctype.getParameter("charset"); if (cset == null) cset = "US-ASCII"; String content = null; try { // interpret the content content = new String(rawdata, cset); } // end try catch (UnsupportedEncodingException e) { // fall back and try just using US-ASCII cset = null; try { // interpret the content content = new String(rawdata, "US-ASCII"); } // end try catch (UnsupportedEncodingException e2) { // can't happen logger.debug("WTF? US-ASCII should damn well be a supported character set!", e2); } // end catch } // end catch // Look for <META HTTP-EQUIV=...> tags in the content. Map http_attrs = extractHttpEquivTags(content); // Try to get a Content-Type attribute from there. s = (String) (http_attrs.get("CONTENT-TYPE")); String cset2 = null; if (s != null) { // look for the content type try { // parse into Content-Type ContentType c = new ContentType(s); if (c.getPrimaryType().equals("text")) cset2 = c.getParameter("charset"); } // end try catch (javax.mail.internet.ParseException e) { // can't get a second Content-Type logger.debug("parse of Content-Type from META tags failed", e); cset2 = null; } // end catch } // end if if ((cset == null) && (cset2 == null)) throw new TrackbackException("unable to determine character set for " + url); if ((cset2 != null) && ((cset == null) || !(cset.equalsIgnoreCase(cset2)))) { // reinterpret content in new character set try { // reinterpret content in new character set s = new String(rawdata, cset2); content = s; // the contents of the HTTP-EQUIV tags may have changed as a result http_attrs = extractHttpEquivTags(content); } // end try catch (UnsupportedEncodingException e) { // just use original character set if (cset == null) throw new TrackbackException("unable to determine character set for " + url); } // end catch } // end if newattrs.updateFromPage(http_attrs); // update the page attributes from the META tag data // Search the page content for RDF blocks. RE m = new RE(s_rdf_start, RE.MATCH_NORMAL); int pos = 0; while (m.match(content, pos)) { // look for the end of this RDF block RE m2 = new RE(getEndRecognizer(m.getParen(1)), RE.MATCH_NORMAL); if (m2.match(content, m.getParenEnd(0))) { // we now have a block to feed to the XML parser try { // run the block through the XML parser InputSource isrc = new InputSource( new StringReader(content.substring(m.getParenStart(0), m2.getParenEnd(0)))); Document doc = m_rdf_parser.parse(isrc); // examine topmost element, which should be rdf:RDF Element root = doc.getDocumentElement(); if (NS_RDF.equals(root.getNamespaceURI()) && (root.getLocalName() != null) && root.getLocalName().equals("RDF")) { // this is most definitely an rdf:RDF node...look for rdf:Description nodes under it NodeList nl = root.getChildNodes(); for (int i = 0; i < nl.getLength(); i++) { // check each node in the list Node n = nl.item(i); if ((n.getNodeType() == Node.ELEMENT_NODE) && NS_RDF.equals(n.getNamespaceURI()) && (n.getLocalName() != null) && n.getLocalName().equals("Description")) { // we've got an rdf:Description node...extract the attributes from it Element elt = (Element) n; try { // look for the item and trackback URLs URL item = null, trackback = null; s = elt.getAttributeNS(NS_DC, "identifier"); if ((s != null) && (s.length() > 0)) item = new URL(s); s = elt.getAttributeNS(NS_TRACKBACK, "ping"); if ((s != null) && (s.length() > 0)) trackback = new URL(s); if ((item != null) && (trackback != null)) { // create the item s = elt.getAttributeNS(NS_DC, "title"); m_item_cache.put(item, new MyTrackbackItem(item, trackback, s, newattrs)); } // end if } // end try catch (MalformedURLException e) { // this means skip this item logger.warn("URL parse failure", e); } // end catch } // end if } // end for } // end if } // end try catch (IOException e) { // disregard this block logger.warn("RDF block parse failure", e); } // end catch catch (SAXException e) { // disregard this block logger.warn("RDF block parse failure", e); } // end catch } // end if // else ignore this possible block pos = m.getParenEnd(0); } // end while return true; }
From source file:com.twinsoft.convertigo.beans.connectors.SiteClipperConnector.java
private void doProcessRequest(Shuttle shuttle) throws IOException, ServletException, EngineException { shuttle.statisticsTaskID = context.statistics.start(EngineStatistics.GET_DOCUMENT); try {// w ww. ja va2 s .c o m shuttle.sharedScope = context.getSharedScope(); String domain = shuttle.getRequest(QueryPart.host) + shuttle.getRequest(QueryPart.port); Engine.logSiteClipper.trace("(SiteClipperConnector) Prepare the request for the domain " + domain); if (!shouldRewrite(domain)) { Engine.logSiteClipper.info( "(SiteClipperConnector) The domain " + domain + " is not allowed with this connector"); shuttle.response.sendError(HttpServletResponse.SC_FORBIDDEN, "The domain " + domain + " is not allowed with this connector"); return; } String uri = shuttle.getRequest(QueryPart.uri); Engine.logSiteClipper.info("Preparing " + shuttle.request.getMethod() + " " + shuttle.getRequestUrl()); HttpMethod httpMethod = null; XulRecorder xulRecorder = context.getXulRecorder(); if (xulRecorder != null) { httpMethod = shuttle.httpMethod = xulRecorder.getRecord(shuttle.getRequestUrlAndQuery()); } if (httpMethod == null) { try { switch (shuttle.getRequestHttpMethodType()) { case GET: httpMethod = new GetMethod(uri); break; case POST: httpMethod = new PostMethod(uri); ((PostMethod) httpMethod) .setRequestEntity(new InputStreamRequestEntity(shuttle.request.getInputStream())); break; case PUT: httpMethod = new PutMethod(uri); ((PutMethod) httpMethod) .setRequestEntity(new InputStreamRequestEntity(shuttle.request.getInputStream())); 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: throw new ServletException( "(SiteClipperConnector) unknown http method " + shuttle.request.getMethod()); } httpMethod.setFollowRedirects(false); } catch (Exception e) { throw new ServletException( "(SiteClipperConnector) unexpected exception will building the http method : " + e.getMessage()); } shuttle.httpMethod = httpMethod; SiteClipperScreenClass screenClass = getCurrentScreenClass(); Engine.logSiteClipper.info("Request screen class: " + screenClass.getName()); for (String name : Collections .list(GenericUtils.<Enumeration<String>>cast(shuttle.request.getHeaderNames()))) { if (requestHeadersToIgnore.contains(HeaderName.parse(name))) { Engine.logSiteClipper.trace("(SiteClipperConnector) Ignoring request header " + name); } else { String value = shuttle.request.getHeader(name); Engine.logSiteClipper .trace("(SiteClipperConnector) Copying request header " + name + "=" + value); shuttle.setRequestCustomHeader(name, value); } } Engine.logSiteClipper.debug("(SiteClipperConnector) applying request rules for the screenclass " + screenClass.getName()); for (IRequestRule rule : screenClass.getRequestRules()) { if (rule.isEnabled()) { Engine.logSiteClipper .trace("(SiteClipperConnector) applying request rule " + rule.getName()); rule.fireEvents(); boolean done = rule.applyOnRequest(shuttle); Engine.logSiteClipper.debug("(SiteClipperConnector) the request rule " + rule.getName() + " is " + (done ? "well" : "not") + " applied"); } else { Engine.logSiteClipper .trace("(SiteClipperConnector) skip the disabled request rule " + rule.getName()); } } for (Entry<String, String> header : shuttle.requestCustomHeaders.entrySet()) { Engine.logSiteClipper.trace("(SiteClipperConnector) Push request header " + header.getKey() + "=" + header.getValue()); httpMethod.addRequestHeader(header.getKey(), header.getValue()); } String queryString = shuttle.request.getQueryString(); if (queryString != null) { try { // Fake test in order to check query string validity new URI("http://localhost/index?" + queryString, true, httpMethod.getParams().getUriCharset()); } catch (URIException e) { // Bugfix #2103 StringBuffer newQuery = new StringBuffer(); for (String part : RegexpUtils.pattern_and.split(queryString)) { String[] pair = RegexpUtils.pattern_equals.split(part, 2); try { newQuery.append('&') .append(URLEncoder.encode(URLDecoder.decode(pair[0], "UTF-8"), "UTF-8")); if (pair.length > 1) { newQuery.append('=').append( URLEncoder.encode(URLDecoder.decode(pair[1], "UTF-8"), "UTF-8")); } } catch (UnsupportedEncodingException ee) { Engine.logSiteClipper .trace("(SiteClipperConnector) failed to encode query part : " + part); } } queryString = newQuery.length() > 0 ? newQuery.substring(1) : newQuery.toString(); Engine.logSiteClipper.trace("(SiteClipperConnector) re-encode query : " + queryString); } } Engine.logSiteClipper.debug("(SiteClipperConnector) Copying the query string : " + queryString); httpMethod.setQueryString(queryString); // if (context.httpState == null) { // Engine.logSiteClipper.debug("(SiteClipperConnector) Creating new HttpState for context id " + context.contextID); // context.httpState = new HttpState(); // } else { // Engine.logSiteClipper.debug("(SiteClipperConnector) Using HttpState of context id " + context.contextID); // } getHttpState(shuttle); HostConfiguration hostConfiguration = getHostConfiguration(shuttle); HttpMethodParams httpMethodParams = httpMethod.getParams(); httpMethodParams.setBooleanParameter("http.connection.stalecheck", true); httpMethodParams.setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, true)); Engine.logSiteClipper.info("Requesting " + httpMethod.getName() + " " + hostConfiguration.getHostURL() + httpMethod.getURI().toString()); HttpClient httpClient = context.getHttpClient3(shuttle.getHttpPool()); HttpUtils.logCurrentHttpConnection(httpClient, hostConfiguration, shuttle.getHttpPool()); httpClient.executeMethod(hostConfiguration, httpMethod, context.httpState); } else { Engine.logSiteClipper.info("Retrieve recorded response from Context"); } int status = httpMethod.getStatusCode(); shuttle.processState = ProcessState.response; Engine.logSiteClipper.info("Request terminated with status " + status); shuttle.response.setStatus(status); if (Engine.isStudioMode() && status == HttpServletResponse.SC_OK && shuttle.getResponseMimeType().startsWith("text/")) { fireDataChanged(new ConnectorEvent(this, shuttle.getResponseAsString())); } SiteClipperScreenClass screenClass = getCurrentScreenClass(); Engine.logSiteClipper.info("Response screen class: " + screenClass.getName()); if (Engine.isStudioMode()) { Engine.theApp.fireObjectDetected(new EngineEvent(screenClass)); } for (Header header : httpMethod.getResponseHeaders()) { String name = header.getName(); if (responseHeadersToIgnore.contains(HeaderName.parse(name))) { Engine.logSiteClipper.trace("(SiteClipperConnector) Ignoring response header " + name); } else { String value = header.getValue(); Engine.logSiteClipper .trace("(SiteClipperConnector) Copying response header " + name + "=" + value); shuttle.responseCustomHeaders.put(name, value); } } Engine.logSiteClipper.debug( "(SiteClipperConnector) applying response rules for the screenclass " + screenClass.getName()); for (IResponseRule rule : screenClass.getResponseRules()) { if (rule.isEnabled()) { Engine.logSiteClipper.trace("(SiteClipperConnector) applying response rule " + rule.getName()); rule.fireEvents(); boolean done = rule.applyOnResponse(shuttle); Engine.logSiteClipper.debug("(SiteClipperConnector) the response rule " + rule.getName() + " is " + (done ? "well" : "not") + " applied"); } else { Engine.logSiteClipper .trace("(SiteClipperConnector) skip the disabled response rule " + rule.getName()); } } for (Entry<String, String> header : shuttle.responseCustomHeaders.entrySet()) { Engine.logSiteClipper.trace( "(SiteClipperConnector) Push request header " + header.getKey() + "=" + header.getValue()); shuttle.response.addHeader(header.getKey(), header.getValue()); } if (shuttle.postInstructions != null) { JSONArray instructions = new JSONArray(); for (IClientInstruction instruction : shuttle.postInstructions) { try { instructions.put(instruction.getInstruction()); } catch (JSONException e) { Engine.logSiteClipper.error( "(SiteClipperConnector) Failed to add a post instruction due to a JSONException", e); } } String codeToInject = "<script>C8O_postInstructions = " + instructions.toString() + "</script>\n" + "<script src=\"" + shuttle.getRequest(QueryPart.full_convertigo_path) + "/scripts/jquery.min.js\"></script>\n" + "<script src=\"" + shuttle.getRequest(QueryPart.full_convertigo_path) + "/scripts/siteclipper.js\"></script>\n"; String content = shuttle.getResponseAsString(); Matcher matcher = HtmlLocation.head_top.matcher(content); String newContent = RegexpUtils.inject(matcher, codeToInject); if (newContent == null) { matcher = HtmlLocation.body_top.matcher(content); newContent = RegexpUtils.inject(matcher, codeToInject); } if (newContent != null) { shuttle.setResponseAsString(newContent); } else { Engine.logSiteClipper.info( "(SiteClipperConnector) Failed to find a head or body tag in the response content"); Engine.logSiteClipper.trace("(SiteClipperConnector) Response content : \"" + content + "\""); } } long nbBytes = 0L; if (shuttle.responseAsString != null && shuttle.responseAsString.hashCode() != shuttle.responseAsStringOriginal.hashCode()) { OutputStream os = shuttle.response.getOutputStream(); switch (shuttle.getResponseContentEncoding()) { case gzip: os = new GZIPOutputStream(os); break; case deflate: os = new DeflaterOutputStream(os, new Deflater(Deflater.DEFAULT_COMPRESSION | Deflater.DEFAULT_STRATEGY, true)); break; default: break; } nbBytes = shuttle.responseAsByte.length; IOUtils.write(shuttle.responseAsString, os, shuttle.getResponseCharset()); os.close(); } else { InputStream is = (shuttle.responseAsByte == null) ? httpMethod.getResponseBodyAsStream() : new ByteArrayInputStream(shuttle.responseAsByte); if (is != null) { nbBytes = 0; OutputStream os = shuttle.response.getOutputStream(); int read = is.read(); while (read >= 0) { os.write(read); os.flush(); read = is.read(); nbBytes++; } is.close(); // nbBytes = IOUtils.copyLarge(is, shuttle.response.getOutputStream()); Engine.logSiteClipper .trace("(SiteClipperConnector) Response body copyied (" + nbBytes + " bytes)"); } } shuttle.response.getOutputStream().close(); shuttle.score = getScore(nbBytes); Engine.logSiteClipper .debug("(SiteClipperConnector) Request terminated with a score of " + shuttle.score); } finally { long duration = context.statistics.stop(shuttle.statisticsTaskID); if (context.requestedObject != null) { try { Engine.theApp.billingManager.insertBilling(context, Long.valueOf(duration), Long.valueOf(shuttle.score)); } catch (Exception e) { Engine.logContext.warn("Unable to insert billing ticket (the billing is thus ignored): [" + e.getClass().getName() + "] " + e.getMessage()); } } } }
From source file:nz.co.jsrsolutions.ds3.provider.EodDataEodDataProvider.java
public EodDataEodDataProvider(String url, String username, String password, long timeout) throws EodDataProviderException { this.url = url; this.username = username; this.password = password; this.timeout = timeout; try {/*from w ww.j av a2 s .c om*/ HttpMethodParams methodParams = new HttpMethodParams(); DefaultHttpMethodRetryHandler retryHandler = new DefaultHttpMethodRetryHandler(3, false); methodParams.setParameter(HttpMethodParams.RETRY_HANDLER, retryHandler); eodDataStub = new DataStub(url); eodDataStub._getServiceClient().getOptions().setTimeOutInMilliSeconds(timeout); eodDataStub._getServiceClient().getOptions().setProperty(HTTPConstants.HTTP_METHOD_PARAMS, methodParams); DataStub.Login loginRequest = new DataStub.Login(); loginRequest.setUsername(username); loginRequest.setPassword(password); // Login DataStub.LoginResponse0 loginResponse0 = eodDataStub.login(loginRequest); DataStub.LOGINRESPONSE loginResponse = loginResponse0.getLoginResult(); if (loginResponse == null) { throw new EodDataProviderException("Failed to authenticate with EOD Data web service."); } token = loginResponse.getToken(); if (token == null || token.isEmpty()) { throw new EodDataProviderException("Failed to authenticate with EOD Data web service."); } logger.info(loginResponse.getMessage()); logger.info(token); logger.info(loginResponse.getDataFormat()); } catch (org.apache.axis2.AxisFault afe) { logger.error(afe.toString()); EodDataProviderException edpe = new EodDataProviderException("Unable to construct EodDataDataProvider"); edpe.initCause(afe); throw edpe; } catch (java.rmi.RemoteException re) { logger.error(re.toString()); EodDataProviderException edpe = new EodDataProviderException("Unable to construct EodDataDataProvider"); edpe.initCause(re); throw edpe; } }
From source file:org.apache.hadoop.chukwa.datacollection.sender.ChukwaHttpSender.java
/** * Handles the HTTP post. Throws HttpException on failure */// w ww . j a va 2s. c o m @SuppressWarnings("deprecation") private void doPost(PostMethod method, RequestEntity data, String dest) throws IOException, HttpException { HttpMethodParams pars = method.getParams(); pars.setParameter(HttpMethodParams.RETRY_HANDLER, (Object) new HttpMethodRetryHandler() { public boolean retryMethod(HttpMethod m, IOException e, int exec) { return !(e instanceof java.net.ConnectException) && (exec < MAX_RETRIES_PER_COLLECTOR); } }); method.setParams(pars); method.setPath(dest); //send it across the network method.setRequestEntity(data); log.info("HTTP post to " + dest + " length = " + data.getContentLength()); // Send POST request client.setTimeout(8000); int statusCode = client.executeMethod(method); if (statusCode != HttpStatus.SC_OK) { log.error("HTTP post response statusCode: " + statusCode + ", statusLine: " + method.getStatusLine()); //do something aggressive here throw new HttpException("got back a failure from server"); } //implicitly "else" log.info( "got success back from the remote collector; response length " + method.getResponseContentLength()); //FIXME: should parse acks here InputStream rstream = null; // Get the response body byte[] resp_buf = method.getResponseBody(); rstream = new ByteArrayInputStream(resp_buf); BufferedReader br = new BufferedReader(new InputStreamReader(rstream)); String line; while ((line = br.readLine()) != null) { System.out.println("response: " + line); } }
From source file:org.apache.hadoop.fs.swift.http.SwiftRestClient.java
/** * Performs the HTTP request, validates the response code and returns * the received data. HTTP Status codes are converted into exceptions. * * @param uri URI to source/*from ww w . j ava 2s .c om*/ * @param processor HttpMethodProcessor * @param <M> method * @param <R> result type * @return result of HTTP request * @throws IOException IO problems * @throws SwiftBadRequestException the status code indicated "Bad request" * @throws SwiftInvalidResponseException the status code is out of range * for the action (excluding 404 responses) * @throws SwiftInternalStateException the internal state of this client * is invalid * @throws FileNotFoundException a 404 response was returned */ private <M extends HttpMethod, R> R perform(URI uri, HttpMethodProcessor<M, R> processor) throws IOException, SwiftBadRequestException, SwiftInternalStateException, SwiftInvalidResponseException, FileNotFoundException { checkNotNull(uri); checkNotNull(processor); final M method = processor.createMethod(uri.toString()); //retry policy HttpMethodParams methodParams = method.getParams(); methodParams.setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(retryCount, false)); methodParams.setSoTimeout(connectTimeout); try { int statusCode = exec(method); //look at the response and see if it was valid or not. //Valid is more than a simple 200; even 404 "not found" is considered //valid -which it is for many methods. //validate the allowed status code for this operation int[] allowedStatusCodes = processor.getAllowedStatusCodes(); boolean validResponse = isStatusCodeExpected(statusCode, allowedStatusCodes); if (!validResponse) { IOException ioe = buildException(uri, method, statusCode); throw ioe; } return processor.extractResult(method); } catch (IOException e) { //release the connection -always method.releaseConnection(); throw e; } }
From source file:org.apache.maven.wagon.providers.webdav.HttpMethodConfiguration.java
private void fillParams(HttpMethodParams p) { if (!hasParams()) { return;// w w w . java 2 s . c om } if (connectionTimeout > 0) { p.setSoTimeout(connectionTimeout); } if (params != null) { Pattern coercePattern = Pattern.compile(COERCE_PATTERN); for (Map.Entry<Object, Object> entry : params.entrySet()) { String key = (String) entry.getKey(); String value = (String) entry.getValue(); Matcher matcher = coercePattern.matcher(value); if (matcher.matches()) { char type = matcher.group(1).charAt(0); value = matcher.group(2); switch (type) { case 'i': { p.setIntParameter(key, Integer.parseInt(value)); break; } case 'd': { p.setDoubleParameter(key, Double.parseDouble(value)); break; } case 'l': { p.setLongParameter(key, Long.parseLong(value)); break; } case 'b': { p.setBooleanParameter(key, Boolean.valueOf(value).booleanValue()); break; } case 'c': { String[] entries = value.split(","); List<String> collection = new ArrayList<String>(); for (String e : entries) { collection.add(e.trim()); } p.setParameter(key, collection); break; } case 'm': { String[] entries = value.split(","); Map<String, String> map = new LinkedHashMap<String, String>(); for (String e : entries) { int idx = e.indexOf("=>"); if (idx < 1) { break; } String mapKey = e.substring(0, idx); String mapVal = e.substring(idx + 1, e.length()); map.put(mapKey.trim(), mapVal.trim()); } p.setParameter(key, map); break; } } } else { p.setParameter(key, value); } } } }
From source file:org.apache.nutch.protocol.httpclient.HttpFormAuthentication.java
/** * NUTCH-2280 Set the cookie policy value from httpclient-auth.xml for the * Post httpClient action./*w w w . j a va 2 s . c o m*/ * * @param fromConfigurer * - the httpclient-auth.xml values * * @param params * - the HttpMethodParams from the current httpclient instance * * @throws NoSuchFieldException * @throws SecurityException * @throws IllegalArgumentException * @throws IllegalAccessException */ private void setCookieParams(HttpFormAuthConfigurer formConfigurer, HttpMethodParams params) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException { // NUTCH-2280 - set the HttpClient cookie policy if (formConfigurer.getCookiePolicy() != null) { String policy = formConfigurer.getCookiePolicy(); Object p = FieldUtils.readDeclaredStaticField(CookiePolicy.class, policy); if (null != p) { LOG.debug("reflection of cookie value: " + p.toString()); params.setParameter(HttpMethodParams.COOKIE_POLICY, p); } } }
From source file:org.eclipse.osee.framework.core.util.HttpProcessor.java
public static boolean isAlive(URL url) { boolean isAlive = false; boolean recheck = true; String key = toIsAliveKey(url); Pair<Integer, Long> lastChecked = isAliveMap.get(key); if (lastChecked != null) { long checkedOffset = System.currentTimeMillis() - lastChecked.getSecond().longValue(); if (checkedOffset < CHECK_WINDOW) { recheck = false;//from ww w .ja v a 2 s . c o m isAlive = lastChecked.getFirst() != -1; } } if (recheck) { try { GetMethod method = new GetMethod(url.toString()); try { HttpMethodParams params = new HttpMethodParams(); params.setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(0, false)); params.setSoTimeout(1000); method.setParams(params); int responseCode = executeMethod(url, method); if (responseCode == HttpURLConnection.HTTP_NOT_FOUND || responseCode == HttpURLConnection.HTTP_ACCEPTED || responseCode == HttpURLConnection.HTTP_ACCEPTED || responseCode == HttpURLConnection.HTTP_OK) { isAlive = true; } } finally { method.releaseConnection(); } } catch (Exception ex) { // Do Nothing } } return isAlive; }
From source file:org.eclipse.smila.connectivity.framework.crawler.web.http.HttpResponse.java
/** * Sets the http parameters./* w ww . j a v a 2s. com*/ * * @param http * the http * @param httpMethod * the http method */ private void setHttpParameters(HttpBase http, HttpMethodBase httpMethod) { httpMethod.setFollowRedirects(false); httpMethod.setRequestHeader("User-Agent", http.getUserAgent()); httpMethod.setRequestHeader("Referer", http.getReferer()); httpMethod.setDoAuthentication(true); for (Header header : http.getHeaders()) { httpMethod.addRequestHeader(header); } final HttpMethodParams params = httpMethod.getParams(); if (http.getUseHttp11()) { params.setVersion(HttpVersion.HTTP_1_1); } else { params.setVersion(HttpVersion.HTTP_1_0); } params.makeLenient(); params.setContentCharset("UTF-8"); if (http.isCookiesEnabled()) { params.setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY); } else { params.setCookiePolicy(CookiePolicy.IGNORE_COOKIES); } params.setBooleanParameter(HttpMethodParams.SINGLE_COOKIE_HEADER, true); // the default is to retry 3 times; if // the request body was sent the method is not retried, so there is // little danger in retrying // retries are handled on the higher level params.setParameter(HttpMethodParams.RETRY_HANDLER, null); }