001    package org.hackystat.sensorbase.resource.sensorbase;
002    
003    import java.util.logging.Logger;
004    
005    import org.hackystat.sensorbase.resource.users.jaxb.User;
006    import org.hackystat.utilities.stacktrace.StackTrace;
007    
008    /**
009     * Provides standardized strings and formatting for response codes.  
010     * This class is intended to make error reporting more uniform and informative. 
011     * A good error message will always include an explanation for why the operation failed,
012     * and what the requested operation was. 
013     * @author Philip Johnson
014     */
015    public class ResponseMessage {
016      
017      /**
018       * The error message for requests that only the admin can handle. 
019       * @param resource The resource associated with this request. 
020       * @return A string describing the problem.
021       */
022       static String adminOnly(SensorBaseResource resource) {
023        return String.format("Request requires administrator privileges:%n  Request: %s %s",
024            resource.getRequest().getMethod().getName(),
025            resource.getRequest().getResourceRef().toString());
026      }
027      
028      /**
029       * The error message for requests where the authorized user must be the same as the user
030       * in the URI string, or the authorized use is the admin (and then the user in the URI string
031       * can be anyone).
032       * @param resource The resource associated with this request.
033       * @param authUser The authorized user. 
034       * @param uriUser The user in the URI string.  
035       * @return A string describing the problem.
036       */
037      static String adminOrAuthUserOnly(SensorBaseResource resource, String authUser, 
038          String uriUser) {
039        return String.format("Request requires authorized user (%s) to be the same user as the " +
040            "URL user (%s):%n  Request: %s %s", authUser, uriUser, 
041            resource.getRequest().getMethod().getName(),
042            resource.getRequest().getResourceRef().toString());
043      }
044      
045      /**
046       * The error message for requests that generate an unspecified internal error. 
047       * @param resource The resource associated with this request. 
048       * @param logger The logger. 
049       * @param e The exception. 
050       * @return A string describing the problem.
051       */
052      static String internalError(SensorBaseResource resource, Logger logger, Exception e) {
053        String message =  String.format("Internal error %s:%n  Request: %s %s",
054            e.getMessage(),
055            resource.getRequest().getMethod().getName(),
056            resource.getRequest().getResourceRef().toString());
057        logger.info(String.format("%s\n%s", message, StackTrace.toString(e)));
058        return message;
059      }
060    
061      /**
062       * The error message for miscellaneous "one off" error messages. 
063       * @param resource The resource associated with this request. 
064       * @param message A short string describing the problem.
065       * @return A string describing the problem.
066       */
067       static String miscError(SensorBaseResource resource, String message) {
068        return String.format("Request generated error: %s:%n  Request: %s %s", 
069            message,  
070            resource.getRequest().getMethod().getName(),
071            resource.getRequest().getResourceRef().toString());
072      }
073      
074      /**
075       * The error message for unknown users.
076       * @param resource The resource associated with this request. 
077       * @param user A short string describing the problem.
078       * @return A string describing the problem.
079       */
080      static String undefinedUser(SensorBaseResource resource, String user) {
081        return String.format("Undefined user %s:%n  Request: %s %s", 
082            user,
083            resource.getRequest().getMethod().getName(),
084            resource.getRequest().getResourceRef().toString());
085      }
086      
087      /**
088       * The error message for requests involving projects not owned by the specified user. 
089       * @param resource The resource associated with this request. 
090       * @param user The user. 
091       * @param project The project. 
092       * @return A string describing the problem.
093       */
094      static String undefinedProject(SensorBaseResource resource, User user, String project) {
095        return String.format("Undefined project %s for user %s:%n  Request: %s %s", 
096             project, user.getEmail(),
097            resource.getRequest().getMethod().getName(),
098            resource.getRequest().getResourceRef().toString());
099      } 
100      
101      /**
102       * The error message for requests involving projects not owned by the specified user. 
103       * @param resource The resource associated with this request. 
104       * @param user The user. 
105       * @param project The project. 
106       * @return A string describing the problem.
107       */
108      static String cannotViewProject(SensorBaseResource resource, String user, String project) {
109        return String.format("User %s not allowed to view project %s:%n  Request: %s %s", 
110            user, project, 
111            resource.getRequest().getMethod().getName(),
112            resource.getRequest().getResourceRef().toString());
113      }
114      
115      /**
116       * The error message for requests where the requesting user is not the owner. 
117       * @param resource The resource associated with this request. 
118       * @param user The user. 
119       * @param project The project
120       * @return A string describing the problem.
121       */
122      static String notProjectOwner(SensorBaseResource resource, String user, String project) {
123        return String.format("Authorized user %s is not owner of project %s%n  Request: %s %s", 
124            user, project,
125            resource.getRequest().getMethod().getName(),
126            resource.getRequest().getResourceRef().toString());
127      }
128      
129      
130      /**
131       * The error message for requests where a timestamp is not supplied or is not parsable.
132       * @param resource The resource associated with this request.
133       * @param timestamp The bogus timestamp. 
134       * @return A string describing the problem.
135       */
136      static String badTimestamp(SensorBaseResource resource, String timestamp) {
137        return String.format("Bad timestamp %s:%n  Request: %s %s", 
138            timestamp,
139            resource.getRequest().getMethod().getName(),
140            resource.getRequest().getResourceRef().toString());
141      }
142    
143    }