//ServerResource code
package firstSteps;
import org.restlet.data.Status;
import org.restlet.ext.json.JsonRepresentation;
import org.restlet.representation.Representation;
import org.restlet.resource.ResourceException;
import org.restlet.resource.ServerResource;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONStringer;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
public class PrefPushResource extends ServerResource {
@Post("json:json")
public Representation acceptJson(JsonRepresentation represent)
throws ResourceException {
Representation rep = null;
int pr=10;
try {
JSONObject jsonobject = represent.getJsonObject();
String requestString = jsonobject.getString("request");
JSONObject json = new JSONObject(requestString);
String user = json.getString("user_name");
String password = json.getString("password");
Properties prop = new Properties();
try {
//load a properties file
prop.load(new FileInputStream("super.properties"));
if(((prop.getProperty("dbuser").equals(user))==true)&&((prop.getProperty("dbpassword").equals(password)==true)))
{
getResponse().setStatus(Status.SUCCESS_ACCEPTED);
pr=1;
}
} catch (IOException ex) {
ex.printStackTrace();
}
try {
//load a properties file
prop.load(new FileInputStream("admin.properties"));
if(((prop.getProperty("dbuser").equals(user))==true)&&((prop.getProperty("dbpassword").equals(password)==true)))
{
getResponse().setStatus(Status.SUCCESS_ACCEPTED);
pr=2;
}
} catch (IOException ex) {
ex.printStackTrace();
}
try {
//load a properties file
prop.load(new FileInputStream("normal.properties"));
if(((prop.getProperty("dbuser").equals(user))==true)&&((prop.getProperty("dbpassword").equals(password)==true)))
{
getResponse().setStatus(Status.SUCCESS_ACCEPTED);
pr=3;
}
} catch (IOException ex) {
ex.printStackTrace();
}
if(getResponse().getStatus()==Status.SUCCESS_ACCEPTED)
{
JSONStringer jsReply = new JSONStringer();
jsReply.object();
jsReply.key("User").value(user);
jsReply.key("Priority").value(pr);
jsReply.key("STATUS").value("OK");
jsReply.endObject();
rep = new JsonRepresentation(jsReply);
getResponse().setStatus(Status.SUCCESS_OK);
}
else
{
JSONStringer jsReply = new JSONStringer();
jsReply.object();
jsReply.key("ERROR").value("Wrong User name or password.");
jsReply.key("STATUS").value("ERROR");
jsReply.endObject();
rep = new JsonRepresentation(jsReply);
//getResponse().setStatus(Status.CLIENT_ERROR_UNAUTHORIZED);
}
}
catch (Exception e) {
e.printStackTrace();
JSONStringer jsReply = new JSONStringer();
try {
jsReply.object();
jsReply.key("STATUS").value("SERVER_ERROR");
jsReply.key("ERROR").value(e.getMessage());
jsReply.endObject();
rep = new JsonRepresentation(jsReply);
getResponse().setStatus(Status.SERVER_ERROR_INTERNAL);
} catch (JSONException e1) {
e1.printStackTrace();
}
}
return rep;
}
}
Server Application with main class :
package firstSteps;
import org.restlet.Application;
import org.restlet.Component;
import org.restlet.Restlet;
import org.restlet.data.Protocol;
import org.restlet.routing.Router;
import org.restlet.service.MetadataService;
public class DiscoveryApplication extends Application{
@Override
public synchronized Restlet createInboundRoot() {
Router router = new Router(getContext());
router.attach("/pref/push",PrefPushResource.class);
return router;
}
public static void main(String[] args) throws Exception {
MetadataService media = new MetadataService();
media.getAllMediaTypes(null);
Component component = new Component();
component.getServers().add(Protocol.HTTP, 8182);
component.getDefaultHost().attach("/discovery",new DiscoveryApplication());
component.start();
}
}
Example client code sending and recieving Json (This is to be replaced by client code for gwt.This is for JEE.GWT submit will be coded to send json to server) :
package firstSteps;
import java.io.IOException;
import org.json.JSONException;
//import org.json.JSONObject;
import org.json.JSONStringer;
import org.restlet.data.MediaType;
import org.restlet.ext.json.JsonRepresentation;
import org.restlet.representation.Representation;
import org.restlet.resource.ClientResource;
public class PrefPushResourceClient {
public static void main(String[] args) throws JSONException, IOException {
JSONStringer jsRequest = new JSONStringer();
JSONStringer js = new JSONStringer();
try {
js.object();
jsRequest.object();
jsRequest.key("user_name").value("super");
jsRequest.key("password").value("abc");
jsRequest.endObject();
js.key("request").value(jsRequest);
js.endObject();
} catch (JSONException e1) {
e1.printStackTrace();
}
System.out.println(jsRequest.toString());
System.out.println(js.toString());
Representation rep;
rep = new JsonRepresentation(js);
rep.setMediaType(MediaType.APPLICATION_JSON);
Representation reply = requestResource.post(rep);
String replyText = reply.getText();
System.out.println("Reply Text:" + replyText);
System.out.println("Reply Media Type:" + reply.getMediaType());
reply.release();
}
}
The Properties files used are attached .Right now the passwords are simple not hashed.Tasks left
1) implement client side code in the GWT Login page
2) implementing md5 hash.