Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.fasterxml.clustermate.jaxrs;
  
  import java.io.*;
  
  import javax.ws.rs.*;
  import javax.ws.rs.core.*;
  
  
 
Main resource to interface with storage system when deploying on a JAX-RS container, exposing basic CRUD (PUT, GET, DELETE) entry points.
 
 @Path("/prefix/store/entry")
 public abstract class StoreResource<K extends EntryKey, E extends StoredEntry<K>>
 {
     /*
     /**********************************************************************
     /* Helper objects
     /**********************************************************************
      */
 
     protected final SharedServiceStuff _stuff;
     
     protected final StoreHandler<K,E,?> _storeHandler;
 
     protected final ClusterViewByServer _clusterView;
 
     protected final EntryKeyConverter<K> _keyConverter;
     
     /*
     /**********************************************************************
     /* Life-cycle
     /**********************************************************************
      */
     
     public StoreResource(SharedServiceStuff stuffClusterViewByServer clusterView,
             StoreHandler<K,E,?> storeHandler)
     {
          = stuff;
          = storeHandler;
          = clusterView;
          = stuff.getKeyConverter();
     }
 
     /*
     /**********************************************************************
     /* Helper methods for unit tests
     /**********************************************************************
      */
 
     public ServiceConfig getServiceConfig() {
         return .getServiceConfig();
     }
    
    
Method to be only used by unit tests...
 
     public StoreHandler<K,E,?> getHandler() {
         return ;
     }
 
     public Stores<K,E> getStores() {
         return .getStores();
     }
 
     public ClusterViewByServer getCluster() {
         return ;
     }
     
     /*
     /**********************************************************************
     /* Content creation
     /**********************************************************************
      */
     
     @PUT @Timed
     @Path("{externalPath: .*}")
     public Response putPrimary(@Context UriInfo uriInfo, @Context HttpHeaders headers,
             @PathParam("externalPath"String externalPath,
             InputStream dataIn)
     	throws IOExceptionStoreException
     {
         return handlePut(new JaxrsHttpRequest(uriInfoheadersexternalPath.),
                dataIn);
    }
    // Alias for PUT (why not)
    @POST @Timed
    @Path("{externalPath: .*}")
    public Response postPrimary(@Context UriInfo uriInfo, @Context HttpHeaders headers,
            @PathParam("externalPath"String externalPath,
            InputStream dataIn)
        throws IOExceptionStoreException
    {
        return handlePut(new JaxrsHttpRequest(uriInfoheadersexternalPath.), dataIn);
    }
    
    protected final Response handlePut(JaxrsHttpRequest requestInputStream dataIn)
        throws IOExceptionStoreException
    {
        JaxrsHttpResponse response = new JaxrsHttpResponse();
        K key = _findKey(requestresponse);
        if (key != null) {
            .putEntry(requestresponsekeydataIn);
            // for all requests, or just non-fail ones?
            _addStdHeaders(response);
        }
        return response.buildResponse();
    }
    /*
    /**********************************************************************
    /* Content lookups
    /**********************************************************************
     */
    @GET
    @Path("{externalPath: .*}")
    @Timed
    // can't define content type: successful requests give 'raw' type; errors JSON
    public Response getPrimary(@Context UriInfo uriInfo, @Context HttpHeaders headers,
            @PathParam("externalPath"String externalPath)
                    throws StoreException
    {
        JaxrsHttpRequest request = new JaxrsHttpRequest(uriInfoheadersexternalPath.);
        JaxrsHttpResponse response = new JaxrsHttpResponse();
        K key = _findKey(requestresponse);
        if (key != null) {
            .getEntry(requestresponsekey);
            // for all requests, or just non-fail ones?
            _addStdHeaders(response);
        }
        return response.buildResponse();
    }
    @HEAD
    @Path("{externalPath: .*}")
    @Timed
    // no content type, as per GET
    public Response headPrimary(@Context UriInfo uriInfo, @Context HttpHeaders headers,
            @PathParam("externalPath"String externalPath)
                    throws StoreException
    {
        JaxrsHttpRequest request = new JaxrsHttpRequest(uriInfoheadersexternalPath.);
        JaxrsHttpResponse response = new JaxrsHttpResponse();
        K key = _findKey(requestresponse);
        if (key != null) {
            .getEntryStats(requestresponsekey);
            _addStdHeaders(response);
        }
        return response.buildResponse();
    }
    /*
    /**********************************************************************
    /* Content removal
    /**********************************************************************
     */
    @DELETE
    @Path("{externalPath: .*}")
    @Timed
    public Response deletePrimary(@Context UriInfo uriInfo, @Context HttpHeaders headers,
            @PathParam("externalPath"String externalPath)
    	throws IOExceptionStoreException
    {
        JaxrsHttpRequest request = new JaxrsHttpRequest(uriInfoheadersexternalPath.);
        JaxrsHttpResponse response = new JaxrsHttpResponse();
        K key = _findKey(requestresponse);
        if (key != null) {
            .removeEntry(requestresponsekey);
            _addStdHeaders(response);
        }
        return response.buildResponse();
    }
    /*
    /**********************************************************************
    /* Internal methods
    /**********************************************************************
     */
    protected K _findKey(JaxrsHttpRequest requestJaxrsHttpResponse response) {
        return .extractFromPath(request);
    }
    
    protected ServiceResponse _addStdHeaders(ServiceResponse response)
    {
        if ( != null) {
            response = .addClusterStateInfo(response);
        }
        return response;
    }
New to GrepCode? Check out our FAQ X