private ClientResponse cache(ClientRequest request, ClientResponse response) throws Exception { if (response.getStatus() != 200) return response; return cacheIfPossible(request, (BaseClientResponse) response); }
private ClientResponse cache(ClientRequest request, ClientResponse response) throws Exception { if (response.getStatus() != 200) return response; return cacheIfPossible(request, (BaseClientResponse) response); }
public String print(ClientResponse<String> response) throws Exception { String result = null; // Check the HTTP status of the request // HTTP 200 indicates the request is OK if (response.getStatus() != 200) { throw new RuntimeException("Failed request with HTTP status: " + response.getStatus()); } // We have a good response, let's now read it BufferedReader br = new BufferedReader(new InputStreamReader( new ByteArrayInputStream(response.getEntity().getBytes()))); // Loop over the br in order to print out the contents System.out.println("\n*** Response from Server ***\n"); String output = null; while ((output = br.readLine()) != null) { System.out.println(output); result = output; } System.out.println("\n==============================================="); return result; }
public ClientResponseFailure(ClientResponse response) { super("Failed with status: " + response.getStatus()); this.response = BaseClientResponse.copyFromError(response); // release connection just in case it doesn't get garbage collected or manually released response.releaseConnection(); }
public ClientResponseFailure(ClientResponse response) { super("Failed with status: " + response.getStatus()); this.response = BaseClientResponse.copyFromError(response); // release connection just in case it doesn't get garbage collected or manually released response.releaseConnection(); }
protected void cacheIfPossible(final ClientRequest request, final ClientResponse<?> response) throws Exception { if (response.getStatus() == 200) { final String uri = request.getUri(); final String etag = (String) response.getHeaders().getFirst(HttpHeaders.ETAG); final String contentType = (String) response.getHeaders().getFirst(HttpHeaders.CONTENT_TYPE); if (etag != null && !etag.trim().isEmpty()) { cache.put(uri, etag, contentType); } } } }
protected void handleResponse(ClientResponse response) { if (response != null) { int status = response.getStatus(); response.releaseConnection(); if (status >= 400) { throw new OperationFailedException(status, "Operation failed. See server log messages."); } } } }
protected void handleResponse(ClientResponse response) { if (response != null) { int status = response.getStatus(); response.releaseConnection(); if (status >= 400) { throw new OperationFailedException(status, "Operation failed. See server log messages."); } } } }
protected ClientResponse handleExpired(ClientExecutionContext ctx, ClientRequest request, BrowserCache.Entry entry) throws Exception { ClientResponse response = ctx.proceed(); if (response.getStatus() == Response.Status.NOT_MODIFIED.getStatusCode()) { return updateOnNotModified(request, entry, (BaseClientResponse) response); } return cache(request, response); }
protected ClientResponse handleExpired(ClientExecutionContext ctx, ClientRequest request, BrowserCache.Entry entry) throws Exception { ClientResponse response = ctx.proceed(); if (response.getStatus() == Response.Status.NOT_MODIFIED.getStatusCode()) { return updateOnNotModified(request, entry, (BaseClientResponse) response); } return cache(request, response); }
public BaseResponse(ClientResponse<String> clientResponse) { if (clientResponse != null) { status = clientResponse.getStatus(); if (clientResponse.getLocationLink() != null) { location = clientResponse.getLocationLink().getHref(); } entity = clientResponse.getEntity(String.class); headers = clientResponse.getMetadata(); } }
/** * Helper methods */ private ClientResponse<?> checkResponse(ClientResponse<?> responseObj) throws Exception { responseObj.resetStream(); int status = responseObj.getStatus(); if (status != 200) { logger.warn("Response with exception:\n" + responseObj.getEntity(String.class)); assertEquals("Status OK", 200, status); } return responseObj; }
protected void initialize() throws Exception { super.start(); initialized = true; initAuthentication(); ClientRequest request = executor.createRequest(registration.getTarget().getHref()); for (XmlHttpHeader header : registration.getHeaders()) { request.header(header.getName(), header.getValue()); } ClientResponse<?> res = request.head(); if (res.getStatus() != 200) { throw new RuntimeException("Failed to query REST destination for init information. Status: " + res.getStatus()); } String url = (String) res.getHeaders().getFirst("msg-create-with-id"); if (url == null) { if (res.getLinkHeader() == null) { throw new RuntimeException("Could not find create-with-id URL"); } Link link = res.getLinkHeader().getLinkByTitle("create-with-id"); if (link == null) { throw new RuntimeException("Could not find create-with-id URL"); } url = link.getHref(); } targetUri = ResteasyUriBuilder.fromTemplate(url); }
protected void initialize() throws Exception { super.start(); initialized = true; initAuthentication(); ClientRequest request = executor.createRequest(registration.getTarget().getHref()); for (XmlHttpHeader header : registration.getHeaders()) { request.header(header.getName(), header.getValue()); } ClientResponse<?> res = request.head(); if (res.getStatus() != 200) { throw new RuntimeException("Failed to query REST destination for init information. Status: " + res.getStatus()); } String url = (String) res.getHeaders().getFirst("msg-create-with-id"); if (url == null) { if (res.getLinkHeader() == null) { throw new RuntimeException("Could not find create-with-id URL"); } Link link = res.getLinkHeader().getLinkByTitle("create-with-id"); if (link == null) { throw new RuntimeException("Could not find create-with-id URL"); } url = link.getHref(); } targetUri = ResteasyUriBuilder.fromTemplate(url); }
protected RuntimeException handleException(final Exception e) { if (e instanceof ClientResponseFailure) { final ClientResponseFailure crf = ((ClientResponseFailure) e); final ClientResponse response = crf.getResponse(); final int status = response.getStatus(); switch (status) { case HttpURLConnection.HTTP_BAD_REQUEST: return new BadRequestException(e); case HttpURLConnection.HTTP_INTERNAL_ERROR: return new InternalServerErrorException(e); case HttpURLConnection.HTTP_UNAUTHORIZED: return new UnauthorisedException(e); case 426: return new UpgradeException(e); case HttpURLConnection.HTTP_NOT_FOUND: return new NotFoundException(e); case HttpURLConnection.HTTP_FORBIDDEN: return new ForbiddenException(e); default: return crf; } } else if (e instanceof ProviderException) { return (ProviderException) e; } else if (e instanceof RuntimeException) { return (RuntimeException) e; } else { return new RuntimeException(e); } } }
/** * Test for registering permissions for resource */ @Test public void testRegisterPermissionForInvalidResource() throws Exception { showTitle("testRegisterPermissionForInvalidResource"); UmaPermission permission = new UmaPermission(); permission.setResourceId(this.registerResourceTest.resourceId + "1"); permission.setScopes(Arrays.asList("http://photoz.example.com/dev/scopes/view", "http://photoz.example.com/dev/scopes/all")); PermissionTicket ticket = null; try { ticket = getPermissionService().registerPermission( "Bearer " + this.registerResourceTest.pat.getAccessToken(), UmaPermissionList.instance(permission)); } catch (ClientResponseFailure ex) { System.err.println(ex.getResponse().getEntity(String.class)); assertTrue(ex.getResponse().getStatus() != Response.Status.CREATED.getStatusCode() && ex.getResponse().getStatus() != Response.Status.OK.getStatusCode() , "Unexpected response status"); } assertNull(ticket, "Resource permission is not null"); } }
@Override public void handle(ClientResponse<?> response) throws RuntimeException { final int status = response.getStatus(); String message = null; try { message = response.getEntity(String.class); } catch (Exception e) { } finally { response.releaseConnection(); } switch (status) { case HttpURLConnection.HTTP_BAD_REQUEST: throw new BadRequestException(message); case HttpURLConnection.HTTP_INTERNAL_ERROR: throw new InternalServerErrorException(message); case HttpURLConnection.HTTP_UNAUTHORIZED: throw new UnauthorisedException(message); case 426: throw new UpgradeException(message); case HttpURLConnection.HTTP_NOT_FOUND: throw new NotFoundException(message); } } }
/** * RP requests RPT with ticket and gets needs_info error (not all claims are provided, so redirect to claims-gathering endpoint) */ @Test(dependsOnMethods = {"rsRegisterPermissions"}) @Parameters({"umaPatClientId", "umaPatClientSecret"}) public void requestRptAndGetNeedsInfo(String umaPatClientId, String umaPatClientSecret) throws Exception { showTitle("requestRptAndGetNeedsInfo"); try { tokenService.requestRpt( "Basic " + encodeCredentials(umaPatClientId, umaPatClientSecret), GrantType.OXAUTH_UMA_TICKET.getValue(), permissionFlowTest.ticket, null, null, null, null, null); } catch (ClientResponseFailure ex) { // expected need_info error : // sample: {"error":"need_info","ticket":"c024311b-f451-41db-95aa-cd405f16eed4","required_claims":[{"issuer":["https://localhost:8443"],"name":"country","claim_token_format":["http://openid.net/specs/openid-connect-core-1_0.html#IDToken"],"claim_type":"string","friendly_name":"country"},{"issuer":["https://localhost:8443"],"name":"city","claim_token_format":["http://openid.net/specs/openid-connect-core-1_0.html#IDToken"],"claim_type":"string","friendly_name":"city"}],"redirect_user":"https://localhost:8443/restv1/uma/gather_claimsgathering_id=sampleClaimsGathering&&?gathering_id=sampleClaimsGathering&&"} String entity = (String) ex.getResponse().getEntity(String.class); System.out.println(entity); assertEquals(ex.getResponse().getStatus(), Response.Status.FORBIDDEN.getStatusCode(), "Unexpected response status"); needInfo = Util.createJsonMapper().readValue(entity, UmaNeedInfoResponse.class); assert_(needInfo); return; } throw new AssertionError("need_info error was not returned"); }
/** * Test UMA resource description modification with invalid PAT */ @Test(dependsOnMethods = {"modifyResource"}) public void testModifyResourceWithInvalidPat() throws Exception { showTitle("testModifyResourceWithInvalidPat"); UmaResourceResponse resourceStatus = null; try { UmaResource resource = new UmaResource(); resource.setName("Photo Album 4"); resource.setIconUri("http://www.example.com/icons/flower.png"); resource.setScopes(Arrays.asList("http://photoz.example.com/dev/scopes/view", "http://photoz.example.com/dev/scopes/all")); resourceStatus = getResourceService().updateResource("Bearer " + pat.getAccessToken() + "_invalid", this.resourceId + "_invalid", resource); } catch (ClientResponseFailure ex) { System.err.println(ex.getResponse().getEntity(String.class)); assertEquals(ex.getResponse().getStatus(), Response.Status.UNAUTHORIZED.getStatusCode(), "Unexpected response status"); } assertNull(resourceStatus, "Resource status is not null"); }
/** * Test non existing UMA resource description modification */ @Test(dependsOnMethods = {"modifyResource"}) public void modifyNotExistingResource() throws Exception { showTitle("modifyNotExistingResource"); try { UmaResource resource = new UmaResource(); resource.setName("Photo Album 3"); resource.setIconUri("http://www.example.com/icons/flower.png"); resource.setScopes(Arrays.asList("http://photoz.example.com/dev/scopes/view", "http://photoz.example.com/dev/scopes/all")); getResourceService().updateResource("Bearer " + pat.getAccessToken(), "fake_resource_id", resource); } catch (ClientResponseFailure ex) { System.err.println(ex.getResponse().getEntity(String.class)); int status = ex.getResponse().getStatus(); assertTrue(status != Response.Status.OK.getStatusCode(), "Unexpected response status"); } }