List of usage examples for org.apache.commons.httpclient DefaultHttpMethodRetryHandler DefaultHttpMethodRetryHandler
public DefaultHttpMethodRetryHandler(int paramInt, boolean paramBoolean)
From source file:org.blackdog.lyrics.type.LeoLyrics.java
/** return the page containing the result of the search * @param searchArtist true to search for artist * @param artistTokens a list of String representing the critera for artist * @param searchSong true to search for song * @param songTokens a list of String representing the critera for the song * @return the id of the page containing the lyrics of the first candidate or null if not found *//*from w w w . java 2s . c om*/ private String getResultPage(boolean searchArtist, List<String> artistTokens, boolean searchSong, List<String> songTokens) throws HttpException, IOException { String id = null; if ((searchArtist || searchSong) && ((artistTokens == null ? false : artistTokens.size() > 0) || (songTokens == null ? false : songTokens.size() > 0))) { HttpClient client = new HttpClient(); StringBuffer uri = new StringBuffer(); // create uri : http://www.leoslyrics.com/search.php?search=zero+smashing&sartist=1&ssongtitle=1 uri.append(LEO_URL + SEARCH_SUFFIX); boolean tokenAddedForSong = false; if (searchSong && songTokens != null) { for (int i = 0; i < songTokens.size(); i++) { String currentToken = songTokens.get(i); if (currentToken != null) { currentToken = currentToken.trim(); if (currentToken.length() > 0) { if (tokenAddedForSong) { uri.append("+"); } uri.append(currentToken); tokenAddedForSong = true; } } } } if (searchSong && artistTokens != null) { for (int i = 0; i < artistTokens.size(); i++) { String currentToken = artistTokens.get(i); if (currentToken != null) { currentToken = currentToken.trim(); if (currentToken.length() > 0) { if (tokenAddedForSong) { uri.append("+"); } uri.append(currentToken); tokenAddedForSong = true; } } } } if (searchArtist) { uri.append("&"); uri.append("sartist=1"); } if (searchSong) { uri.append("&"); uri.append("ssongtitle=1"); } logger.info("leo lyrics request : " + uri); GetMethod method = new GetMethod(uri.toString()); method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(5, false)); try { int statusCode = client.executeMethod(method); if (statusCode == HttpStatus.SC_OK) { InputStream stream = method.getResponseBodyAsStream(); if (stream != null) { /* search first occurrence of '?hid=' */ String hidString = "?hid="; StringBuffer bufferId = new StringBuffer(); StringBuffer bufferTmp = new StringBuffer(hidString.length()); boolean idFound = false; byte[] buffer = new byte[128]; try { int byteRead = stream.read(buffer); while (byteRead != -1) { if (!idFound) { for (int i = 0; i < byteRead; i++) { byte b = buffer[i]; if (!idFound) { if (bufferTmp.length() >= hidString.length()) { if (b != '"') { bufferId.append((char) b); } else { idFound = true; } } else { if (b == hidString.charAt(bufferTmp.length())) { bufferTmp.append((char) b); } else if (bufferTmp.length() > 0) { bufferTmp.delete(0, bufferTmp.length() - 1); } } } } } byteRead = stream.read(buffer); } } finally { if (stream != null) { try { stream.close(); } catch (IOException e) { logger.warn("unable to close the input stream from request " + uri, e); } } } if (bufferId.length() > 0) { id = bufferId.toString(); } } } else { logger.error("getting error while trying to connect to : " + uri + " (error code=" + statusCode + ")"); } } finally { method.releaseConnection(); } } return id; }
From source file:org.blackdog.lyrics.type.LeoLyrics.java
/** return an html String representing the lyrics content of the page with the given uri * @param uri the uri of the page containing the song lyrics * @return a String or null if failed/*from w w w . j a v a2 s . c om*/ */ private String getLyricsContent(String uri) throws HttpException, IOException { String content = null; if (uri != null) { HttpClient client = new HttpClient(); GetMethod method = new GetMethod(uri); // method.addRequestHeader(Header.) method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(5, false)); try { int statusCode = client.executeMethod(method); if (statusCode == HttpStatus.SC_OK) { String response = method.getResponseBodyAsString(); if (response != null) { /* search first occurrence of 'This song is on the following albums' * * if not found, search <font face="Trebuchet MS, Verdana, Arial" size=-1> * take to next </font> */ String headerString = "This song is on the following albums"; String headerString2 = "<font face=\"Trebuchet MS, Verdana, Arial\" size=-1>"; String paragraphString = "<p>"; String paragraphEndString = "</p>"; StringBuffer bufferContent = new StringBuffer(800); StringBuffer bufferHeaderTmp = new StringBuffer(headerString.length()); StringBuffer bufferParagraphTmp = new StringBuffer(paragraphString.length()); StringBuffer bufferParagraphEndTmp = new StringBuffer(paragraphEndString.length()); boolean headerFound = false; boolean paragraphFound = false; boolean contentFound = false; // for (int i = 0; i < response.length() && !contentFound; i++) { char b = response.charAt(i); /** header found */ if (headerFound) { if (paragraphFound) { bufferContent.append((char) b); if (b == paragraphEndString.charAt(bufferParagraphEndTmp.length())) { bufferParagraphEndTmp.append((char) b); } else if (bufferParagraphEndTmp.length() > 0) { bufferParagraphEndTmp.delete(0, bufferParagraphEndTmp.length() - 1); } /* did we find the end of the paragraph ? */ if (bufferParagraphEndTmp.length() >= paragraphEndString.length()) { contentFound = true; } } else { // System.out.println(" <<"); if (b == paragraphString.charAt(bufferParagraphTmp.length())) { bufferParagraphTmp.append((char) b); } else if (bufferParagraphTmp.length() > 0) { bufferParagraphTmp.delete(0, bufferParagraphTmp.length() - 1); } if (bufferParagraphTmp.length() >= paragraphString.length()) { paragraphFound = true; bufferHeaderTmp.append(headerString); } } } else { // System.out.println(" <<"); if (b == headerString.charAt(bufferHeaderTmp.length())) { bufferHeaderTmp.append((char) b); } else if (bufferHeaderTmp.length() > 0) { bufferHeaderTmp.delete(0, bufferHeaderTmp.length() - 1); } if (bufferHeaderTmp.length() >= headerString.length()) { headerFound = true; } } } if (!contentFound) { int headerString2Index = response.indexOf(headerString2); if (headerString2Index > -1) { String truncatedResponse = response.substring(headerString2Index); int lastEndfont = truncatedResponse.indexOf("</font>"); if (lastEndfont > -1) { bufferContent.append(truncatedResponse.substring(0, lastEndfont)); contentFound = true; } } } if (bufferContent.length() > 0) { bufferContent.insert(0, "<html>"); bufferContent.append("</html>"); try { content = new String(bufferContent.toString().getBytes(), method.getResponseCharSet()); // "UTF-8" } catch (UnsupportedEncodingException e) { logger.warn("unable to encode lyrics content in UTF-8"); content = bufferContent.toString(); } } } } else { logger.error("getting error while trying to connect to : " + uri + " (error code=" + statusCode + ")"); } } finally { method.releaseConnection(); } } return content; }
From source file:org.bonitasoft.console.common.application.ApplicationURLUtils.java
/** * Check if the dedicated application URL is reachable * //from www . j a v a 2 s . c o m * @param anUrlToTest the URL to check * @return true if the URL is reacheable, false otherwise */ private boolean checkURLConnection(final URL anUrlToTest) { final HttpClient client = new HttpClient(); // Create a method instance. final GetMethod method = new GetMethod(anUrlToTest.toString()); // Provide custom retry handler is necessary method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(0, false)); boolean urlReachable = false; try { // Execute the method. final int statusCode = client.executeMethod(method); urlReachable = (statusCode == HttpStatus.SC_OK); } catch (final HttpException e) { if (LOGGER.isLoggable(Level.WARNING)) { LOGGER.warning("Fatal protocol violation: " + e.getMessage()); } } catch (final IOException e) { if (LOGGER.isLoggable(Level.WARNING)) { LOGGER.warning("Fatal transport error: " + e.getMessage()); } } finally { // Release the connection. method.releaseConnection(); } return urlReachable; }
From source file:org.cancergrid.ws.util.HttpContentReader.java
public static String getHttpContent(String httpUrl, String query, Method method) { LOG.debug("getHttpContent(httpUrl): " + httpUrl); LOG.debug("getHttpContent(query): " + query); LOG.debug("getHttpContent(method): " + method); HttpMethod httpMethod = null;// w w w . j a va2s. c o m if (httpUrl.contains("&")) { httpUrl = httpUrl.replace("&", "&"); } if (query != null && query.length() > 0 && query.startsWith("?") && query.contains("&")) { query = query.replace("&", "&"); } try { //LOG.debug("Querying: " + httpUrl); if (method == Method.GET) { httpMethod = new GetMethod(httpUrl); if (query != null && query.length() > 0) { httpMethod.setQueryString(query); } } else if (method == Method.POST) { httpMethod = new PostMethod(httpUrl); if (query != null && query.length() > 0) { RequestEntity entity = new StringRequestEntity(query, "text/xml", "UTF-8"); ((PostMethod) httpMethod).setRequestEntity(entity); } } httpMethod.setFollowRedirects(true); httpMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false)); Protocol.registerProtocol("https", new Protocol("https", new org.apache.commons.httpclient.contrib.ssl.EasySSLProtocolSocketFactory(), 443)); HttpClient client = new HttpClient(); int statusCode = client.executeMethod(httpMethod); if (statusCode != HttpStatus.SC_OK) { LOG.error("Method failed: " + httpMethod.getStatusLine()); LOG.error("Error querying: " + httpMethod.getURI().toString()); throw new Exception("Method failed: " + httpMethod.getStatusLine()); } byte[] responseBody = httpMethod.getResponseBody(); return new String(responseBody, "UTF-8"); } catch (HttpException e) { LOG.error("Fatal protocol violation: " + e.getMessage()); } catch (IOException e) { LOG.error("Fatal transport error: " + e.getMessage()); } catch (Exception e) { LOG.error(e.getMessage()); } finally { httpMethod.releaseConnection(); } return null; }
From source file:org.codelabor.system.remoting.http.services.HttpAdapterServiceImpl.java
public String requestByGetMethod(Map<String, String> parameterMap) throws Exception { String responseBody = null;// ww w . ja va 2 s . c om GetMethod method = null; try { StringBuilder sb = new StringBuilder(); sb.append(url); if (url.indexOf("?") == -1) { sb.append("?"); } Set<String> keySet = parameterMap.keySet(); Iterator<String> iter = keySet.iterator(); String parameterKey = null; while (iter.hasNext()) { parameterKey = iter.next(); sb.append(parameterKey); sb.append("="); sb.append(parameterMap.get(parameterKey)); if (iter.hasNext()) { sb.append("&"); } } String encodedURI = URIUtil.encodeQuery(sb.toString()); logger.debug("encodedURI: {}", encodedURI); method = new GetMethod(encodedURI); HttpParams httpParams = new DefaultHttpParams(); DefaultHttpMethodRetryHandler retryHandler = new DefaultHttpMethodRetryHandler(retry, false); httpParams.setParameter(HttpMethodParams.RETRY_HANDLER, retryHandler); HttpClient httpClient = new HttpClient(new HttpClientParams(httpParams)); int statusCode = httpClient.executeMethod(method); logger.debug("statusCode: {}", statusCode); switch (statusCode) { case HttpStatus.SC_OK: responseBody = method.getResponseBodyAsString(); logger.debug("responseBody: {}", responseBody); break; } } catch (Exception e) { if (logger.isErrorEnabled()) { String messageKey = "error.http.request"; String userMessage = messageSource.getMessage(messageKey, new String[] {}, "default message", Locale.getDefault()); logger.error(userMessage, e); } e.printStackTrace(); throw e; } finally { if (method != null) { method.releaseConnection(); } } return responseBody; }
From source file:org.codelabor.system.services.HttpAdapterServiceImpl.java
public String request(Map<String, String> parameterMap) { StringBuilder stringBuilder = new StringBuilder(); String responseBody = null;/* www . j av a2 s .c om*/ GetMethod method = null; try { stringBuilder.append(url); if (url.indexOf("?") == -1) { stringBuilder.append("?"); } Set<String> keySet = parameterMap.keySet(); Iterator<String> iter = keySet.iterator(); String parameterKey = null; while (iter.hasNext()) { parameterKey = iter.next(); stringBuilder.append(parameterKey); stringBuilder.append("="); stringBuilder.append(parameterMap.get(parameterKey)); if (iter.hasNext()) { stringBuilder.append("&"); } } String encodedURI = URIUtil.encodeQuery(stringBuilder.toString()); if (log.isDebugEnabled()) { log.debug(encodedURI); } method = new GetMethod(encodedURI); HttpParams httpParams = new DefaultHttpParams(); DefaultHttpMethodRetryHandler retryHandler = new DefaultHttpMethodRetryHandler(retry, false); httpParams.setParameter(HttpMethodParams.RETRY_HANDLER, retryHandler); HttpClient httpClient = new HttpClient(new HttpClientParams(httpParams)); int statusCode = httpClient.executeMethod(method); if (log.isDebugEnabled()) { stringBuilder = new StringBuilder(); stringBuilder.append("statusCode: ").append(statusCode); log.debug(stringBuilder.toString()); } switch (statusCode) { case HttpStatus.SC_OK: responseBody = method.getResponseBodyAsString(); break; } } catch (Exception e) { if (log.isErrorEnabled()) { String messageKey = "error.http.request"; String userMessage = messageSource.getMessage(messageKey, new String[] {}, "default message", Locale.getDefault()); log.error(userMessage, e); } } finally { if (method != null) { method.releaseConnection(); } } return responseBody; }
From source file:org.copperengine.monitoring.client.context.ApplicationContext.java
protected void connect(final String serverAddressParam, final String user, final String password) { boolean secureConnect = StringUtils.hasText(user) && StringUtils.hasText(password); String serverAddress = serverAddressParam; if (!serverAddress.endsWith("/")) { serverAddress = serverAddress + "/"; }//from w ww. j a v a 2 s.com final LoginService loginService; final CommonsHttpInvokerRequestExecutor httpInvokerRequestExecutor = new CommonsHttpInvokerRequestExecutor(); DefaultHttpMethodRetryHandler retryHandler = new DefaultHttpMethodRetryHandler(10, false); httpInvokerRequestExecutor.getHttpClient().getParams().setParameter(HttpMethodParams.RETRY_HANDLER, retryHandler); httpInvokerRequestExecutor.getHttpClient().getParams().setSoTimeout(1000 * 60 * 5); { HttpInvokerProxyFactoryBean httpInvokerProxyFactoryBean = new HttpInvokerProxyFactoryBean(); httpInvokerProxyFactoryBean.setServiceUrl(serverAddress + "copperMonitoringService"); httpInvokerProxyFactoryBean.setServiceInterface(LoginService.class); httpInvokerProxyFactoryBean.setServiceUrl(serverAddress + "loginService"); httpInvokerProxyFactoryBean.afterPropertiesSet(); httpInvokerProxyFactoryBean.setHttpInvokerRequestExecutor(httpInvokerRequestExecutor); loginService = (LoginService) httpInvokerProxyFactoryBean.getObject(); } final String sessionId; if (secureConnect) { try { sessionId = loginService.doLogin(user, password); } catch (RemoteException e) { throw new RuntimeException(e); } } else { sessionId = ""; } if (sessionId == null) { getIssueReporterSingleton().reportWarning("Invalid user/password", null, new Runnable() { @Override public void run() { createLoginForm().show(); } }); } else { HttpInvokerProxyFactoryBean httpInvokerProxyFactoryBean = new HttpInvokerProxyFactoryBean(); httpInvokerProxyFactoryBean.setServiceUrl(serverAddress + "copperMonitoringService"); httpInvokerProxyFactoryBean.setServiceInterface(CopperMonitoringService.class); RemoteInvocationFactory remoteInvocationFactory = secureConnect ? new SecureRemoteInvocationFactory(sessionId) : new DefaultRemoteInvocationFactory(); httpInvokerProxyFactoryBean.setRemoteInvocationFactory(remoteInvocationFactory); httpInvokerProxyFactoryBean.setHttpInvokerRequestExecutor(httpInvokerRequestExecutor); httpInvokerProxyFactoryBean.afterPropertiesSet(); final CopperMonitoringService copperMonitoringService = (CopperMonitoringService) httpInvokerProxyFactoryBean .getObject(); final String serverAddressFinal = serverAddress; Platform.runLater(new Runnable() { @Override public void run() { setGuiCopperDataProvider(copperMonitoringService, serverAddressFinal, sessionId); } }); } }
From source file:org.craftercms.cstudio.share.service.impl.TranslationServiceBingImpl.java
/** * fire the bing service/*from w ww. j a va 2s. c o m*/ */ protected String fireBingService(String content, String fromLang, String toLang) { String translatedContent = content; HttpClient client = new HttpClient(); String url = getTranslateServiceUrl(); url = url.replace("{appId}", getApiKey()); url = url.replace("{fl}", fromLang); url = url.replace("{tl}", toLang); url = url.replace("{text}", content); GetMethod method = null; try { method = new GetMethod(url); method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false)); int statusCode = client.executeMethod(method); if (statusCode != HttpStatus.SC_OK) { LOGGER.error("Method failed: " + method.getStatusLine()); } else { byte[] responseBody = method.getResponseBody(); translatedContent = new String(responseBody); translatedContent = translatedContent.replaceAll("\\/", "/"); translatedContent = translatedContent.substring(1, translatedContent.length() - 1); } } catch (HttpException e) { LOGGER.error("Fatal protocol violation: " + e.getMessage(), e); } catch (IOException e) { LOGGER.error("Fatal transport error: " + e.getMessage(), e); } finally { // Release the connection. method.releaseConnection(); } return translatedContent; }
From source file:org.dbpedia.spotlight.sparql.SparqlQueryExecuter.java
public String request(URL url) throws SparqlExecutionException { GetMethod method = new GetMethod(url.toString()); String response = null;/* w w w . j av a 2 s . c o m*/ // Provide custom retry handler is necessary method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false)); try { // Execute the method. int statusCode = client.executeMethod(method); if (statusCode != HttpStatus.SC_OK) { LOG.error("SparqlQuery failed: " + method.getStatusLine()); throw new SparqlExecutionException(String.format("%s (%s). %s", method.getStatusLine(), method.getURI(), method.getResponseBodyAsString())); } // Read the response body. byte[] responseBody = method.getResponseBody(); // Deal with the response. // Use caution: ensure correct character encoding and is not binary data response = new String(responseBody); } catch (HttpException e) { System.err.println("Fatal protocol violation: " + e.getMessage()); throw new SparqlExecutionException(e); } catch (IOException e) { System.err.println("Fatal transport error: " + e.getMessage()); throw new SparqlExecutionException(e); } finally { // Release the connection. method.releaseConnection(); } return response; }
From source file:org.deegree.portal.owswatch.ServiceInvoker.java
/** * @param method/* ww w. j a va 2s .co m*/ * @return The response OGCWebServiceResponseData * @throws OGCWebServiceException */ protected ValidatorResponse executeHttpMethod(HttpMethodBase method) throws OGCWebServiceException { HttpClient client = new HttpClient(); HttpConnectionManagerParams cmParams = client.getHttpConnectionManager().getParams(); cmParams.setConnectionTimeout(serviceConfig.getTimeout() * 1000); client.getHttpConnectionManager().setParams(cmParams); // Provide custom retry handler is necessary method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(2, false)); ValidatorResponse response = null; try { long startTime = System.currentTimeMillis(); int statusCode = client.executeMethod(method); long lapse = System.currentTimeMillis() - startTime; response = serviceConfig.getValidator().validateAnswer(method, statusCode); response.setLastLapse(lapse); Calendar date = Calendar.getInstance(); date.setTimeInMillis(startTime); response.setLastTest(date.getTime()); } catch (Exception e) { throw new OGCWebServiceException(e.getLocalizedMessage()); } finally { method.releaseConnection(); } return response; }