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:org.myrobotlab.service.HttpClient.java

public HttpData processResponse(HttpUriRequest request, HashMap<String, String> fields) throws IOException {
    HttpData data = new HttpData(request.getURI().toString());
    if (fields == null) {

        fields = formFields;/*  www  .j av  a  2 s.c o m*/
    }

    if (request.getClass().equals(HttpPost.class) && formFields.size() > 0) {
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(fields.size());
        for (String nvPairKey : fields.keySet()) {
            nameValuePairs.add(new BasicNameValuePair(nvPairKey, fields.get(nvPairKey)));
            ((HttpPost) request).setEntity(new UrlEncodedFormEntity(nameValuePairs));
        }
    }
    HttpResponse response = client.execute(request);
    StatusLine statusLine = response.getStatusLine();
    data.responseCode = statusLine.getStatusCode();
    HttpEntity entity = response.getEntity();
    Header header = entity.getContentType();
    if (header != null) {
        data.contentType = header.getValue().toString();
    }

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    response.getEntity().writeTo(baos);
    data.data = baos.toByteArray();

    // publishing
    invoke("publishHttpData", data);
    if (data.data != null) {
        invoke("publishHttpResponse", new String(data.data));
    }

    return data;
}

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

void testEntity(String entityValue, ByteArrayInputStream requestEntity, boolean addContentLengthHeader,
        String method) throws IOException {
    String lengthString = String.valueOf(entityValue.length());
    Long length = null;//w w  w .  jav a  2 s .co m
    URI uri = URI.create("http://example.com");
    LinkedMultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
    if (addContentLengthHeader) {
        headers.add("Content-Length", lengthString);
        length = (long) entityValue.length();
    }

    RibbonRequestCustomizer requestCustomizer = new RibbonRequestCustomizer<RequestBuilder>() {
        @Override
        public boolean accepts(Class builderClass) {
            return builderClass == RequestBuilder.class;
        }

        @Override
        public void customize(RequestBuilder builder) {
            builder.addHeader("from-customizer", "foo");
        }
    };
    RibbonCommandContext context = new RibbonCommandContext("example", method, uri.toString(), false, headers,
            new LinkedMultiValueMap<String, String>(), requestEntity,
            Collections.singletonList(requestCustomizer));
    context.setContentLength(length);
    RibbonApacheHttpRequest httpRequest = new RibbonApacheHttpRequest(context);

    HttpUriRequest request = httpRequest.toRequest(RequestConfig.custom().build());

    assertThat("request is wrong type", request, is(instanceOf(HttpEntityEnclosingRequest.class)));
    assertThat("uri is wrong", request.getURI().toString(), startsWith(uri.toString()));
    if (addContentLengthHeader) {
        assertThat("Content-Length is missing", request.getFirstHeader("Content-Length"), is(notNullValue()));
        assertThat("Content-Length is wrong", request.getFirstHeader("Content-Length").getValue(),
                is(equalTo(lengthString)));
    }
    assertThat("from-customizer is missing", request.getFirstHeader("from-customizer"), is(notNullValue()));
    assertThat("from-customizer is wrong", request.getFirstHeader("from-customizer").getValue(),
            is(equalTo("foo")));

    HttpEntityEnclosingRequest entityRequest = (HttpEntityEnclosingRequest) request;
    assertThat("entity is missing", entityRequest.getEntity(), is(notNullValue()));
    HttpEntity entity = entityRequest.getEntity();
    assertThat("contentLength is wrong", entity.getContentLength(), is(equalTo((long) entityValue.length())));
    assertThat("content is missing", entity.getContent(), is(notNullValue()));
    String string = StreamUtils.copyToString(entity.getContent(), Charset.forName("UTF-8"));
    assertThat("content is wrong", string, is(equalTo(entityValue)));
}

From source file:org.mythdroid.services.JSONClient.java

private InputStream RequestStream(HttpUriRequest req) throws IOException {

    req.setHeader("Accept", "application/json"); //$NON-NLS-1$ //$NON-NLS-2$
    LogUtil.debug("JSON request: " + req.getURI().toString()); //$NON-NLS-1$

    try {/* w ww. j av a 2 s.  c o m*/
        fetcher = new HttpFetcher(req, Globals.muxConns);
        return fetcher.getInputStream();
    } catch (SocketTimeoutException e) {
        throw new IOException(Messages.getString("JSONClient.0") + //$NON-NLS-1$
                req.getURI().getHost() + ":" + req.getURI().getPort() //$NON-NLS-1$
        );
    } catch (ClientProtocolException e) {
        ErrUtil.logErr(e);
        return null;
    }

}

From source file:com.supernovapps.audio.jstreamsourcer.ShoutcastV1Test.java

