Example usage for org.apache.commons.httpclient HttpMethodBase getName

List of usage examples for org.apache.commons.httpclient HttpMethodBase getName

Introduction

In this page you can find the example usage for org.apache.commons.httpclient HttpMethodBase getName.

Prototype

@Override
public abstract String getName();

Source Link

Document

Obtains the name of the HTTP method as used in the HTTP request line, for example <tt>"GET"</tt> or <tt>"POST"</tt>.

Usage

From source file:org.alfresco.repo.remoteconnector.RemoteConnectorRequestImpl.java

private RemoteConnectorRequestImpl(String url, HttpMethodBase method) {
    this.url = url;
    this.method = method;
    this.methodName = method.getName();
}

From source file:org.apache.ivy.util.url.HttpClientHandler.java

private boolean checkStatusCode(URL url, HttpMethodBase method) throws IOException {
    int status = method.getStatusCode();
    if (status == HttpStatus.SC_OK) {
        return true;
    }/* w  w w .ja va 2 s.  c o m*/

    // IVY-1328: some servers return a 204 on a HEAD request
    if ("HEAD".equals(method.getName()) && (status == 204)) {
        return true;
    }

    Message.debug("HTTP response status: " + status + " url=" + url);
    if (status == HttpStatus.SC_PROXY_AUTHENTICATION_REQUIRED) {
        Message.warn("Your proxy requires authentication.");
    } else if (String.valueOf(status).startsWith("4")) {
        Message.verbose("CLIENT ERROR: " + method.getStatusText() + " url=" + url);
    } else if (String.valueOf(status).startsWith("5")) {
        Message.error("SERVER ERROR: " + method.getStatusText() + " url=" + url);
    }

    return false;
}

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

byte[] execute(HttpMethodBase method, boolean checkStatus) {
    try {//from   w  ww  .ja  va2s  .  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();
    }
}

From source file:org.fao.geonet.csw.common.requests.CatalogRequest.java

private void setupSentData(HttpMethodBase httpMethod) {
    sentData = httpMethod.getName() + " " + httpMethod.getPath();

    if (httpMethod.getQueryString() != null)
        sentData += "?" + httpMethod.getQueryString();

    sentData += "\r\n";

    for (Header h : httpMethod.getRequestHeaders())
        sentData += h;//from ww w.  j  a  v  a2s  .  c  om

    sentData += "\r\n";

    if (httpMethod instanceof PostMethod)
        sentData += postData;
}

From source file:org.fao.oaipmh.requests.Transport.java

private void setupSentData(HttpMethodBase httpMethod) {
    sentData = httpMethod.getName() + " " + httpMethod.getPath();

    if (httpMethod.getQueryString() != null)
        sentData += "?" + httpMethod.getQueryString();

    sentData += "\r\n";

    for (Header h : httpMethod.getRequestHeaders())
        sentData += h;/*from  w  ww  . ja v  a2 s . c o  m*/

    sentData += "\r\n";
}

From source file:org.sonatype.nexus.proxy.storage.remote.commonshttpclient.CommonsHttpClientRemoteStorage.java

