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

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

Introduction

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

Prototype

@Override
public void addRequestHeader(String headerName, String headerValue) 

Source Link

Document

Adds the specified request header, NOT overwriting any previous value.

Usage

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

/**
 * Only return file names (no directory)
 * // ww  w. ja va 2 s .  c o 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();
    }

}