@Test
public void testUpdateMetadata() throws IOException, URISyntaxException {
    Socket sockMock = EasyMock.createNiceMock(Socket.class);

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    ByteArrayInputStream in = new ByteArrayInputStream(new String("HTTP OK").getBytes());

    EasyMock.expect(sockMock.getOutputStream()).andReturn(out);
    EasyMock.expect(sockMock.getInputStream()).andReturn(in);
    EasyMock.expect(sockMock.isConnected()).andReturn(true);
    EasyMock.replay(sockMock);/*from   w w  w .j av a2 s  .  c om*/

    shoutcast.start(sockMock);

    HttpUriRequest request = shoutcast.getUpdateMetadataRequest("song", "artist", "album");
    Assert.assertEquals(shoutcast.getHost(), request.getURI().getHost());
    Assert.assertEquals(shoutcast.getPort(), request.getURI().getPort());

    HashMap<String, String> paramsMap = getParams(request);
    Assert.assertEquals("album song artist", paramsMap.get("song"));
}

From source file:org.godotengine.godot.utils.HttpRequester.java

private String request(HttpUriRequest request) {
    //      Log.d("XXX", "Haciendo request a: " + request.getURI() );
    Log.d("PPP", "Haciendo request a: " + request.getURI());
    long init = new Date().getTime();
    HttpClient httpclient = getNewHttpClient();
    HttpParams httpParameters = httpclient.getParams();
    HttpConnectionParams.setConnectionTimeout(httpParameters, 0);
    HttpConnectionParams.setSoTimeout(httpParameters, 0);
    HttpConnectionParams.setTcpNoDelay(httpParameters, true);
    try {/*from   w  w w .  ja v a 2 s. c o m*/
        HttpResponse response = httpclient.execute(request);
        Log.d("PPP", "Fin de request (" + (new Date().getTime() - init) + ") a: " + request.getURI());
        //           Log.d("XXX1", "Status:" + response.getStatusLine().toString());
        if (response.getStatusLine().getStatusCode() == 200) {
            String strResponse = EntityUtils.toString(response.getEntity());
            //              Log.d("XXX2", strResponse);
            return strResponse;
        } else {
            Log.d("XXX3", "Response status code:" + response.getStatusLine().getStatusCode() + "\n"
                    + EntityUtils.toString(response.getEntity()));
            return null;
        }

    } catch (ClientProtocolException e) {
        Log.d("XXX3", e.getMessage());
    } catch (IOException e) {
        Log.d("XXX4", e.getMessage());
    }
    return null;
}

From source file:org.metaeffekt.dcc.agent.DccRequestBuilderTest.java

@Test
public void stop() {

    HttpUriRequest request = builder.createRequest("stop", "depId", "somePackage", "someUnit");

    assertCommonParts(request);//  www .ja  v a2 s  .c  om
    assertEquals("PUT", request.getMethod());
    assertEquals("/dcc/depId/packages/somePackage/units/someUnit/stop", request.getURI().getPath());
}

From source file:org.metaeffekt.dcc.agent.DccRequestBuilderTest.java

@Test
public void start() {

    HttpUriRequest request = builder.createRequest("start", "depId", "somePackage", "someUnit");

    assertCommonParts(request);/*from www. j  av  a2s  . com*/
    assertEquals("PUT", request.getMethod());
    assertEquals("/dcc/depId/packages/somePackage/units/someUnit/start", request.getURI().getPath());
}

From source file:com.supernovapps.audio.jstreamsourcer.IcecastTest.java

@Test
public void testUpdateMetadata() throws IOException, URISyntaxException {
    Socket sockMock = EasyMock.createNiceMock(Socket.class);

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    ByteArrayInputStream in = new ByteArrayInputStream(new String("HTTP OK").getBytes());

    EasyMock.expect(sockMock.getOutputStream()).andReturn(out);
    EasyMock.expect(sockMock.getInputStream()).andReturn(in);
    EasyMock.expect(sockMock.isConnected()).andReturn(true);
    EasyMock.replay(sockMock);/*from w ww  .  ja v a 2  s . com*/

    icecast.start(sockMock);

    HttpUriRequest request = icecast.getUpdateMetadataRequest("song", "artist", "album");
    Assert.assertEquals(icecast.getHost(), request.getURI().getHost());
    Assert.assertEquals(icecast.getPort(), request.getURI().getPort());

    HashMap<String, String> paramsMap = getParams(request);
    Assert.assertEquals(icecast.getPath(), paramsMap.get("mount"));
    Assert.assertEquals("updinfo", paramsMap.get("mode"));
    Assert.assertEquals("album song artist", paramsMap.get("song"));
}

From source file:org.metaeffekt.dcc.agent.DccRequestBuilderTest.java

@Test
public void install() {

    HttpUriRequest request = builder.createRequest("install", "depId", "somePackage", "someUnit");

    assertCommonParts(request);/*from   ww  w.j av  a2  s. co m*/
    assertEquals("PUT", request.getMethod());
    assertEquals("/dcc/depId/packages/somePackage/units/someUnit/install", request.getURI().getPath());
}

From source file:tech.sirwellington.alchemy.http.AlchemyRequestMapperTest.java

@Test
public void testGetExpandsURL() throws Exception {
    instance = AlchemyRequestMapper.GET;

    when(request.hasQueryParams()).thenReturn(true);

    HttpUriRequest result = instance.convertToApacheRequest(request);
    assertThat(result.getURI(), is(expandedUrl.toURI()));
}