@Override
protected boolean checkRemoteAvailability(long newerThen, ProxyRepository repository,
        ResourceStoreRequest request, boolean isStrict) throws RemoteStorageException {
    URL remoteURL = getAbsoluteUrlFromBase(repository, request);

    HttpMethodBase method = new HeadMethod(remoteURL.toString());

    int response = HttpStatus.SC_BAD_REQUEST;

    // artifactory hack, it pukes on HEAD so we will try with GET if HEAD fails
    boolean doGet = false;

    try {//from ww w  . j  av  a  2 s  .c om
        response = executeMethod(repository, request, method, remoteURL);
    } catch (RemoteStorageException e) {
        // If HEAD failed, attempt a GET. Some repos may not support HEAD method
        doGet = true;

        getLogger().debug("HEAD method failed, will attempt GET.  Exception: " + e.getMessage(), e);
    } finally {
        method.releaseConnection();

        // HEAD returned error, but not exception, try GET before failing
        if (!doGet && response != HttpStatus.SC_OK) {
            doGet = true;

            getLogger().debug("HEAD method failed, will attempt GET.  Status: " + response);
        }
    }

    if (doGet) {
        // create a GET
        method = new GetMethod(remoteURL.toString());

        try {
            // execute it
            response = executeMethod(repository, request, method, remoteURL);
        } finally {
            // and release it immediately
            method.releaseConnection();
        }
    }

    // if we are not strict and remote is S3
    if (!isStrict && isRemotePeerAmazonS3Storage(repository)) {
        // if we are relaxed, we will accept any HTTP response code below 500. This means anyway the HTTP
        // transaction succeeded. This method was never really detecting that the remoteUrl really denotes a root of
        // repository (how could we do that?)
        // this "relaxed" check will help us to "pass" S3 remote storage.
        return response >= HttpStatus.SC_OK && response <= HttpStatus.SC_INTERNAL_SERVER_ERROR;
    } else {
        // non relaxed check is strict, and will select only the OK response
        if (response == HttpStatus.SC_OK) {
            // we have it
            // we have newer if this below is true
            return makeDateFromHeader(method.getResponseHeader("last-modified")) > newerThen;
        } else if ((response >= HttpStatus.SC_MULTIPLE_CHOICES && response < HttpStatus.SC_BAD_REQUEST)
                || response == HttpStatus.SC_NOT_FOUND) {
            return false;
        } else {
            throw new RemoteStorageException("Unexpected response code while executing " + method.getName()
                    + " method [repositoryId=\"" + repository.getId() + "\", requestPath=\""
                    + request.getRequestPath() + "\", remoteUrl=\"" + remoteURL.toString()
                    + "\"]. Expected: \"SUCCESS (200)\". Received: " + response + " : "
                    + HttpStatus.getStatusText(response));
        }
    }
}

From source file:org.svenk.redmine.core.client.AbstractRedmineClient.java

protected <T extends Object> T executeMethod(HttpMethodBase method, IRedmineResponseParser<T> parser,
        IProgressMonitor monitor, int... expectedSC) throws RedmineException {
    monitor = Policy.monitorFor(monitor);
    method.setFollowRedirects(false);// w  w  w.  j a  v a2s . c  o  m
    HostConfiguration hostConfiguration = WebUtil.createHostConfiguration(httpClient, location, monitor);

    T response = null;
    try {
        int sc = executeMethod(method, hostConfiguration, monitor);

        if (parser != null && expectedSC != null) {
            boolean found = false;
            for (int i : expectedSC) {
                if (i == sc) {
                    InputStream input = WebUtil.getResponseBodyAsStream(method, monitor);
                    try {
                        found = true;
                        response = parser.parseResponse(input, sc);
                    } finally {
                        input.close();
                    }
                    break;
                }
            }
            if (!found) {
                String msg = Messages.AbstractRedmineClient_UNEXPECTED_RESPONSE_CODE;
                msg = String.format(msg, sc, method.getPath(), method.getName());
                IStatus status = new Status(IStatus.ERROR, RedmineCorePlugin.PLUGIN_ID, msg);
                StatusHandler.fail(status);
                throw new RedmineStatusException(status);
            }
        }
    } catch (RedmineErrorException e) {
        IStatus status = RedmineCorePlugin.toStatus(e, null);
        StatusHandler.fail(status);
        throw new RedmineStatusException(status);
    } catch (IOException e) {
        IStatus status = RedmineCorePlugin.toStatus(e, null);
        StatusHandler.log(status);
        throw new RedmineStatusException(status);
    } finally {
        method.releaseConnection();
    }

    return response;
}

From source file:org.wso2.carbon.appfactory.common.util.MutualAuthHttpClient.java

