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

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

Introduction

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

Prototype

public static String getStatusText(int statusCode) 

Source Link

Document

Get the reason phrase for a particular status code.

Usage

From source file:org.apache.sling.maven.bundlesupport.AbstractBundleInstallMojo.java

/**
 * Add a new configuration for the file system provider
 * @throws MojoExecutionException/*  w w  w.  jav  a  2 s.c om*/
 */
protected void addConfiguration(final HttpClient client, final String targetURL, String dir, String path)
        throws MojoExecutionException {
    final String postUrl = targetURL + "/configMgr/" + FS_FACTORY;
    final PostMethod post = new PostMethod(postUrl);
    post.addParameter("apply", "true");
    post.addParameter("factoryPid", FS_FACTORY);
    post.addParameter("pid", "[Temporary PID replaced by real PID upon save]");
    post.addParameter("provider.file", dir);
    post.addParameter("provider.roots", path);
    post.addParameter("propertylist", "provider.roots,provider.file");
    try {
        final int status = client.executeMethod(post);
        // we get a moved temporarily back from the configMgr plugin
        if (status == HttpStatus.SC_MOVED_TEMPORARILY || status == HttpStatus.SC_OK) {
            getLog().info("Configuration created.");
        } else {
            getLog().error("Configuration failed, cause: " + HttpStatus.getStatusText(status));
        }
    } catch (HttpException ex) {
        throw new MojoExecutionException("Configuration on " + postUrl + " failed, cause: " + ex.getMessage(),
                ex);
    } catch (IOException ex) {
        throw new MojoExecutionException("Configuration on " + postUrl + " failed, cause: " + ex.getMessage(),
                ex);
    } finally {
        post.releaseConnection();
    }
}

From source file:org.apache.sling.maven.bundlesupport.BundleUninstallMojo.java

protected void deleteViaWebDav(String targetURL, File file) throws MojoExecutionException {

    final DeleteMethod delete = new DeleteMethod(getURLWithFilename(targetURL, file.getName()));

    try {//  w w  w  . j a  v  a2  s.  c o  m

        int status = getHttpClient().executeMethod(delete);
        if (status >= 200 && status < 300) {
            getLog().info("Bundle uninstalled");
        } else {
            getLog().error("Uninstall failed, cause: " + HttpStatus.getStatusText(status));
        }
    } catch (Exception ex) {
        throw new MojoExecutionException("Uninstall from " + targetURL + " failed, cause: " + ex.getMessage(),
                ex);
    } finally {
        delete.releaseConnection();
    }
}

From source file:org.apache.sling.maven.bundlesupport.BundleUninstallMojo.java

@Override
protected void postToSling(String targetURL, File file) throws MojoExecutionException {
    final PostMethod post = new PostMethod(getURLWithFilename(targetURL, file.getName()));

    try {/*from  w  w  w  .j a  v  a 2 s .  co m*/
        // Add SlingPostServlet operation flag for deleting the content
        Part[] parts = new Part[1];
        parts[0] = new StringPart(":operation", "delete");
        post.setRequestEntity(new MultipartRequestEntity(parts, post.getParams()));

        // Request JSON response from Sling instead of standard HTML
        post.setRequestHeader("Accept", JSON_MIME_TYPE);

        int status = getHttpClient().executeMethod(post);
        if (status == HttpStatus.SC_OK) {
            getLog().info("Bundle uninstalled");
        } else {
            getLog().error("Uninstall failed, cause: " + HttpStatus.getStatusText(status));
        }
    } catch (Exception ex) {
        throw new MojoExecutionException("Uninstall from " + targetURL + " failed, cause: " + ex.getMessage(),
                ex);
    } finally {
        post.releaseConnection();
    }
}

From source file:org.apache.sling.maven.bundlesupport.BundleUninstallMojo.java

protected void postToFelix(String targetURL, String symbolicName) throws MojoExecutionException {
    final PostMethod post = new PostMethod(targetURL + "/bundles/" + symbolicName);
    post.addParameter("action", "uninstall");

    try {//w  ww  .ja v  a 2 s  .c o m

        int status = getHttpClient().executeMethod(post);
        if (status == HttpStatus.SC_OK) {
            getLog().info("Bundle uninstalled");
        } else {
            getLog().error("Uninstall failed, cause: " + HttpStatus.getStatusText(status));
        }
    } catch (Exception ex) {
        throw new MojoExecutionException("Uninstall from " + targetURL + " failed, cause: " + ex.getMessage(),
                ex);
    } finally {
        post.releaseConnection();
    }
}

From source file:org.apache.webdav.ant.Utils.java

public static BuildException makeBuildException(String msg, Exception e) {
    if (e instanceof HttpException) {
        HttpException he = (HttpException) e;
        return new BuildException(msg + " " + e.getMessage() + " ("
                + (he.getReason() != null ? he.getReason() : HttpStatus.getStatusText(he.getReasonCode()))
                + ")");

    } else {/*  www. jav a2  s . c om*/
        return new BuildException(msg + " (" + e.toString() + ")", e);
    }
}

