/** * Indicates if an error is recoverable, meaning that simply retrying after * a delay could result in a success. Tests {@link #isConnectorError()} and * if the status is {@link #CLIENT_ERROR_REQUEST_TIMEOUT} or * {@link #SERVER_ERROR_GATEWAY_TIMEOUT} or * {@link #SERVER_ERROR_SERVICE_UNAVAILABLE}. * * @return True if the error is recoverable. */ public boolean isRecoverableError() { return isConnectorError() || equals(Status.CLIENT_ERROR_REQUEST_TIMEOUT) || equals(Status.SERVER_ERROR_GATEWAY_TIMEOUT) || equals(Status.SERVER_ERROR_SERVICE_UNAVAILABLE); }
/** * Indicates if an error is recoverable, meaning that simply retrying after * a delay could result in a success. Tests {@link #isConnectorError()} and * if the status is {@link #CLIENT_ERROR_REQUEST_TIMEOUT} or * {@link #SERVER_ERROR_GATEWAY_TIMEOUT} or * {@link #SERVER_ERROR_SERVICE_UNAVAILABLE}. * * @return True if the error is recoverable. */ public boolean isRecoverableError() { return isConnectorError() || equals(Status.CLIENT_ERROR_REQUEST_TIMEOUT) || equals(Status.SERVER_ERROR_GATEWAY_TIMEOUT) || equals(Status.SERVER_ERROR_SERVICE_UNAVAILABLE); }
public void assertNotExists(String... ids) throws IOException { StringBuilder result = new StringBuilder(); for (String id : ids) { Response response = this.sendMessage(Method.GET, null, id); if (!Status.CLIENT_ERROR_NOT_FOUND.equals(response.getStatus())) { result.append("Privilege shouldn't exist '").append(id).append("': "); result.append(response.getEntity().getText()).append('\n'); } } if (result.length() != 0) { Assert.fail(result.toString()); } }
protected boolean deleteFromRepository(String repository, String groupOrArtifactPath) throws IOException { String serviceURI = String.format("service/local/repositories/%s/content/%s", repository, groupOrArtifactPath); Status status = RequestFacade.doGetForStatus(serviceURI); if (status.equals(Status.CLIENT_ERROR_NOT_FOUND)) { log.debug("Not deleted because it didn't exist: " + serviceURI); return true; } log.debug("deleting: " + serviceURI); status = RequestFacade.doDeleteForStatus(serviceURI, null); boolean deleted = status.isSuccess(); if (!deleted) { log.debug("Failed to delete: " + serviceURI + " - Status: " + status); } // fake it because the artifact doesn't exist // TODO: clean this up. if (status.getCode() == 404) { deleted = true; } return deleted; }
private DataREST handleAddResponse(Response response) { if (! response.getStatus().equals(Status.SUCCESS_CREATED)) { logger.warn("Did not create baclava document"); return null; } if (response.getRedirectRef() == null) { logger.error("Did not get redirect reference for data document"); return null; } invalidate(); return new DataREST(context, response.getRedirectRef()); }
@Override protected void doError(Status status) { if (status.equals(Status.CLIENT_ERROR_NOT_ACCEPTABLE) && (status.getThrowable() == null)) { // we have an ontopia error for this List<Preference<MediaType>> acceptedMediaTypes = getClientInfo().getAcceptedMediaTypes(); List<MediaType> accepted = new ArrayList<>(acceptedMediaTypes.size()); for (Preference<MediaType> mt : acceptedMediaTypes) { accepted.add(mt.getMetadata()); } super.doError(OntopiaRestErrors.UNSUPPORTED_MIME_TYPE.build(getClass().getName(), Arrays.toString(accepted.toArray())).getStatus()); } else { super.doError(status); } }
public WorkflowREST add(String workflow) throws NotSuccessException { Response response = context.post(getURIReference(), workflow, RESTContext.scuflType); if (! response.getStatus().equals(Status.SUCCESS_CREATED)) { logger.warn("Did not create workflow: " + workflow); return null; } if (response.getRedirectRef() == null) { logger.error("Did not get redirect reference for workflow " + workflow); return null; } invalidate(); return new WorkflowREST(context, response.getRedirectRef()); }
@Override public Object call(final Class<?> clazz, final String path) throws ServiceException { ClientResource service = createClient(path); try { Representation result = service.delete(); if (service.getStatus().equals(Status.SUCCESS_NO_CONTENT)) { LOGGER.info("Successfully deleted {}", path); } else { JsonRepresentation jsonRepresentation = new JsonRepresentation(result); JSONObject jsonResult = jsonRepresentation.getJsonObject(); LOGGER.error("Unexpected HTTP status code returned: {}", service.getStatus().getCode()); ResponseEntity response = JsonUtil.fromJson(jsonResult, ResponseEntity.class); throw new ServiceException(response); } } catch (JSONException | IOException e) { LOGGER.error("Unexpected Exception", e); throw new ServiceException(e); } return null; }
@Override public <T> String encodeMetadata(MetadataDataType metadataDataType, T instance) throws ServiceException { String encodedRawValue; ClientResource service = createClient(MetadataEndpoints.encodeMetadata(metadataDataType)); try { Representation result = service.post(new StringRepresentation(instance.toString())); JsonRepresentation jsonRepresentation = new JsonRepresentation(result); JSONObject jsonResult = jsonRepresentation.getJsonObject(); if (service.getStatus().equals(Status.SUCCESS_OK)) { encodedRawValue = jsonResult.getString(Field.RAW_VALUE_FIELD); } else { LOGGER.error("Unexpected HTTP status code returned: {}", service.getStatus().getCode()); ResponseEntity response = JsonUtil.fromJson(jsonResult, ResponseEntity.class); throw new ServiceException(response); } } catch (JSONException | IOException e) { LOGGER.error("Unexpected Exception", e); throw new ServiceException(e); } return encodedRawValue; }
/** * Evaluates the preconditions of the current request against the given last * modified date and / or the given entity tag. This method does not check, * if the arguments are not null. * * @param lastModified * @param entityTag * @return * @see Request#evaluateConditions(Tag, Date) */ private ResponseBuilder evaluatePreconditionsInternal( final Date lastModified, final EntityTag entityTag) { Status status = this.request.getConditions().getStatus( this.request.getMethod(), true, Converter.toRestletTag(entityTag), lastModified); if (status == null) return null; if (status.equals(Status.REDIRECTION_NOT_MODIFIED)) { final ResponseBuilder rb = Response.notModified(); rb.lastModified(lastModified); rb.tag(entityTag); return rb; } return Response.status(STATUS_PREC_FAILED); }
public static void main(String[] args) throws Exception { // Prepare the request ClientResource resource = new ClientResource("http://localhost:8111/"); // Add the client authentication to the call ChallengeScheme scheme = ChallengeScheme.HTTP_BASIC; ChallengeResponse authentication = new ChallengeResponse(scheme, "scott", "tiger"); resource.setChallengeResponse(authentication); // Send the HTTP GET request resource.get(); if (resource.getStatus().isSuccess()) { // Output the response entity on the JVM console resource.getResponseEntity().write(System.out); } else if (resource.getStatus() .equals(Status.CLIENT_ERROR_UNAUTHORIZED)) { // Unauthorized access System.out .println("Access authorized by the server, check your credentials"); } else { // Unexpected status System.out.println("An unexpected status was returned: " + resource.getStatus()); } }
/** * Posts a member to the collection resulting in the creation of a new * resource. * * @param member * The member representation to post. * @return The reference of the new resource. * @throws Exception */ public Reference postMember(Representation member) throws Exception { final Request request = new Request(Method.POST, getHref(), member); final Response response = getWorkspace().getService() .getClientDispatcher().handle(request); if (response.getStatus().equals(Status.SUCCESS_CREATED)) { return response.getLocationRef(); } throw new Exception( "Couldn't post the member representation. Status returned: " + response.getStatus()); }
/** * Posts a member to the collection resulting in the creation of a new * resource. * * @param member * The member representation to post. * @return The reference of the new resource. * @throws Exception */ public Reference postMember(Representation member) throws Exception { final Request request = new Request(Method.POST, getHref(), member); final Response response = getWorkspace().getService() .getClientDispatcher().handle(request); if (response.getStatus().equals(Status.SUCCESS_CREATED)) { return response.getLocationRef(); } throw new Exception( "Couldn't post the member representation. Status returned: " + response.getStatus()); }
public static void main(String[] args) throws Exception { ClientResource cr = new ClientResource("http://localhost:8182/path"); cr.addQueryParameter("q", "hello"); cr.get().write(System.out); cr = new ClientResource("http://localhost:8182/path"); cr.addQueryParameter("q", "bye"); cr.get().write(System.out); cr = new ClientResource("http://localhost:8182/path"); cr.addQueryParameter("q", "test"); try { cr.get(); } catch (Exception e) { if (Status.CLIENT_ERROR_NOT_FOUND.equals(cr.getStatus())) { System.out.println("fine."); } else { System.out.println("Should be 404 not found response."); } } } }
public static void main(String[] args) throws ResourceException, IOException { // Prepare the request ClientResource cr = new ClientResource("http://localhost:8182/"); ChallengeRequest c1 = null; // first try: unauthenticated request try { cr.get(); } catch (ResourceException re) { if (Status.CLIENT_ERROR_UNAUTHORIZED.equals(cr.getStatus())) { c1 = getDigestChallengeRequest(cr); } } // second try: authenticated request if (c1 != null) { ChallengeResponse c2 = new ChallengeResponse(c1, cr.getResponse(), "scott", "tiger".toCharArray()); cr.setChallengeResponse(c2); cr.get().write(System.out); } }
@Override public JSONObject decodeMetadata(MetadataDataType metadataDataType, JSONObject jsonObject) throws ServiceException { JSONObject jsonResult; ClientResource service = createClient(MetadataEndpoints.decodeMetadata(metadataDataType)); try { Representation result = service.post(new JsonRepresentation(jsonObject)); JsonRepresentation jsonRepresentation = new JsonRepresentation(result); jsonResult = jsonRepresentation.getJsonObject(); if (service.getStatus().equals(Status.SUCCESS_OK)) { return jsonResult; } else { LOGGER.error("Unexpected HTTP status code returned: {}", service.getStatus().getCode()); ResponseEntity response = JsonUtil.fromJson(jsonResult, ResponseEntity.class); throw new ServiceException(response); } } catch (JSONException | IOException e) { LOGGER.error("Unexpected Exception", e); throw new ServiceException(e); } }
/** * Posts a member to the collection resulting in the creation of a new * resource. * * @param member * The member representation to post. * @return The reference of the new resource. * @throws Exception */ public Reference postMember(Representation member) throws Exception { final Request request = new Request(Method.POST, getHref(), member); final Response response = getWorkspace().getService() .getClientDispatcher().handle(request); if (response.getStatus().equals(Status.SUCCESS_CREATED)) { return response.getLocationRef(); } else { throw new Exception( "Couldn't post the member representation. Status returned: " + response.getStatus().getDescription()); } }
/** * Returns the feed representation. * * @return The feed representation. * @throws Exception */ public Feed getFeed() throws Exception { final Reference feedRef = getHref(); if (feedRef.isRelative()) { feedRef.setBaseRef(getWorkspace().getService().getReference()); } final Request request = new Request(Method.GET, feedRef.getTargetRef()); final Response response = getWorkspace().getService() .getClientDispatcher().handle(request); if (response.getStatus().equals(Status.SUCCESS_OK)) { return new Feed(response.getEntity()); } throw new Exception( "Couldn't get the feed representation. Status returned: " + response.getStatus()); }
/** * Returns the feed representation. * * @return The feed representation. * @throws Exception */ public Feed getFeed() throws Exception { final Reference feedRef = getHref(); if (feedRef.isRelative()) { feedRef.setBaseRef(getWorkspace().getService().getReference()); } final Request request = new Request(Method.GET, feedRef.getTargetRef()); final Response response = getWorkspace().getService() .getClientDispatcher().handle(request); if (response.getStatus().equals(Status.SUCCESS_OK)) { return new Feed(response.getEntity()); } throw new Exception( "Couldn't get the feed representation. Status returned: " + response.getStatus()); }
/** * Returns the feed representation. * * @return The feed representation. * @throws Exception */ public Feed getFeed() throws Exception { final Reference feedRef = getHref(); if (feedRef.isRelative()) { feedRef.setBaseRef(getWorkspace().getService().getReference()); } final Request request = new Request(Method.GET, feedRef.getTargetRef()); final Response response = getWorkspace().getService() .getClientDispatcher().handle(request); if (response.getStatus().equals(Status.SUCCESS_OK)) { return new Feed(response.getEntity()); } else { throw new Exception( "Couldn't get the feed representation. Status returned: " + response.getStatus().getDescription()); } }