Example usage for org.apache.commons.httpclient HttpStatus SC_NOT_FOUND

List of usage examples for org.apache.commons.httpclient HttpStatus SC_NOT_FOUND

Introduction

In this page you can find the example usage for org.apache.commons.httpclient HttpStatus SC_NOT_FOUND.

Prototype

int SC_NOT_FOUND

To view the source code for org.apache.commons.httpclient HttpStatus SC_NOT_FOUND.

Click Source Link

Document

<tt>404 Not Found</tt> (HTTP/1.0 - RFC 1945)

Usage

From source file:org.opencastproject.adminui.endpoint.ThemesEndpointTest.java

@Test
public void testGetTheme() throws ParseException {
    // Test invalid id
    given().pathParam("themeId", "asdasd").expect().statusCode(HttpStatus.SC_NOT_FOUND).when()
            .get(rt.host("/{themeId}.json")).asString();
    // Test unknown id
    given().pathParam("themeId", notFoundId).expect().statusCode(HttpStatus.SC_NOT_FOUND).when()
            .get(rt.host("/{themeId}.json")).asString();
    // Test correct id
    String result = given().pathParam("themeId", foundId).expect().statusCode(HttpStatus.SC_OK).when()
            .get(rt.host("/{themeId}.json")).asString();
    JSONObject theme = ((JSONObject) parser.parse(result));
    assertEquals(foundId.toString(), theme.get("id").toString());
    assertEquals("test.mp4", theme.get("bumperFileName").toString());
    assertEquals("http://localhost:8080/staticfiles/uuid1", theme.get("bumperFileUrl").toString());
}

From source file:org.opencastproject.adminui.endpoint.ThemesEndpointTest.java

@Test
public void testGetThemeUsage() throws ParseException {
    // Test invalid id
    given().pathParam("themeId", "asdasd").expect().statusCode(HttpStatus.SC_NOT_FOUND).when()
            .get(rt.host("/{themeId}/usage.json")).asString();
    // Test unknown id
    given().pathParam("themeId", notFoundId).expect().statusCode(HttpStatus.SC_NOT_FOUND).when()
            .get(rt.host("/{themeId}/usage.json")).asString();
    // Test correct id
    String result = given().pathParam("themeId", foundId).expect().statusCode(HttpStatus.SC_OK).when()
            .get(rt.host("/{themeId}/usage.json")).asString();
    JSONObject series = ((JSONObject) parser.parse(result));
    JSONArray seriesArr = (JSONArray) series.get("series");
    Assert.assertEquals(3, seriesArr.size());
    JSONObject series1 = (JSONObject) seriesArr.get(0);
    Assert.assertEquals("Series1Id", series1.get("id").toString());
    Assert.assertEquals("Series 1 Title", series1.get("title").toString());
}

From source file:org.opencastproject.adminui.endpoint.ThemesEndpointTest.java

@Test
public void testDeleteThemes() {
    // Test invalid id
    given().pathParam("themeId", "asdasd").expect().statusCode(HttpStatus.SC_NOT_FOUND).when()
            .delete(rt.host("/{themeId}")).asString();
    // Test unknown id
    given().pathParam("themeId", notFoundId).expect().statusCode(HttpStatus.SC_NOT_FOUND).when()
            .delete(rt.host("/{themeId}")).asString();
    // Test correct id
    given().pathParam("themeId", foundId).expect().statusCode(HttpStatus.SC_NO_CONTENT).when()
            .delete(rt.host("/{themeId}")).asString();
}

From source file:org.opens.tanaguru.util.http.HttpRequestHandler.java

public int getHttpStatus(String url) {
    String encodedUrl = getEncodedUrl(url);
    HttpClient httpClient = getHttpClient(encodedUrl);
    HeadMethod head = new HeadMethod(encodedUrl);
    try {/*  www  .  ja v  a2  s  . co  m*/
        LOGGER.debug("executing head request to retrieve page status on " + head.getURI());
        int status = httpClient.executeMethod(head);
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("received " + status + " from head request");
            for (Header h : head.getResponseHeaders()) {
                LOGGER.debug("header : " + h.toExternalForm());
            }
        }

        return status;
    } catch (UnknownHostException uhe) {
        LOGGER.warn("UnknownHostException on " + encodedUrl);
        return HttpStatus.SC_NOT_FOUND;
    } catch (IllegalArgumentException iae) {
        LOGGER.warn("IllegalArgumentException on " + encodedUrl);
        return HttpStatus.SC_NOT_FOUND;
    } catch (IOException ioe) {
        LOGGER.warn("IOException on " + encodedUrl);
        ioe.fillInStackTrace();
        return HttpStatus.SC_NOT_FOUND;
    } finally {
        // When HttpClient instance is no longer needed,
        // shut down the connection manager to ensure
        // immediate deallocation of all system resources
        head.releaseConnection();
    }
}

From source file:org.opens.tanaguru.util.http.HttpRequestHandler.java

public int getHttpStatusFromGet(String url) {
    String encodedUrl = getEncodedUrl(url);
    HttpClient httpClient = getHttpClient(encodedUrl);
    GetMethod get = new GetMethod(encodedUrl);
    try {//from www  .ja  v  a 2s.  c  om
        LOGGER.debug("executing get request to retrieve status on " + get.getURI());
        int status = httpClient.executeMethod(get);
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("received " + status + " from get request");
            for (Header h : get.getResponseHeaders()) {
                LOGGER.debug("header : " + h.toExternalForm());
            }
        }
        return status;
    } catch (UnknownHostException uhe) {
        LOGGER.warn("UnknownHostException on " + encodedUrl);
        return HttpStatus.SC_NOT_FOUND;
    } catch (IOException ioe) {
        LOGGER.warn("IOException on " + encodedUrl);
        return HttpStatus.SC_NOT_FOUND;
    } finally {
        // When HttpClient instance is no longer needed,
        // shut down the connection manager to ensure
        // immediate deallocation of all system resources
        get.releaseConnection();
    }
}

From source file:org.opens.tanaguru.util.http.HttpRequestHandler.java

private int computeStatus(int status) {
    switch (status) {
    case HttpStatus.SC_FORBIDDEN:
    case HttpStatus.SC_METHOD_NOT_ALLOWED:
    case HttpStatus.SC_BAD_REQUEST:
    case HttpStatus.SC_UNAUTHORIZED:
    case HttpStatus.SC_PAYMENT_REQUIRED:
    case HttpStatus.SC_NOT_FOUND:
    case HttpStatus.SC_NOT_ACCEPTABLE:
    case HttpStatus.SC_PROXY_AUTHENTICATION_REQUIRED:
    case HttpStatus.SC_REQUEST_TIMEOUT:
    case HttpStatus.SC_CONFLICT:
    case HttpStatus.SC_GONE:
    case HttpStatus.SC_LENGTH_REQUIRED:
    case HttpStatus.SC_PRECONDITION_FAILED:
    case HttpStatus.SC_REQUEST_TOO_LONG:
    case HttpStatus.SC_REQUEST_URI_TOO_LONG:
    case HttpStatus.SC_UNSUPPORTED_MEDIA_TYPE:
    case HttpStatus.SC_REQUESTED_RANGE_NOT_SATISFIABLE:
    case HttpStatus.SC_EXPECTATION_FAILED:
    case HttpStatus.SC_INSUFFICIENT_SPACE_ON_RESOURCE:
    case HttpStatus.SC_METHOD_FAILURE:
    case HttpStatus.SC_UNPROCESSABLE_ENTITY:
    case HttpStatus.SC_LOCKED:
    case HttpStatus.SC_FAILED_DEPENDENCY:
    case HttpStatus.SC_INTERNAL_SERVER_ERROR:
    case HttpStatus.SC_NOT_IMPLEMENTED:
    case HttpStatus.SC_BAD_GATEWAY:
    case HttpStatus.SC_SERVICE_UNAVAILABLE:
    case HttpStatus.SC_GATEWAY_TIMEOUT:
    case HttpStatus.SC_HTTP_VERSION_NOT_SUPPORTED:
    case HttpStatus.SC_INSUFFICIENT_STORAGE:
        return 0;
    case HttpStatus.SC_CONTINUE:
    case HttpStatus.SC_SWITCHING_PROTOCOLS:
    case HttpStatus.SC_PROCESSING:
    case HttpStatus.SC_OK:
    case HttpStatus.SC_CREATED:
    case HttpStatus.SC_ACCEPTED:
    case HttpStatus.SC_NON_AUTHORITATIVE_INFORMATION:
    case HttpStatus.SC_NO_CONTENT:
    case HttpStatus.SC_RESET_CONTENT:
    case HttpStatus.SC_PARTIAL_CONTENT:
    case HttpStatus.SC_MULTI_STATUS:
    case HttpStatus.SC_MULTIPLE_CHOICES:
    case HttpStatus.SC_MOVED_PERMANENTLY:
    case HttpStatus.SC_MOVED_TEMPORARILY:
    case HttpStatus.SC_SEE_OTHER:
    case HttpStatus.SC_NOT_MODIFIED:
    case HttpStatus.SC_USE_PROXY:
    case HttpStatus.SC_TEMPORARY_REDIRECT:
        return 1;
    default://  w  ww  . j ava  2  s.co  m
        return 1;
    }
}

From source file:org.paxle.crawler.http.impl.HttpCrawler.java

public ICrawlerDocument request(URI requestUri) {
    if (requestUri == null)
        throw new NullPointerException("URL was null");
    this.logger.debug(String.format("Crawling URL '%s' ...", requestUri));

    ICrawlerDocument doc = null;/*from  ww  w  . jav  a  2s. c o  m*/
    HttpMethod method = null;
    try {
        final ICrawlerContext ctx = this.contextLocal.getCurrentContext();

        // creating an empty crawler-document
        doc = ctx.createDocument();
        doc.setLocation(requestUri);

        final String uriAsciiString = requestUri.toASCIIString();

        /* ==============================================================================
         * HTTP HEAD request
         * 
         * first use the HEAD method to determine whether the MIME-type is supported
         * and to compare the content-length with the maximum allowed download size
         * (both only if the server provides this information, if not, the file is
         * fetched)
         * ============================================================================== */
        method = new HeadMethod(uriAsciiString); // automatically follows redirects
        this.initRequestMethod(method);
        int statusCode = this.getHttpClient().executeMethod(method);

        final boolean headUnsupported = (statusCode == HttpStatus.SC_METHOD_FAILURE
                || statusCode == HttpStatus.SC_METHOD_NOT_ALLOWED);
        if (!headUnsupported) {
            if (statusCode != HttpStatus.SC_OK) {
                // RFC 2616 states that the GET and HEAD methods _must_ be supported by any
                // general purpose servers (which are in fact the ones we are connecting to here)

                if (statusCode == HttpStatus.SC_NOT_FOUND) {
                    doc.setStatus(ICrawlerDocument.Status.NOT_FOUND);
                } else {
                    doc.setStatus(ICrawlerDocument.Status.UNKNOWN_FAILURE,
                            String.format("Server returned: %s", method.getStatusLine()));
                }

                this.logger.warn(String.format("Crawling of URL '%s' failed. Server returned: %s", requestUri,
                        method.getStatusLine()));
                return doc;
            }

            // getting the mimetype and charset
            Header contentTypeHeader = method.getResponseHeader(HTTPHEADER_CONTENT_TYPE);
            if (!handleContentTypeHeader(contentTypeHeader, doc))
                return doc;

            // reject the document if content-length is above our limit
            Header contentLengthHeader = method.getResponseHeader(HTTPHEADER_CONTENT_LENGTH);
            if (!handleContentLengthHeader(contentLengthHeader, doc))
                return doc;

            // FIXME: we've been redirected, re-enqueue the new URL and abort processing
            //if (!requestUri.equals(method.getURI())) ;            
        }

        /* ==============================================================================
         * HTTP GET request
         * 
         * secondly - if everything is alright up to now - proceed with getting the 
         * actual document
         * ============================================================================== */
        HttpMethod getMethod = new GetMethod(uriAsciiString); // automatically follows redirects
        method.releaseConnection();

        method = getMethod;
        this.initRequestMethod(method);

        // send the request to the server
        statusCode = this.getHttpClient().executeMethod(method);

        // check the response status code
        if (statusCode != HttpStatus.SC_OK) {
            if (statusCode == HttpStatus.SC_NOT_FOUND) {
                doc.setStatus(ICrawlerDocument.Status.NOT_FOUND);
            } else {
                doc.setStatus(ICrawlerDocument.Status.UNKNOWN_FAILURE,
                        String.format("Server returned: %s", method.getStatusLine()));
            }

            this.logger.warn(String.format("Crawling of URL '%s' failed. Server returned: %s", requestUri,
                    method.getStatusLine()));
            return doc;
        }

        // FIXME: we've been redirected, re-enqueue the new URL and abort processing
        // if (!requestUri.equals(method.getURI())) ; 

        /*
         * HTTP Content-Type
         * - getting the mimetype and charset
         */
        Header contentTypeHeader = method.getResponseHeader(HTTPHEADER_CONTENT_TYPE);
        if (!handleContentTypeHeader(contentTypeHeader, doc))
            return doc;

        /* 
         * HTTP Content-Length
         * - Reject the document if content-length is above our limit
         * 
         *   We do this a second time here because some servers may have set the content-length
         *   of the head response to <code>0</code>
         */
        Header contentLengthHeader = method.getResponseHeader(HTTPHEADER_CONTENT_LENGTH);
        if (!handleContentLengthHeader(contentLengthHeader, doc))
            return doc;

        extractHttpHeaders(method, doc); // externalised into this method to cleanup here a bit

        // getting the response body
        InputStream respBody = method.getResponseBodyAsStream();

        // handle the content-encoding, i.e. decompress the server's response
        Header contentEncodingHeader = method.getResponseHeader(HTTPHEADER_CONTENT_ENCODING);
        try {
            respBody = handleContentEncoding(contentEncodingHeader, respBody);

            /* Limit the max allowed length of the content to copy. -1 is used for no limit.
             * 
             * We need to set a limit if:
             * a) the user has configured a max-download-size AND
             * b) the server returned no content-length header
             */
            int copyLimit = (this.maxDownloadSize <= 0 || contentLengthHeader != null) ? -1
                    : this.maxDownloadSize;

            // copy the content to file
            final ICrawlerTools crawlerTools = ctx.getCrawlerTools();
            crawlerTools.saveInto(doc, respBody, lrc, copyLimit);

            doc.setStatus(ICrawlerDocument.Status.OK);
            this.logger.debug(String.format("Crawling of URL '%s' finished.", requestUri));
        } catch (IOException e) {
            String msg = e.getMessage();
            if (msg == null || !msg.equals("Corrupt GZIP trailer"))
                throw e;

            setHostSetting(method.getURI().getHost(), PREF_NO_ENCODING);
            msg = String.format("server sent a corrupt gzip trailer at URL '%s'", requestUri);
            logger.warn(msg);

            // FIXME re-enqueue command
            doc.setStatus(ICrawlerDocument.Status.UNKNOWN_FAILURE, msg);
        } finally {
            respBody.close();
        }
    } catch (NoRouteToHostException e) {
        this.logger.warn(String.format("Error crawling %s: %s", requestUri, e.getMessage()));
        doc.setStatus(ICrawlerDocument.Status.NOT_FOUND, e.getMessage());
    } catch (UnknownHostException e) {
        this.logger.warn(String.format("Error crawling %s: Unknown host.", requestUri));
        doc.setStatus(ICrawlerDocument.Status.NOT_FOUND, e.getMessage());
    } catch (ConnectException e) {
        this.logger.warn(String.format("Error crawling %s: Unable to connect to host.", requestUri));
        doc.setStatus(ICrawlerDocument.Status.NOT_FOUND, e.getMessage());
    } catch (ConnectTimeoutException e) {
        this.logger.warn(String.format("Error crawling %s: %s.", requestUri, e.getMessage()));
        doc.setStatus(ICrawlerDocument.Status.NOT_FOUND, e.getMessage());
    } catch (SocketTimeoutException e) {
        this.logger.warn(String.format("Error crawling %s: Connection timeout.", requestUri));
        doc.setStatus(ICrawlerDocument.Status.NOT_FOUND, e.getMessage());
    } catch (CircularRedirectException e) {
        this.logger.warn(String.format("Error crawling %s: %s", requestUri, e.getMessage()));
        doc.setStatus(ICrawlerDocument.Status.NOT_FOUND, e.getMessage());
    } catch (NoHttpResponseException e) {
        this.logger.warn(String.format("Error crawling %s: %s", requestUri, e.getMessage()));
        doc.setStatus(ICrawlerDocument.Status.NOT_FOUND, e.getMessage());
    } catch (ContentLengthLimitExceededException e) {
        this.logger.warn(String.format("Error crawling %s: %s", requestUri, e.getMessage()));
        doc.setStatus(ICrawlerDocument.Status.UNKNOWN_FAILURE, e.getMessage());
    } catch (Throwable e) {
        String errorMsg;
        if (e instanceof HttpException) {
            errorMsg = "Unrecovered protocol exception: [%s] %s";
        } else if (e instanceof IOException) {
            errorMsg = "Transport exceptions: [%s] %s";
        } else {
            errorMsg = "Unexpected exception: [%s] %s";
        }
        errorMsg = String.format(errorMsg, e.getClass().getName(), e.getMessage());

        this.logger.error(String.format("Error crawling %s: %s", requestUri, errorMsg));
        doc.setStatus(ICrawlerDocument.Status.UNKNOWN_FAILURE, errorMsg);
        e.printStackTrace();
    } finally {
        if (method != null)
            method.releaseConnection();
    }

    return doc;
}

