Example usage for org.apache.http.client.utils URIBuilder URIBuilder

List of usage examples for org.apache.http.client.utils URIBuilder URIBuilder

Introduction

In this page you can find the example usage for org.apache.http.client.utils URIBuilder URIBuilder.

Prototype

public URIBuilder() 

Source Link

Document

Constructs an empty instance.

Usage

From source file:se.inera.certificate.proxy.mappings.remote.RemoteMappingTest.java

@Test
public void testMapToUrl() throws URISyntaxException {
    RemoteMapping m = new RemoteMapping("/services", "http://www.google.com:80/test");
    URIBuilder builder = new URIBuilder();
    builder.setScheme(m.getMappedProtocol()).setHost(m.getMappedHost()).setPort(m.getMappedPort())
            .setPath("/some/new/uri").setQuery("some=1&less=34");

    assertEquals("http://www.google.com:80/some/new/uri?some=1&less=34", builder.build().toString());
}

From source file:com.clxcommunications.xms.GroupFilterTest.java

@Property
public void generatesValidQueryParameters(int page, int pageSize, Set<String> tags) throws Exception {
    // Constrain `tags` to strings not containing ','
    assumeThat(tags, not(hasItem(containsString(","))));

    GroupFilter filter = ClxApi.groupFilter().pageSize(pageSize).tags(tags).build();

    List<NameValuePair> params = filter.toQueryParams(page);

    // Will throw IllegalArgumentException if an invalid URI is attempted.
    new URIBuilder().addParameters(params).build();
}

From source file:com.m3958.apps.anonymousupload.integration.java.FileUploadTest.java

@Test
public void testPostRename() throws ClientProtocolException, IOException, URISyntaxException {

    File f = new File("README.md");

    Assert.assertTrue(f.exists());/*from w  w  w . j a  va  2s .  co m*/

    String c = Request.Post(new URIBuilder().setScheme("http").setHost("localhost").setPort(8080).build())
            .body(MultipartEntityBuilder.create()
                    .addBinaryBody("afile", f, ContentType.MULTIPART_FORM_DATA, f.getName())
                    .addTextBody("fn", "abcfn.md").build())
            .execute().returnContent().asString();

    String url = c.trim();
    Assert.assertTrue(url.endsWith("abcfn.md"));

    testComplete();
}

From source file:com.ibm.nytimes.NewYorkTimes.java

public BestSellerList getList() throws Exception {
    BestSellerList returnedList = new BestSellerList();

    try {// ww  w .  j a  v  a 2  s.c o  m
        if (listName != null && listDate != null && url != null && apiKey != null) {
            RequestConfig config = RequestConfig.custom().setSocketTimeout(10 * 1000)
                    .setConnectTimeout(10 * 1000).build();
            CloseableHttpClient httpclient = HttpClients.custom().setDefaultRequestConfig(config).build();

            URIBuilder builder = new URIBuilder();
            builder.setScheme("http").setHost(url).setPath("/svc/books/v2/lists/" + listDate + "/" + listName)
                    .setParameter("api-key", apiKey);
            URI uri = builder.build();
            HttpGet httpGet = new HttpGet(uri);
            httpGet.setHeader("Content-Type", "text/plain");

            HttpResponse httpResponse = httpclient.execute(httpGet);

            if (httpResponse.getStatusLine().getStatusCode() == 200) {
                BufferedReader rd = new BufferedReader(
                        new InputStreamReader(httpResponse.getEntity().getContent(), "UTF-8"));

                // Read all the books from the best seller list
                ObjectMapper mapper = new ObjectMapper();
                returnedList = mapper.readValue(rd, BestSellerList.class);
            } else {
                logger.error("could not get list from ny times http code {}",
                        httpResponse.getStatusLine().getStatusCode());
            }
        }
    } catch (Exception e) {
        logger.error("could not get list from ny times {}", e.getMessage());
        throw e;
    }

    return returnedList;
}

