@GET @Path("/v1/info") @Produces(APPLICATION_JSON) public void getInfo( @Context HttpServletRequest servletRequest, @Suspended AsyncResponse asyncResponse) { Request.Builder request = prepareGet() .setUri(uriBuilderFrom(remoteUri).replacePath("/v1/info").build()); performRequest(servletRequest, asyncResponse, request, response -> responseWithHeaders(Response.ok(response.getBody()), response)); }
@POST @Path("/v1/statement") @Produces(APPLICATION_JSON) public void postStatement( String statement, @Context HttpServletRequest servletRequest, @Context UriInfo uriInfo, @Suspended AsyncResponse asyncResponse) { Request.Builder request = preparePost() .setUri(uriBuilderFrom(remoteUri).replacePath("/v1/statement").build()) .setBodyGenerator(createStaticBodyGenerator(statement, UTF_8)); performRequest(servletRequest, asyncResponse, request, response -> buildResponse(uriInfo, response)); }
private long getTotalCpuTime() { long totalCpuTime = 0; for (URI server : nodes) { URI addressUri = uriBuilderFrom(server).replacePath("/v1/jmx/mbean/java.lang:type=OperatingSystem/ProcessCpuTime").build(); String data = httpClient.execute(prepareGet().setUri(addressUri).build(), createStringResponseHandler()).getBody(); totalCpuTime += parseLong(data.trim()); } return TimeUnit.NANOSECONDS.toNanos(totalCpuTime); }
private List<URI> getAllNodes(URI server) { Request request = prepareGet().setUri(uriBuilderFrom(server).replacePath("/v1/service/presto").build()).build(); JsonResponseHandler<ServiceDescriptorsRepresentation> responseHandler = createJsonResponseHandler(jsonCodec(ServiceDescriptorsRepresentation.class)); ServiceDescriptorsRepresentation serviceDescriptors = httpClient.execute(request, responseHandler); ImmutableList.Builder<URI> addresses = ImmutableList.builder(); for (ServiceDescriptor serviceDescriptor : serviceDescriptors.getServiceDescriptors()) { String httpUri = serviceDescriptor.getProperties().get("http"); if (httpUri != null) { addresses.add(URI.create(httpUri)); } } return addresses.build(); } }
public URI uriFor(String path) { return HttpUriBuilder.uriBuilderFrom(server.getBaseUrl()).replacePath(path).build(); } }
@BeforeClass public void setup() { Request request1 = preparePost() .setUri(uriBuilderFrom(server.getBaseUrl()).replacePath("/v1/statement").build()) .setBodyGenerator(createStaticBodyGenerator(LONG_LASTING_QUERY, UTF_8)) .setHeader(PRESTO_USER, "user1") .build(); queryResults = client.execute(request1, createJsonResponseHandler(QUERY_RESULTS_JSON_CODEC)); client.execute(prepareGet().setUri(queryResults.getNextUri()).build(), createJsonResponseHandler(QUERY_RESULTS_JSON_CODEC)); Request request2 = preparePost() .setUri(uriBuilderFrom(server.getBaseUrl()).replacePath("/v1/statement").build()) .setBodyGenerator(createStaticBodyGenerator(LONG_LASTING_QUERY, UTF_8)) .setHeader(PRESTO_USER, "user2") .build(); QueryResults queryResults2 = client.execute(request2, createJsonResponseHandler(jsonCodec(QueryResults.class))); client.execute(prepareGet().setUri(queryResults2.getNextUri()).build(), createJsonResponseHandler(QUERY_RESULTS_JSON_CODEC)); // queries are started in the background, so they may not all be immediately visible while (true) { List<BasicQueryInfo> queryInfos = client.execute( prepareGet().setUri(uriBuilderFrom(server.getBaseUrl()).replacePath("/v1/query").build()).build(), createJsonResponseHandler(listJsonCodec(BasicQueryInfo.class))); if ((queryInfos.size() == 2) && queryInfos.stream().allMatch(info -> info.getState() == RUNNING)) { break; } } }
@Test public void testReplacePathWithRelative() { URI uri = uriBuilder() .scheme("http") .host("www.example.com") .replacePath("a/b/c") .build(); assertEquals(uri.toASCIIString(), "http://www.example.com/a/b/c"); }
@Test public void testWithPath() { URI uri = uriBuilder() .scheme("http") .host("www.example.com") .replacePath("/a/b/c") .build(); assertEquals(uri.toASCIIString(), "http://www.example.com/a/b/c"); }
@Test public void testWithPath() { URI uri = uriBuilder() .scheme("http") .host("www.example.com") .replacePath("/a/b/c") .build(); assertEquals(uri.toASCIIString(), "http://www.example.com/a/b/c"); }
@Test public void testEncodesPath() { URI uri = uriBuilder() .scheme("http") .host("www.example.com") .replacePath("/`#%^{}|[]<>?áéíóú") .build(); assertEquals(uri.toASCIIString(), "http://www.example.com/%60%23%25%5E%7B%7D%7C%5B%5D%3C%3E%3F%C3%A1%C3%A9%C3%AD%C3%B3%C3%BA"); }
@Test public void testReplacePathWithRelative() { URI uri = uriBuilder() .scheme("http") .host("www.example.com") .replacePath("a/b/c") .build(); assertEquals(uri.toASCIIString(), "http://www.example.com/a/b/c"); }
@Test public void testEncodesPath() { URI uri = uriBuilder() .scheme("http") .host("www.example.com") .replacePath("/`#%^{}|[]<>?áéíóú") .build(); assertEquals(uri.toASCIIString(), "http://www.example.com/%60%23%25%5E%7B%7D%7C%5B%5D%3C%3E%3F%C3%A1%C3%A9%C3%AD%C3%B3%C3%BA"); }
@Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "host starts with a bracket") public void testHostWithBracketedIpv6() { URI uri = uriBuilder() .scheme("http") .host("[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]") .port(8081) .replacePath("/a/b") .build(); assertEquals(uri.toASCIIString(), "http://[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]:8081/a/b"); }
@Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "host starts with a bracket") public void testHostWithBracketedIpv6() { URI uri = uriBuilder() .scheme("http") .host("[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]") .port(8081) .replacePath("/a/b") .build(); assertEquals(uri.toASCIIString(), "http://[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]:8081/a/b"); }
@Test public void testHostWithIpv6() { URI uri = uriBuilder() .scheme("http") .host("FEDC:BA98:7654:3210:FEDC:BA98:7654:3210") .port(8081) .replacePath("/a/b") .build(); assertEquals(uri.toASCIIString(), "http://[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]:8081/a/b"); }
@Test public void testAddParameterMultivalued() { URI uri = uriBuilder() .scheme("http") .host("www.example.com") .replacePath("/") .addParameter("k1", "1", "2", "0") .build(); assertEquals(uri.toASCIIString(), "http://www.example.com/?k1=1&k1=2&k1=0"); }
@Test public void testHostWithIpv6() { URI uri = uriBuilder() .scheme("http") .host("FEDC:BA98:7654:3210:FEDC:BA98:7654:3210") .port(8081) .replacePath("/a/b") .build(); assertEquals(uri.toASCIIString(), "http://[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]:8081/a/b"); }
@Test public void testAddParameterMultivalued() { URI uri = uriBuilder() .scheme("http") .host("www.example.com") .replacePath("/") .addParameter("k1", "1", "2", "0") .build(); assertEquals(uri.toASCIIString(), "http://www.example.com/?k1=1&k1=2&k1=0"); }
@Test public void testFull() { URI uri = uriBuilder() .scheme("http") .host("www.example.com") .port(8081) .replacePath("/a/b/c") .replaceParameter("k", "1") .build(); assertEquals(uri.toASCIIString(), "http://www.example.com:8081/a/b/c?k=1"); }
public URI uriFor(String path) { return HttpUriBuilder.uriBuilderFrom(server.getBaseUrl()).replacePath(path).build(); } }