001 package org.hackystat.dailyprojectdata.resource.cache; 002 003 import java.util.logging.Logger; 004 import org.hackystat.dailyprojectdata.resource.dailyprojectdata.DailyProjectDataResource; 005 import org.hackystat.sensorbase.client.SensorBaseClient; 006 import org.restlet.Context; 007 import org.restlet.data.Request; 008 import org.restlet.data.Response; 009 import org.restlet.data.Status; 010 import org.restlet.resource.Representation; 011 import org.restlet.resource.Variant; 012 013 /** 014 * This resource responds to requests of form: 015 * <pre>DELETE {host}/cache</pre> 016 * This one clears all entries associated with the authorized user; in other words, all projects 017 * that this user owns will have any cached DPDs removed. 018 * <p> 019 * It also responds to 020 * <pre>DELETE {host}/cache/{user}/{project}</pre> 021 * This one clears only those cached DPD instances for the specified project owned by that user. 022 * In this case, the authorized user must be in the project specified by (project, user). 023 * 024 * @author Philip Johnson 025 */ 026 public class CacheResource extends DailyProjectDataResource { 027 028 /** 029 * The default constructor. 030 * @param context The context. 031 * @param request The request. 032 * @param response The response. 033 */ 034 public CacheResource(Context context, Request request, Response response) { 035 super(context, request, response); 036 } 037 038 /** 039 * Returns 200 if cache delete command succeeded. 040 * If deleting the entire cache, then the authUser must be the UriUser. 041 * If deleting a project cache, then the authUser must be in the project 042 * identified by UriUser and project. 043 */ 044 @Override 045 public void removeRepresentations() { 046 Logger logger = this.server.getLogger(); 047 logger.fine(String.format("Delete cache: %s %s %s ", authUser, uriUser, project)); 048 try { 049 // Delete entire cache for this user. 050 if (this.uriUser == null) { 051 // Invoke the clear operation on the entire user's cache. 052 super.server.getFrontSideCache().clear(authUser); 053 logger.info(String.format("All DPD cache entries deleted for %s ", authUser)); 054 getResponse().setStatus(Status.SUCCESS_OK); 055 return; 056 } 057 058 // Otherwise user and project specified. Return now if authUser not in project. 059 SensorBaseClient client = super.getSensorBaseClient(); 060 if (!client.inProject(uriUser, project)) { 061 String msg = String.format("Authenticated user (%s) isn't in project (%s) owned by %s", 062 authUser, project, uriUser); 063 setStatusError(msg); 064 return; 065 } 066 067 // If we're here, we are OK to delete the cache associated with the user and project. 068 super.server.getFrontSideCache().clear(uriUser, project); 069 logger.info(String.format("All DPD cache entries deleted for %s/%s. ", uriUser, project)); 070 return; 071 } 072 catch (Exception e) { 073 setStatusError("Error during cache deletion", e); 074 return; 075 } 076 } 077 078 /** 079 * Indicate the DELETE method is supported. 080 * 081 * @return True. 082 */ 083 @Override 084 public boolean allowDelete() { 085 return true; 086 } 087 088 /** 089 * Indicate that GET is not supported. 090 * 091 * @return False. 092 */ 093 @Override 094 public boolean allowGet() { 095 return false; 096 } 097 098 /** 099 * Get is not supported, but the method must be implemented. 100 * 101 * @param variant Ignored. 102 * @return Null. 103 */ 104 @Override 105 public Representation represent(Variant variant) { 106 getResponse().setStatus(Status.CLIENT_ERROR_METHOD_NOT_ALLOWED); 107 return null; 108 } 109 110 }