Source code: org/vrspace/server/Request.java
1 package org.vrspace.server;
2 import java.util.StringTokenizer;
3 import java.util.NoSuchElementException;
4 import org.vrspace.util.*;
5
6 /**
7 This class represents a single request from the client. It extends Message
8 to adding request logging capabilites, and also adds Client
9 @see RequestLog
10 */
11 public class Request extends Message implements Cloneable {
12 protected Client client;
13 protected Session session;
14 protected boolean sendObject = false;
15 public Exception exception;
16 public VRObject object;
17
18 /**
19 Creates new Request as specified by <b>request</b>
20
21 @param Client client which issued request
22 @param String request specification
23 */
24 public Request( Client c, String request ) {
25 this.client = c;
26 parseLine( request );
27 }
28
29 /**
30 This constructor uses client and session info from passed request.
31 Intended for usage in commands that need to provide feedback over a
32 specific session.
33 @param Request Request from which client & session info are copied
34 @param String request spec
35 */
36 public Request( Request r, String request ) {
37 this.client = r.client;
38 this.session = r.session;
39 parseLine( request );
40 }
41
42 /**
43 This constructor is intended to send object over net
44 */
45 public Request( Client c, VRObject obj ) {
46 this.time = System.currentTimeMillis();
47 this.className = obj.getClassName();
48 this.objectId = obj.db_id;
49
50 //eventName;
51 //eventValue;
52 //arguments;
53 //request;
54 this.object = obj;
55 this.sendObject = true;
56 //Logger.logDebug( "new Request( "+c+","+obj+" ) -> "+this );
57 }
58 /**
59 Returns the originating client
60 */
61 public Client getClient() {
62 return client;
63 }
64 /**
65 Returns request string suitable for logging
66 */
67 public String toLogEntry() {
68 return getTime() + " " +
69 getClient().getClassName() + " " +
70 getClient().getId() + " " +
71 getClassName() + " " +
72 getId() + " " +
73 getEventName() + " " +
74 getEventValue() + ";";
75 }
76 /**
77 If this request contains VRObject, returns its toText(), otherwise returns Message.toString()
78 */
79 public String toString() {
80 String ret = null;
81 if ( this.sendObject ) {
82 try {
83 ret = object.toText("_");
84 } catch (Exception e) {
85 Logger.logError( "Error in toText", e );
86 }
87 } else {
88 ret = super.toString();
89 }
90 return ret;
91 }
92 /**
93 Returns true if this Request contains a VRObject intended for transfer over network. Set by Client.addObject().
94 */
95 public boolean sendObject() {
96 return sendObject;
97 }
98 /**
99 Returns true if this request represents an event (VRObject state change) rather than an object. For now, = !sendObject()
100 */
101 public boolean isEvent() {
102 return !sendObject;
103 }
104 /**
105 Returns wrapped object. May return null before Dispatcher processes request, thus unsafe to use in Client extensions
106 */
107 public VRObject getObject() {
108 return object;
109 }
110 void setId( long id ) {
111 this.objectId = id;
112 }
113 void setClass( String cls ) {
114 this.className = cls;
115 }
116 /**
117 */
118 public Object clone() {
119 Request ret = null;
120 try {
121 ret = (Request) super.clone();
122 } catch ( CloneNotSupportedException e ) {
123 Logger.logError("Request: can't clone me", e);
124 }
125 return ret;
126 }
127 }