From source file:org.apache.webdav.ant.Utils.java

public static BuildException makeBuildException(String msg, int status) {
    return new BuildException(msg + " (" + HttpStatus.getStatusText(status) + ")");
}

From source file:org.apache.webdav.ant.Utils.java

public static BuildException makeBuildException(String msg, Enumeration enumOfResponseEntities) {
    StringBuffer b = new StringBuffer();

    b.append(msg).append("\n");

    for (; enumOfResponseEntities.hasMoreElements();) {
        ResponseEntity e = (ResponseEntity) enumOfResponseEntities.nextElement();

        b.append(e.getHref()).append(" ").append(HttpStatus.getStatusText(e.getStatusCode())).append("\n");
    }//from  ww w.ja  va  2  s  .c  o m

    return new BuildException(b.toString());
}

From source file:org.artifactory.cli.rest.RestClient.java

/**
 * Validates the expected returned status
 *
 * @param uri            Target URL/*from  w ww . j  a  v a2 s . com*/
 * @param expectedStatus Expected returned status
 * @param method         The method after execution (holds the returned status)
 */
private static void checkStatus(String uri, int expectedStatus, HttpMethod method) {
    int status = method.getStatusCode();
    if (status != expectedStatus) {
        throw new RemoteCommandException("\nUnexpected response status for request: " + uri + "\n"
                + "Expected status: " + expectedStatus + " (" + HttpStatus.getStatusText(expectedStatus) + ")"
                + "\n " + "Received status: " + status + " (" + HttpStatus.getStatusText(status) + ") - "
                + method.getStatusText() + "\n");
    }
}

From source file:org.codebistro.jsonrpc.HTTPSession.java

public JSONObject sendAndReceive(JSONObject message) {
    if (log.isDebugEnabled())
        log.debug("Sending: " + message.toString(2));
    PostMethod postMethod = new PostMethod(uri.toString());
    postMethod.setRequestHeader("Content-Type", "text/plain");

    RequestEntity requestEntity = new StringRequestEntity(message.toString());
    postMethod.setRequestEntity(requestEntity);
    try {/*from w w w.ja v  a 2 s . c  o m*/
        http().executeMethod(null, postMethod, state);
        int statusCode = postMethod.getStatusCode();
        if (statusCode != HttpStatus.SC_OK)
            throw new ClientError(
                    "HTTP Status - " + HttpStatus.getStatusText(statusCode) + " (" + statusCode + ")");
        JSONTokener tokener = new JSONTokener(postMethod.getResponseBodyAsString());
        Object rawResponseMessage = tokener.nextValue();
        JSONObject responseMessage = (JSONObject) rawResponseMessage;
        if (responseMessage == null)
            throw new ClientError("Invalid response type - " + rawResponseMessage.getClass());
        return responseMessage;
    } catch (ParseException e) {
        throw new ClientError(e);
    } catch (HttpException e) {
        throw new ClientError(e);
    } catch (IOException e) {
        throw new ClientError(e);
    }
}

From source file:org.conqat.engine.bugzilla.lib.BugzillaWebClient.java

/**
 * Authenticate with Bugzilla server. Note that Bugzilla servers can often
 * be accessed without credentials in a read-only manner. Hence, credentials
 * are not necessarily required although your Bugzilla server requires them
 * to edit bugs. Depending on the permissions set on the Bugzilla server,
 * calls to {@link #query(Set, Set, Set)} may return different results
 * whether you are authenticated or not.
 * /*from www . jav  a 2 s.c o m*/
 * @throws BugzillaException
 *             If Bugzilla server responded with an error.
 * @throws IOException
 *             if an I/O problem occurs while obtaining the response from
 *             Bugzilla
 * @throws HttpException
 *             if a protocol exception occurs
 */
public void authenticate(String username, String password)
        throws BugzillaException, HttpException, IOException {
    NameValuePair[] formData = new NameValuePair[2];
    formData[0] = new NameValuePair("Bugzilla_login", username);
    formData[1] = new NameValuePair("Bugzilla_password", password);

    PostMethod postMethod = new PostMethod(server + "/index.cgi");

    postMethod.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    postMethod.setRequestBody(formData);
    postMethod.setDoAuthentication(true);
    postMethod.setFollowRedirects(false);
    client.getState().clearCookies();

    try {
        int code = client.executeMethod(postMethod);
        if (code != HttpStatus.SC_OK) {
            throw new BugzillaException("Authentication failed: " + HttpStatus.getStatusText(code));
        }

        // Bugzilla assigns cookies if everything went ok.
        Cookie[] cookies = client.getState().getCookies();
        if (cookies.length == 0) {
            throw new BugzillaException("Authentication failed!");
        }

        // the following loop fixes CR#2801. For some reason, Bugzilla only
        // accepts the cookies if they are not limited to secure
        // connections.
        for (Cookie c : cookies) {
            c.setSecure(false);
        }
    } finally {
        postMethod.releaseConnection();
    }
}