From source file:org.wuspba.ctams.ws.ITVenueController.java

@Test
public void testList() throws Exception {
    CloseableHttpClient httpclient = HttpClients.createDefault();

    URI uri = new URIBuilder().setScheme(PROTOCOL).setHost(HOST).setPort(PORT).setPath(PATH).build();

    LOG.info("Connecting to " + uri.toString());

    HttpGet httpGet = new HttpGet(uri);

    try (CloseableHttpResponse response = httpclient.execute(httpGet)) {
        assertEquals(response.getStatusLine().toString(), IntegrationTestUtils.OK_STRING);

        HttpEntity entity = response.getEntity();

        CTAMSDocument doc = IntegrationTestUtils.convertEntity(entity);

        assertEquals(doc.getVenues().size(), 1);
        testEquality(doc.getVenues().get(0), TestFixture.INSTANCE.venue);

        EntityUtils.consume(entity);//from  www.  j ava2  s.c o m
    }
}

From source file:org.wuspba.ctams.ws.ITHiredJudgeController.java

@Test
public void testListAll() throws Exception {
    CloseableHttpClient httpclient = HttpClients.createDefault();

    URI uri = new URIBuilder().setScheme(PROTOCOL).setHost(HOST).setPort(PORT).setPath(PATH).build();

    LOG.info("Connecting to " + uri.toString());

    HttpGet httpGet = new HttpGet(uri);

    try (CloseableHttpResponse response = httpclient.execute(httpGet)) {
        assertEquals(response.getStatusLine().toString(), IntegrationTestUtils.OK_STRING);

        HttpEntity entity = response.getEntity();

        CTAMSDocument doc = IntegrationTestUtils.convertEntity(entity);

        assertEquals(doc.getHiredJudges().size(), 4);

        EntityUtils.consume(entity);//from ww  w.jav  a  2  s .c om
    }
}

From source file:msgclient.ServerConnection.java

public JSONObject makeGetToServer(String path) {
    CloseableHttpResponse response;//from   w  w  w  .  j av  a2s .co  m
    JSONObject jsonObject = null;

    // Send a GET request to the server
    try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) {
        // Create a URI            
        URI uri = new URIBuilder().setScheme(PROTOCOL_TYPE).setHost(mServerName + ":" + mPort).setPath(path)
                .build();
        HttpGet httpget = new HttpGet(uri);
        httpget.addHeader("accept", "application/json");
        //System.out.println(httpget.getURI());

        response = httpClient.execute(httpget);
        if (response.getStatusLine().getStatusCode() == 200) {
            String jsonData = IOUtils.toString(response.getEntity().getContent());
            JSONParser parser = new JSONParser();
            Object obj = parser.parse(jsonData);
            jsonObject = (JSONObject) obj;
        } else {
            System.out.println(
                    "Received status code " + response.getStatusLine().getStatusCode() + " from server");
        }

        response.close();
        httpClient.close();
    } catch (Exception e) {
        System.out.println(e);
        return null;
    }

    return jsonObject;
}

From source file:org.wuspba.ctams.ws.ITJudgeController.java

