Example usage for org.apache.commons.httpclient HttpException setReason

List of usage examples for org.apache.commons.httpclient HttpException setReason

Introduction

In this page you can find the example usage for org.apache.commons.httpclient HttpException setReason.

Prototype

public void setReason(String reason) 

Source Link

Document

Sets the text description of the reason for an exception.

Usage

From source file:org.apache.ambari.view.slider.rest.client.BaseHttpClient.java

@SuppressWarnings("deprecation")
public JsonElement doGetJson(String url, String path) throws HttpException, IOException {
    GetMethod get = new GetMethod(url + path);
    if (isNeedsAuthentication()) {
        get.setDoAuthentication(true);//from   ww  w . j  a v a  2 s.c om
    }
    int executeMethod = getHttpClient().executeMethod(get);
    switch (executeMethod) {
    case HttpStatus.SC_OK:
        JsonElement jsonElement = new JsonParser()
                .parse(new JsonReader(new InputStreamReader(get.getResponseBodyAsStream())));
        return jsonElement;
    case HttpStatus.SC_NOT_FOUND:
        return null;
    default:
        HttpException httpException = new HttpException(get.getResponseBodyAsString());
        httpException.setReason(HttpStatus.getStatusText(executeMethod));
        httpException.setReasonCode(executeMethod);
        throw httpException;
    }
}

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

/**
 * Returns <code>true</code> if the resource given as URL does exist.
 * @param client/*from  www. j  av a2s .c o  m*/
 * @param httpURL
 * @return <code>true</code>if the resource exists
 * @throws IOException
 * @throws HttpException
 */
public static boolean resourceExists(HttpClient client, HttpURL httpURL) throws IOException, HttpException {
    HeadMethod head = new HeadMethod(httpURL.getEscapedURI());
    head.setFollowRedirects(true);
    int status = client.executeMethod(head);

    switch (status) {
    case WebdavStatus.SC_OK:
        return true;
    case WebdavStatus.SC_NOT_FOUND:
        return false;
    default:
        HttpException ex = new HttpException();
        ex.setReasonCode(status);
        ex.setReason(head.getStatusText());
        throw ex;
    }
}

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

public static boolean collectionExists(HttpClient client, HttpURL httpURL) throws IOException, HttpException {
    Vector props = new Vector(1);
    props.add(RESOURCETYPE);/*from  w w  w  . j a v  a2 s. co m*/
    PropFindMethod propFind = new PropFindMethod(httpURL.getEscapedURI(), 0, PropFindMethod.BY_NAME);
    propFind.setFollowRedirects(true);
    propFind.setPropertyNames(props.elements());
    propFind.setAssertHrefsArePathes(true);
    propFind.setDecodeResponseHrefs("UTF-8");

    int status = client.executeMethod(propFind);
    switch (status) {
    case WebdavStatus.SC_MULTI_STATUS:
        Property p = findProperty(propFind, RESOURCETYPE, httpURL.getPath());
        if (p instanceof ResourceTypeProperty) {
            return ((ResourceTypeProperty) p).isCollection();
        } else {
            throw new WebdavException("PROPFFIND does not return resourcetype");
        }
    case WebdavStatus.SC_NOT_FOUND:
        return false;
    default:
        HttpException ex = new HttpException();
        ex.setReasonCode(status);
        ex.setReason(propFind.getStatusText());
        throw ex;
    }
}

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

public static long getLastModified(HttpClient client, HttpURL url) throws IOException, HttpException {
    Vector props = new Vector(1);
    props.add(GETLASTMODIFIED);/*from   w  ww .  j  a v a2 s  . co  m*/
    PropFindMethod propFind = new PropFindMethod(url.getEscapedURI(), 0);
    propFind.setPropertyNames(props.elements());
    propFind.setFollowRedirects(true);
    propFind.setAssertHrefsArePathes(true);
    propFind.setDecodeResponseHrefs("UTF-8");

    int status = client.executeMethod(propFind);
    switch (status) {
    case WebdavStatus.SC_MULTI_STATUS:
        Property p = findProperty(propFind, GETLASTMODIFIED, url.getPath());
        if (p != null) {
            try {
                Date d = GETLASTMODIFIED_FORMAT.parse(p.getPropertyAsString());
                return d.getTime();
            } catch (ParseException e) {
                throw new HttpException("Invalid lastmodified property: " + p.getPropertyAsString());
            }
        }
        throw new HttpException("PROPFIND does not return lastmodified.");
    default:
        HttpException ex = new HttpException();
        ex.setReasonCode(status);
        ex.setReason(propFind.getStatusText());
        throw ex;
    }
}

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

