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

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

Introduction

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

Prototype

public abstract Header getResponseHeader(String paramString);

Source Link

Usage

From source file:com.htmlhifive.tools.jslint.engine.download.AbstractDownloadEngineSupport.java

@Override
public EngineInfo getEngineInfo(IProgressMonitor monitor) throws IOException {
    IProgressMonitor actualMonitor = monitor;
    if (monitor == null) {
        actualMonitor = new NullProgressMonitor();
    }//from   ww w. j a va 2s.c  om
    actualMonitor.setTaskName(Messages.T0009.getText());
    HttpClient client = createHttpClient(getEngineSourceUrl());
    HttpMethod getMethod = new GetMethod(getEngineSourceUrl());
    int result = client.executeMethod(getMethod);
    if (result != HttpStatus.SC_OK) {
        // TODO 
        return null;
    }
    StringBuilder licenseSb = new StringBuilder();
    StringBuilder rawSource = new StringBuilder();
    Header header = getMethod.getResponseHeader("Content-Length");
    int content = Integer.valueOf(header.getValue());
    actualMonitor.beginTask(Messages.T0010.getText(), content);
    BufferedReader reader = new BufferedReader(new InputStreamReader(getMethod.getResponseBodyAsStream()));
    String temp = reader.readLine();
    int progress = 0;
    while (!isEndLicenseLine(temp)) {
        progress += temp.length();
        actualMonitor.subTask(Messages.T0011.format(progress, content));
        actualMonitor.worked(temp.length());
        rawSource.append(temp);
        temp = StringUtils.trim(temp);
        temp = StringUtils.substring(temp, 2);
        temp = StringUtils.trim(temp);
        licenseSb.append(temp);
        licenseSb.append(System.getProperty("line.separator"));
        rawSource.append(System.getProperty("line.separator"));
        temp = reader.readLine();
    }
    EngineInfo info = new EngineInfo();
    info.setLicenseStr(licenseSb.toString());

    while ((temp = reader.readLine()) != null) {
        progress += temp.length();
        actualMonitor.subTask(Messages.T0011.format(progress, content));
        actualMonitor.worked(temp.length());
        rawSource.append(temp);
        rawSource.append(System.getProperty("line.separator"));
    }
    info.setMainSource(rawSource.toString());
    monitor.done();
    return info;
}

From source file:com.zimbra.cs.dav.client.WebDavClient.java

protected HttpMethod executeFollowRedirect(DavRequest req) throws IOException {
    HttpMethod method = null;
    boolean done = false;
    while (!done) {
        method = execute(req);// w ww. j av  a  2s  .  c om
        int ret = method.getStatusCode();
        if (ret == HttpStatus.SC_MOVED_PERMANENTLY || ret == HttpStatus.SC_MOVED_TEMPORARILY) {
            Header newLocation = method.getResponseHeader("Location");
            if (newLocation != null) {
                String uri = newLocation.getValue();
                ZimbraLog.dav.debug("redirect to new url = " + uri);
                method.releaseConnection();
                req.setRedirectUrl(uri);
                continue;
            }
        }
        done = true;
    }
    return method;
}

From source file:net.sf.ehcache.constructs.web.filter.GzipFilterTest.java

/**
 * When the servlet container generates a 404 page not found, we want to pass
 * it through without caching and without adding anything to it.
 * <p/>//from   w ww .ja  v a2  s .c  o  m
 * Manual Test: wget -d --server-response --header='Accept-Encoding: gzip'  http://localhost:9080/non_ok/SendRedirectGzip.jsp
 */
public void testRedirect() throws Exception {

    String url = "http://localhost:9080/non_ok/SendRedirectGzip.jsp";
    HttpClient httpClient = new HttpClient();
    HttpMethod httpMethod = new GetMethod(url);
    httpMethod.addRequestHeader("Accept-Encoding", "gzip");
    int responseCode = httpClient.executeMethod(httpMethod);
    //httpclient follows redirects, so gets the home page.
    assertEquals(HttpURLConnection.HTTP_OK, responseCode);
    String responseBody = httpMethod.getResponseBodyAsString();
    assertNotNull(responseBody);
    assertNull(httpMethod.getResponseHeader("Content-Encoding"));
}

From source file:net.sf.ehcache.constructs.web.filter.GzipFilterTest.java

/**
 * When the servlet container forwards to a page does it work?
 * <p/>/*from   ww w .jav a 2  s  . co m*/
 * Manual Test: wget -d --server-response --header='Accept-Encoding: gzip'  http://localhost:9080/non_ok/ForwardFromGzip.jsp
 */
public void testForward() throws Exception {

    String url = "http://localhost:9080/non_ok/ForwardFromGzip.jsp";
    HttpClient httpClient = new HttpClient();
    HttpMethod httpMethod = new GetMethod(url);
    httpMethod.addRequestHeader("Accept-Encoding", "gzip");
    int responseCode = httpClient.executeMethod(httpMethod);
    //httpclient follows redirects, so gets the home page.
    assertEquals(HttpURLConnection.HTTP_OK, responseCode);
    String responseBody = httpMethod.getResponseBodyAsString();
    assertNotNull(responseBody);
    assertEquals("gzip", httpMethod.getResponseHeader("Content-Encoding").getValue());
}

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

/**
 * @param feedUrl//from w w  w.j av  a 2  s. c om
 * @param urlStr
 * @param method
 * @param feed
 * @return
 * @throws MalformedURLException
 */
private SyndFeedInfo buildSyndFeedInfo(URL feedUrl, String urlStr, HttpMethod method, SyndFeed feed,
        int statusCode) throws MalformedURLException {
    SyndFeedInfo syndFeedInfo;
    syndFeedInfo = new SyndFeedInfo();

    // this may be different to feedURL because of 3XX redirects
    syndFeedInfo.setUrl(new URL(urlStr));
    syndFeedInfo.setId(feedUrl.toString());

    Header imHeader = method.getResponseHeader("IM");
    if (imHeader != null && imHeader.getValue().indexOf("feed") >= 0 && isUsingDeltaEncoding()) {
        FeedFetcherCache cache = getFeedInfoCache();
        if (cache != null && statusCode == 226) {
            // client is setup to use http delta encoding and the server supports it and has returned a delta
            // encoded response.
            // This response only includes new items
            SyndFeedInfo cachedInfo = cache.getFeedInfo(feedUrl);
            if (cachedInfo != null) {
                SyndFeed cachedFeed = cachedInfo.getSyndFeed();

                // set the new feed to be the orginal feed plus the new items
                feed = combineFeeds(cachedFeed, feed);
            }
        }
    }

    Header lastModifiedHeader = method.getResponseHeader("Last-Modified");
    if (lastModifiedHeader != null) {
        syndFeedInfo.setLastModified(lastModifiedHeader.getValue());
    }

    Header eTagHeader = method.getResponseHeader("ETag");
    if (eTagHeader != null) {
        syndFeedInfo.setETag(eTagHeader.getValue());
    }

    syndFeedInfo.setSyndFeed(feed);

    return syndFeedInfo;
}

From source file:net.sf.ehcache.constructs.web.filter.GzipFilterTest.java

/**
 * JSPs and Servlets can send bodies when the response is SC_NOT_MODIFIED.
 * In this case there should not be a body but there is. Orion seems to kill the body
 * after is has left the Servlet filter chain. To avoid wget going into an inifinite
 * retry loop, and presumably some other web clients, the content length should be 0
 * and the body 0./*from ww w  . ja  v  a2 s.c  o m*/
 * <p/>
 * Manual test: wget -d --server-response --timestamping --header='If-modified-Since: Fri, 13 May 3006 23:54:18 GMT' --header='Accept-Encoding: gzip' http://localhost:9080/empty_gzip/SC_NO_CONTENT.jsp
 */
public void testNoContentJSPGzipFilter() throws Exception {

    String url = "http://localhost:9080/empty_gzip/SC_NO_CONTENT.jsp";
    HttpClient httpClient = new HttpClient();
    HttpMethod httpMethod = new GetMethod(url);
    httpMethod.addRequestHeader("If-modified-Since", "Fri, 13 May 3006 23:54:18 GMT");
    httpMethod.addRequestHeader("Accept-Encoding", "gzip");
    int responseCode = httpClient.executeMethod(httpMethod);
    assertEquals(HttpURLConnection.HTTP_NO_CONTENT, responseCode);
    byte[] responseBody = httpMethod.getResponseBody();
    assertEquals(null, responseBody);
    assertNull(httpMethod.getResponseHeader("Content-Encoding"));
    assertNotNull(httpMethod.getResponseHeader("Last-Modified").getValue());
    checkNullOrZeroContentLength(httpMethod);
}

From source file:net.sf.ehcache.constructs.web.filter.GzipFilterTest.java

/**
* When the servlet container generates a 404 page not found, we want to pass
* it through without caching and without adding anything to it.
* <p/>/* w  ww .  j  a  v  a2s  . c  o m*/
* Manual Test: wget -d --server-response --header='Accept-Encoding: gzip'  http://localhost:9080/non_ok/PageNotFoundGzip.jsp
*/
public void testNotFound() throws Exception {

    String url = "http://localhost:9080/non_ok/PageNotFoundGzip.jsp";
    HttpClient httpClient = new HttpClient();
    HttpMethod httpMethod = new GetMethod(url);
    httpMethod.addRequestHeader("If-modified-Since", "Fri, 13 May 3006 23:54:18 GMT");
    httpMethod.addRequestHeader("Accept-Encoding", "gzip");
    int responseCode = httpClient.executeMethod(httpMethod);
    assertEquals(HttpURLConnection.HTTP_NOT_FOUND, responseCode);
    String responseBody = httpMethod.getResponseBodyAsString();
    assertNotNull(responseBody);
    assertNull(httpMethod.getResponseHeader("Content-Encoding"));
}

From source file:net.sf.ehcache.constructs.web.filter.GzipFilterTest.java

/**
 * A 0 length body should give a 0 length gzip body and content length
 * <p/>//from  w w w.j av a2  s.  c om
 * Manual test: wget -d --server-response --timestamping --header='If-modified-Since: Fri, 13 May 3006 23:54:18 GMT' --header='Accept-Encoding: gzip' http://localhost:9080/empty_gzip/empty.html
 */
public void testZeroLengthHTML() throws Exception {

    String url = "http://localhost:9080/empty_gzip/empty.html";
    HttpClient httpClient = new HttpClient();
    HttpMethod httpMethod = new GetMethod(url);
    httpMethod.addRequestHeader("If-modified-Since", "Fri, 13 May 3006 23:54:18 GMT");
    httpMethod.addRequestHeader("Accept-Encoding", "gzip");
    int responseCode = httpClient.executeMethod(httpMethod);
    assertEquals(HttpURLConnection.HTTP_NOT_MODIFIED, responseCode);
    byte[] responseBody = httpMethod.getResponseBody();
    assertEquals(null, responseBody);
    assertNull(httpMethod.getResponseHeader("Content-Encoding"));
    checkNullOrZeroContentLength(httpMethod);
}

From source file:net.sf.ehcache.constructs.web.filter.GzipFilterTest.java

/**
 * JSPs and Servlets can send bodies when the response is SC_NOT_MODIFIED.
 * In this case there should not be a body but there is. Orion seems to kill the body
 * after is has left the Servlet filter chain. To avoid wget going into an inifinite
 * retry loop, and presumably some other web clients, the content length should be 0
 * and the body 0./*w w  w  .  j ava  2 s. c  o  m*/
 * <p/>
 * Manual test: wget -d --server-response --header='If-modified-Since: Fri, 13 May 3006 23:54:18 GMT' --header='Accept-Encoding: gzip' http://localhost:9080/empty_gzip/SC_NOT_MODIFIED.jsp
 */
public void testNotModifiedJSPGzipFilter() throws Exception {

    String url = "http://localhost:9080/empty_gzip/SC_NOT_MODIFIED.jsp";
    HttpClient httpClient = new HttpClient();
    HttpMethod httpMethod = new GetMethod(url);
    httpMethod.addRequestHeader("If-modified-Since", "Fri, 13 May 3006 23:54:18 GMT");
    httpMethod.addRequestHeader("Accept-Encoding", "gzip");
    int responseCode = httpClient.executeMethod(httpMethod);
    assertEquals(HttpURLConnection.HTTP_NOT_MODIFIED, responseCode);
    byte[] responseBody = httpMethod.getResponseBody();
    assertEquals(null, responseBody);
    assertNull(httpMethod.getResponseHeader("Content-Encoding"));
    assertNotNull(httpMethod.getResponseHeader("Last-Modified").getValue());
    checkNullOrZeroContentLength(httpMethod);
}

From source file:com.sun.syndication.fetcher.impl.HttpClientFeedFetcher.java

/**
  * @param feedUrl/*from  w  w w. j a  v  a 2 s.  c o  m*/
  * @param urlStr
  * @param method
  * @param feed
  * @return
  * @throws MalformedURLException
  */
private SyndFeedInfo buildSyndFeedInfo(URL feedUrl, String urlStr, HttpMethod method, SyndFeed feed,
        int statusCode) throws MalformedURLException {
    SyndFeedInfo syndFeedInfo;
    syndFeedInfo = new SyndFeedInfo();

    // this may be different to feedURL because of 3XX redirects
    syndFeedInfo.setUrl(new URL(urlStr));
    syndFeedInfo.setId(feedUrl.toString());

    Header imHeader = method.getResponseHeader("IM");
    if (imHeader != null && imHeader.getValue().indexOf("feed") >= 0 && isUsingDeltaEncoding()) {
        FeedFetcherCache cache = getFeedInfoCache();
        if (cache != null && statusCode == 226) {
            // client is setup to use http delta encoding and the server supports it and has returned a delta encoded response
            // This response only includes new items
            SyndFeedInfo cachedInfo = cache.getFeedInfo(feedUrl);
            if (cachedInfo != null) {
                SyndFeed cachedFeed = cachedInfo.getSyndFeed();

                // set the new feed to be the orginal feed plus the new items
                feed = combineFeeds(cachedFeed, feed);
            }
        }
    }

    Header lastModifiedHeader = method.getResponseHeader("Last-Modified");
    if (lastModifiedHeader != null) {
        syndFeedInfo.setLastModified(lastModifiedHeader.getValue());
    }

    Header eTagHeader = method.getResponseHeader("ETag");
    if (eTagHeader != null) {
        syndFeedInfo.setETag(eTagHeader.getValue());
    }

    syndFeedInfo.setSyndFeed(feed);

    return syndFeedInfo;
}