Example usage for org.apache.commons.httpclient HttpMethod releaseConnection

List of usage examples for org.apache.commons.httpclient HttpMethod releaseConnection

Introduction

In this page you can find the example usage for org.apache.commons.httpclient HttpMethod releaseConnection.

Prototype

public abstract void releaseConnection();

Source Link

Usage

From source file:fedora.test.api.TestRESTAPI.java

private HttpResponse getOrDelete(String method, boolean authenticate) throws Exception {
    if (url == null || url.length() == 0) {
        throw new IllegalArgumentException("url must be a non-empty value");
    } else if (!(url.startsWith("http://") || url.startsWith("https://"))) {
        url = getBaseURL() + url;//from ww w .  java 2 s  .  c  o  m
    }
    HttpMethod httpMethod = null;
    try {
        if (method.equals("GET")) {
            httpMethod = new GetMethod(url);
        } else if (method.equals("DELETE")) {
            httpMethod = new DeleteMethod(url);
        } else {
            throw new IllegalArgumentException("method must be one of GET or DELETE.");
        }

        httpMethod.setDoAuthentication(authenticate);
        httpMethod.getParams().setParameter("Connection", "Keep-Alive");
        getClient(authenticate).executeMethod(httpMethod);
        return new HttpResponse(httpMethod);
    } finally {
        if (httpMethod != null) {
            httpMethod.releaseConnection();
        }
    }
}

From source file:it.drwolf.ridire.session.CrawlerManager.java

@Restrict("#{s:hasRole('Admin')}")
public String stopCrawlerEngine() throws HeritrixException {
    HttpMethod method = null;
    try {/*from w w w  .ja va 2s  . c o m*/
        String job = this.getJobsArray()[0];
        method = new PostMethod(this.engineUri + "job/" + job + "/script");
        // method.setFollowRedirects(true);
        ((PostMethod) method).addParameter(new NameValuePair("engine", "groovy"));
        ((PostMethod) method).addParameter(new NameValuePair("script", "System.exit(0);"));
        int status = this.httpClient.executeMethod(method);
        method.releaseConnection();
    } catch (HttpException e) {
        e.printStackTrace();
        throw new HeritrixException();
    } catch (IOException e) {
        e.printStackTrace();
        throw new HeritrixException();
    } catch (XPathExpressionException e) {
        e.printStackTrace();
        throw new HeritrixException();
    } catch (SAXException e) {
        e.printStackTrace();
        throw new HeritrixException();
    } finally {
        if (method != null) {
            method.releaseConnection();
        }
    }
    return "OK";
}

From source file:CertStreamCallback.java

private static void invokeActions(String url, String params, byte[] data, Part[] parts, String accept,
        String contentType, String sessionID, String language, String method, Callback callback) {

    if (params != null && !"".equals(params)) {
        if (url.contains("?")) {
            url = url + "&" + params;
        } else {//ww  w.  j a  v a  2 s.  c o  m
            url = url + "?" + params;
        }
    }

    if (language != null && !"".equals(language)) {
        if (url.contains("?")) {
            url = url + "&language=" + language;
        } else {
            url = url + "?language=" + language;
        }
    }

    HttpMethod httpMethod = null;

    try {
        HttpClient httpClient = new HttpClient(new HttpClientParams(), new SimpleHttpConnectionManager(true));
        httpMethod = getHttpMethod(url, method);
        if ((httpMethod instanceof PostMethod) || (httpMethod instanceof PutMethod)) {
            if (null != data)
                ((EntityEnclosingMethod) httpMethod).setRequestEntity(new ByteArrayRequestEntity(data));
            if (httpMethod instanceof PostMethod && null != parts)
                ((PostMethod) httpMethod)
                        .setRequestEntity(new MultipartRequestEntity(parts, httpMethod.getParams()));
        }

        if (sessionID != null) {
            httpMethod.addRequestHeader("Cookie", "JSESSIONID=" + sessionID);
        }

        if (!(httpMethod instanceof PostMethod && null != parts)) {
            if (null != accept && !"".equals(accept))
                httpMethod.addRequestHeader("Accept", accept);
            if (null != contentType && !"".equals(contentType))
                httpMethod.addRequestHeader("Content-Type", contentType);
        }

        int statusCode = httpClient.executeMethod(httpMethod);
        if (statusCode != HttpStatus.SC_OK) {
            throw ActionException.create(ClientMessages.CON_ERROR1, httpMethod.getStatusLine());
        }

        contentType = null != httpMethod.getResponseHeader("Content-Type")
                ? httpMethod.getResponseHeader("Content-Type").getValue()
                : accept;

        InputStream o = httpMethod.getResponseBodyAsStream();
        if (callback != null) {
            callback.execute(o, contentType, sessionID);
        }

    } catch (Exception e) {
        String result = BizClientUtils.doError(e, contentType);
        ByteArrayInputStream in = null;
        try {
            in = new ByteArrayInputStream(result.getBytes("UTF-8"));
            if (callback != null) {
                callback.execute(in, contentType, null);
            }

        } catch (UnsupportedEncodingException e1) {
            throw new RuntimeException(e1.getMessage() + "", e1);
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e1) {
                }
            }
        }
    } finally {
        // 
        if (httpMethod != null) {
            httpMethod.releaseConnection();
        }
    }
}