/**
 * /*from   w w  w. ja  v  a 2s .com*/
 * @param client
 * @param httpURL
 * @param lockToken the locktoken to be used or <code>null</code> if 
 *         none is to be used
 * @throws IOException
 * @throws HttpException
 */
public static boolean assureExistingCollection(HttpClient client, HttpURL httpURL, String lockToken)
        throws IOException, HttpException {
    String path = httpURL.getPath();
    if (!path.endsWith("/")) {
        path = path + "/";
    }
    Stack toBeCreated = new Stack();

    while (!path.equals("/")) {
        HttpURL parent = Utils.createHttpURL(httpURL, path);
        if (!collectionExists(client, parent)) {
            toBeCreated.push(path);
            path = path.substring(0, path.lastIndexOf("/", path.length() - 2) + 1);
        } else {
            break;
        }
    }

    boolean created = !toBeCreated.empty();
    while (!toBeCreated.empty()) {
        HttpURL newColl = Utils.createHttpURL(httpURL, (String) toBeCreated.pop());
        MkcolMethod mkcol = new MkcolMethod(newColl.getEscapedURI());
        mkcol.setFollowRedirects(true);
        generateIfHeader(mkcol, lockToken);
        int status = client.executeMethod(mkcol);
        if (status != WebdavStatus.SC_CREATED) {
            HttpException ex = new HttpException("Can't create collection " + newColl);
            ex.setReasonCode(status);
            ex.setReason(mkcol.getStatusText());
            throw ex;
        }
    }
    return created;
}

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

public static void putFile(HttpClient client, HttpURL url, InputStream is, String contentType, String lockToken)
        throws IOException, HttpException {
    PutMethod put = new PutMethod(url.getEscapedURI());
    generateIfHeader(put, lockToken);/*w ww . j  a  va 2  s .c o m*/
    put.setRequestHeader("Content-Type", contentType);
    put.setRequestBody(is);
    put.setFollowRedirects(true);
    int status = client.executeMethod(put);
    switch (status) {
    case WebdavStatus.SC_OK:
    case WebdavStatus.SC_CREATED:
    case WebdavStatus.SC_NO_CONTENT:
        return;
    default:
        HttpException ex = new HttpException();
        ex.setReason(put.getStatusText());
        ex.setReasonCode(status);
        throw ex;
    }
}

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

public static InputStream getFile(HttpClient client, HttpURL url) throws IOException, HttpException {
    GetMethod get = new GetMethod(url.toString());
    get.setFollowRedirects(true);//from  w w w .  j a v a 2s .c om
    int status = client.executeMethod(get);

    switch (status) {
    case WebdavStatus.SC_OK:
        return get.getResponseBodyAsStream();
    default:
        HttpException ex = new HttpException();
        ex.setReason(get.getStatusText());
        ex.setReasonCode(status);
        throw ex;
    }

}

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

public static void unlockResource(HttpClient client, HttpURL url, String lockToken)
        throws IOException, HttpException {
    UnlockMethod unlock = new UnlockMethod(url.getEscapedURI(), lockToken);
    unlock.setFollowRedirects(true);//ww  w .  j  av a  2 s . c  o m
    int status = client.executeMethod(unlock);

    switch (status) {
    case WebdavStatus.SC_OK:
    case WebdavStatus.SC_NO_CONTENT:
        return;

    default:
        HttpException ex = new HttpException();
        ex.setReasonCode(status);
        ex.setReason(unlock.getStatusText());
        throw ex;
    }
}

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

public static void copyResource(HttpClient client, HttpURL url, String destination, int depth,
        boolean overwrite) throws IOException, HttpException {
    CopyMethod copy = new CopyMethod(url.getEscapedURI(), destination, overwrite, depth);
    copy.setFollowRedirects(true);/*from   w w  w.j  a v a2s .  co  m*/
    int status = client.executeMethod(copy);
    switch (status) {
    case WebdavStatus.SC_OK:
    case WebdavStatus.SC_CREATED:
    case WebdavStatus.SC_NO_CONTENT:
        return;

    default:
        HttpException ex = new HttpException();
        ex.setReasonCode(status);
        ex.setReason(copy.getStatusText());
        throw ex;
    }
}

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

public static void moveResource(HttpClient client, HttpURL url, String destination, boolean overwrite)
        throws IOException, HttpException {
    MoveMethod move = new MoveMethod(url.getEscapedURI(), destination, overwrite);
    move.setFollowRedirects(true);/*from w  w  w. jav a 2 s . com*/
    int status = client.executeMethod(move);
    switch (status) {
    case WebdavStatus.SC_OK:
    case WebdavStatus.SC_CREATED:
    case WebdavStatus.SC_NO_CONTENT:
        return;

    default:
        HttpException ex = new HttpException();
        ex.setReasonCode(status);
        ex.setReason(move.getStatusText());
        throw ex;
    }
}