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.rhq.enterprise.server.legacy.rss.DownloadPatchTest.java

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

From source file:org.rhq.enterprise.server.plugins.url.HttpProvider.java

/**
 * Given any URL, will return a stream to that URL using the HTTP client and GET method
 * for the authentication as defined in this content source's configuration.
 * //w w  w.  j  a v a 2s . c o m
 * @param url the URL whose stream of content is returned
 *
 * @return stream containing the content for the given URL
 *
 * @throws Exception if cannot get the streamed content
 */
protected InputStream getInputStreamForUrl(URL url) throws Exception {
    String fullLocation = url.toString();

    HttpClient client = new HttpClient();
    HttpMethodBase method = new GetMethod(fullLocation);
    prepareHttpClient(client, method);
    int status = client.executeMethod(method);

    switch (status) {
    case HttpStatus.SC_OK: {
        break; // good to go
    }

    case HttpStatus.SC_NOT_FOUND: {
        throw new Exception("Could not find the content at URL [" + fullLocation
                + "]. Make sure the content source defines a valid URL.");
    }

    case HttpStatus.SC_UNAUTHORIZED:
    case HttpStatus.SC_FORBIDDEN: {
        throw new Exception("Invalid login credentials specified for user [" + username + "]. Make sure "
                + "this user is valid and the password specified for this content source is correct.");
    }

    default: {
        throw new Exception("Failed to retrieve content. status code=" + status);
    }
    }

    InputStream stream = method.getResponseBodyAsStream();

    return stream;
}

From source file:org.sakaiproject.nakamura.grouper.changelog.util.NakamuraHttpUtils.java

/**
 * Prepare an HTTP request to Sakai OAE and parse the response (if JSON).
 * @param client an {@link HttpClient} to execute the request.
 * @param method an HTTP method to send/*from w  w  w. j  a v a 2  s .  co m*/
 * @return a JSONObject of the response if the request returns JSON
 * @throws GroupModificationException if there was an error updating the group information.
 */
public static JSONObject http(HttpClient client, HttpMethod method) throws GroupModificationException {

    method.setRequestHeader("User-Agent", HTTP_USER_AGENT);
    method.setRequestHeader("Referer", HTTP_REFERER);

    String errorMessage = null;
    String responseString = null;
    JSONObject responseJSON = null;

    boolean isJSONRequest = !method.getPath().toString().endsWith(".html");

    if (log.isDebugEnabled() && method instanceof PostMethod) {
        log.debug(method.getName() + " " + method.getPath() + " params:");
        for (NameValuePair nvp : ((PostMethod) method).getParameters()) {
            log.debug(nvp.getName() + " = " + nvp.getValue());
        }
    }

    int responseCode = -1;
    try {
        responseCode = client.executeMethod(method);
        responseString = StringUtils.trimToNull(IOUtils.toString(method.getResponseBodyAsStream()));

        if (isJSONRequest) {
            responseJSON = parseJSONResponse(responseString);
        }

        if (log.isDebugEnabled()) {
            log.debug(responseCode + " " + method.getName() + " " + method.getPath());
        }
        if (log.isTraceEnabled()) {
            log.trace("reponse: " + responseString);
        }

        switch (responseCode) {

        case HttpStatus.SC_OK: // 200
        case HttpStatus.SC_CREATED: // 201
            break;
        case HttpStatus.SC_BAD_REQUEST: // 400
        case HttpStatus.SC_UNAUTHORIZED: // 401
        case HttpStatus.SC_NOT_FOUND: // 404
        case HttpStatus.SC_INTERNAL_SERVER_ERROR: // 500
            if (isJSONRequest && responseJSON != null) {
                errorMessage = responseJSON.getString("status.message");
            }
            if (errorMessage == null) {
                errorMessage = "Empty " + responseCode + " error. Check the logs on the Sakai OAE server.";
            }
            break;
        default:
            errorMessage = "Unknown HTTP response " + responseCode;
            break;
        }
    } catch (Exception e) {
        errorMessage = "An exception occurred communicatingSakai OAE. " + e.toString();
    } finally {
        method.releaseConnection();
    }

    if (errorMessage != null) {
        log.error(errorMessage);
        errorToException(responseCode, errorMessage);
    }
    return responseJSON;
}

From source file:org.sonar.wsclient.connectors.HttpClient3Connector.java

