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); } }
public String start() { final Response response = addProperties(client.target(requestTokenUri).request()) .post(null); if (response.getStatus() != 200) { throw new RuntimeException(LocalizationMessages.ERROR_REQUEST_REQUEST_TOKEN(response.getStatus())); } final MultivaluedMap<String, String> formParams = response.readEntity(Form.class).asMap(); parameters.token(formParams.getFirst(OAuth1Parameters.TOKEN)); secrets.tokenSecret(formParams.getFirst(OAuth1Parameters.TOKEN_SECRET)); return UriBuilder.fromUri(authorizationUri).queryParam(OAuth1Parameters.TOKEN, parameters.getToken()) .build().toString(); }
@Test public void testGet() { Response response = target("/ready").request().get(); Assert.assertEquals(Response.Status.OK.getStatusCode(), response.getStatus()); }
@Test public void testAllUserIDsCSV() { String response = target("/user/allIDs").request().get(String.class); Assert.assertEquals(7, response.split("\n").length); }
@Test public void shouldPostOrderAndGetItBack() { OrderBean bean = new OrderBean(id(1L), 2L, OrderState.CREATED, Product.JUMPERS, 10, 100d); .request(APPLICATION_JSON_TYPE) .post(Entity.json(bean)); assertThat(response.getStatus()).isEqualTo(HttpURLConnection.HTTP_CREATED); .request(APPLICATION_JSON_TYPE) .get(new GenericType<OrderBean>() { }); .request(APPLICATION_JSON_TYPE) .get(new GenericType<OrderBean>() { });
@Test public void testTableSizeNotFound() { Response response = _webTarget.path("table/unknownTable/size").request().get(Response.class); Assert.assertEquals(response.getStatus(), Response.Status.NOT_FOUND.getStatusCode()); }
private Optional<SignedJwt> getAndCacheAppTokenFromServer() { MultivaluedMap<String, String> formData = new MultivaluedHashMap<>(); formData.putSingle("grant_type", "client_credentials"); formData.putSingle("scope", "urn:opc:idm:__myscopes__"); Response tokenResponse = tokenEndpoint .request() .accept(MediaType.APPLICATION_JSON_TYPE) .post(Entity.form(formData)); if (tokenResponse.getStatusInfo().getFamily() == Response.Status.Family.SUCCESSFUL) { JsonObject response = tokenResponse.readEntity(JsonObject.class); String accessToken = response.getString(ACCESS_TOKEN_KEY); LOGGER.finest(() -> "Access token: " + accessToken); SignedJwt signedJwt = SignedJwt.parseToken(accessToken); this.appToken = signedJwt; this.appJwt = signedJwt.getJwt(); return Optional.of(signedJwt); } else { LOGGER.severe("Failed to obtain access token for application to read groups" + " from IDCS. Response code: " + tokenResponse.getStatus() + ", entity: " + tokenResponse.readEntity(String.class)); return Optional.empty(); } }
final Invocation.Builder request = ClientBuilder.newClient() .target(host) .path("api") .path("v1") .path("configuration") .path("projectsEnabled") .request(); final String enabled = request.get(String.class); if (enabled == null || !Boolean.valueOf(enabled)) { final Response r = request.put(Entity.text(Boolean.TRUE.toString())); if (r.getStatusInfo().getFamily() != Response.Status.Family.SUCCESSFUL) { throw new WebApplicationException(String.format("Unable to enable projects: %s", r.getStatusInfo().getReasonPhrase()), r.getStatus());
public void get(final GetStatusConfig config) { final SSLContext sslContext = config.getSslContext(); final ClientBuilder clientBuilder = ClientBuilder.newBuilder(); if (sslContext != null) { clientBuilder.sslContext(sslContext); } final ClientConfig clientConfig = new ClientConfig(); clientConfig.property(ClientProperties.CONNECT_TIMEOUT, 10000); clientConfig.property(ClientProperties.READ_TIMEOUT, 10000); clientBuilder.withConfig(clientConfig); final Client client = clientBuilder.build(); final WebTarget target = client.target(config.getUrl()); final Response response = target.request().get(); System.out.println("Response Code - " + response.getStatus()); }
@Test public void testTableSizeNoDetails() { TableSizeInfo tableSizeInfo = _webTarget.path(TABLE_SIZE_PATH).queryParam("detailed", "false").request().get(TableSizeInfo.class); ImmutableSegment defaultSegment = _indexSegments.get(0); Assert.assertEquals(tableSizeInfo.tableName, TABLE_NAME); Assert.assertEquals(tableSizeInfo.diskSizeInBytes, defaultSegment.getSegmentSizeBytes()); Assert.assertEquals(tableSizeInfo.segments.size(), 0); }
@Test public void getTables() throws Exception { String tablesPath = "/tables"; Response response = _webTarget.path(tablesPath).request().get(Response.class); String responseBody = response.readEntity(String.class); TablesList tablesList = JsonUtils.stringToObject(responseBody, TablesList.class); Assert.assertNotNull(tablesList); List<String> tables = tablesList.getTables(); Assert.assertNotNull(tables); Assert.assertEquals(tables.size(), 1); Assert.assertEquals(tables.get(0), TABLE_NAME); String secondTable = "secondTable"; addTable(secondTable); response = _webTarget.path(tablesPath).request().get(Response.class); responseBody = response.readEntity(String.class); tablesList = JsonUtils.stringToObject(responseBody, TablesList.class); Assert.assertNotNull(tablesList); tables = tablesList.getTables(); Assert.assertNotNull(tables); Assert.assertEquals(tables.size(), 2); Assert.assertTrue(tables.contains(TABLE_NAME)); Assert.assertTrue(tables.contains(secondTable)); }
@Test public void testGet() { Response response = target("/ready").request().get(); Assert.assertEquals(Response.Status.OK.getStatusCode(), response.getStatus()); }
@Test public void testPopularRepresentativeItemsCSV() { String response = target("/popularRepresentativeItems").request().get(String.class); Assert.assertEquals(2, response.split("\n").length); }
@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); }
@Override public TokenResult refreshAccessToken(final String refreshToken) { refreshTokenProperties.put(OAuth2Parameters.REFRESH_TOKEN, refreshToken); final Form form = new Form(); for (final Map.Entry<String, String> entry : refreshTokenProperties.entrySet()) { form.param(entry.getKey(), entry.getValue()); } final Response response = client.target(refreshTokenUri) .request(MediaType.APPLICATION_JSON_TYPE) .post(Entity.entity(form, MediaType.APPLICATION_FORM_URLENCODED_TYPE)); if (response.getStatus() != 200) { throw new ProcessingException(LocalizationMessages.ERROR_FLOW_REQUEST_REFRESH_TOKEN(response.getStatus())); } this.tokenResult = response.readEntity(TokenResult.class); return tokenResult; }
@Override public EurekaHttpResponse<Void> statusUpdate(String asgName, ASGStatus newStatus) { Response response = null; try { String urlPath = "asg/" + asgName + "/status"; response = jerseyClient.target(serviceUrl) .path(urlPath) .queryParam("value", newStatus.name()) .request() .header(PeerEurekaNode.HEADER_REPLICATION, "true") .put(Entity.text("")); return EurekaHttpResponse.status(response.getStatus()); } finally { if (response != null) { response.close(); } } }
final Response response = client.target(FRIENDS_TIMELINE_URI).request().get(); if (response.getStatus() != 200) { String errorEntity = null; if (response.hasEntity()) { errorEntity = response.readEntity(String.class); + response.getStatus() + ", reason: " + response.getStatusInfo().getReasonPhrase() + ", entity: " + errorEntity); final List<Status> statuses = response.readEntity(new GenericType<List<Status>>() { });