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