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

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

Introduction

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

Prototype

int SC_MULTI_STATUS

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

Click Source Link

Document

<tt>207 Multi-Status</tt> (WebDAV - RFC 2518) or <tt>207 Partial Update OK</tt> (HTTP/1.1 - draft-ietf-http-v11-spec-rev-01?)

Usage

From source file:davmail.exchange.dav.DavExchangeSession.java

/**
 * @inheritDoc//w  w  w. ja v a  2 s.co m
 */
@Override
public void updateMessage(ExchangeSession.Message message, Map<String, String> properties) throws IOException {
    PropPatchMethod patchMethod = new PropPatchMethod(encodeAndFixUrl(message.permanentUrl),
            buildProperties(properties)) {
        @Override
        protected void processResponseBody(HttpState httpState, HttpConnection httpConnection) {
            // ignore response body, sometimes invalid with exchange mapi properties
        }
    };
    try {
        int statusCode = httpClient.executeMethod(patchMethod);
        if (statusCode != HttpStatus.SC_MULTI_STATUS) {
            throw new DavMailException("EXCEPTION_UNABLE_TO_UPDATE_MESSAGE");
        }

    } finally {
        patchMethod.releaseConnection();
    }
}

From source file:org.apache.webdav.lib.WebdavResource.java

/**
 * Execute PROPFIND method with allprop for the given path.
 * Get list of all WebDAV properties on the given resource.
 *
 * <p>Once used this method, the the status code in the 207
 * reponse is need to be set for the method of WebdavResource.
 *
 * <p>The values of DepthSupport.DEPTH_0, DepthSupport.DEPTH_1,
 * DepthSupport.DEPTH_INFINITY is possbile for the depth.
 *
 * @param path the server relative path of the resource to request
 * @param depth//  w ww. j  a va  2s  .c o m
 * @return an enumeration of <code>ResponseEntity</code>
 * @exception HttpException
 * @exception IOException
 */
public Enumeration propfindMethod(String path, int depth) throws HttpException, IOException {

    setClient();
    // Change the depth for allprop
    PropFindMethod method = new PropFindMethod(URIUtil.encodePath(path), depth);

    method.setDebug(debug);

    // Default depth=infinity, type=allprop
    generateTransactionHeader(method);
    generateAdditionalHeaders(method);
    int status = client.executeMethod(method);

    // Set status code for this resource.
    if (thisResource == true) {
        setStatusCode(status);
    }
    // Also accept OK sent by buggy servers.
    if (status != HttpStatus.SC_MULTI_STATUS && status != HttpStatus.SC_OK) {
        HttpException ex = new HttpException();
        ex.setReasonCode(status);
        throw ex;
    }
    thisResource = false;

    return method.getResponses();
}

From source file:org.apache.webdav.lib.WebdavResource.java

/**
 * Execute PROPFIND method with by propname for the given path.
 * Get list of named WebDAV properties on the given resource.
 *
 * <p>Once used this method, the the status code in the 207
 * reponse is need to be set for the method of WebdavResource.
 *
 * <p>The values of DepthSupport.DEPTH_0, DepthSupport.DEPTH_1
 * DepthSupport.DEPTH_INFINITY is possbile for the depth.
 *
 * @param path the server relative path of the resource to request
 * @param depth The depth.//  w ww  .j a  v a  2s  .  co  m
 * @param properties The named properties.
 * @return an enumeration of <code>ResponseEntity</code>
 * @exception HttpException
 * @exception IOException
 */
public Enumeration propfindMethod(String path, int depth, Vector properties) throws HttpException, IOException {

    setClient();
    // Change the depth for prop
    PropFindMethod method = new PropFindMethod(URIUtil.encodePath(path), depth, properties.elements());

    method.setDebug(debug);
    method.setFollowRedirects(this.followRedirects);
    generateTransactionHeader(method);
    generateAdditionalHeaders(method);
    int status = client.executeMethod(method);

    // Set status code for this resource.
    if (thisResource == true) {
        // Set the status code.
        setStatusCode(method.getStatusLine().getStatusCode());
    }
    // Also accept OK sent by buggy servers.
    if (status != HttpStatus.SC_MULTI_STATUS && status != HttpStatus.SC_OK) {
        HttpException ex = new HttpException();
        ex.setReasonCode(status);
        throw ex;
    }
    thisResource = false;

    return method.getResponses();
}

From source file:org.apache.webdav.lib.WebdavResource.java

/**
 * Execute PROPFIND method for the given path and properties.
 * Get list of given WebDAV properties on the given resource.
 *
 * @param path the server relative path of the resource to request
 * @param properties the WebDAV properties to find.
 * @return Enumeration list of WebDAV properties on a resource.
 * @exception HttpException//from  w w  w.  j  a v a  2s . c o m
 * @exception IOException
 */
