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

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

Introduction

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

Prototype

public void setReasonCode(int code) 

Source Link

Document

Sets the status code description of the reason for an exception.

Usage

From source file:com.idega.slide.util.WebdavLocalResource.java

@SuppressWarnings("deprecation")
private Enumeration<LocalResponse> propfindMethod(NodeRevisionDescriptor descriptor)
        throws HttpException, IOException {
    if (descriptor == null) {
        return null;
    }// w  ww .  ja v a2  s . co  m

    if (properties != null) {
        return properties;
    }

    try {
        Vector<LocalResponse> responses = new Vector<LocalResponse>();
        LocalResponse response = new LocalResponse();
        response.setHref(getPath());
        responses.add(response);

        @SuppressWarnings("unchecked")
        List<NodeProperty> nodeProperties = Collections.list(descriptor.enumerateProperties());
        List<Property> properties = new ArrayList<Property>();
        for (NodeProperty p : nodeProperties) {
            String localName = p.getPropertyName().getName();
            Property property = null;

            if (localName.equals(RESOURCETYPE)) {
                Object oValue = p.getValue();
                String value = oValue == null ? null : oValue.toString();

                Element element = null;
                if ("<collection/>".equals(value)) {
                    element = getCollectionElement();
                } else if (CoreConstants.EMPTY.equals(value)) {
                    element = getEmptyElement();
                } else {
                    Document doc = XmlUtil.getDocumentBuilder().newDocument();
                    String namespace = p.getNamespace();
                    String tagName = p.getName();
                    element = doc.createElementNS(namespace, tagName);
                    element.appendChild(doc.createTextNode(value));
                }

                property = new ResourceTypeProperty(response, element);
            } else if (localName.equals(LOCKDISCOVERY)) {
                /*DocumentBuilderFactory factory =
                 DocumentBuilderFactory.newInstance();
                 factory.setNamespaceAware(true);
                 DocumentBuilder builder = factory.newDocumentBuilder();
                 Document doc = builder.newDocument();
                 Element element = doc.createElement("collection");
                 property = new LockDiscoveryProperty(response,element);*/
                throw new RuntimeException("LockDiscoveryProperty not yet implemented for: " + getPath());
            } else if (CREATIONDATE.equals(localName)) {
                setCreationDate((String) p.getValue());
            } else if (GETLASTMODIFIED.equals(localName)) {
                setGetLastModified((String) p.getValue());
            } else {
                LocalProperty lProperty = new LocalProperty(response);
                property = lProperty;
                lProperty.setName(p.getName());
                lProperty.setNamespaceURI(p.getNamespace());
                lProperty.setLocalName(p.getName());
                Object oValue = p.getValue();
                String value = oValue == null ? null : oValue.toString();
                lProperty.setPropertyAsString(value);
            }

            if (property != null) {
                properties.add(property);
            }
        }

        if (!ListUtil.isEmpty(properties)) {
            response.setProperties(new Vector<Property>(properties));
        }

        this.properties = responses.elements();
        if (this.properties != null) {
            setProperties(3, 0); //   Need to set basic properties
        }

        return this.properties;
    } catch (Exception e) {
        LOGGER.log(Level.WARNING, "Error getting properties for: " + getPath() + ": " + e.getMessage(), e);

        if (e instanceof ObjectNotFoundException) {
            getSlideAPI().deletetDefinitionFile(((ObjectNotFoundException) e).getObjectUri());

            HttpException he = new HttpException("Resource on path: " + getPath() + " not found");
            he.setReasonCode(WebdavStatus.SC_NOT_FOUND);
            throw he;
        } else if (e instanceof RevisionDescriptorNotFoundException) {
            getSlideAPI().deletetDefinitionFile(((RevisionDescriptorNotFoundException) e).getObjectUri());
        }

        return null;
    }
}

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);/*w  w w  .  j a v a  2 s.c  o  m*/
    }
    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.taskdefs.Delete.java

private void delete(HttpURL url, String logName) throws IOException, HttpException {
    validate();/*from w w w . java2s .c o m*/
    log("Deleting " + logName, ifVerbose());
    DeleteMethod delete = new DeleteMethod(url.getEscapedURI());
    delete.setFollowRedirects(true);
    if (this.locktoken != null) {
        Utils.generateIfHeader(delete, this.locktoken);
    }
    int status = getHttpClient().executeMethod(delete);

    switch (status) {
    case WebdavStatus.SC_OK:
    case WebdavStatus.SC_NO_CONTENT:
    case WebdavStatus.SC_NOT_FOUND:
        // ok
        this.count++;
        break;
    default:
        HttpException ex = new HttpException();
        ex.setReasonCode(status);
        throw ex;
    }
}

From source file:org.apache.webdav.ant.taskdefs.Proppatch.java

protected void proppatch(HttpURL url, String logName) throws IOException, HttpException {
    log(logName, ifVerbose());//from  w w  w  .  ja  v a  2 s  .  c  om
    PropPatchMethod propPatch = new PropPatchMethod(url.getEscapedURI());
    if (this.locktoken != null) {
        Utils.generateIfHeader(propPatch, this.locktoken);
    }

    int c = 1;
    for (Iterator i = toRemove.iterator(); i.hasNext();) {
        Remove r = (Remove) i.next();
        propPatch.addPropertyToRemove(r.name, r.abbrev != null ? r.abbrev : "NS" + (c++), r.namespace);
    }
    for (Iterator i = toSet.iterator(); i.hasNext();) {
        Set a = (Set) i.next();
        propPatch.addPropertyToSet(a.name, a.getValue(), a.abbrev != null ? a.abbrev : "NS" + (c++),
                a.namespace);
    }

    int status = getHttpClient().executeMethod(propPatch);
    count++;

    switch (status) {
    case WebdavStatus.SC_OK:
        // ok
        break;
    case WebdavStatus.SC_MULTI_STATUS:
        for (Enumeration e = propPatch.getResponses(); e.hasMoreElements();) {
            ResponseEntity response = (ResponseEntity) e.nextElement();

            if (response.getStatusCode() > 400) {
                throw Utils.makeBuildException("Error while PROPPATCH", propPatch.getResponses());
            }
        }
        break;

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

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

/**
 * Returns <code>true</code> if the resource given as URL does exist.
 * @param client//w  w w . j  a  v  a 2 s  .co  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  .  com*/
    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);/*w ww.ja v a  2  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   ww w . j a  va  2  s.  c  o m
 * @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);//from   w ww.  j a  v  a 2s  . c om
    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  ww w.j a va 2s  . c o m
    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;
    }

}