@Test
public void testList() throws Exception {
    CloseableHttpClient httpclient = HttpClients.createDefault();

    URI uri = new URIBuilder().setScheme(PROTOCOL).setHost(HOST).setPort(PORT).setPath(PATH).build();

    LOG.info("Connecting to " + uri.toString());

    HttpGet httpGet = new HttpGet(uri);

    try (CloseableHttpResponse response = httpclient.execute(httpGet)) {
        assertEquals(response.getStatusLine().toString(), IntegrationTestUtils.OK_STRING);

        HttpEntity entity = response.getEntity();

        CTAMSDocument doc = IntegrationTestUtils.convertEntity(entity);

        assertEquals(doc.getJudges().size(), 4);
        for (Judge j : doc.getJudges()) {
            if (j.getId().equals(TestFixture.INSTANCE.judgeAndy.getId())) {
                testEquality(j, TestFixture.INSTANCE.judgeAndy);
            } else if (j.getId().equals(TestFixture.INSTANCE.judgeBob.getId())) {
                testEquality(j, TestFixture.INSTANCE.judgeBob);
            } else if (j.getId().equals(TestFixture.INSTANCE.judgeEoin.getId())) {
                testEquality(j, TestFixture.INSTANCE.judgeEoin);
            } else if (j.getId().equals(TestFixture.INSTANCE.judgeJamie.getId())) {
                testEquality(j, TestFixture.INSTANCE.judgeJamie);
            } else {
                fail();/*from  w w  w . j  a va 2  s . c om*/
            }
        }

        EntityUtils.consume(entity);
    }
}

From source file:org.wuspba.ctams.ws.ITBandMemberController.java

@Test
public void testList() throws Exception {
    CloseableHttpClient httpclient = HttpClients.createDefault();

    URI uri = new URIBuilder().setScheme(PROTOCOL).setHost(HOST).setPort(PORT).setPath(PATH).build();

    LOG.info("Connecting to " + uri.toString());

    HttpGet httpGet = new HttpGet(uri);

    try (CloseableHttpResponse response = httpclient.execute(httpGet)) {
        assertEquals(response.getStatusLine().toString(), IntegrationTestUtils.OK_STRING);

        HttpEntity entity = response.getEntity();

        CTAMSDocument doc = IntegrationTestUtils.convertEntity(entity);

        assertEquals(doc.getBandMembers().size(), 3);
        for (BandMember m : doc.getBandMembers()) {
            if (m.getId().equals(TestFixture.INSTANCE.andyMember.getId())) {
                testEquality(m, TestFixture.INSTANCE.andyMember);
            } else if (m.getId().equals(TestFixture.INSTANCE.bobMember.getId())) {
                testEquality(m, TestFixture.INSTANCE.bobMember);
            } else if (m.getId().equals(TestFixture.INSTANCE.jamieMember.getId())) {
                testEquality(m, TestFixture.INSTANCE.jamieMember);
            } else {
                fail();//from w w w  .  j a va2  s.co  m
            }
        }

        EntityUtils.consume(entity);
    }
}

From source file:org.wuspba.ctams.ui.server.DataUtils.java

protected static CTAMSDocument getBandRegistration(HttpServletRequest request) {

    BandRegistration registration = new BandRegistration();

    URIBuilder builder = new URIBuilder().setScheme(ServerUtils.PROTOCOL).setHost(ServerUtils.HOST)
            .setPort(ServerUtils.PORT).setParameter("name", request.getParameter("band"))
            .setPath(ServerUtils.URI + "/band");

    try {/*from  ww  w .j  av a  2  s .c o m*/
        String xml = ServerUtils.get(builder.build());
        CTAMSDocument bands = XMLUtils.unmarshal(xml);
        registration.setBand(bands.getBands().get(0));
    } catch (IOException ex) {
        LOG.error("Error finding band", ex);
    } catch (URISyntaxException uex) {
        LOG.error("Invalide URI", uex);
    }

    registration.setId(request.getParameter("id"));
    try {
        registration.setEnd(dateParser.parse(request.getParameter("end")));
    } catch (ParseException ex) {
        LOG.error("Cannot parse date", ex);
    }
    try {
        Date date = dateParser.parse(request.getParameter("start"));
        registration.setStart(date);
    } catch (ParseException ex) {
        LOG.error("Cannot parse date", ex);
    }
    registration.setGrade(Grade.valueOf(request.getParameter("grade")));
    registration.setSeason(Integer.parseInt(request.getParameter("season")));

    CTAMSDocument doc = new CTAMSDocument();
    doc.getBandRegistrations().add(registration);

    return doc;
}