public Enumeration propfindMethod(String path, Vector properties) throws HttpException, IOException {

    setClient();
    // Default depth=0, type=by_name
    PropFindMethod method = new PropFindMethod(URIUtil.encodePath(path), DepthSupport.DEPTH_0,
            properties.elements());
    method.setDebug(debug);
    method.setFollowRedirects(this.followRedirects);
    generateTransactionHeader(method);
    generateAdditionalHeaders(method);
    int status = client.executeMethod(method);

    // Also accept OK sent by buggy servers.
    if (status != HttpStatus.SC_MULTI_STATUS && status != HttpStatus.SC_OK) {
        HttpException ex = new HttpException();
        ex.setReasonCode(status);
        throw ex;
    }

    // It contains the results.
    Vector results = new Vector();

    Enumeration responses = method.getResponses();
    if (responses.hasMoreElements()) {
        ResponseEntity response = (ResponseEntity) responses.nextElement();
        String href = response.getHref();

        // Set status code for this resource.
        if ((thisResource == true) && (response.getStatusCode() > 0))
            setStatusCode(response.getStatusCode());
        thisResource = false;

        Enumeration responseProperties = method.getResponseProperties(href);
        while (responseProperties.hasMoreElements()) {
            Property property = (Property) responseProperties.nextElement();
            results.addElement(property.getPropertyAsString());
        }
    }

    return results.elements();
}

From source file:org.apache.webdav.lib.WebdavResource.java

/**
 * Execute REPORT method.// w w  w.  jav a2 s .  c o m
 * This method is for the special Access Control Reports:
 * - acl-principal-prop-set (not supported yet)
 * - principal-match (not supported yet)
 * - principal-property-search
 * - principal-search-property-set (not supported yet)
 *
 * @param path the server relative path of the resource to request
 * @param properties The named properties.
 * @return an enumeration of <code>ResponseEntity</code>
 * @exception HttpException
 * @exception IOException
 */
public Enumeration aclReportMethod(String path, Collection properties, int reportType)
        throws HttpException, IOException {

    setClient();
    AclReportMethod method = new AclReportMethod(URIUtil.encodePath(path), properties, DepthSupport.DEPTH_0,
            reportType);

    method.setDebug(debug);
    method.setFollowRedirects(this.followRedirects);
    generateTransactionHeader(method);
    generateAdditionalHeaders(method);
    int status = client.executeMethod(method);

    // Set status code for this resource.
    if (thisResource == true) {
        // Set the status code.
        setStatusCode(method.getStatusLine().getStatusCode());
    }

    //slide/tamino delivers status code OK.
    //can be removed when the server sends MULTI_STATUS
    if (status != HttpStatus.SC_MULTI_STATUS && status != HttpStatus.SC_OK) {
        HttpException ex = new HttpException();
        ex.setReasonCode(status);
        throw ex;
    }
    thisResource = false;

    return method.getResponses();
}

From source file:org.apache.webdav.lib.WebdavResource.java

/**
 * Asks the server whether events for a given subscription are fired.
 * @param contentLocation URL path returned by the SUBSCRIBE methods
 *                        Content-Location header
 * @param subscriptionId id of the subscription
 * @return <code>true</code> if an event was fired
 *///from w  ww . j  a va 2 s. co  m
public boolean pollMethod(String contentLocation, int subscriptionId) throws HttpException, IOException {
    setClient();

    PollMethod method = new PollMethod(contentLocation);
    method.setDebug(debug);
    method.setFollowRedirects(this.followRedirects);

    method.addSubscriptionId(subscriptionId);
    generateTransactionHeader(method);
    generateAdditionalHeaders(method);

    int statusCode = client.executeMethod(method);

    if (statusCode == HttpStatus.SC_MULTI_STATUS) {
        return method.getSubscriptionsWithEvents().size() > 0;
    } else {
        return false;
    }
}

From source file:org.apache.webdav.lib.methods.LockMethod.java

/**
 * Parse response./*from  www  . j  a  v a 2  s  .com*/
 *
 * @param input Input stream
 */
public void parseResponse(InputStream input, HttpState state, HttpConnection conn)
        throws IOException, HttpException {
    int status = getStatusLine().getStatusCode();
    if (status == HttpStatus.SC_OK || status == HttpStatus.SC_CREATED || status == HttpStatus.SC_MULTI_STATUS) {

        parseXMLResponse(input);

        NodeList list = getResponseDocument().getDocumentElement().getElementsByTagNameNS("DAV:", "locktoken");

        if (list.getLength() == 1) {
            Element locktoken = (Element) list.item(0);
            NodeList list2 = locktoken.getElementsByTagNameNS("DAV:", "href");
            if (list2.getLength() == 1) {
                this.lockToken = DOMUtils.getTextValue(list2.item(0));
                if (state instanceof WebdavState) {
                    /* 
                     * lockMethod/unlockMethod take unescaped URIs but LockMathod.getPath()
                     * func returns an escaped URI so searching for the lock by path name in 
                     * the state object doesn't work. Convert escaped back to unescaped.
                     */
                    ((WebdavState) state).addLock(URIUtil.decode(getPath()), this.lockToken);
                }
            }
        }

        list = getResponseDocument().getDocumentElement().getElementsByTagNameNS("DAV:", "owner");

        if (list.getLength() == 1) {
            Element owner = (Element) list.item(0);

            this.owner = DOMUtils.getTextValue(owner);
        }
    }
}

