001 package org.hackystat.telemetry.service.resource.ping; 002 003 import static org.hackystat.telemetry.service.server.ServerProperties.SENSORBASE_FULLHOST_KEY; 004 005 import org.hackystat.sensorbase.client.SensorBaseClient; 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.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 "Telemetry". 016 * It responds to GET {host}/ping?user={user}&password={password} with 017 * "Telemetry authenticated" if the user and password are valid, and 018 * "Telemetry" if not valid. 019 * @author Philip Johnson 020 */ 021 public class PingResource extends TelemetryResource { 022 /** From the URI, if authentication is desired. */ 023 private String user; 024 /** From the URI, if authentication is desired. */ 025 private String password; 026 027 /** 028 * The standard constructor. 029 * @param context The context. 030 * @param request The request object. 031 * @param response The response object. 032 */ 033 public PingResource(Context context, Request request, Response response) { 034 super(context, request, response); 035 this.user = (String) request.getAttributes().get("user"); 036 this.password = (String) request.getAttributes().get("password"); 037 } 038 039 /** 040 * Returns the string "DailyProjectData" or "DailyProjectData authenticated", 041 * depending upon whether credentials are passed as form parameters and whether 042 * they are valid. 043 * @param variant The representational variant requested. 044 * @return The representation as a string. 045 */ 046 @Override 047 public Representation represent(Variant variant) { 048 try { 049 String unauthenticated = "Telemetry"; 050 String authenticated = "Telemetry 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 if they're OK. 056 String sensorBaseHost = telemetryServer.getServerProperties().get(SENSORBASE_FULLHOST_KEY); 057 boolean OK = SensorBaseClient.isRegistered(sensorBaseHost, user, password); 058 return new StringRepresentation((OK ? authenticated : unauthenticated)); 059 } 060 catch (Exception e) { 061 setStatusError("Error during ping", e); 062 return null; 063 } 064 } 065 }