From source file:org.paxle.filter.robots.impl.RobotsTxtManager.java

/**
 * Downloads a <i>robots.txt</i> file from the given url and parses it
 * @param robotsUrlStr the URL to the robots.txt. This must be a http(s) resource
 * @return the parsed robots.txt file as a {@link RobotsTxt}-object
 * @throws IOException//from  w  w  w.j av a2s.c  om
 * @throws URISyntaxException 
 */
RobotsTxt getFromWeb(URI robotsURL) throws IOException, URISyntaxException {
    String hostPort = this.getHostPort(robotsURL);

    String statusLine = null;
    if (!robotsURL.getScheme().startsWith("http")) {
        throw new IOException(String.format("Unsupported protocol: %s", robotsURL.getScheme()));
    }

    InputStream inputStream = null;
    HttpMethod getMethod = null;
    try {
        getMethod = new GetMethod(robotsURL.toASCIIString());
        int code = this.httpClient.executeMethod(getMethod);
        statusLine = getMethod.getStatusLine().toString();

        if (code == HttpStatus.SC_UNAUTHORIZED || code == HttpStatus.SC_FORBIDDEN) {
            // access to the whole website is restricted
            return new RobotsTxt(hostPort, RobotsTxt.RELOAD_INTERVAL_DEFAULT, statusLine, true);
        } else if (code == HttpStatus.SC_NOT_FOUND) {
            // no robots.txt provided
            return new RobotsTxt(hostPort, RobotsTxt.RELOAD_INTERVAL_DEFAULT, statusLine);
        } else if (code != HttpStatus.SC_OK) {
            // the robots.txt seems not to be deliverable
            return new RobotsTxt(hostPort, RobotsTxt.RELOAD_INTERVAL_DEFAULT, statusLine);
        }

        Header contentTypeHeader = getMethod.getResponseHeader("Content-Type");
        if (contentTypeHeader != null && !contentTypeHeader.getValue().startsWith("text/plain")) {
            // the robots.txt seems not to be available
            return new RobotsTxt(hostPort, RobotsTxt.RELOAD_INTERVAL_ERROR,
                    "Wrong mimeType " + contentTypeHeader.getValue());
        }

        inputStream = getMethod.getResponseBodyAsStream();
        RobotsTxt robotsTxt = new RobotsTxt(hostPort, RobotsTxt.RELOAD_INTERVAL_DEFAULT, statusLine);
        return this.parseRobotsTxt(robotsTxt, inputStream);
    } catch (IOException e) {
        long reloadInterval = RobotsTxt.RELOAD_INTERVAL_TEMP_ERROR;
        String status = e.getMessage();

        if (e instanceof UnknownHostException) {
            reloadInterval = RobotsTxt.RELOAD_INTERVAL_ERROR;
            status = "Unknown host";
            logger.info(String.format("Unknown host '%s'.", robotsURL.getHost()));
        } else if (e instanceof CircularRedirectException || e instanceof RedirectException
                || e instanceof InvalidRedirectLocationException) {
            reloadInterval = RobotsTxt.RELOAD_INTERVAL_ERROR;
            logger.info(String.format("Invalid redirection on host '%s'.", hostPort));
        } else if (e instanceof SocketTimeoutException || e instanceof ConnectTimeoutException
                || e instanceof NoHttpResponseException) {
            logger.debug(String.format("TimeOut while loading robots.txt from host '%s'.", hostPort));
        } else if (!(e instanceof ConnectException || e instanceof SocketException)) {
            logger.error("Exception while loading robots.txt from " + hostPort, e);
        }

        return new RobotsTxt(hostPort, reloadInterval, status);
    } catch (IllegalArgumentException e) {
        // occurs if redirected to an invalid URI, see https://bugs.pxl.li/view.php?id=172
        // we treat it like a 404, see above
        logger.info(String.format("Invalid redirection URI on host '%s'.", hostPort));
        return new RobotsTxt(hostPort, RobotsTxt.RELOAD_INTERVAL_DEFAULT, "Redirected to illegal URI");
    } catch (IllegalStateException e) {
        // occurs if redirected to an URI with an invalid protocol, see https://bugs.pxl.li/view.php?id=169
        // we treat it like a 404, see above
        logger.info(String.format("Invalid redirection URI on host '%s'.", hostPort));
        return new RobotsTxt(hostPort, RobotsTxt.RELOAD_INTERVAL_DEFAULT, "Redirected to illegal URI");

    } finally {
        if (inputStream != null)
            try {
                inputStream.close();
            } catch (Exception e) {
                this.logger.error(e);
            }
        if (getMethod != null)
            getMethod.releaseConnection();
    }
}

From source file:org.review_board.ereviewboard.core.client.ReviewboardHttpClient.java

private String executeMethod(HttpMethodBase request, IProgressMonitor monitor) throws ReviewboardException {

    monitor = Policy.monitorFor(monitor);

    ensureIsLoggedIn(monitor);/*from  www .  j a v a2s.c  o m*/

    try {
        monitor.beginTask("Executing request", IProgressMonitor.UNKNOWN);

        int statusCode = executeRequest(request, monitor);

        if (statusCode == HttpStatus.SC_NOT_FOUND)
            throw new ReviewboardResourceNotFoundException(request.getPath());

        return getResponseBodyAsString(request, monitor);
    } finally {
        request.releaseConnection();
        monitor.done();
    }
}

From source file:org.rhq.enterprise.server.legacy.rss.DownloadPatchTest.java

public void testAccessDownloadsNoSoftware() throws Exception {
    int statusCode = accessDownload("fmerenda@jboss.org", "password", "");
    assertEquals(HttpStatus.SC_NOT_FOUND, statusCode);
}