From source file:org.apache.webdav.lib.methods.PollMethod.java

public void parseResponse(InputStream input, HttpState state, HttpConnection conn)
        throws IOException, HttpException {
    int status = getStatusLine().getStatusCode();

    if (status == HttpStatus.SC_MULTI_STATUS) {
        parseXMLResponse(input);//from   w  w  w .j a  v  a 2 s.  c  om
        NodeList list = getResponseDocument().getDocumentElement().getElementsByTagNameNS("DAV:", "response");

        for (int i = 0; i < list.getLength(); i++) {
            Element e = (Element) list.item(i);

            NodeList s = e.getElementsByTagNameNS("DAV:", "status");
            if (s.getLength() > 0) {
                Element response = ((Element) (s.item(0)).getParentNode());
                String statusText = DOMUtils.getTextValue(s.item(0));
                if (statusText.indexOf(" 200 ") != -1) {
                    // polled subscriptions for which *AN* event is fired
                    NodeList p = response.getElementsByTagNameNS(EXCHANGE_NS, "subscriptionID");
                    if (p.getLength() > 0) {
                        NodeList li = ((Element) p.item(0)).getElementsByTagName("li");
                        for (int l = 0; l < li.getLength(); ++l) {
                            String id = DOMUtils.getTextValue(li.item(i));
                            this.subscriptionsWithEvents.add(Integer.getInteger(id));
                        }
                    }
                } else if (statusText.indexOf(" 204 ") != -1) {
                    // polled subscriptions for which *NO* event is fired
                    NodeList p = response.getElementsByTagNameNS(EXCHANGE_NS, "subscriptionID");
                    if (p.getLength() > 0) {
                        NodeList li = ((Element) p.item(0)).getElementsByTagName("li");
                        for (int l = 0; l < li.getLength(); ++l) {
                            String id = DOMUtils.getTextValue(li.item(i));
                            this.subscriptionsWithoutEvents.add(Integer.getInteger(id));
                        }
                    }
                }
            }
        }
    }
}

From source file:org.apache.webdav.lib.NotificationListener.java

protected void poll(String notifiedSubscribers) {
    StringBuffer registeredSubscribers = new StringBuffer(256);
    StringTokenizer tokenizer = new StringTokenizer(notifiedSubscribers, ",");
    boolean first = true;
    while (tokenizer.hasMoreTokens()) {
        String subscriber = tokenizer.nextToken().trim();
        if (isRegistered(Integer.valueOf(subscriber).intValue())) {
            if (!first)
                registeredSubscribers.append(',');
            registeredSubscribers.append(subscriber);
            first = false;/*w  w w . j  a  v a2  s . co m*/
        }
    }
    if (!first) {
        String pollSubscribers = registeredSubscribers.toString();
        logger.log(Level.INFO, "Poll for subscribers: " + pollSubscribers);
        PollMethod pollMethod = new PollMethod(repositoryDomain + "/");
        pollMethod.addRequestHeader(SubscribeMethod.H_SUBSCRIPTION_ID, pollSubscribers);
        try {
            pollMethod.setDoAuthentication(true);
            HttpState httpState = new HttpState();
            httpState.setCredentials(null, repositoryHost, credentials);
            HttpConnection httpConnection = new HttpConnection(repositoryHost, repositoryPort, protocol);
            httpConnection.setConnectionTimeout(CONNECTION_TIMEOUT);
            int state = pollMethod.execute(httpState, httpConnection);
            if (state == HttpStatus.SC_MULTI_STATUS) {
                List events = pollMethod.getEvents();
                for (Iterator i = events.iterator(); i.hasNext();) {
                    Event event = (Event) i.next();
                    fireEvent(event.getId(), event.getInformation());
                }
            } else {
                logger.log(Level.SEVERE, "Poll failed. State: " + state);
            }
        } catch (IOException e) {
            logger.log(Level.SEVERE, "Poll for subscribers '" + subscribers + "' failed!");
        }
    }
}

From source file:org.elasticsearch.hadoop.rest.RestClient.java

byte[] execute(HttpMethodBase method, boolean checkStatus) {
    try {/*from   ww w  .j a  va  2  s  . c  o  m*/
        int status = client.executeMethod(method);
        if (checkStatus && status >= HttpStatus.SC_MULTI_STATUS) {
            String body;
            try {
                body = method.getResponseBodyAsString();
            } catch (IOException ex) {
                body = "";
            }
            throw new IllegalStateException(String.format("[%s] on [%s] failed; server[%s] returned [%s]",
                    method.getName(), method.getURI(), client.getHostConfiguration().getHostURL(), body));
        }
        return method.getResponseBody();
    } catch (IOException io) {
        String target;
        try {
            target = method.getURI().toString();
        } catch (IOException ex) {
            target = method.getPath();
        }
        throw new IllegalStateException(
                String.format("Cannot get response body for [%s][%s]", method.getName(), target));
    } finally {
        method.releaseConnection();
    }
}