Restlet Server code for login (accepting and sending JSON Objects)

168 views
Skip to first unread message

jainastha265

unread,
Jun 12, 2012, 8:14:35 AM6/12/12
to supr-intern...@googlegroups.com
//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());
 ClientResource requestResource = new ClientResource("http://localhost:8182/discovery/pref/push");
 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.


admin.properties
normal.properties
super.properties

Ujwal P

unread,
Jun 13, 2012, 12:49:50 AM6/13/12
to supr-intern...@googlegroups.com
Good stuff Astha...

just a note to all : the Server Application written by Astha will not be used in our context. We will define in our web.xml a path that will link to a class similar to what is written without the creation of the server.

Anyway I hope others are also able to show me something like this by 15th.

Astha,

Please go through Mongo if you have not already.

Ujwal

Amrita Pritam

unread,
Jun 13, 2012, 1:14:54 AM6/13/12
to supr-intern...@googlegroups.com
anyone online please send the link for todays meeting.
--
with regards
amrita

Astha Jain

unread,
Jun 14, 2012, 6:09:54 PM6/14/12
to supr-intern...@googlegroups.com

The SERVER RESOURCE CODE USING MONGODB :
(instead of the properties file used before)

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.net.UnknownHostException;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.Mongo;
import com.mongodb.MongoException;

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");

 try {
 
Mongo mongo = new Mongo("localhost", 27017);
DB db = mongo.getDB("Trydb");
DBCollection collection = db.getCollection("tryCollection");

BasicDBObject searchQuery = new BasicDBObject();
searchQuery.put("user", user);
searchQuery.put("password", password);

DBCursor cursor = collection.find(searchQuery);
while(cursor.hasNext()){
       DBObject result = cursor.next();
pr=Integer.parseInt(result.get("priority").toString());
getResponse().setStatus(Status.SUCCESS_ACCEPTED);
   }
 } catch (UnknownHostException e) {
e.printStackTrace();
} catch (MongoException e) {
e.printStackTrace();

Ujwal P

unread,
Jun 15, 2012, 12:58:37 AM6/15/12
to supr-intern...@googlegroups.com

Astha,

That's good I guess you are ready to start-off with the real thing.

thers,
I am not seeing any progress from many of you?

Ujwal
Reply all
Reply to author
Forward
0 new messages