Example usage for org.apache.commons.httpclient.methods OptionsMethod getStatusLine

List of usage examples for org.apache.commons.httpclient.methods OptionsMethod getStatusLine

Introduction

In this page you can find the example usage for org.apache.commons.httpclient.methods OptionsMethod getStatusLine.

Prototype

@Override
public StatusLine getStatusLine() 

Source Link

Document

Provides access to the response status line.

Usage

From source file:edu.indiana.d2i.registryext.RegistryExtAgent.java

/**
 * Only return file names (no directory)
 * /*from w w  w  . j  a va  2 s .co  m*/
 * @param repoPath
 *            path in registry
 * @return
 * @throws RegistryExtException
 * @throws ClientProtocolException
 * @throws IOException
 * @throws IllegalStateException
 * @throws JAXBException
 * @throws OAuthProblemException
 * @throws OAuthSystemException
 */
public ListResourceResponse getAllChildren(String repoPath)
        throws RegistryExtException, ClientProtocolException, IOException, IllegalStateException, JAXBException,
        OAuthSystemException, OAuthProblemException {

    Map<String, Object> session = ActionContext.getContext().getSession();

    String accessToken = (String) session.get(Constants.SESSION_TOKEN);
    int statusCode = 200;

    String requestURL = composeURL(FILEOPPREFIX, repoPath);

    if (logger.isDebugEnabled()) {
        logger.debug("List request URL=" + requestURL);
    }

    HttpClient httpclient = new HttpClient();
    OptionsMethod options = new OptionsMethod(requestURL);

    options.addRequestHeader("Accept", "application/xml");
    options.addRequestHeader("Authorization", "Bearer " + accessToken);

    try {

        httpclient.executeMethod(options);

        statusCode = options.getStatusLine().getStatusCode();

        /* handle token expiration */
        if (statusCode == 401) {
            logger.info(String.format("Access token %s expired, going to refresh it", accessToken));

            refreshToken(session);

            // use refreshed access token
            accessToken = (String) session.get(Constants.SESSION_TOKEN);
            options.addRequestHeader("Authorization", "Bearer " + accessToken);

            // re-send the request
            httpclient.executeMethod(options);

            statusCode = options.getStatusLine().getStatusCode();

        }

        if (statusCode == 404) {
            logger.equals("List request URL=" + requestURL + " doesn't exist");
            return new ListResourceResponse(null, statusCode);
        }

        if (statusCode != 200) {
            throw new RegistryExtException("Failed in listing children : HTTP error code : "
                    + options.getStatusLine().getStatusCode());
        }

        Entry xmlFile = FileSchemaUtil.readConfigXML(options.getResponseBodyAsStream());

        if (logger.isDebugEnabled()) {
            logger.debug(String.format("Returned content:%n%s", FileSchemaUtil.toXMLString(xmlFile)));

        }

        Entries entries = xmlFile.getEntries();

        return new ListResourceResponse(entries, statusCode);
    } finally {
        if (options != null)
            options.releaseConnection();
    }

}

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

/**
 * Execute OPTIONS method for the given http URL.
 *
 * @param httpURL the http URL./*from  ww  w. j a  v  a 2  s  .  c o m*/
 * @return the allowed methods and capabilities.
 * @exception HttpException
 * @exception IOException
 */