/**
 * Send rest request.//from w  ww  .  j  a  v a 2  s.  c  o  m
 *
 * @param httpClient client object
 * @param method     method type
 * @throws org.wso2.carbon.appfactory.common.AppFactoryException
 */
private static ServerResponse send(HttpClient httpClient, HttpMethodBase method) throws AppFactoryException {
    int responseCode;
    String responseString = null;
    try {
        responseCode = httpClient.executeMethod(method);
    } catch (IOException e) {
        String msg = "Error occurred while executing method " + method.getName();
        log.error(msg, e);
        throw new AppFactoryException(msg, e);
    }
    try {
        responseString = method.getResponseBodyAsString();
    } catch (IOException e) {
        String msg = "error while getting response as String for " + method.getName();
        log.error(msg, e);
        throw new AppFactoryException(msg, e);

    } finally {
        method.releaseConnection();
    }
    if (log.isDebugEnabled()) {
        log.debug("Response id: " + responseCode + " message:  " + responseString);
    }
    return new ServerResponse(responseString, responseCode);
}

From source file:org.wso2.carbon.appfactory.s4.integration.utils.DomainMappingUtils.java

/**
 * Send rest request.//from   w w  w.j  av  a 2  s .  c  om
 *
 * @param httpClient client object
 * @param method     method type
 * @throws AppFactoryException
 */
private static DomainMappingResponse send(HttpClient httpClient, HttpMethodBase method)
        throws AppFactoryException {
    int responseCode;
    String responseString = null;
    try {
        responseCode = httpClient.executeMethod(method);
    } catch (IOException e) {
        String msg = "Error occurred while executing method " + method.getName();
        log.error(msg, e);
        throw new AppFactoryException(msg, e);
    }
    try {
        responseString = method.getResponseBodyAsString();
    } catch (IOException e) {
        String msg = "error while getting response as String for " + method.getName();
        log.error(msg, e);
        throw new AppFactoryException(msg, e);

    } finally {
        method.releaseConnection();
    }
    if (log.isDebugEnabled()) {
        log.debug(" DomainMappingManagementService response id: " + responseCode + " message:  "
                + responseString);
    }
    return new DomainMappingResponse(responseString, responseCode);
}

From source file:org.wso2.carbon.appmgt.mdm.wso2mdm.MDMOperationsImpl.java

private boolean executeMethod(String tokenApiURL, String clientKey, String clientSecret, String authUser,
        String authPass, HttpClient httpClient, HttpMethodBase httpMethod) {
    String authKey = getAPIToken(tokenApiURL, clientKey, clientSecret, authUser, authPass, false);
    if (log.isDebugEnabled())
        log.debug("Access token received : " + authKey);
    //String authKey = "12345";
    try {/*from  w  w w .j a v  a2  s  .  com*/
        int statusCode = 401;
        int tries = 0;
        while (statusCode != 200) {
            if (log.isDebugEnabled())
                log.debug("Trying to call API : trying for " + (tries + 1) + " time(s)");
            httpMethod.setRequestHeader("Authorization", "Bearer " + authKey);
            if (log.isDebugEnabled())
                log.debug("Sending " + httpMethod.getName() + " request to " + httpMethod.getURI());
            statusCode = httpClient.executeMethod(httpMethod);
            if (log.isDebugEnabled())
                log.debug("Status code received : " + statusCode);

            if (++tries >= 3) {
                log.info("API Call failed for the 3rd time: No or Unauthorized Access Aborting...");
                return false;
            }
            if (statusCode == 401) {
                authKey = getAPIToken(tokenApiURL, clientKey, clientSecret, authUser, authPass, true);
                if (log.isDebugEnabled())
                    log.debug("Access token getting again, Access token received :  " + authKey + " in  try "
                            + tries);
            }

        }
        return true;
    } catch (IOException e) {
        String errorMessage = "No OK response received form the API";
        if (log.isDebugEnabled()) {
            log.error(errorMessage, e);
        } else {
            log.error(errorMessage);
        }
        return false;
    }
}