List of usage examples for org.apache.commons.httpclient HttpMethod releaseConnection
public abstract void releaseConnection();
From source file:de.topicmapslab.tmcledit.model.psiprovider.internal.Subj3ctPSIProvider.java
public Set<PSIProviderResult> getSubjectIdentifier() { if (getName().length() == 0) return Collections.emptySet(); HttpMethod method = null; try {//ww w.ja va 2s .co m String url = "http://api.subj3ct.com/subjects/search"; HttpClient client = new HttpClient(); method = new GetMethod(url); ArrayList<NameValuePair> params = new ArrayList<NameValuePair>(2); params.add(new NameValuePair("format", "xml")); params.add(new NameValuePair("query", getName())); method.setQueryString(params.toArray(new NameValuePair[params.size()])); client.getParams().setSoTimeout(5000); client.executeMethod(method); String result = method.getResponseBodyAsString(); SAXParser parser = SAXParserFactory.newInstance().newSAXParser(); Subj3ctXmlHandler handler = new Subj3ctXmlHandler(); parser.parse(new InputSource(new StringReader(result)), handler); List<Subje3ctResult> resultList = handler.getResultList(); if (resultList.size() == 0) { return Collections.emptySet(); } Set<PSIProviderResult> resultSet = new HashSet<PSIProviderResult>(resultList.size()); for (Subje3ctResult r : resultList) { String description = ""; if (r.name != null) description = "Name: " + r.name + "\n"; if (r.description != null) description += "Description: " + r.description + "\n"; description += "\n\nThis service is provided by http://www.subj3ct.com"; resultSet.add(new PSIProviderResult(r.identifier, description)); } return Collections.unmodifiableSet(resultSet); } catch (UnknownHostException e) { // no http connection -> no results TmcleditEditPlugin.logInfo(e); return Collections.emptySet(); } catch (SocketTimeoutException e) { // timeout -> no results TmcleditEditPlugin.logInfo(e); return Collections.emptySet(); } catch (Exception e) { throw new RuntimeException(e); } finally { if (method != null) method.releaseConnection(); } }
From source file:com.sun.syndication.fetcher.impl.HttpClientFeedFetcher.java
/** * @see com.sun.syndication.fetcher.FeedFetcher#retrieveFeed(java.net.URL) *//*from w w w . jav a 2s . c o m*/ public SyndFeed retrieveFeed(URL feedUrl) throws IllegalArgumentException, IOException, FeedException, FetcherException { if (feedUrl == null) { throw new IllegalArgumentException("null is not a valid URL"); } // TODO Fix this //System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.SimpleLog"); HttpClient client = new HttpClient(httpClientParams); if (getCredentialSupplier() != null) { client.getState().setAuthenticationPreemptive(true); // TODO what should realm be here? Credentials credentials = getCredentialSupplier().getCredentials(null, feedUrl.getHost()); if (credentials != null) { client.getState().setCredentials(null, feedUrl.getHost(), credentials); } } System.setProperty("httpclient.useragent", getUserAgent()); String urlStr = feedUrl.toString(); HttpMethod method = new GetMethod(urlStr); method.addRequestHeader("Accept-Encoding", "gzip"); method.addRequestHeader("User-Agent", getUserAgent()); method.setFollowRedirects(true); if (httpClientMethodCallback != null) { synchronized (httpClientMethodCallback) { httpClientMethodCallback.afterHttpClientMethodCreate(method); } } FeedFetcherCache cache = getFeedInfoCache(); if (cache != null) { // retrieve feed try { if (isUsingDeltaEncoding()) { method.setRequestHeader("A-IM", "feed"); } // get the feed info from the cache // Note that syndFeedInfo will be null if it is not in the cache SyndFeedInfo syndFeedInfo = cache.getFeedInfo(feedUrl); if (syndFeedInfo != null) { method.setRequestHeader("If-None-Match", syndFeedInfo.getETag()); if (syndFeedInfo.getLastModified() instanceof String) { method.setRequestHeader("If-Modified-Since", (String) syndFeedInfo.getLastModified()); } } int statusCode = client.executeMethod(method); fireEvent(FetcherEvent.EVENT_TYPE_FEED_POLLED, urlStr); handleErrorCodes(statusCode); SyndFeed feed = getFeed(syndFeedInfo, urlStr, method, statusCode); syndFeedInfo = buildSyndFeedInfo(feedUrl, urlStr, method, feed, statusCode); cache.setFeedInfo(new URL(urlStr), syndFeedInfo); // the feed may have been modified to pick up cached values // (eg - for delta encoding) feed = syndFeedInfo.getSyndFeed(); return feed; } finally { method.releaseConnection(); method.recycle(); } } else { // cache is not in use try { int statusCode = client.executeMethod(method); fireEvent(FetcherEvent.EVENT_TYPE_FEED_POLLED, urlStr); handleErrorCodes(statusCode); return getFeed(null, urlStr, method, statusCode); } finally { method.releaseConnection(); method.recycle(); } } }
From source file:cz.vity.freerapid.plugins.services.rapidshare_premium.RapidShareRunner.java
private void checkFile() throws Exception { Matcher matcher = PlugUtils//from www . j a v a 2s .c o m .matcher("!download(?:%7C|\\|)(?:[^%\\|]+)(?:%7C|\\|)(\\d+)(?:%7C|\\|)([^%\\|]+)", fileURL); if (matcher.find()) { fileURL = "http://rapidshare.com/files/" + matcher.group(1) + "/" + matcher.group(2); httpFile.setNewURL(new URL(fileURL)); } else { matcher = PlugUtils.matcher("/share/([A-Z0-9]+)", fileURL); if (matcher.find()) { HttpMethod method = getMethodBuilder().setReferer(fileURL) .setAction("https://api.rapidshare.com/cgi-bin/rsapi.cgi").setParameter("rsource", "web") .setParameter("sub", "sharelinkcontent").setParameter("share", matcher.group(1)) .setParameter("cbid", "2").setParameter("cbf", "rsapi.system.jsonp.callback") .setParameter("callt", String.valueOf(System.currentTimeMillis())).toGetMethod(); if (!makeRedirectedRequest(method)) { checkFileProblems(); throw new ServiceConnectionProblemException(); } checkFileProblems(); matcher = getMatcherAgainstContent("\"file:(\\d+),([^,]+),"); if (!matcher.find()) { throw new PluginImplementationException("Error getting file ID and file name"); } fileURL = "http://rapidshare.com/files/" + matcher.group(1) + "/" + matcher.group(2); httpFile.setNewURL(new URL(fileURL)); } } matcher = PlugUtils.matcher("/files/(\\d+)/(.+)", fileURL); if (!matcher.find()) { throw new PluginImplementationException("Error parsing file URL"); } final String fileId = matcher.group(1); final String fileName = URLDecoder.decode(matcher.group(2), "UTF-8"); HttpMethod method = getMethodBuilder().setAction("http://api.rapidshare.com/cgi-bin/rsapi.cgi") .setParameter("sub", "checkfiles").setParameter("files", fileId).setParameter("filenames", fileName) .toGetMethod(); int status = 0; String responseString = ""; try { status = client.makeRequest(method, true); responseString = client.getContentAsString(); logger.log(Level.INFO, "Response check:{0}", responseString); } finally { method.abort(); method.releaseConnection(); } if (status == HttpStatus.SC_OK && responseString != null && !responseString.isEmpty()) { String[] response = responseString.split(","); int fileStatus = Integer.parseInt(response[4]); if (fileStatus == 1 || fileStatus == 2 || fileStatus == 6 || fileStatus >= 50) { //http://rs$serverid$shorthost.rapidshare.com/files/$fileid/$filename) finalUrl = String.format("http://rs%s%s.rapidshare.com/files/%s/%s?directstart=1", response[3], response[5], response[0], response[1]); logger.info(finalUrl); httpFile.setFileName(response[1]); httpFile.setFileSize(Long.parseLong(response[2])); httpFile.setFileState(FileState.CHECKED_AND_EXISTING); } if (fileStatus == 0) { throw new URLNotAvailableAnymoreException("File not found"); } if (fileStatus == 4) { throw new URLNotAvailableAnymoreException("File marked as illegal"); } if (fileStatus == 5) { throw new URLNotAvailableAnymoreException( "Anonymous file locked, because it has more than 10 downloads already"); } if (fileStatus == 3) { throw new InvalidURLOrServiceProblemException("Server down"); } } else { throw new ServiceConnectionProblemException("Server return status " + status); } }
From source file:edu.du.penrose.systems.fedora.ResourceIndexUtils.java
/** * //from w ww .j a v a 2 s .c om * Get a list of all objects in the topCollection. The administrator is just used to get host,port,user, and pwd info. * The call to the resource index is actually via httpClient and rest call. * * @see FedoraAppBatchIngestController#getAdministrator() * @param administrator The administrator is just used to get host,port,user,password info. * @param topCollection * @param onlyCollections =true to return only collection objects * * @return all object pids in the topCollection that are also collections */ private static ArrayList<String> getChildren(Administrator administrator, String topCollection, boolean onlyCollections) { final String host = administrator.getHost(); final int port = administrator.getPort(); String userName = administrator.getUserName(); String pwd = administrator.getUserPassword(); String realm = null; // "realm is actually Fedora or Fedora Repository Server" ArrayList<String> children = new ArrayList<String>(); String UTF_8_ENCONDING = "UTF-8"; String type = "tuples"; String flush = "false"; // optional default false; String lang = "itql"; String format = "CSV"; String limit = "100"; // optional default unlimited; NOT USED String distinct = "off"; // optional default off; String stream = "off"; // optional default off; String query = "select $subject from <#ri> where ( $subject <fedora-rels-ext:isMemberOfCollection><info:fedora/" + topCollection + "> )"; // TBD kludge, I don't have time to do things right. String query_2 = "select $subject from <#ri> where ( $subject <fedora-rels-ext:isMemberOf><info:fedora/" + topCollection + "> )"; String queryEncoded; String queryEncoded_2; HttpClient myClient = null; HttpMethod method = null; HttpMethod method_2 = null; try { queryEncoded = URLEncoder.encode(query, UTF_8_ENCONDING); queryEncoded_2 = URLEncoder.encode(query_2, UTF_8_ENCONDING); myClient = new HttpClient(); String finalQuery = "http://" + host + ":" + port + "/fedora/risearch?type=" + type + "&lang=" + lang + "&format=" + format + "&distinct=" + distinct + "&stream=" + stream + "&query=" + queryEncoded; String finalQuery_2 = "http://" + host + ":" + port + "/fedora/risearch?type=" + type + "&lang=" + lang + "&format=" + format + "&distinct=" + distinct + "&stream=" + stream + "&query=" + queryEncoded_2; method = new GetMethod(finalQuery); method_2 = new GetMethod(finalQuery_2); myClient.getState().setCredentials(new AuthScope(host, port, realm), new UsernamePasswordCredentials(userName, pwd)); method.setDoAuthentication(true); myClient.getParams().setAuthenticationPreemptive(true); myClient.executeMethod(method); String response = method.getResponseBodyAsString(); String lines[] = response.split("\n"); for (int i = 0; i < lines.length; i++) { if (!lines[i].contains("islandora") && !lines[i].equalsIgnoreCase("\"subject\"")) { String collectionPid = lines[i].replace("info:fedora/", ""); if (onlyCollections) { if (isCollection(administrator, collectionPid)) { children.add(collectionPid); } } else { children.add(collectionPid); } } } myClient.executeMethod(method_2); String response_2 = method_2.getResponseBodyAsString(); String lines_2[] = response_2.split("\n"); for (int i = 0; i < lines_2.length; i++) { if (!lines_2[i].contains("islandora") && !lines_2[i].equalsIgnoreCase("\"subject\"")) { String collectionPid = lines_2[i].replace("info:fedora/", ""); if (onlyCollections) { if (isCollection(administrator, collectionPid)) { children.add(collectionPid); } } else { children.add(collectionPid); } } } } catch (Exception e) { children.add("Unable to get Collections:" + e); return children; } finally { method.releaseConnection(); method_2.releaseConnection(); } return children; }
From source file:edu.unc.lib.dl.admin.controller.RESTProxyController.java
@RequestMapping(value = { "/services/rest", "/services/rest/*", "/services/rest/**/*" }) public final void proxyAjaxCall(HttpServletRequest request, HttpServletResponse response) throws IOException { log.debug("Prepending service url " + this.servicesUrl + " to " + request.getRequestURI()); String url = request.getRequestURI().replaceFirst(".*/services/rest/?", this.servicesUrl); if (request.getQueryString() != null) url = url + "?" + request.getQueryString(); OutputStreamWriter writer = new OutputStreamWriter(response.getOutputStream()); HttpClient client = new HttpClient(); HttpMethod method = null; try {//from www. j a v a 2 s . c o m log.debug("Proxying ajax request to services REST api via " + request.getMethod()); // Split this according to the type of request if (request.getMethod().equals("GET")) { method = new GetMethod(url); } else if (request.getMethod().equals("POST")) { method = new PostMethod(url); // Set any eventual parameters that came with our original // request (POST params, for instance) Enumeration<String> paramNames = request.getParameterNames(); while (paramNames.hasMoreElements()) { String paramName = paramNames.nextElement(); ((PostMethod) method).setParameter(paramName, request.getParameter(paramName)); } } else { throw new NotImplementedException("This proxy only supports GET and POST methods."); } // Forward the user's groups along with the request method.addRequestHeader(HttpClientUtil.SHIBBOLETH_GROUPS_HEADER, GroupsThreadStore.getGroupString()); method.addRequestHeader("On-Behalf-Of", GroupsThreadStore.getUsername()); // Execute the method client.executeMethod(method); // Set the content type, as it comes from the server Header[] headers = method.getResponseHeaders(); for (Header header : headers) { if ("Content-Type".equalsIgnoreCase(header.getName())) { response.setContentType(header.getValue()); } } try (InputStream responseStream = method.getResponseBodyAsStream()) { int b; while ((b = responseStream.read()) != -1) { response.getOutputStream().write(b); } } response.getOutputStream().flush(); } catch (HttpException e) { writer.write(e.toString()); throw e; } catch (IOException e) { e.printStackTrace(); writer.write(e.toString()); throw e; } finally { if (method != null) method.releaseConnection(); } }
From source file:ensen.controler.AnnotationClient.java
public String request(HttpMethod method) throws AnnotationException { String response = null;//from w ww . j a v a2 s .c om // Provide custom retry handler is necessary method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false)); try { // Execute the method. client.setHttpConnectionManager(new MultiThreadedHttpConnectionManager()); int statusCode = client.executeMethod(method); if (statusCode != HttpStatus.SC_OK) { System.out.println("Method failed: " + method.getStatusLine()); } // Read the response body. InputStream responseBodyStream = method.getResponseBodyAsStream(); //Going to buffer response body of large or unknown size. Using getResponseBodyAsStream instead is recommended. int b = responseBodyStream.read(); ArrayList<Integer> bytes = new ArrayList<Integer>(); while (b != -1) { bytes.add(b); b = responseBodyStream.read(); } byte[] responseBody = new byte[bytes.size()]; for (int i = 0; i < bytes.size(); i++) { responseBody[i] = bytes.get(i).byteValue(); } // Deal with the response. // Use caution: ensure correct character encoding and is not binary data response = new String(responseBody); } catch (HttpException e) { System.out.println("Fatal protocol violation: " + e.getMessage()); try { System.err.println(method.getURI()); } catch (URIException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } throw new AnnotationException("Protocol error executing HTTP request.", e); } catch (IOException e) { System.out.println("Fatal transport error: " + e.getMessage()); System.out.println(method.getQueryString()); throw new AnnotationException("Transport error executing HTTP request.", e); } finally { // Release the connection. method.releaseConnection(); } return response; }
From source file:it.drwolf.ridire.session.CrawlerManager.java
private String getJobStatus_old(String encodedJobName) throws HttpException, IOException, DocumentException { Pattern finishedPattern = Pattern.compile("<h1>Job <i>" + encodedJobName + "</i> \\(0 launches"); HttpMethod method = null; String ret = ""; try {/*w w w .j a v a 2 s .com*/ method = new GetMethod(this.engineUri + "job/" + URLEncoder.encode(encodedJobName, "UTF-8")); // TODO check status int status = this.httpClient.executeMethod(method); String body = method.getResponseBodyAsString(); Matcher mFinished = finishedPattern.matcher(body); boolean finished = false; if (mFinished.find()) { finished = true; } Matcher m = CrawlerManager.pStatus.matcher(body); if (m.find()) { ret = m.group(1).trim(); if (ret.equals("Active")) { ret = m.group(2).substring(1).trim(); if (ret.equalsIgnoreCase("Pausing")) { ret = CrawlStatus.PAUSED.toString(); } } } if (ret.equalsIgnoreCase("Unbuilt") && finished) { ret = CrawlStatus.FINISHED.toString(); } method.releaseConnection(); } finally { if (method != null) { method.releaseConnection(); } } return ret; }
From source file:mitm.common.net.HTTPMethodExecutor.java
protected void internalExecuteMethod(HttpMethod httpMethod, ResponseHandler responseHandler, TaskScheduler watchdog) throws IOException { HttpClient httpClient = createHttpClient(); HttpConnectionManagerParams params = httpClient.getHttpConnectionManager().getParams(); if (connectTimeout > 0) { params.setConnectionTimeout(connectTimeout); }/*from w ww . ja v a2s . co m*/ if (readTimeout > 0) { params.setSoTimeout(readTimeout); } if (proxyInjector != null) { try { proxyInjector.setProxy(httpClient); } catch (ProxyException e) { throw new IOException(e); } } initDefaultSettings(httpMethod); /* * Add last resort watchdog that will interrupt the thread on timeout. we want the abort the HTTP method * first so add 50% to totalTimeout. */ Task threadWatchdogTask = new ThreadInterruptTimeoutTask(Thread.currentThread(), watchdog.getName()); watchdog.addTask(threadWatchdogTask, (long) (totalTimeout * 1.5)); /* * Add watchdog that will abort the HTTPMethod on timeout. */ Task httpMethodAbortTimeoutTask = new HTTPMethodAbortTimeoutTask(httpMethod, watchdog.getName()); watchdog.addTask(httpMethodAbortTimeoutTask, (long) (totalTimeout)); try { logger.debug("Setting up a connection to: " + httpMethod.getURI()); int statusCode = 0; try { statusCode = httpClient.executeMethod(httpMethod); } catch (IllegalArgumentException e) { /* * HttpClient can throw IllegalArgumentException when the host is not set */ throw new IOException(e); } responseHandler.handleResponse(statusCode, httpMethod, watchdog); if (threadWatchdogTask.hasRun() || httpMethodAbortTimeoutTask.hasRun()) { /* * a timeout has occurred. In most cases, a exception was probably already thrown because the * connection was forcefully closed. */ throw new IOException(TIMEOUT_ERROR + httpMethod.getURI()); } } finally { httpMethod.releaseConnection(); } }
From source file:it.drwolf.ridire.session.CrawlerManager.java
@Restrict("#{identity.loggedIn}") public List<String> getJobSeeds(String profileName, String jobName, User currentUser) throws HeritrixException { List<String> seeds = new ArrayList<String>(); HttpMethod method = null; String cxml = "crawler-beans.cxml"; try {/*from w ww. j av a 2 s. c o m*/ this.updateJobsList(currentUser); String filename = null; Job j = this.getPersistedJob(jobName); if (j != null) { if (j.getChildJobName() != null) { filename = j.getChildJobName(); } else { filename = j.getName(); } } else { filename = profileName; cxml = "profile-crawler-beans.cxml"; } if (j != null) { String[] s = j.getSeeds().split("\n"); for (int i = 0; i < s.length; i++) { seeds.add(s[i].trim()); } } else { method = new GetMethod( this.engineUri + "job/" + URLEncoder.encode(filename, "UTF-8") + "/jobdir/" + cxml); int status = this.httpClient.executeMethod(method); SAXReader saxReader = new SAXReader(); Document d = saxReader.read(method.getResponseBodyAsStream()); method.releaseConnection(); Element seedsElement = (Element) d.selectSingleNode( "//*[name()='bean' and @id='longerOverrides']/*[name()='property']/*[name()='props']/*[name()='prop']"); if (seedsElement != null) { String seedsText = seedsElement.getText(); StringTokenizer stringTokenizer = new StringTokenizer(seedsText, "\n"); while (stringTokenizer.hasMoreTokens()) { String t = stringTokenizer.nextToken().trim(); if (!t.startsWith("#")) { seeds.add(t); } } } } } catch (IOException e) { e.printStackTrace(); throw new HeritrixException(); } catch (DocumentException e) { e.printStackTrace(); throw new HeritrixException(); } finally { if (method != null) { method.releaseConnection(); } } return seeds; }
From source file:com.worldline.easycukes.rest.client.RestService.java
/** * Gets the result of the execution of a get request. the attempt will be * repeated until obtain the exepected result * * @param path the path on which the request should be executed * @param expression the result that should be returned by the GET request, which * allows to know if that request is completely processed or not * @throws Exception if something's going wrong... *///from ww w. j a va2s. c o m public void retryGetRequestUntilObtainExpectedResponse(@NonNull String path, @NonNull String expression) throws Exception { String fullpath = path; if (path.startsWith("/")) fullpath = baseUrl + path; log.debug("Sending GET request to " + fullpath + " with several attemps"); final int maxAttempts = Integer.parseInt(ExecutionContext.get(RestConstants.MAX_ATTEMPTS_KEY)); final int timeToWait = Integer.parseInt(ExecutionContext.get(RestConstants.TIME_TO_WAIT_KEY)); final HttpMethod method = new GetMethod(fullpath); try { for (final Map.Entry<String, String> header : requestHeaders.entrySet()) method.setRequestHeader(header.getKey(), header.getValue()); String responseAsString = null; String toCheck = null; String expected = expression; String prop = null; final int idx = expression.indexOf("="); if (idx > 0) { prop = expression.substring(0, idx); expected = expression.substring(idx + 1); } int statusCode; int attempts = 0; boolean success = false; do { // waiting timeToWait seconds Thread.sleep(timeToWait * 1000); statusCode = httpClient.executeMethod(method); attempts++; if (statusCode == HttpStatus.SC_OK) { responseAsString = method.getResponseBodyAsString(); toCheck = responseAsString; if (prop != null) toCheck = JSONHelper.getPropertyValue(responseAsString, prop); if (toCheck.contains(expected)) { success = true; log.debug("The result is available! "); } else log.warn("The result is not yet available! | Waiting " + timeToWait + " seconds ..."); } else log.warn("unsuccessful GET request : " + method.getStatusLine() + " | Waiting " + timeToWait + " seconds ..."); } while (!success && maxAttempts > attempts); response = new ResponseWrapper(responseAsString, statusCode); } catch (final Exception e) { log.error(e.getMessage(), e); throw e; } finally { method.releaseConnection(); } }