public Enumeration optionsMethod(HttpURL httpURL) throws HttpException, IOException {

    HttpClient client = getSessionInstance(httpURL, true);

    OptionsMethod method = new OptionsMethod(httpURL.getEscapedPath());
    method.setDebug(debug);
    method.setFollowRedirects(this.followRedirects);

    generateTransactionHeader(method);
    generateAdditionalHeaders(method);
    client.executeMethod(method);

    Vector options = new Vector();
    int statusCode = method.getStatusLine().getStatusCode();
    if (statusCode >= 200 && statusCode < 300) {
        // check if the specific method is possbile
        Enumeration allowedMethods = method.getAllowedMethods();
        while (allowedMethods.hasMoreElements()) {
            options.addElement(allowedMethods.nextElement());
        }
        // check WebDAV capabilities.
        Enumeration davCapabilities = method.getDavCapabilities();
        while (davCapabilities.hasMoreElements()) {
            options.addElement(davCapabilities.nextElement());
        }
        Enumeration responses = method.getResponses();
        if (responses.hasMoreElements()) {
            ResponseEntity response = (ResponseEntity) responses.nextElement();
            Enumeration workspaces = response.getWorkspaces();
            String sResult = "";
            while (workspaces.hasMoreElements()) {
                sResult += workspaces.nextElement().toString();
            }
            Enumeration histories = response.getHistories();
            while (histories.hasMoreElements()) {
                sResult += histories.nextElement().toString();
            }
            // Set status code for this resource.
            if ((thisResource == true) && (response.getStatusCode() > 0))
                setStatusCode(response.getStatusCode());
            thisResource = false;
            options.addElement(sResult);
        }
    }

    return options.elements();
}

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

/**
 * Execute OPTIONS method for the given http URL, DELTAV
 *
 * @param httpURL the http URL.// w  ww  .  ja v  a  2 s. co m
 * @return the allowed methods and capabilities.
 * @exception HttpException
 * @exception IOException
 */
public Enumeration optionsMethod(HttpURL httpURL, int type) throws HttpException, IOException {

    HttpClient client = getSessionInstance(httpURL, true);

    OptionsMethod method = new OptionsMethod(httpURL.getEscapedPath(), type);
    method.setDebug(debug);
    method.setFollowRedirects(this.followRedirects);

    generateTransactionHeader(method);
    generateAdditionalHeaders(method);
    client.executeMethod(method);

    Vector options = new Vector();
    int statusCode = method.getStatusLine().getStatusCode();
    if (statusCode >= 200 && statusCode < 300) {
        Enumeration responses = method.getResponses();
        if (responses.hasMoreElements()) {
            ResponseEntity response = (ResponseEntity) responses.nextElement();
            // String sResult="";
            if (type == OPTIONS_WORKSPACE) {
                Enumeration workspaces = response.getWorkspaces();
                while (workspaces.hasMoreElements()) {
                    options.add(workspaces.nextElement().toString());
                }
            } else if (type == OPTIONS_VERSION_HISTORY) {
                Enumeration histories = response.getHistories();
                while (histories.hasMoreElements()) {
                    options.add(histories.nextElement().toString());
                }
            }

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

    return options.elements();
}

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

/**
 * Execute OPTIONS method for the given path.
 *
 * @param path the server relative path of the resource to request
 * @return the allowed methods and capabilities.
 * @exception HttpException// ww  w .j  a  v a 2  s . c om
 * @exception IOException
 * @see #getAllowedMethods()
 */
public Enumeration optionsMethod(String path, int type) throws HttpException, IOException {

    setClient();

    OptionsMethod method = new OptionsMethod(URIUtil.encodePath(path), type);
    method.setDebug(debug);
    method.setFollowRedirects(this.followRedirects);

    generateTransactionHeader(method);
    generateAdditionalHeaders(method);
    client.executeMethod(method);

    Vector options = new Vector();
    int statusCode = method.getStatusLine().getStatusCode();
    if (statusCode >= 200 && statusCode < 300) {
        Enumeration responses = method.getResponses();
        if (responses.hasMoreElements()) {
            ResponseEntity response = (ResponseEntity) responses.nextElement();
            // String sResult="";
            if (type == OPTIONS_WORKSPACE) {
                Enumeration workspaces = response.getWorkspaces();
                while (workspaces.hasMoreElements()) {
                    options.add(workspaces.nextElement().toString());
                }
            } else if (type == OPTIONS_VERSION_HISTORY) {
                Enumeration histories = response.getHistories();
                while (histories.hasMoreElements()) {
                    options.add(histories.nextElement().toString());
                }
            }

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

    return options.elements();
}