private String executeRequest(HttpMethodBase method) {
    String json = null;//from  ww  w. j a v  a2s. c  om
    try {
        httpClient.executeMethod(method);

        if (method.getStatusCode() == HttpStatus.SC_OK) {
            json = getResponseBodyAsString(method);

        } else if (method.getStatusCode() != HttpStatus.SC_NOT_FOUND) {
            throw new ConnectionException("HTTP error: " + method.getStatusCode() + ", msg: "
                    + method.getStatusText() + ", query: " + method);
        }

    } catch (HttpException e) {
        throw new ConnectionException("Query: " + method, e);

    } catch (IOException e) {
        throw new ConnectionException("Query: " + method, e);

    } finally {
        if (method != null) {
            method.releaseConnection();
        }
    }
    return json;
}

From source file:org.sonarqube.ws.connectors.HttpClient3Connector.java

/**
 * Reauest execution//from   ww  w.j a va  2  s  .co  m
 * 
 * @param method
 *            method
 * @return String result
 */
private String executeRequest(HttpMethodBase method) throws ConnectionException {
    String json = null;
    try {
        httpClient.executeMethod(method);

        if (method.getStatusCode() == HttpStatus.SC_OK) {
            json = getResponseBodyAsString(method);

        } else if (method.getStatusCode() != HttpStatus.SC_NOT_FOUND) {
            throw new ConnectionException("HTTP error: " + method.getStatusCode() + ", msg: "
                    + method.getStatusText() + ", query: " + method);
        }

    } catch (IOException e) {
        throw new ConnectionException("Query: " + method, e);

    } finally {
        if (method != null) {
            method.releaseConnection();
        }
    }
    return json;
}

From source file:org.sonatype.nexus.integrationtests.nexus176.Nexus176DeployToInvalidRepoIT.java

@Test
public void wagonDeployTest() throws Exception {

    Gav gav = new Gav(this.getTestId(), "simpleArtifact", "1.0.0", null, "xml", 0, new Date().getTime(),
            "Simple Test Artifact", false, null, false, null);

    // file to deploy
    File fileToDeploy = this.getTestFile(gav.getArtifactId() + "." + gav.getExtension());

    // url to upload to
    String uploadURL = this.getBaseNexusUrl() + "service/local/artifact/maven/content";

    // the method we are calling
    PostMethod filePost = new PostMethod(uploadURL);
    filePost.getParams().setBooleanParameter(HttpMethodParams.USE_EXPECT_CONTINUE, true);

    int status = getDeployUtils().deployUsingGavWithRest(uploadURL, TEST_RELEASE_REPO, gav, fileToDeploy);

    if (status != HttpStatus.SC_NOT_FOUND) {
        Assert.fail("Upload attempt should have returned a 400, it returned:  " + status);
    }//from w  w  w  .  ja va2 s . c  om

    boolean fileWasUploaded = true;
    try {
        // download it
        downloadArtifact(gav, "./target/downloaded-jars");
    } catch (FileNotFoundException e) {
        fileWasUploaded = false;
    }

    Assert.assertFalse("The file was uploaded and it should not have been.", fileWasUploaded);

}

From source file:org.sonatype.nexus.integrationtests.nexus176.Nexus176DeployToInvalidRepoIT.java

@Test
public void deploywithGavUsingRest() throws Exception {

    Gav gav = new Gav(this.getTestId(), "uploadWithGav", "1.0.0", null, "xml", 0, new Date().getTime(),
            "Simple Test Artifact", false, null, false, null);

    // file to deploy
    File fileToDeploy = this.getTestFile(gav.getArtifactId() + "." + gav.getExtension());

    // the Restlet Client does not support multipart forms: http://restlet.tigris.org/issues/show_bug.cgi?id=71

    // url to upload to
    String uploadURL = this.getBaseNexusUrl() + "service/local/artifact/maven/content";

    // the method we are calling
    PostMethod filePost = new PostMethod(uploadURL);
    filePost.getParams().setBooleanParameter(HttpMethodParams.USE_EXPECT_CONTINUE, true);

    int status = getDeployUtils().deployUsingGavWithRest(uploadURL, TEST_RELEASE_REPO, gav, fileToDeploy);

    if (status != HttpStatus.SC_NOT_FOUND) {
        Assert.fail("Upload attempt should have returned a 400, it returned:  " + status);
    }//  w  w w . ja  v a2 s.c o m

    boolean fileWasUploaded = true;
    try {
        // download it
        downloadArtifact(gav, "./target/downloaded-jars");
    } catch (FileNotFoundException e) {
        fileWasUploaded = false;
    }

    Assert.assertFalse("The file was uploaded and it should not have been.", fileWasUploaded);

}

