Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example;
- import java.util.concurrent.atomic.AtomicLong;
- import org.restlet.data.MediaType;
- import org.restlet.data.Method;
- import org.restlet.representation.Representation;
- import org.restlet.resource.ClientResource;
- import org.restlet.resource.ResourceException;
- /**
- * Helps to perform REST request and transform the result.
- * It wraps a ClientResource instance.
- *
- * @author by
- */
- public final class SimpleClientResource implements SimpleClientResourceMBean {
- private RestClientResource resource;
- private AtomicLong postCount = new AtomicLong();
- private String url;
- /**
- * Constructor.
- *
- * @param uri The target URI.
- */
- public SimpleClientResource(final String uri) {
- resource = new RestClientResource(uri);
- url = uri;
- }
- /**
- * Executes GET request
- */
- public Representation get() {
- return resource.get();
- }
- /**
- * Executes POST request
- */
- public Representation post(Object entity, String mediaType) {
- Representation jaxbEntity = resource.toRepresentation(entity);
- jaxbEntity.setMediaType(new MediaType(mediaType));
- postCount.incrementAndGet();
- return resource.post(jaxbEntity);
- }
- /**
- * Executes PUT request
- */
- public Representation put(Object entity, String mediaType) {
- Representation jaxbEntity = resource.toRepresentation(entity);
- jaxbEntity.setMediaType(new MediaType(mediaType));
- return resource.put(jaxbEntity);
- }
- /**
- * Executes DELETE request
- */
- public Representation delete(Object entity, String mediaType) {
- Representation jaxbEntity = resource.toRepresentation(entity);
- jaxbEntity.setMediaType(new MediaType(mediaType));
- return resource.delete();
- }
- /**
- * Converts response to the given class instance
- */
- public <T> T toObject(Representation response, Class<T> target) throws ResourceException {
- return resource.toObject(response, target);
- }
- /**
- * @return <code>true</code> if the result is EventValue object which reports about any problems with request data
- */
- public boolean isErrorValue(Representation response) {
- return response.getMediaType().getName().equals(MediaTypes.ERROR_MEDIA_TYPE);
- }
- /**
- * @return True if the status is a success status.
- */
- public boolean isSuccess() {
- return resource.getStatus().isSuccess();
- }
- /**
- * @return True if the status is an error (client or server) status.
- */
- public boolean isError() {
- return resource.getStatus().isError();
- }
- /**
- * @return the HTTP status code of the response
- */
- public int getStatusCode() {
- return resource.getStatus().getCode();
- }
- /**
- * @return the wrapped ClientResource instance
- */
- public ClientResource getWrappedClientResouce() {
- return resource;
- }
- /**
- * Releases the resource, cleaning everything up.
- */
- public void dispose() {
- resource.release();
- }
- /**
- * Gets the amount of times we have issued a POST request.
- * @return The amount of times we have issued a POST request.
- */
- public Long getPostCount() {
- return postCount.get();
- }
- /**
- * Gets the URL with which this resource is associated.
- * @return The REST resource URL.
- */
- public String getURL() {
- return url;
- }
- /**
- * <code>RestClientResource</code>
- */
- private final class RestClientResource extends ClientResource {
- /**
- * Constructor.
- *
- * @param uri The target URI.
- */
- public RestClientResource(String uri) {
- super(uri);
- }
- /**
- * Executes GET request
- */
- @Override
- public Representation get() {
- setMethod(Method.GET);
- return handle();
- }
- /**
- * Executes POST request
- *
- * @param entity
- */
- public Representation post(Representation entity) throws ResourceException {
- setMethod(Method.POST);
- getRequest().setEntity(entity);
- return handle();
- }
- @Override
- protected Representation toRepresentation(Object source) {
- return super.toRepresentation(source);
- }
- /**
- * Converts response to the given class instance
- */
- @Override
- public <T> T toObject(Representation response, Class<T> target) throws ResourceException {
- T result = null;
- if (response != null) {
- try {
- org.restlet.service.ConverterService cs = getConverterService();
- result = cs.toObject(response, target, this);
- } catch (Exception e) {
- throw new ResourceException(e);
- }
- }
- return result;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement