001    package org.hackystat.telemetry.service.resource.cache;
002    
003    import java.util.Map;
004    
005    import org.hackystat.dailyprojectdata.client.DailyProjectDataClient;
006    import org.hackystat.telemetry.service.resource.telemetry.TelemetryResource;
007    import org.restlet.Context;
008    import org.restlet.data.Request;
009    import org.restlet.data.Response;
010    import org.restlet.data.Status;
011    import org.restlet.resource.Representation;
012    import org.restlet.resource.Variant;
013    import static org.hackystat.telemetry.service.server.Authenticator.AUTHENTICATOR_DPD_CLIENTS_KEY;
014    
015    
016    /**
017     * This resource responds to requests of form:
018     * DELETE {host}/cache/{user}
019     * DELETE {host}/cache/{user}/{project}
020     * 
021     * The DELETE requests clear the caches for the user, dpdtype, or individual entry.
022     * The UriUser must always be the same as the authenticated user. 
023     * 
024     * @author Philip Johnson
025     */
026    public class CacheResource extends TelemetryResource {
027     
028      /**
029       * The standard constructor.
030       * 
031       * @param context The context.
032       * @param request The request object.
033       * @param response The response object.
034       */
035      public CacheResource(Context context, Request request, Response response) {
036        super(context, request, response);
037      }
038    
039      /**
040       * Indicate that GET is not supported.
041       * 
042       * @return False.
043       */
044      @Override
045      public boolean allowGet() {
046        return false;
047      }
048    
049      /**
050       * Get is not supported, but the method must be implemented.
051       * 
052       * @param variant Ignored.
053       * @return Null.
054       */
055      @Override
056      public Representation represent(Variant variant) {
057        getResponse().setStatus(Status.CLIENT_ERROR_METHOD_NOT_ALLOWED);
058        return null;
059      }
060      
061      /**
062       * Indicate the DELETE method is supported.
063       * 
064       * @return True.
065       */
066      @Override
067      public boolean allowDelete() {
068        return true;
069      }
070      
071      /**
072       * Responds to DELETE requests for clearing the cache. Includes:
073       * <ul>
074       * <li> DELETE {host}/cache/
075       * <li> DELETE {host}/cache/{user}/{project}
076       * </ul>
077       * Returns 200 if cache delete command succeeded. 
078       * The authorized user must be the same as the user specified in the URI.
079       */
080      @SuppressWarnings("unchecked")
081      @Override
082      public void removeRepresentations() {
083        try {
084          // [1] Get the associated dpdClient. Return immediately if we can't find one.
085          Map<String, DailyProjectDataClient> dpdMap =
086            (Map<String, DailyProjectDataClient>)this.telemetryServer.getContext().getAttributes()
087            .get(AUTHENTICATOR_DPD_CLIENTS_KEY);
088          DailyProjectDataClient client = dpdMap.get(this.authUser);
089          if (client == null) {
090            setStatusError("Error: DPD client not available for " + this.authUser);
091            return;
092          }
093          // Now clear the local DPD cache. 
094          if (projectName == null) {
095            client.clearLocalCache();
096            logRequest(String.format("Deleted all DPD cache entries for user %s", authUser));
097          }
098          else {
099            client.clearLocalCache(this.uriUser, this.projectName);
100            logRequest(String.format("Deleted some cache DPDs (project %s owned by %s) for %s", 
101                projectName, this.uriUser, this.authUser));
102          }
103        }
104        catch (Exception e) {
105          setStatusError("Error during cache delete", e);
106        }
107      }
108    }