From source file:org.sonatype.nexus.integrationtests.nexus176.Nexus176DeployToInvalidRepoIT.java

@Test
public void deploywithPomUsingRest() throws Exception {

    Gav gav = new Gav(this.getTestId(), "uploadWithGav", "1.0.0", null, "xml", 0, new Date().getTime(),
            "Simple Test Artifact", false, null, false, null);

    // file to deploy
    File fileToDeploy = this.getTestFile(gav.getArtifactId() + "." + gav.getExtension());

    File pomFile = this.getTestFile("pom.xml");

    // the Restlet Client does not support multipart forms: http://restlet.tigris.org/issues/show_bug.cgi?id=71

    // url to upload to
    String uploadURL = this.getBaseNexusUrl() + "service/local/artifact/maven/content";

    int status = getDeployUtils().deployUsingPomWithRest(uploadURL, TEST_RELEASE_REPO, fileToDeploy, pomFile,
            null, null);/*from  ww w . j av a  2s . co m*/

    if (status != HttpStatus.SC_NOT_FOUND) {
        Assert.fail("Upload attempt should have returned a 400, it returned:  " + status);
    }

    boolean fileWasUploaded = true;
    try {
        // download it
        downloadArtifact(gav, "./target/downloaded-jars");
    } catch (FileNotFoundException e) {
        fileWasUploaded = false;
    }

    Assert.assertFalse("The file was uploaded and it should not have been.", fileWasUploaded);

}

From source file:org.sonatype.nexus.integrationtests.nexus176.Nexus176DeployToInvalidRepoIT.java

@Test
public void wagonSnapshotDeployTest() throws Exception {

    Gav gav = new Gav(this.getTestId(), "simpleArtifact", "1.0.0-SNAPSHOT", null, "xml", 0,
            new Date().getTime(), "Simple Test Artifact", false, null, false, null);

    // file to deploy
    File fileToDeploy = this.getTestFile(gav.getArtifactId() + "." + gav.getExtension());

    // url to upload to
    String uploadURL = this.getBaseNexusUrl() + "service/local/artifact/maven/content";

    // the method we are calling
    PostMethod filePost = new PostMethod(uploadURL);
    filePost.getParams().setBooleanParameter(HttpMethodParams.USE_EXPECT_CONTINUE, true);

    int status = getDeployUtils().deployUsingGavWithRest(uploadURL, TEST_RELEASE_REPO, gav, fileToDeploy);

    if (status != HttpStatus.SC_NOT_FOUND) {
        Assert.fail("Upload attempt should have returned a 400, it returned:  " + status);
    }/*ww  w. j a  v a2  s .  com*/

    boolean fileWasUploaded = true;
    try {
        // download it
        downloadArtifact(gav, "./target/downloaded-jars");
    } catch (FileNotFoundException e) {
        fileWasUploaded = false;
    }

    Assert.assertFalse("The file was uploaded and it should not have been.", fileWasUploaded);

}

From source file:org.sonatype.nexus.integrationtests.nexus176.Nexus176DeployToInvalidRepoIT.java

@Test
public void deploySnapshotWithGavUsingRest() throws Exception {

    Gav gav = new Gav(this.getTestId(), "uploadWithGav", "1.0.0-SNAPSHOT", null, "xml", 0, new Date().getTime(),
            "Simple Test Artifact", false, null, false, null);

    // file to deploy
    File fileToDeploy = this.getTestFile(gav.getArtifactId() + "." + gav.getExtension());

    // the Restlet Client does not support multipart forms: http://restlet.tigris.org/issues/show_bug.cgi?id=71

    // url to upload to
    String uploadURL = this.getBaseNexusUrl() + "service/local/artifact/maven/content";

    // the method we are calling
    PostMethod filePost = new PostMethod(uploadURL);
    filePost.getParams().setBooleanParameter(HttpMethodParams.USE_EXPECT_CONTINUE, true);

    int status = getDeployUtils().deployUsingGavWithRest(uploadURL, TEST_RELEASE_REPO, gav, fileToDeploy);

    if (status != HttpStatus.SC_NOT_FOUND) {
        Assert.fail("Upload attempt should have returned a 400, it returned:  " + status);
    }//  w  w w .  ja v a  2s .c  om

    boolean fileWasUploaded = true;
    try {
        // download it
        downloadArtifact(gav, "./target/downloaded-jars");
    } catch (FileNotFoundException e) {
        fileWasUploaded = false;
    }

    Assert.assertFalse("The file was uploaded and it should not have been.", fileWasUploaded);

}