Example usage for org.apache.http.client.methods HttpGet METHOD_NAME

List of usage examples for org.apache.http.client.methods HttpGet METHOD_NAME

Introduction

In this page you can find the example usage for org.apache.http.client.methods HttpGet METHOD_NAME.

Prototype

String METHOD_NAME

To view the source code for org.apache.http.client.methods HttpGet METHOD_NAME.

Click Source Link

Usage

From source file:org.jolokia.client.request.J4pRequestHandler.java

/**
 * Get the HttpRequest for executing the given single request
 *
 * @param pRequest request to convert//w  w  w  .j av  a 2 s  .  c o m
 * @param pPreferredMethod HTTP method preferred
 * @param pProcessingOptions optional map of processiong options
 * @return the request used with HttpClient to obtain the result.
 */
public HttpUriRequest getHttpRequest(J4pRequest pRequest, String pPreferredMethod,
        Map<J4pQueryParameter, String> pProcessingOptions)
        throws UnsupportedEncodingException, URISyntaxException {
    String method = pPreferredMethod;
    if (method == null) {
        method = pRequest.getPreferredHttpMethod();
    }
    if (method == null) {
        method = doUseProxy(pRequest) ? HttpPost.METHOD_NAME : HttpGet.METHOD_NAME;
    }
    String queryParams = prepareQueryParameters(pProcessingOptions);

    // GET request
    if (method.equals(HttpGet.METHOD_NAME)) {
        if (doUseProxy(pRequest)) {
            throw new IllegalArgumentException("Proxy mode can only be used with POST requests");
        }
        List<String> parts = pRequest.getRequestParts();
        // If parts == null the request decides, that POST *must* be used
        if (parts != null) {
            String base = prepareBaseUrl(j4pServerUrl);
            StringBuilder requestPath = new StringBuilder(base);
            requestPath.append(pRequest.getType().getValue());
            for (String p : parts) {
                requestPath.append("/");
                requestPath.append(escape(p));
            }
            return new HttpGet(createRequestURI(requestPath.toString(), queryParams));
        }
    }

    // We are using a post method as fallback
    JSONObject requestContent = getJsonRequestContent(pRequest);
    HttpPost postReq = new HttpPost(createRequestURI(j4pServerUrl.getPath(), queryParams));
    postReq.setEntity(new StringEntity(requestContent.toJSONString(), "utf-8"));
    return postReq;
}

From source file:com.manning.androidhacks.hack023.net.FollowPostRedirectHandler.java

/**
 * HttpClient is compliant with the requirements of the HTTP specification
 * (RFC 2616) and does not automatically redirect other methods than GET and
 * HEAD. We have to override this method to automatically follow redirects
 * when using the POST method./*w  w  w.j  a v a 2s .  c  om*/
 */
@Override
public boolean isRedirectRequested(final HttpResponse response, final HttpContext context) {
    if (response == null) {
        throw new IllegalArgumentException("HTTP response may not be null");
    }

    int statusCode = response.getStatusLine().getStatusCode();
    switch (statusCode) {
    case HttpStatus.SC_MOVED_TEMPORARILY:
    case HttpStatus.SC_MOVED_PERMANENTLY:
    case HttpStatus.SC_TEMPORARY_REDIRECT:
        HttpRequest request = (HttpRequest) context.getAttribute(ExecutionContext.HTTP_REQUEST);
        String method = request.getRequestLine().getMethod();
        return method.equalsIgnoreCase(HttpGet.METHOD_NAME) || method.equalsIgnoreCase(HttpHead.METHOD_NAME)
                || method.equalsIgnoreCase(HttpPost.METHOD_NAME);
    case HttpStatus.SC_SEE_OTHER:
        return true;
    default:
        return false;
    }
}

From source file:org.apache.metamodel.elasticsearch.rest.ElasticSearchRestClient.java

private static Request getMappings(final String indexName) {
    return new Request(HttpGet.METHOD_NAME, "/" + indexName, Collections.emptyMap(), null);
}

From source file:org.jboss.as.test.integration.management.console.WebConsoleRedirectionTestCase.java

private HttpURLConnection getConnection() throws Exception {
    final URL url = new URL("http://" + TestSuiteEnvironment.getServerAddress() + ":"
            + TestSuiteEnvironment.getServerPort() + "/");
    final HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    assertNotNull(connection);/*  w  w w .  ja  v  a2s.co  m*/
    connection.setInstanceFollowRedirects(false);
    connection.setRequestMethod(HttpGet.METHOD_NAME);
    connection.connect();
    return connection;
}

From source file:org.apache.edgent.connectors.http.runtime.HttpRequester.java

