List of usage examples for org.apache.commons.httpclient.params HttpMethodParams RETRY_HANDLER
String RETRY_HANDLER
To view the source code for org.apache.commons.httpclient.params HttpMethodParams RETRY_HANDLER.
Click Source Link
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 w w w.j a v a 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.http.client.benchmark.TestHttpClient3.java
public TestHttpClient3() { super();/*w w w .java2 s . com*/ this.mgr = new MultiThreadedHttpConnectionManager(); this.httpclient = new HttpClient(this.mgr); this.httpclient.getParams().setVersion(HttpVersion.HTTP_1_1); this.httpclient.getParams().setBooleanParameter(HttpMethodParams.USE_EXPECT_CONTINUE, false); this.httpclient.getHttpConnectionManager().getParams().setStaleCheckingEnabled(false); this.httpclient.getParams().setSoTimeout(15000); HttpMethodRetryHandler retryhandler = new HttpMethodRetryHandler() { public boolean retryMethod(final HttpMethod httpmethod, final IOException ex, int count) { return false; } }; this.httpclient.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, retryhandler); }
From source file:org.apache.jmeter.protocol.http.sampler.HTTPHC3Impl.java
/** * Returns an <code>HttpConnection</code> fully ready to attempt * connection. This means it sets the request method (GET or POST), headers, * cookies, and authorization for the URL request. * <p>//from w ww. j a va2 s.c o m * The request infos are saved into the sample result if one is provided. * * @param u * <code>URL</code> of the URL request * @param httpMethod * GET/PUT/HEAD etc * @param res * sample result to save request infos to * @return <code>HttpConnection</code> ready for .connect * @exception IOException * if an I/O Exception occurs */ protected HttpClient setupConnection(URL u, HttpMethodBase httpMethod, HTTPSampleResult res) throws IOException { String urlStr = u.toString(); org.apache.commons.httpclient.URI uri = new org.apache.commons.httpclient.URI(urlStr, false); String schema = uri.getScheme(); if ((schema == null) || (schema.length() == 0)) { schema = HTTPConstants.PROTOCOL_HTTP; } final boolean isHTTPS = HTTPConstants.PROTOCOL_HTTPS.equalsIgnoreCase(schema); if (isHTTPS) { SSLManager.getInstance(); // ensure the manager is initialised // we don't currently need to do anything further, as this sets the default https protocol } Protocol protocol = Protocol.getProtocol(schema); String host = uri.getHost(); int port = uri.getPort(); /* * We use the HostConfiguration as the key to retrieve the HttpClient, * so need to ensure that any items used in its equals/hashcode methods are * not changed after use, i.e.: * host, port, protocol, localAddress, proxy * */ HostConfiguration hc = new HostConfiguration(); hc.setHost(host, port, protocol); // All needed to ensure re-usablility // Set up the local address if one exists final InetAddress inetAddr = getIpSourceAddress(); if (inetAddr != null) {// Use special field ip source address (for pseudo 'ip spoofing') hc.setLocalAddress(inetAddr); } else { hc.setLocalAddress(localAddress); // null means use the default } final String proxyHost = getProxyHost(); final int proxyPort = getProxyPortInt(); boolean useStaticProxy = isStaticProxy(host); boolean useDynamicProxy = isDynamicProxy(proxyHost, proxyPort); if (useDynamicProxy) { hc.setProxy(proxyHost, proxyPort); useStaticProxy = false; // Dynamic proxy overrules static proxy } else if (useStaticProxy) { if (log.isDebugEnabled()) { log.debug("Setting proxy: " + PROXY_HOST + ":" + PROXY_PORT); } hc.setProxy(PROXY_HOST, PROXY_PORT); } Map<HostConfiguration, HttpClient> map = httpClients.get(); // N.B. HostConfiguration.equals() includes proxy settings in the compare. HttpClient httpClient = map.get(hc); if (httpClient != null && resetSSLContext && isHTTPS) { httpClient.getHttpConnectionManager().closeIdleConnections(-1000); httpClient = null; JsseSSLManager sslMgr = (JsseSSLManager) SSLManager.getInstance(); sslMgr.resetContext(); resetSSLContext = false; } if (httpClient == null) { httpClient = new HttpClient(new SimpleHttpConnectionManager()); httpClient.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(RETRY_COUNT, false)); if (log.isDebugEnabled()) { log.debug("Created new HttpClient: @" + System.identityHashCode(httpClient)); } httpClient.setHostConfiguration(hc); map.put(hc, httpClient); } else { if (log.isDebugEnabled()) { log.debug("Reusing the HttpClient: @" + System.identityHashCode(httpClient)); } } // Set up any required Proxy credentials if (useDynamicProxy) { String user = getProxyUser(); if (user.length() > 0) { httpClient.getState().setProxyCredentials( new AuthScope(proxyHost, proxyPort, null, AuthScope.ANY_SCHEME), new NTCredentials(user, getProxyPass(), localHost, PROXY_DOMAIN)); } else { httpClient.getState().clearProxyCredentials(); } } else { if (useStaticProxy) { if (PROXY_USER.length() > 0) { httpClient.getState().setProxyCredentials( new AuthScope(PROXY_HOST, PROXY_PORT, null, AuthScope.ANY_SCHEME), new NTCredentials(PROXY_USER, PROXY_PASS, localHost, PROXY_DOMAIN)); } } else { httpClient.getState().clearProxyCredentials(); } } int rto = getResponseTimeout(); if (rto > 0) { httpMethod.getParams().setSoTimeout(rto); } int cto = getConnectTimeout(); if (cto > 0) { httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(cto); } // Allow HttpClient to handle the redirects: httpMethod.setFollowRedirects(getAutoRedirects()); // a well-behaved browser is supposed to send 'Connection: close' // with the last request to an HTTP server. Instead, most browsers // leave it to the server to close the connection after their // timeout period. Leave it to the JMeter user to decide. if (getUseKeepAlive()) { httpMethod.setRequestHeader(HTTPConstants.HEADER_CONNECTION, HTTPConstants.KEEP_ALIVE); } else { httpMethod.setRequestHeader(HTTPConstants.HEADER_CONNECTION, HTTPConstants.CONNECTION_CLOSE); } setConnectionHeaders(httpMethod, u, getHeaderManager(), getCacheManager()); String cookies = setConnectionCookie(httpMethod, u, getCookieManager()); setConnectionAuthorization(httpClient, u, getAuthManager()); if (res != null) { res.setCookies(cookies); } return httpClient; }
From source file:org.apache.oodt.cas.filemgr.system.XmlRpcFileManagerClient.java
/** * <p> Constructs a new XmlRpcFileManagerClient with the given <code>url</code>. </p> * * @param url The url pointer to the xml rpc file manager service. * @param testConnection Whether or not to check if server at given url is alive. *//*w ww . j a v a 2 s.c o m*/ public XmlRpcFileManagerClient(final URL url, boolean testConnection) throws ConnectionException { // set up the configuration, if there is any if (System.getProperty("org.apache.oodt.cas.filemgr.properties") != null) { String configFile = System.getProperty("org.apache.oodt.cas.filemgr.properties"); LOG.log(Level.INFO, "Loading File Manager Configuration Properties from: [" + configFile + "]"); try { System.getProperties().load(new FileInputStream(new File(configFile))); } catch (Exception e) { LOG.log(Level.INFO, "Error loading configuration properties from: [" + configFile + "]"); } } XmlRpcTransportFactory transportFactory = new XmlRpcTransportFactory() { public XmlRpcTransport createTransport() throws XmlRpcClientException { HttpClient client = new HttpClient(); client.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new HttpMethodRetryHandler() { public boolean retryMethod(HttpMethod method, IOException e, int count) { if (count < Integer .getInteger("org.apache.oodt.cas.filemgr.system.xmlrpc.connection.retries", 3)) { try { Thread.sleep(Integer.getInteger( "org.apache.oodt.cas.filemgr.system.xmlrpc.connection.retry.interval.seconds", 0) * 1000); return true; } catch (Exception ignored) { } } return false; } }); CommonsXmlRpcTransport transport = new CommonsXmlRpcTransport(url, client); transport.setConnectionTimeout(Integer.getInteger( "org.apache.oodt.cas.filemgr.system.xmlrpc.connectionTimeout.minutes", 20) * 60 * 1000); transport.setTimeout( Integer.getInteger("org.apache.oodt.cas.filemgr.system.xmlrpc.requestTimeout.minutes", 60) * 60 * 1000); return transport; } public void setProperty(String arg0, Object arg1) { } }; client = new XmlRpcClient(url, transportFactory); fileManagerUrl = url; if (testConnection && !isAlive()) { throw new ConnectionException("Exception connecting to filemgr: [" + this.fileManagerUrl + "]"); } }
From source file:org.apache.servicemix.http.HttpSoapTest.java
public void testSoapRoundtripProviderConsumerProvider() throws Exception { EchoComponent echo = new EchoComponent(); echo.setService(new QName("urn:test", "echo")); echo.setEndpoint("echo"); container.activateComponent(echo, "echo"); HttpComponent http = new HttpComponent(); HttpEndpoint ep1 = new HttpEndpoint(); ep1.setService(new QName("urn:test", "s1")); ep1.setEndpoint("ep1"); ep1.setTargetService(new QName("urn:test", "s2")); ep1.setLocationURI("http://localhost:8192/ep1/"); ep1.setRoleAsString("consumer"); ep1.setDefaultMep(URI.create("http://www.w3.org/2004/08/wsdl/in-out")); ep1.setSoap(true);//from w w w . j av a2 s . c o m HttpEndpoint ep2 = new HttpEndpoint(); ep2.setService(new QName("urn:test", "s2")); ep2.setEndpoint("ep2"); ep2.setLocationURI("http://localhost:8192/ep3/"); ep2.setRoleAsString("provider"); ep2.setSoap(true); HttpEndpoint ep3 = new HttpEndpoint(); ep3.setService(new QName("urn:test", "s3")); ep3.setEndpoint("ep3"); ep3.setTargetService(new QName("urn:test", "echo")); ep3.setLocationURI("http://localhost:8192/ep3/"); ep3.setRoleAsString("consumer"); ep3.setDefaultMep(URI.create("http://www.w3.org/2004/08/wsdl/in-out")); ep3.setSoap(true); http.setEndpoints(new HttpEndpoint[] { ep1, ep2, ep3 }); container.activateComponent(http, "http"); container.start(); PostMethod method = new PostMethod("http://localhost:8192/ep1/"); method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new HttpMethodRetryHandler() { public boolean retryMethod(HttpMethod method, IOException exception, int executionCount) { return false; } }); method.setRequestEntity( new StringRequestEntity("<env:Envelope xmlns:env='http://www.w3.org/2003/05/soap-envelope'>" + "<env:Body><hello>world</hello></env:body>" + "</env:Envelope>")); int state = new HttpClient().executeMethod(method); assertEquals(HttpServletResponse.SC_OK, state); ByteArrayOutputStream baos = new ByteArrayOutputStream(); FileUtil.copyInputStream(method.getResponseBodyAsStream(), baos); logger.info(baos.toString()); }
From source file:org.apache.servicemix.http.processors.ProviderProcessor.java
public void process(MessageExchange exchange) throws Exception { if (exchange.getStatus() == ExchangeStatus.DONE || exchange.getStatus() == ExchangeStatus.ERROR) { PostMethod method = methods.remove(exchange.getExchangeId()); if (method != null) { method.releaseConnection();// w w w . jav a 2 s . co m } return; } boolean txSync = exchange.isTransacted() && Boolean.TRUE.equals(exchange.getProperty(JbiConstants.SEND_SYNC)); txSync |= endpoint.isSynchronous(); NormalizedMessage nm = exchange.getMessage("in"); if (nm == null) { throw new IllegalStateException("Exchange has no input message"); } String locationURI = endpoint.getLocationURI(); // Incorporated because of JIRA SM-695 Object newDestinationURI = nm.getProperty(JbiConstants.HTTP_DESTINATION_URI); if (newDestinationURI != null) { locationURI = (String) newDestinationURI; log.debug("Location URI overridden: " + locationURI); } PostMethod method = new PostMethod(getRelUri(locationURI)); SoapMessage soapMessage = new SoapMessage(); soapHelper.getJBIMarshaler().fromNMS(soapMessage, nm); Context context = soapHelper.createContext(soapMessage); soapHelper.onSend(context); SoapWriter writer = soapHelper.getSoapMarshaler().createWriter(soapMessage); copyHeaderInformation(nm, method); RequestEntity entity = writeMessage(writer); // remove content-type header that may have been part of the in message if (!endpoint.isWantContentTypeHeaderFromExchangeIntoHttpRequest()) { method.removeRequestHeader(HEADER_CONTENT_TYPE); method.addRequestHeader(HEADER_CONTENT_TYPE, entity.getContentType()); } if (entity.getContentLength() < 0) { method.removeRequestHeader(HEADER_CONTENT_LENGTH); } else { method.setRequestHeader(HEADER_CONTENT_LENGTH, Long.toString(entity.getContentLength())); } if (endpoint.isSoap() && method.getRequestHeader(HEADER_SOAP_ACTION) == null) { if (endpoint.getSoapAction() != null) { method.setRequestHeader(HEADER_SOAP_ACTION, endpoint.getSoapAction()); } else { // method.setRequestHeader(HEADER_SOAP_ACTION, "\"\""); } } method.setRequestHeader(HEADER_X_CORRELATION_ID, (String) exchange.getProperty(JbiConstants.CORRELATION_ID)); String smxInstanceName = System.getProperty(SMX_INSTANCE_NAME_PROPERTY); if (smxInstanceName != null) { method.setRequestHeader(HEADER_X_POWERED_BY, smxInstanceName); } else { log.warn(SMX_INSTANCE_NAME_PROPERTY + " property was not set in servicemix.xml file"); } method.setRequestEntity(entity); boolean close = true; try { // Set the retry handler int retries = getConfiguration().isStreamingEnabled() ? 0 : getConfiguration().getRetryCount(); method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(retries, true)); // Set authentication if (endpoint.getBasicAuthentication() != null) { endpoint.getBasicAuthentication().applyCredentials(getClient(), exchange, nm); } // Execute the HTTP method //method.getParams().setLongParameter(HttpConnectionParams.CONNECTION_TIMEOUT, getClient().getParams().getSoTimeout()); int response = -1; try { response = getClient().executeMethod(getHostConfiguration(locationURI, exchange, nm), method); } catch (Exception ex) { try { if (listener != null) { if (ex instanceof SocketException) { log.error("Connection to address: " + locationURI + " was refused"); listener.onConnectionRefused(locationURI); } else if (ex instanceof SocketTimeoutException) { log.error("Connection to address: " + locationURI + " timed out"); listener.onTimeout(locationURI); } } } catch (Exception ex2) { log.warn("Error in HttpConnectionListener: " + ex2.getMessage()); } finally { throw ex; } } try { if (listener != null) { listener.onPostExecuted(locationURI, response); } } catch (Exception ex) { log.warn("Error in HttpConnectionListener: " + ex.getMessage()); } if (response != HttpStatus.SC_OK && response != HttpStatus.SC_ACCEPTED) { if (!(exchange instanceof InOnly)) { SoapReader reader = soapHelper.getSoapMarshaler().createReader(); Header contentType = method.getResponseHeader(HEADER_CONTENT_TYPE); String content = convertResponseBodyToString(method); logInOut(locationURI, response, method, soapMessage, content); soapMessage = reader.read(new ByteArrayInputStream(content.getBytes()), contentType != null ? contentType.getValue() : null); context.setFaultMessage(soapMessage); soapHelper.onAnswer(context); Fault fault = exchange.createFault(); fault.setProperty(JbiConstants.PROTOCOL_HEADERS, getHeaders(method)); soapHelper.getJBIMarshaler().toNMS(fault, soapMessage); exchange.setFault(fault); if (txSync) { channel.sendSync(exchange); } else { methods.put(exchange.getExchangeId(), method); channel.send(exchange); close = false; } return; } else { String content = convertResponseBodyToString(method); // even if it is InOnly, some out could come to us logInOut(locationURI, response, method, soapMessage, content); throw new Exception("Invalid status response: " + response); } } if (exchange instanceof InOut) { close = processInOut(exchange, method, context, txSync, close); } else if (exchange instanceof InOptionalOut) { close = processInOptionalOut(method, exchange, context, txSync, close); } else { exchange.setStatus(ExchangeStatus.DONE); channel.send(exchange); } } finally { if (close) { method.releaseConnection(); } } }
From source file:org.apache.sling.discovery.impl.topology.connector.TopologyConnectorClient.java
/** ping the server and pass the announcements between the two **/ void ping(final boolean force) { if (autoStopped) { // then we suppress any further pings! logger.debug("ping: autoStopped=true, hence suppressing any further pings."); return;// w w w . j ava 2s .c om } if (force) { backoffPeriodEnd = -1; } else if (backoffPeriodEnd > 0) { if (System.currentTimeMillis() < backoffPeriodEnd) { logger.debug("ping: not issueing a heartbeat due to backoff instruction from peer."); return; } else { logger.debug("ping: backoff period ended, issuing another ping now."); } } final String uri = connectorUrl.toString() + "." + clusterViewService.getSlingId() + ".json"; if (logger.isDebugEnabled()) { logger.debug("ping: connectorUrl=" + connectorUrl + ", complete uri=" + uri); } HttpClient httpClient = new HttpClient(); final PutMethod method = new PutMethod(uri); Announcement resultingAnnouncement = null; try { String userInfo = connectorUrl.getUserInfo(); if (userInfo != null) { Credentials c = new UsernamePasswordCredentials(userInfo); httpClient.getState() .setCredentials(new AuthScope(method.getURI().getHost(), method.getURI().getPort()), c); } Announcement topologyAnnouncement = new Announcement(clusterViewService.getSlingId()); topologyAnnouncement.setServerInfo(serverInfo); final ClusterView clusterView = clusterViewService.getClusterView(); topologyAnnouncement.setLocalCluster(clusterView); if (force) { logger.debug("ping: sending a resetBackoff"); topologyAnnouncement.setResetBackoff(true); } announcementRegistry.addAllExcept(topologyAnnouncement, clusterView, new AnnouncementFilter() { public boolean accept(final String receivingSlingId, final Announcement announcement) { // filter out announcements that are of old cluster instances // which I dont really have in my cluster view at the moment final Iterator<InstanceDescription> it = clusterViewService.getClusterView().getInstances() .iterator(); while (it.hasNext()) { final InstanceDescription instance = it.next(); if (instance.getSlingId().equals(receivingSlingId)) { // then I have the receiving instance in my cluster view // all fine then return true; } } // looks like I dont have the receiving instance in my cluster view // then I should also not propagate that announcement anywhere return false; } }); final String p = requestValidator.encodeMessage(topologyAnnouncement.asJSON()); if (logger.isDebugEnabled()) { logger.debug("ping: topologyAnnouncement json is: " + p); } requestValidator.trustMessage(method, p); if (config.isGzipConnectorRequestsEnabled()) { // tell the server that the content is gzipped: method.addRequestHeader("Content-Encoding", "gzip"); // and gzip the body: final ByteArrayOutputStream baos = new ByteArrayOutputStream(); final GZIPOutputStream gzipOut = new GZIPOutputStream(baos); gzipOut.write(p.getBytes("UTF-8")); gzipOut.close(); final byte[] gzippedEncodedJson = baos.toByteArray(); method.setRequestEntity(new ByteArrayRequestEntity(gzippedEncodedJson, "application/json")); lastRequestEncoding = "gzip"; } else { // otherwise plaintext: method.setRequestEntity(new StringRequestEntity(p, "application/json", "UTF-8")); lastRequestEncoding = "plaintext"; } // independent of request-gzipping, we do accept the response to be gzipped, // so indicate this to the server: method.addRequestHeader("Accept-Encoding", "gzip"); DefaultHttpMethodRetryHandler retryhandler = new DefaultHttpMethodRetryHandler(0, false); httpClient.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, retryhandler); httpClient.getHttpConnectionManager().getParams() .setConnectionTimeout(1000 * config.getConnectionTimeout()); httpClient.getHttpConnectionManager().getParams().setSoTimeout(1000 * config.getSoTimeout()); method.getParams().setSoTimeout(1000 * config.getSoTimeout()); httpClient.executeMethod(method); if (logger.isDebugEnabled()) { logger.debug("ping: done. code=" + method.getStatusCode() + " - " + method.getStatusText()); } lastStatusCode = method.getStatusCode(); lastResponseEncoding = null; if (method.getStatusCode() == HttpServletResponse.SC_OK) { final Header contentEncoding = method.getResponseHeader("Content-Encoding"); if (contentEncoding != null && contentEncoding.getValue() != null && contentEncoding.getValue().contains("gzip")) { lastResponseEncoding = "gzip"; } else { lastResponseEncoding = "plaintext"; } String responseBody = requestValidator.decodeMessage(method); // limiting to 16MB, should be way enough if (logger.isDebugEnabled()) { logger.debug("ping: response body=" + responseBody); } if (responseBody != null && responseBody.length() > 0) { Announcement inheritedAnnouncement = Announcement.fromJSON(responseBody); final long backoffInterval = inheritedAnnouncement.getBackoffInterval(); if (backoffInterval > 0) { // then reset the backoffPeriodEnd: /* minus 1 sec to avoid slipping the interval by a few millis */ this.backoffPeriodEnd = System.currentTimeMillis() + (1000 * backoffInterval) - 1000; logger.debug("ping: servlet instructed to backoff: backoffInterval=" + backoffInterval + ", resulting in period end of " + new Date(backoffPeriodEnd)); } else { logger.debug("ping: servlet did not instruct any backoff-ing at this stage"); this.backoffPeriodEnd = -1; } if (inheritedAnnouncement.isLoop()) { if (logger.isDebugEnabled()) { logger.debug( "ping: connector response indicated a loop detected. not registering this announcement from " + inheritedAnnouncement.getOwnerId()); } if (inheritedAnnouncement.getOwnerId().equals(clusterViewService.getSlingId())) { // SLING-3316 : local-loop detected. Check config to see if we should stop this connector if (config.isAutoStopLocalLoopEnabled()) { inheritedAnnouncement = null; // results in connected -> false and representsloop -> true autoStopped = true; // results in isAutoStopped -> true } } } else { inheritedAnnouncement.setInherited(true); if (announcementRegistry.registerAnnouncement(inheritedAnnouncement) == -1) { if (logger.isDebugEnabled()) { logger.debug( "ping: connector response is from an instance which I already see in my topology" + inheritedAnnouncement); } statusDetails = "receiving side is seeing me via another path (connector or cluster) already (loop)"; return; } } resultingAnnouncement = inheritedAnnouncement; statusDetails = null; } else { statusDetails = "no response body received"; } } else { statusDetails = "got HTTP Status-Code: " + lastStatusCode; } // SLING-2882 : reset suppressPingWarnings_ flag in success case suppressPingWarnings_ = false; } catch (URIException e) { logger.warn("ping: Got URIException: " + e + ", uri=" + uri); statusDetails = e.toString(); } catch (IOException e) { // SLING-2882 : set/check the suppressPingWarnings_ flag if (suppressPingWarnings_) { if (logger.isDebugEnabled()) { logger.debug("ping: got IOException: " + e + ", uri=" + uri); } } else { suppressPingWarnings_ = true; logger.warn("ping: got IOException [suppressing further warns]: " + e + ", uri=" + uri); } statusDetails = e.toString(); } catch (JSONException e) { logger.warn("ping: got JSONException: " + e); statusDetails = e.toString(); } catch (RuntimeException re) { logger.warn("ping: got RuntimeException: " + re, re); statusDetails = re.toString(); } finally { method.releaseConnection(); lastInheritedAnnouncement = resultingAnnouncement; lastPingedAt = System.currentTimeMillis(); } }
From source file:org.apache.solr.client.solrj.impl.CommonsHttpSolrServer.java
/** * @see #useMultiPartPost/*from ww w . j av a2 s . c om*/ * @see #_parser */ public CommonsHttpSolrServer(URL baseURL, HttpClient client, ResponseParser parser, boolean useMultiPartPost) { _baseURL = baseURL.toExternalForm(); if (_baseURL.endsWith("/")) { _baseURL = _baseURL.substring(0, _baseURL.length() - 1); } if (_baseURL.indexOf('?') >= 0) { throw new RuntimeException( "Invalid base url for solrj. The base URL must not contain parameters: " + _baseURL); } if (client == null) { _httpClient = new HttpClient(new MultiThreadedHttpConnectionManager()); // prevent retries (note: this didn't work when set on mgr.. needed to be set on client) DefaultHttpMethodRetryHandler retryhandler = new DefaultHttpMethodRetryHandler(0, false); _httpClient.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, retryhandler); // set some better defaults if we created a new connection manager and client // increase the default connections this.setDefaultMaxConnectionsPerHost(32); // 2 this.setMaxTotalConnections(128); // 20 } else { _httpClient = client; } _parser = parser; this.useMultiPartPost = useMultiPartPost; }
From source file:org.apdplat.qa.datasource.GoogleDataSource.java
private List<Evidence> search(String query) { List<Evidence> evidences = new ArrayList<>(); try {// w ww . j ava 2 s. co m HttpClient httpClient = new HttpClient(); GetMethod getMethod = new GetMethod(query); httpClient.executeMethod(getMethod); getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler()); int statusCode = httpClient.executeMethod(getMethod); if (statusCode != HttpStatus.SC_OK) { LOG.error("Method failed: " + getMethod.getStatusLine()); } byte[] responseBody = getMethod.getResponseBody(); String response = new String(responseBody, "UTF-8"); LOG.debug("??" + response); JSONObject json = new JSONObject(response); String totalResult = json.getJSONObject("responseData").getJSONObject("cursor") .getString("estimatedResultCount"); int totalResultCount = Integer.parseInt(totalResult); LOG.info("? " + totalResultCount); JSONArray results = json.getJSONObject("responseData").getJSONArray("results"); LOG.debug(" Results:"); for (int i = 0; i < results.length(); i++) { Evidence evidence = new Evidence(); JSONObject result = results.getJSONObject(i); String title = result.getString("titleNoFormatting"); LOG.debug(title); evidence.setTitle(title); if (SUMMARY) { String content = result.get("content").toString(); content = content.replaceAll("<b>", ""); content = content.replaceAll("</b>", ""); content = content.replaceAll("\\.\\.\\.", ""); LOG.debug(content); evidence.setSnippet(content); } else { //URL??? String url = result.get("url").toString(); String content = Tools.getHTMLContent(url); if (content == null) { content = result.get("content").toString(); content = content.replaceAll("<b>", ""); content = content.replaceAll("</b>", ""); content = content.replaceAll("\\.\\.\\.", ""); } evidence.setSnippet(content); LOG.debug(content); } evidences.add(evidence); } } catch (Exception e) { LOG.error("?", e); } return evidences; }
From source file:org.apdplat.qa.util.Tools.java
public static String getRewindEvidenceText(String question, String answer) { //1??/* ww w . jav a 2 s. c o m*/ String rewindEvidenceText = MySQLUtils.getRewindEvidenceText(question, answer); if (rewindEvidenceText != null) { //? LOG.info("?" + question + " " + answer); return rewindEvidenceText; } //2???google StringBuilder text = new StringBuilder(); String query = question + answer; try { query = URLEncoder.encode(query, "UTF-8"); } catch (UnsupportedEncodingException e) { LOG.error("url", e); return null; } query = "http://ajax.googleapis.com/ajax/services/search/web?start=0&rsz=large&v=1.0&q=" + query; try { HostConfiguration hcf = new HostConfiguration(); hcf.setProxy("127.0.0.1", 8087); HttpClient httpClient = new HttpClient(); GetMethod getMethod = new GetMethod(query); //httpClient.executeMethod(hcf, getMethod); httpClient.executeMethod(getMethod); getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler()); int statusCode = httpClient.executeMethod(getMethod); if (statusCode != HttpStatus.SC_OK) { LOG.error("Method failed: " + getMethod.getStatusLine()); } byte[] responseBody = getMethod.getResponseBody(); String response = new String(responseBody, "UTF-8"); LOG.debug("??" + response); JSONObject json = new JSONObject(response); String totalResult = json.getJSONObject("responseData").getJSONObject("cursor") .getString("estimatedResultCount"); int totalResultCount = Integer.parseInt(totalResult); LOG.info("? " + totalResultCount); JSONArray results = json.getJSONObject("responseData").getJSONArray("results"); LOG.debug(" Results:"); for (int i = 0; i < results.length(); i++) { JSONObject result = results.getJSONObject(i); String title = result.getString("titleNoFormatting"); LOG.debug(title); //URL??? String url = result.get("url").toString(); String content = null;//Tools.getHTMLContent(url); if (content == null) { //???? content = result.get("content").toString(); content = content.replaceAll("<b>", ""); content = content.replaceAll("</b>", ""); content = content.replaceAll("\\.\\.\\.", ""); } LOG.debug(content); text.append(title).append(content); } LOG.info("" + question + " " + answer + " MySQL?"); MySQLUtils.saveRewindEvidenceText(question, answer, text.toString()); return text.toString(); } catch (Exception e) { LOG.debug("?", e); } return null; }