Example usage for org.apache.http.client.methods HttpUriRequest getURI

List of usage examples for org.apache.http.client.methods HttpUriRequest getURI

Introduction

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

Prototype

URI getURI();

Source Link

Document

Returns the URI this request uses, such as <code>http://example.org/path/to/file</code>.

Usage

From source file:com.loganlinn.pivotaltrackie.io.RemoteExecutor.java

/**
 * Execute this {@link HttpUriRequest}, passing a valid response through
 * {@link XmlHandler#parseAndApply(XmlPullParser, ContentResolver)}.
 * @throws ResponseException /*from   w ww.ja va  2 s.c  om*/
 */
public void execute(HttpUriRequest request, XmlHandler handler) throws HandlerException {
    Log.i(TAG, "Executing Remote " + request.getMethod() + " Request: " + request.getURI());

    try {
        // Execute the HttpRequest
        final HttpResponse resp = mHttpClient.execute(request);
        final int status = resp.getStatusLine().getStatusCode();
        Log.i(TAG, "Request Response: " + status);

        final boolean notFound = (status != HttpStatus.SC_NOT_FOUND);

        if (status != HttpStatus.SC_OK && !notFound) {
            throw new HandlerException(
                    "Unexpected server response " + resp.getStatusLine() + " for " + request.getRequestLine());
        }

        if (notFound) {
            final InputStream input = resp.getEntity().getContent();
            // Parse the input stream using the handler
            try {

                final XmlPullParser parser = ParserUtils.newPullParser(input);
                handler.parseAndApply(parser, mResolver);

            } catch (XmlPullParserException e) {
                throw new HandlerException("Malformed response for " + request.getRequestLine(), e);
            } finally {
                if (input != null)
                    input.close();
            }
        } else if (handler.getContentUri() != null) {
            Log.i(TAG, "Deleting " + handler.getContentUri().toString());
            mResolver.delete(handler.getContentUri(), null, null);
        }
    } catch (HandlerException e) {
        throw e;
    } catch (IOException e) {
        throw new HandlerException("Problem reading remote response for " + request.getRequestLine(), e);
    }
}

From source file:net.netheos.pcsapi.request.CResponse.java

public CResponse(HttpUriRequest request, HttpResponse response) {
    method = request.getMethod();//from  w ww .  ja  v a  2s. co m
    uri = request.getURI();

    status = response.getStatusLine().getStatusCode();
    entity = response.getEntity();

    String tmp = null;
    if (entity != null && entity.getContentType() != null) {
        tmp = entity.getContentType().getValue();
    }
    contentType = tmp;

    tmp = response.getStatusLine().getReasonPhrase();
    if (tmp == null || tmp.isEmpty()) {
        tmp = "No reason specified";
    }
    reason = tmp;
    headers = new Headers(response.getAllHeaders());
}

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

public String describe(final HttpRequest object) {
    final StringBuilder sb = new StringBuilder();
    if (object instanceof HttpUriRequest) {
        final HttpUriRequest converted = (HttpUriRequest) object;
        sb.append("method = ").append(converted.getMethod());
        sb.append(", query = ").append(converted.getURI().getQuery());
        sb.append(", payload = ").append(getPayload(object));
    }/* w  w w  .  jav a2  s.co  m*/
    return sb.toString();
}

From source file:com.cloudmine.api.rest.VolleyAsynchronousHttpClient.java

@Override
public <T> void executeCommand(HttpUriRequest command, final Callback<T> callback,
        final ResponseConstructor<T> constructor) {
    try {//from   w ww .  java 2s . c  om
        String url = command.getURI().toString();
        int method = getMethod(command.getMethod());
        final Map<String, String> headers = getHeadersAsMap(command);
        final byte[] body = getBody(command, callback);
        queue.add(new Request<T>(method, url, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                NetworkResponse response = volleyError.networkResponse;
                if (response != null)
                    callback.onCompletion(
                            constructor.construct(new String(response.data), response.statusCode));
                else
                    callback.onFailure(volleyError, "Volley failed");
            }
        }) {
            @Override
            protected Response parseNetworkResponse(NetworkResponse networkResponse) {
                String messageBody = new String(networkResponse.data);
                int responseCode = networkResponse.statusCode;
                T responseBody = constructor.construct(messageBody, responseCode);
                return Response.success(responseBody, getCacheEntry());
            }

            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                return headers;
            }

            public byte[] getBody() {
                return body;
            }

            @Override
            protected void deliverResponse(T response) {
                callback.onCompletion(response);
            }

            @Override
            public String getBodyContentType() {
                return "application/json; charset=" + getParamsEncoding();
            }
        });
    } catch (Throwable t) {
        callback.onFailure(t, "Failed creating request");
    }
}

From source file:org.fcrepo.integration.SanityCheckIT.java

private HttpResponse executeAndVerify(final HttpUriRequest method, final int statusCode) throws IOException {
    logger.debug("Executing: " + method.getMethod() + " to " + method.getURI());
    final HttpResponse response = client.execute(method);

    assertEquals(statusCode, response.getStatusLine().getStatusCode());
    return response;
}

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));
}

From source file:org.springframework.cloud.netflix.ribbon.apache.RibbonLoadBalancingHttpClient.java

@Override
public RibbonApacheHttpResponse execute(RibbonApacheHttpRequest request, final IClientConfig configOverride)
        throws Exception {
    final RequestConfig.Builder builder = RequestConfig.custom();
    IClientConfig config = configOverride != null ? configOverride : this.config;
    builder.setConnectTimeout(config.get(CommonClientConfigKey.ConnectTimeout, this.connectTimeout));
    builder.setSocketTimeout(config.get(CommonClientConfigKey.ReadTimeout, this.readTimeout));
    builder.setRedirectsEnabled(config.get(CommonClientConfigKey.FollowRedirects, this.followRedirects));

    final RequestConfig requestConfig = builder.build();

    if (isSecure(configOverride)) {
        final URI secureUri = UriComponentsBuilder.fromUri(request.getUri()).scheme("https").build().toUri();
        request = request.withNewUri(secureUri);
    }/* w w  w.  j  a  v a2 s .c  om*/

    final HttpUriRequest httpUriRequest = request.toRequest(requestConfig);
    final HttpResponse httpResponse = this.delegate.execute(httpUriRequest);
    return new RibbonApacheHttpResponse(httpResponse, httpUriRequest.getURI());
}

From source file:org.jasig.portlet.proxy.service.web.HttpContentServiceImplTest.java

@Test
public void testGetFormNoParams() {
    when(request.getParameter(HttpContentServiceImpl.IS_FORM_PARAM)).thenReturn("true");
    when(request.getParameter(HttpContentServiceImpl.FORM_METHOD_PARAM)).thenReturn("GET");
    when(processor.process(any(String.class), any(PortletRequest.class)))
            .thenReturn("http://somewhere.com/path/page.html");
    HttpContentRequestImpl proxyRequest = new HttpContentRequestImpl(request, processor);

    final HttpUriRequest httpRequest = service.getHttpRequest(proxyRequest, request);
    assertEquals("GET", httpRequest.getMethod());
    assertEquals("http://somewhere.com/path/page.html", httpRequest.getURI().toString());

}

From source file:org.jasig.portlet.proxy.service.web.HttpContentServiceImplTest.java

@Test
public void testNonForm() {

    when(request.getParameter(HttpContentServiceImpl.IS_FORM_PARAM)).thenReturn("false");
    when(processor.process(any(String.class), any(PortletRequest.class)))
            .thenReturn("http://somewhere.com/path/page.html");
    HttpContentRequestImpl proxyRequest = new HttpContentRequestImpl(request, processor);

    final HttpUriRequest httpRequest = service.getHttpRequest(proxyRequest, request);
    assertEquals("GET", httpRequest.getMethod());
    assertEquals("http://somewhere.com/path/page.html", httpRequest.getURI().toString());

}

From source file:org.jasig.portlet.proxy.service.web.HttpContentServiceImplTest.java

@Test
public void testPostFormNoParams() {
    when(request.getParameter(HttpContentServiceImpl.IS_FORM_PARAM)).thenReturn("true");
    when(request.getParameter(HttpContentServiceImpl.FORM_METHOD_PARAM)).thenReturn("POST");
    when(processor.process(any(String.class), any(PortletRequest.class)))
            .thenReturn("http://somewhere.com/path/page.html");
    HttpContentRequestImpl proxyRequest = new HttpContentRequestImpl(request, processor);

    final HttpUriRequest httpRequest = service.getHttpRequest(proxyRequest, request);
    assertEquals("POST", httpRequest.getMethod());
    assertEquals("http://somewhere.com/path/page.html", httpRequest.getURI().toString());

}