Refine search
private <T> RestResponse<T> getRequest(final String path, final Class<T> type) { try (Response response = client.target(serverAddress) .path(path) .request(MediaType.APPLICATION_JSON_TYPE) .get()) { return response.getStatus() == Response.Status.OK.getStatusCode() ? RestResponse.successful(response.readEntity(type)) : createErrorResponse(path, response); } catch (final Exception e) { throw new KsqlRestClientException("Error issuing GET to KSQL server. path:" + path, e); } }
@Override protected AuthResponse execute(AuthCmd command) { WebTarget webResource = getBaseResource().path("/auth"); LOGGER.trace("POST: {}", webResource); Response response = webResource.request().accept(MediaType.APPLICATION_JSON) .post(entity(command.getAuthConfig(), MediaType.APPLICATION_JSON)); if (response.getStatus() == 401) { throw new UnauthorizedException("Unauthorized"); } return response.readEntity(AuthResponse.class); }
/** * Creates a new Invocation.Builder for the given WebTarget with the headers added to the builder. * * @param webTarget the target for the request * @return the builder for the target with the headers added */ protected Invocation.Builder getRequestBuilder(final WebTarget webTarget) { final Invocation.Builder requestBuilder = webTarget.request(); headers.entrySet().stream().forEach(e -> requestBuilder.header(e.getKey(), e.getValue())); return requestBuilder; }
private EurekaHttpResponse<InstanceInfo> getInstanceInternal(String urlPath) { Response response = null; try { Builder requestBuilder = jerseyClient.target(serviceUrl).path(urlPath).request(); addExtraProperties(requestBuilder); addExtraHeaders(requestBuilder); response = requestBuilder.accept(MediaType.APPLICATION_JSON_TYPE).get(); InstanceInfo infoFromPeer = null; if (response.getStatus() == Status.OK.getStatusCode() && response.hasEntity()) { infoFromPeer = response.readEntity(InstanceInfo.class); } return anEurekaHttpResponse(response.getStatus(), infoFromPeer).headers(headersOf(response)).build(); } finally { if (logger.isDebugEnabled()) { logger.debug("Jersey2 HTTP GET {}/{}; statusCode={}", serviceUrl, urlPath, response == null ? "N/A" : response.getStatus()); } if (response != null) { response.close(); } } }
/** * Send message to webapp to refresh SearcherManagers for given projects. * This is used for partial reindex. * * @param subFiles list of directories to refresh corresponding SearcherManagers * @param host the host address to receive the configuration */ public void signalTorefreshSearcherManagers(List<String> subFiles, String host) { // subFile entries start with path separator so get basename // to convert them to project names. subFiles.stream().map(proj -> new File(proj).getName()).forEach(project -> { Response r = ClientBuilder.newClient() .target(host) .path("api") .path("v1") .path("system") .path("refresh") .request() .put(Entity.text(project)); if (r.getStatusInfo().getFamily() != Response.Status.Family.SUCCESSFUL) { LOGGER.log(Level.WARNING, "Could not refresh search manager for {0}", project); } }); }
@Test public void testDropTableNotFound() throws Exception { Response response = restClient.target(tablesURI) .path("/{tableName}").resolveTemplate("tableName", "TestDropTableNotFound") .request().delete(); assertNotNull(response); assertEquals(Status.NOT_FOUND.getStatusCode(), response.getStatus()); } }
@Override public void run() { final Client resourceClient = ClientBuilder.newClient(); resourceClient.register(new MoxyJsonFeature()); final WebTarget messageStreamResource = resourceClient.target(App.getApiUri()).path("message/stream"); Message message = null; try { while (!cancelled && (message = messages.take()) != null) { msgListener.onMessage(message); final Response r = messageStreamResource.request().put(Entity.json(message)); if (r.getStatusInfo().getFamily() != Response.Status.Family.SUCCESSFUL) { LOGGER.warning("Unexpected PUT message response status code: " + r.getStatus()); } } if (message == null) { LOGGER.info("Timed out while waiting for a message."); } } catch (InterruptedException ex) { LOGGER.log(Level.WARNING, "Waiting for a message has been interrupted.", ex); } finally { readerHandle.cancel(true); msgListener.onComplete(); } } });
@Test public void testNoTrain() { Assert.assertEquals(Response.Status.FORBIDDEN.getStatusCode(), target("/train").request().post(Entity.text(TrainTest.TRAIN_DATA)).getStatus()); }
public static void main(String[] args) { try { System.out.println("\"Hello World\" Jersey OpenTracing Example App"); prepare(); final ResourceConfig resourceConfig = new ResourceConfig(TracedResource.class, OpenTracingFeature.class, ReqFilterA.class, ReqFilterB.class, RespFilterA.class, RespFilterB.class); final HttpServer server = GrizzlyHttpServerFactory.createHttpServer(BASE_URI, resourceConfig, false); Runtime.getRuntime().addShutdownHook(new Thread(server::shutdownNow)); server.start(); System.out.println(String.format("Application started.\nTry out %s/application.wadl\n" + "Stop the application using CTRL+C", BASE_URI)); // do the first "example" request with tracing-enabled client to show something in Jaegger UI, // include some weird headers and accepted types, that will be visible in the span's tags Client client = ClientBuilder.newBuilder().register(OpenTracingFeature.class).build(); client.target(BASE_URI).path("resource/managedClient").request() .accept("text/plain", "application/json", "*/*") .header("foo", "bar") .header("foo", "baz") .header("Hello", "World").get(); Thread.currentThread().join(); } catch (IOException | InterruptedException ex) { Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex); } }
@Override protected Void execute(UpdateSwarmNodeCmd command) { WebTarget webResource = getBaseResource().path("/nodes/{id}/update") .resolveTemplate("id", command.getSwarmNodeId()) .queryParam("version", command.getVersion()); LOGGER.trace("POST: {}", webResource); webResource.request().accept(MediaType.APPLICATION_JSON) .post(entity(command.getSwarmNodeSpec(), MediaType.APPLICATION_JSON)).close(); return null; } }
/** * Write the current configuration to a socket * * @param host the host address to receive the configuration * @throws IOException if an error occurs */ public void writeConfiguration(String host) throws IOException { String configXML; try { configLock.readLock().lock(); configXML = configuration.getXMLRepresentationAsString(); } finally { configLock.readLock().unlock(); } Response r = ClientBuilder.newClient() .target(host) .path("api") .path("v1") .path("configuration") .queryParam("reindex", true) .request() .put(Entity.xml(configXML)); if (r.getStatusInfo().getFamily() != Response.Status.Family.SUCCESSFUL) { throw new IOException(r.toString()); } }
@Test public void testGet() { Response response = target("/ready").request().get(); Assert.assertEquals(Response.Status.OK.getStatusCode(), response.getStatus()); }
@Test public void testTableSizeNotFound() { Response response = _webTarget.path("table/unknownTable/size").request().get(Response.class); Assert.assertEquals(response.getStatus(), Response.Status.NOT_FOUND.getStatusCode()); }
public <V> T put(final String queryResourceUrl, final V request) { CompletableFuture<T> result = new CompletableFuture<>(); final WebTarget webResource = client.target(queryResourceUrl); return buildResponse(webResource .request(accept) .accept(accept) .put(Entity.entity(request, contentType),String.class)); } public T delete(final String queryResourceUrl) {
@Test public void runAppAndBasicTest() throws InterruptedException, ExecutionException{ Client client = ClientBuilder.newClient(); WebTarget resource = client.target("http://localhost:8080/simple-app/single/ping"); Builder request = resource.request(); request.accept(MediaType.TEXT_PLAIN); assertTrue(request.get().getHeaders().containsKey("Access-Control-Allow-Origin")); }
@Override public InputStream saveMultiple(final String... images) throws DockerException, IOException, InterruptedException { final WebTarget resource = resource().path("images").path("get"); for (final String image : images) { resource.queryParam("names", urlEncode(image)); } return request( GET, InputStream.class, resource, resource.request(APPLICATION_JSON_TYPE).header("X-Registry-Auth", authHeader( registryAuthSupplier.authFor(images[0]))) ); }