Advertisement
Guest User

Untitled

a guest
May 30th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 5.13 KB | None | 0 0
  1. package com.example;
  2.  
  3. import java.util.concurrent.atomic.AtomicLong;
  4.  
  5. import org.restlet.data.MediaType;
  6. import org.restlet.data.Method;
  7. import org.restlet.representation.Representation;
  8. import org.restlet.resource.ClientResource;
  9. import org.restlet.resource.ResourceException;
  10.  
  11. /**
  12.  * Helps to perform REST request and transform the result.
  13.  * It wraps a ClientResource instance.
  14.  *
  15.  * @author by
  16.  */
  17. public final class SimpleClientResource implements SimpleClientResourceMBean {
  18.  
  19.     private RestClientResource resource;
  20.    
  21.     private AtomicLong postCount = new AtomicLong();
  22.    
  23.     private String url;
  24.     /**
  25.      * Constructor.
  26.      *
  27.      * @param uri The target URI.
  28.      */
  29.     public SimpleClientResource(final String uri) {
  30.         resource = new RestClientResource(uri);
  31.         url = uri;
  32.     }
  33.  
  34.     /**
  35.      * Executes GET request
  36.      */
  37.     public Representation get() {
  38.         return resource.get();
  39.     }
  40.  
  41.     /**
  42.      * Executes POST request
  43.      */
  44.     public Representation post(Object entity, String mediaType) {
  45.         Representation jaxbEntity = resource.toRepresentation(entity);
  46.         jaxbEntity.setMediaType(new MediaType(mediaType));
  47.        
  48.         postCount.incrementAndGet();
  49.        
  50.         return resource.post(jaxbEntity);
  51.     }
  52.  
  53.     /**
  54.      * Executes PUT request
  55.      */
  56.     public Representation put(Object entity, String mediaType) {
  57.         Representation jaxbEntity = resource.toRepresentation(entity);
  58.         jaxbEntity.setMediaType(new MediaType(mediaType));
  59.  
  60.         return resource.put(jaxbEntity);
  61.     }
  62.  
  63.     /**
  64.      * Executes DELETE request
  65.      */
  66.     public Representation delete(Object entity, String mediaType) {
  67.         Representation jaxbEntity = resource.toRepresentation(entity);
  68.         jaxbEntity.setMediaType(new MediaType(mediaType));
  69.  
  70.         return resource.delete();
  71.     }
  72.  
  73.     /**
  74.      * Converts response to the given class instance
  75.      */
  76.     public <T> T toObject(Representation response, Class<T> target) throws ResourceException {
  77.         return resource.toObject(response, target);
  78.     }
  79.  
  80.  
  81.     /**
  82.      * @return <code>true</code> if the result is EventValue object which reports about any problems with request data
  83.      */
  84.     public boolean isErrorValue(Representation response) {
  85.         return response.getMediaType().getName().equals(MediaTypes.ERROR_MEDIA_TYPE);
  86.     }
  87.  
  88.     /**
  89.      * @return True if the status is a success status.
  90.      */
  91.     public boolean isSuccess() {
  92.         return resource.getStatus().isSuccess();
  93.     }
  94.  
  95.     /**
  96.      * @return True if the status is an error (client or server) status.
  97.      */
  98.     public boolean isError() {
  99.         return resource.getStatus().isError();
  100.     }
  101.  
  102.     /**
  103.      * @return the HTTP status code of the response
  104.      */
  105.     public int getStatusCode() {
  106.         return resource.getStatus().getCode();
  107.     }
  108.  
  109.     /**
  110.      * @return the wrapped ClientResource instance
  111.      */
  112.     public ClientResource getWrappedClientResouce() {
  113.         return resource;
  114.     }
  115.     /**
  116.      * Releases the resource, cleaning everything up.
  117.      */
  118.     public void dispose() {
  119.         resource.release();
  120.     }
  121.     /**
  122.      * Gets the amount of times we have issued a POST request.
  123.      * @return The amount of times we have issued a POST request.
  124.      */
  125.     public Long getPostCount() {
  126.         return postCount.get();
  127.     }
  128.     /**
  129.      * Gets the URL with which this resource is associated.
  130.      * @return The REST resource URL.
  131.      */
  132.     public String getURL() {
  133.         return url;
  134.     }
  135.  
  136.     /**
  137.      * <code>RestClientResource</code>
  138.      */
  139.     private final class RestClientResource extends ClientResource {
  140.  
  141.         /**
  142.          * Constructor.
  143.          *
  144.          * @param uri The target URI.
  145.          */
  146.         public RestClientResource(String uri) {
  147.             super(uri);
  148.         }
  149.  
  150.         /**
  151.          * Executes GET request
  152.          */
  153.         @Override
  154.         public Representation get() {
  155.             setMethod(Method.GET);
  156.             return handle();
  157.         }
  158.  
  159.         /**
  160.          * Executes POST request
  161.          *
  162.          * @param entity
  163.          */
  164.         public Representation post(Representation entity) throws ResourceException {
  165.  
  166.             setMethod(Method.POST);
  167.             getRequest().setEntity(entity);
  168.  
  169.             return handle();
  170.         }
  171.  
  172.         @Override
  173.         protected Representation toRepresentation(Object source) {
  174.             return super.toRepresentation(source);
  175.         }
  176.  
  177.         /**
  178.          * Converts response to the given class instance
  179.          */
  180.         @Override
  181.         public <T> T toObject(Representation response, Class<T> target) throws ResourceException {
  182.             T result = null;
  183.  
  184.             if (response != null) {
  185.                 try {
  186.                     org.restlet.service.ConverterService cs = getConverterService();
  187.                     result = cs.toObject(response, target, this);
  188.                 } catch (Exception e) {
  189.                     throw new ResourceException(e);
  190.                 }
  191.             }
  192.  
  193.             return result;
  194.         }
  195.  
  196.     }
  197.  
  198.  
  199. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement