001    package org.hackystat.dailyprojectdata.resource.ping;
002    
003    import static org.hackystat.dailyprojectdata.server.ServerProperties.SENSORBASE_FULLHOST_KEY;
004    
005    import org.hackystat.dailyprojectdata.resource.dailyprojectdata.DailyProjectDataResource;
006    import org.hackystat.sensorbase.client.SensorBaseClient;
007    import org.restlet.Context;
008    import org.restlet.data.Request;
009    import org.restlet.data.Response;
010    import org.restlet.resource.Representation;
011    import org.restlet.resource.StringRepresentation;
012    import org.restlet.resource.Variant;
013    
014    /**
015     * The PingResource responds to a GET {host}/ping with the string "DailyProjectData".
016     * It responds to GET  {host}/ping?user={user}&password={password} with
017     * "DailyProjectData authenticated" if the user and password are valid, and 
018     * "DailyProjectData" if not valid. 
019     * @author Philip Johnson
020     */
021    public class PingResource extends DailyProjectDataResource {
022      
023      /** From the URI, if authentication is desired. */
024      private String user; 
025      /** From the URI, if authentication is desired. */
026      private String password;
027      
028      /**
029       * The standard constructor.
030       * @param context The context.
031       * @param request The request object.
032       * @param response The response object.
033       */
034      public PingResource(Context context, Request request, Response response) {
035        super(context, request, response);
036        this.user = (String) request.getAttributes().get("user");
037        this.password = (String) request.getAttributes().get("password");
038      }
039      
040      /**
041       * Returns the string "DailyProjectData" or "DailyProjectData authenticated", 
042       * depending upon whether credentials are passed as form parameters and whether
043       * they are valid. 
044       * @param variant The representational variant requested.
045       * @return The representation as a string.  
046       */
047      @Override
048      public Representation represent(Variant variant) {
049        String unauthenticated = "DailyProjectData";
050        String authenticated = "DailyProjectData authenticated";
051        // Don't try to authenticate unless the user has passed both a user and password. 
052        if ((user == null) || (password == null)) {
053          return new StringRepresentation(unauthenticated);
054        }
055        // There is a user and password. So, check the SensorBase to see. 
056        String sensorBaseHost = server.getServerProperties().get(SENSORBASE_FULLHOST_KEY);
057        boolean OK = SensorBaseClient.isRegistered(sensorBaseHost, user, password);
058        return new StringRepresentation((OK ? authenticated : unauthenticated));
059      }
060      
061    
062    }