From source file:it.drwolf.ridire.session.CrawlerManager.java

@Restrict("#{s:hasRole('Crawler User')}")
public void pauseJob(String jobName, User currentUser) throws HeritrixException {
    this.updateJobsList(currentUser);
    HttpMethod method = null;
    try {/*from w  ww  .jav  a2s .c om*/
        Job j = this.getPersistedJob(jobName);
        if (this.getJobStatus(j.getName()).equals(CrawlStatus.RUNNING.toString())) {
            method = new PostMethod(this.engineUri + "job/" + URLEncoder.encode(jobName, "UTF-8"));
            ((PostMethod) method).addParameter(new NameValuePair("action", "pause"));
            // TODO check status
            int status = this.httpClient.executeMethod(method);
            method.releaseConnection();
            j.setJobStage(CrawlStatus.PAUSED.toString());
            this.entityManager.merge(j);
            this.updateJobsList(currentUser);
        }
    } catch (IOException e) {
        e.printStackTrace();
        throw new HeritrixException();
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
        throw new HeritrixException();
    } catch (DocumentException e) {
        e.printStackTrace();
        throw new HeritrixException();
    } finally {
        if (method != null) {
            method.releaseConnection();
        }
    }
}

From source file:it.drwolf.ridire.session.CrawlerManager.java

@Restrict("#{s:hasRole('Crawler User')}")
public void resumeJob(String jobName, User currentUser) throws HeritrixException {
    this.updateJobsList(currentUser);
    HttpMethod method = null;
    try {/*from ww w  .  ja  va  2 s  . com*/
        Job j = this.getPersistedJob(jobName);
        if (this.getJobStatus(j.getName()).equals(CrawlStatus.PAUSED.toString())) {
            method = new PostMethod(this.engineUri + "job/" + URLEncoder.encode(jobName, "UTF-8"));
            ((PostMethod) method).addParameter(new NameValuePair("action", "unpause"));
            // TODO check status
            int status = this.httpClient.executeMethod(method);
            method.releaseConnection();
            j.setJobStage(CrawlStatus.RUNNING.toString());
            this.entityManager.merge(j);
            this.updateJobsList(currentUser);
        }
    } catch (IOException e) {
        e.printStackTrace();
        throw new HeritrixException();
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
        throw new HeritrixException();
    } catch (DocumentException e) {
        e.printStackTrace();
        throw new HeritrixException();
    } finally {
        if (method != null) {
            method.releaseConnection();
        }
    }
}

From source file:com.xpn.xwiki.plugin.feed.XWikiFeedFetcher.java

/**
 * @see com.sun.syndication.fetcher.FeedFetcher#retrieveFeed(java.net.URL)
 *//* w  w  w.ja  v  a 2s.  com*/
public SyndFeed retrieveFeed(URL feedUrl, int timeout)
        throws IllegalArgumentException, IOException, FeedException, FetcherException {
    if (feedUrl == null) {
        throw new IllegalArgumentException("null is not a valid URL");
    }
    HttpClient client = new HttpClient();
    if (timeout != 0) {
        client.getParams().setSoTimeout(timeout);
        client.getParams().setParameter("http.connection.timeout", new Integer(timeout));
    }

    System.setProperty("http.useragent", getUserAgent());
    client.getParams().setParameter("httpclient.useragent", getUserAgent());

    String proxyHost = System.getProperty("http.proxyHost");
    String proxyPort = System.getProperty("http.proxyPort");
    if ((proxyHost != null) && (!proxyHost.equals(""))) {
        int port = 3128;
        if ((proxyPort != null) && (!proxyPort.equals(""))) {
            port = Integer.parseInt(proxyPort);
        }
        client.getHostConfiguration().setProxy(proxyHost, port);
    }

    String proxyUser = System.getProperty("http.proxyUser");
    if ((proxyUser != null) && (!proxyUser.equals(""))) {
        String proxyPassword = System.getProperty("http.proxyPassword");
        Credentials defaultcreds = new UsernamePasswordCredentials(proxyUser, proxyPassword);
        client.getState().setProxyCredentials(AuthScope.ANY, defaultcreds);
    }

    String urlStr = feedUrl.toString();
    FeedFetcherCache cache = getFeedInfoCache();
    if (cache != null) {
        // retrieve feed
        HttpMethod method = new GetMethod(urlStr);
        method.addRequestHeader("Accept-Encoding", "gzip");
        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());
                }
            }

            method.setFollowRedirects(true);

            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();
        }
    } else {
        // cache is not in use
        HttpMethod method = new GetMethod(urlStr);
        try {
            method.setFollowRedirects(true);

            int statusCode = client.executeMethod(method);
            fireEvent(FetcherEvent.EVENT_TYPE_FEED_POLLED, urlStr);
            handleErrorCodes(statusCode);

            return getFeed(null, urlStr, method, statusCode);
        } finally {
            method.releaseConnection();
        }
    }
}

From source file:it.drwolf.ridire.session.CrawlerManager.java

@Restrict("#{s:hasRole('Crawler User')}")
public void startJob(String jobName, User currentUser) throws HeritrixException {
    this.updateJobsList(currentUser);
    HttpMethod method = null;
    try {//from  w  w w .j  a  v  a 2 s. c  o  m
        Matcher m = CrawlerManager.childJobPattern.matcher(jobName);
        method = new PostMethod(this.engineUri + "job/" + URLEncoder.encode(jobName, "UTF-8"));
        ((PostMethod) method).addParameter(new NameValuePair("action", "build"));
        // TODO check status
        int status = this.httpClient.executeMethod(method);
        Thread.sleep(3000);
        method = new PostMethod(this.engineUri + "job/" + URLEncoder.encode(jobName, "UTF-8"));
        method.releaseConnection();
        ((PostMethod) method).addParameter(new NameValuePair("action", "launch"));
        // TODO check status
        status = this.httpClient.executeMethod(method);
        method.releaseConnection();
        this.updateJobsList(currentUser);
    } catch (IOException e) {
        e.printStackTrace();
        throw new HeritrixException();
    } catch (InterruptedException e) {
        e.printStackTrace();
        throw new HeritrixException();
    } finally {
        if (method != null) {
            method.releaseConnection();
        }
    }
}

From source file:com.panet.imeta.cluster.SlaveServer.java

public String execService(String service) throws Exception {
    // Prepare HTTP get
    // /*from  w  w  w  .j ava 2  s. c  o m*/
    HttpClient client = new HttpClient();
    addCredentials(client);
    HttpMethod method = new GetMethod(constructUrl(service));

    // Execute request
    // 
    try {
        int result = client.executeMethod(method);

        // The status code
        log.logDebug(toString(),
                Messages.getString("SlaveServer.DEBUG_ResponseStatus", Integer.toString(result))); //$NON-NLS-1$

        // the response
        InputStream inputStream = new BufferedInputStream(method.getResponseBodyAsStream());

        StringBuffer bodyBuffer = new StringBuffer();
        int c;
        while ((c = inputStream.read()) != -1) {
            bodyBuffer.append((char) c);
        }
        inputStream.close();

        String body = bodyBuffer.toString();

        log.logDetailed(toString(), Messages.getString("SlaveServer.DETAILED_FinishedReading", //$NON-NLS-1$
                Integer.toString(bodyBuffer.length())));
        log.logDebug(toString(), Messages.getString("SlaveServer.DEBUG_ResponseBody", body)); //$NON-NLS-1$

        return body;
    } finally {
        // Release current connection to the connection pool once you are done
        method.releaseConnection();
        log.logDetailed(toString(),
                Messages.getString("SlaveServer.DETAILED_ExecutedService", service, hostname)); //$NON-NLS-1$
    }

}