@Override
public R apply(T t) {

    if (client == null)
        client = clientCreator.get();/*from  ww  w  .  j  av a2 s . com*/

    String m = method.apply(t);
    String uri = url.apply(t);
    HttpUriRequest request;

    switch (m) {

    case HttpGet.METHOD_NAME:
        request = new HttpGet(uri);
        break;
    case HttpDelete.METHOD_NAME:
        request = new HttpDelete(uri);
        break;
    case HttpPost.METHOD_NAME:
        request = new HttpPost(uri);
        break;
    case HttpPut.METHOD_NAME:
        request = new HttpPut(uri);
        break;

    default:
        throw new IllegalArgumentException();
    }

    // If entity is not null means http request should have a body
    if (entity != null) {

        HttpEntity body = entity.apply(t);

        if (request instanceof HttpEntityEnclosingRequest == false) {
            throw new IllegalArgumentException("Http request does not support body");
        }

        ((HttpEntityEnclosingRequest) request).setEntity(body);
    }

    try {
        try (CloseableHttpResponse response = client.execute(request)) {
            return responseProcessor.apply(t, response);
        }

    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:spaceRedirectStrategy.java

public boolean isRedirected(final HttpRequest request, final HttpResponse response, final HttpContext context)
        throws ProtocolException {
    if (response == null) {
        throw new IllegalArgumentException("HTTP response may not be null");
    }// w w w.  j  a v a 2s  . c o m

    int statusCode = response.getStatusLine().getStatusCode();
    String method = request.getRequestLine().getMethod();
    Header locationHeader = response.getFirstHeader("location");
    switch (statusCode) {
    case HttpStatus.SC_MOVED_TEMPORARILY:
        return (method.equalsIgnoreCase(HttpGet.METHOD_NAME) || method.equalsIgnoreCase(HttpHead.METHOD_NAME))
                && locationHeader != null;
    case HttpStatus.SC_MOVED_PERMANENTLY:
    case HttpStatus.SC_TEMPORARY_REDIRECT:
        return method.equalsIgnoreCase(HttpGet.METHOD_NAME) || method.equalsIgnoreCase(HttpHead.METHOD_NAME);
    case HttpStatus.SC_SEE_OTHER:
        return true;
    default:
        return false;
    } //end of switch
}

From source file:org.deviceconnect.message.http.impl.factory.AbstractHttpMessageFactory.java

/**
 * HTTP 1???dConnect??.//from  w ww  . jav  a2  s . c om
 * @param message HTTP
 * @return dConnect
 */
protected DConnectMessage parseFirstLine(final M message) {
    mLogger.entering(getClass().getName(), "parseFirstLine", message);
    DConnectMessage dmessage = null;

    if (message instanceof HttpRequest) {

        // put action
        String method = ((HttpRequest) message).getRequestLine().getMethod();
        mLogger.fine("HTTP request method: " + method);

        DConnectRequestMessage drequest = new DConnectRequestMessage();
        if (HttpGet.METHOD_NAME.equals(method)) {
            drequest.setMethod(DConnectRequestMessage.METHOD_GET);
        } else if (HttpPut.METHOD_NAME.equals(method)) {
            drequest.setMethod(DConnectRequestMessage.METHOD_PUT);
        } else if (HttpPost.METHOD_NAME.equals(method)) {
            drequest.setMethod(DConnectRequestMessage.METHOD_POST);
        } else if (HttpDelete.METHOD_NAME.equals(method)) {
            drequest.setMethod(DConnectRequestMessage.METHOD_DELETE);
        } else {
            throw new IllegalArgumentException("invalid http request mehtod: " + method);
        }

        dmessage = drequest;
    } else if (message instanceof HttpResponse) {
        dmessage = new DConnectResponseMessage();
    } else {
        throw new IllegalArgumentException(
                "unkown http message class instance: " + message.getClass().getName());
    }

    mLogger.exiting(getClass().getName(), "parseFirstLine", dmessage);
    return dmessage;
}

From source file:com.bincode.util.DefaultRedirectHandler.java

public boolean isRedirectRequested(final HttpResponse response, final HttpContext context) {
    if (response == null) {
        throw new IllegalArgumentException("HTTP response may not be null");
    }//from  w w  w  . j av  a 2s  . c  o m

    int statusCode = response.getStatusLine().getStatusCode();
    switch (statusCode) {
    case HttpStatus.SC_MOVED_TEMPORARILY:
    case HttpStatus.SC_MOVED_PERMANENTLY:
    case HttpStatus.SC_TEMPORARY_REDIRECT:
        HttpRequest request = (HttpRequest) context.getAttribute(ExecutionContext.HTTP_REQUEST);
        String method = request.getRequestLine().getMethod();
        return method.equalsIgnoreCase(HttpGet.METHOD_NAME) || method.equalsIgnoreCase(HttpHead.METHOD_NAME);
    case HttpStatus.SC_SEE_OTHER:
        return true;
    default:
        return false;
    } //end of switch
}

From source file:org.elasticsearch.client.Request.java

static Request info() {
    return new Request(HttpGet.METHOD_NAME, "/", Collections.emptyMap(), null);
}

From source file:com.cloud.utils.rest.HttpUriRequestBuilderTest.java

@Test
public void testBuildRequestWithParameters() throws Exception {
    final HashMap<String, String> parameters = new HashMap<String, String>();
    parameters.put("key1", "value1");
    final HttpUriRequest request = HttpUriRequestBuilder.create().method(HttpMethods.GET).path("/path")
            .parameters(parameters).build();

    assertThat(request, notNullValue());
    assertThat(request.getURI().getPath(), equalTo("/path"));
    assertThat(request.getURI().getQuery(), equalTo("key1=value1"));
    assertThat(request.getURI().getScheme(), nullValue());
    assertThat(request.getURI().getHost(), nullValue());
    assertThat(request.getMethod(), equalTo(HttpGet.METHOD_NAME));
}