From source file:com.discogs.api.webservice.impl.HttpClientWebService.java

@Override
public Resp doGet(String url) throws WebServiceException {
    HttpMethod method = new GZipCapableGetMethod(url);
    method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
            new DefaultHttpMethodRetryHandler(3, false));
    method.setDoAuthentication(true);//from w ww . j  a  va  2s .c om

    try {
        // execute the method
        int statusCode = this.httpClient.executeMethod(method);

        if (logger.isDebugEnabled()) {
            logger.debug(method.getResponseBodyAsString());
        }

        switch (statusCode) {
        case HttpStatus.SC_OK:
            return createResp(method.getResponseBodyAsStream());

        case HttpStatus.SC_NOT_FOUND:
            throw new ResourceNotFoundException("Resource not found.", method.getResponseBodyAsString());

        case HttpStatus.SC_BAD_REQUEST:
            throw new RequestException(method.getResponseBodyAsString());

        case HttpStatus.SC_FORBIDDEN:
            throw new AuthorizationException(method.getResponseBodyAsString());

        case HttpStatus.SC_UNAUTHORIZED:
            throw new AuthorizationException(method.getResponseBodyAsString());

        default:
            String em = "web service returned unknown status '" + statusCode + "', response was: "
                    + method.getResponseBodyAsString();
            logger.error(em);
            throw new WebServiceException(em);
        }
    } catch (HttpException e) {
        logger.error("Fatal protocol violation: " + e.getMessage());
        throw new WebServiceException(e.getMessage(), e);
    } catch (IOException e) {
        logger.error("Fatal transport error: " + e.getMessage());
        throw new WebServiceException(e.getMessage(), e);
    } finally {
        method.releaseConnection();
    }
}

From source file:it.drwolf.ridire.session.CrawlerManager.java

@Restrict("#{s:hasRole('Crawler User')}")
public String deleteJob(String jobName, String jobStatus, User currentUser) throws HeritrixException {
    this.updateJobsList(currentUser);
    Job j = this.getPersistedJob(jobName);
    String childJobName = j.getChildJobName();
    int lastChildIndex = 0;
    if (childJobName != null && childJobName.length() > 0) {
        lastChildIndex = Integer.parseInt(childJobName.substring(childJobName.indexOf("__") + 2));
    }/*w  ww  .ja  va 2 s.  c o m*/
    HttpMethod method = null;
    String jobsDir = this.entityManager.find(Parameter.class, Parameter.JOBS_DIR.getKey()).getValue();
    try {
        if (lastChildIndex > 0) {
            for (int i = lastChildIndex; i > 0; i--) {
                method = new PostMethod(
                        this.engineUri + "job/" + URLEncoder.encode(jobName, "UTF-8") + "__" + i);
                ((PostMethod) method).setParameter("action", "teardown");
                this.httpClient.executeMethod(method);
                method.releaseConnection();
                FileUtils.deleteDirectory(
                        new File(jobsDir + CrawlerManager.FILE_SEPARATOR + jobName + "__" + i));
                Job childJob = this.getPersistedJob(jobName + "__" + i);
                this.entityManager.remove(childJob);
            }
        }

        method = new PostMethod(this.engineUri + "job/" + URLEncoder.encode(jobName, "UTF-8"));
        ((PostMethod) method).setParameter("action", "teardown");
        this.httpClient.executeMethod(method);
        method.releaseConnection();
        FileUtils.deleteDirectory(new File(jobsDir + CrawlerManager.FILE_SEPARATOR + jobName));

        if (j != null) {
            this.entityManager.createQuery("delete from CrawledResource cr where cr.job.id=:id")
                    .setParameter("id", j.getId()).executeUpdate();
            ScheduledJobHandle sjh = j.getScheduledJobHandle();
            if (sjh != null) {
                sjh.setJob(null);
                this.entityManager.remove(sjh);
            }
            this.entityManager.remove(j);
        }
    } catch (HttpException e) {
        e.printStackTrace();
        throw new HeritrixException();
    } catch (IOException e) {
        e.printStackTrace();
        throw new HeritrixException();
    } finally {
        if (method != null) {
            method.releaseConnection();
        }
    }
    return "ok";
}