KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quikj > application > web > oamp > plugin > OAMPClient


1 /*
2  * OAMPClient.java
3  *
4  * Created on May 31, 2002, 4:44 AM
5  */

6
7 package com.quikj.application.web.oamp.plugin;
8
9 import com.quikj.server.framework.*;
10 import com.quikj.server.web.*;
11 import com.quikj.application.web.oamp.messaging.*;
12
13 /**
14  *
15  * @author amit
16  */

17 public class OAMPClient implements PluginAppClientInterface
18 {
19     
20     // constants for feature access level
21
public static final int NO_ACCESS = 0;
22     public static final int TRIAL_ACCESS = 1;
23     public static final int FULL_ACCESS = 2;
24     
25     /** Creates a new instance of OAMPClient */
26     public OAMPClient() throws AceException
27     {
28     }
29     
30     public void dispose()
31     {
32         if (loggedIn == true)
33         {
34             loggedIn = false;
35         }
36         
37         OAMPClientList.Instance().remove(parent);
38     }
39     
40     public void connectionClosed()
41     {
42         // TODO, send logout message to the system OAMP
43

44         dispose();
45     }
46     
47     public boolean responseReceived(int request_id,
48     int status, String JavaDoc reason, String JavaDoc content_type, String JavaDoc body)
49     {
50         // todo
51

52         AceLogger.Instance().log(AceLogger.ERROR,
53         AceLogger.SYSTEM_LOG,
54         "OAMPClient.responseReceived() -- Response message reception not supported");
55         
56         return true;
57     }
58     
59     public boolean eventReceived(AceMessageInterface event)
60     {
61         if ((event instanceof OAMPMessageEvent) == true)
62         {
63             return processOAMPMessageEvent((OAMPMessageEvent)event);
64         }
65         // else, ignore other events
66

67         return true;
68     }
69     
70     public boolean newConnection(String JavaDoc host, HTTPEndPoint parent)
71     {
72         clientHost = host;
73         this.parent = parent;
74                 
75         OAMPClientList.Instance().add(parent, this);
76         
77         return true;
78     }
79     
80     public boolean requestReceived(int request_id, String JavaDoc content_type, String JavaDoc body)
81     {
82         // do some error checking
83
if (checkRequestMessage(content_type, body) == false)
84         {
85             return true;
86         }
87         
88         // next, parse the message
89
OAMPFrameworkMessageParser parser = null;
90         try
91         {
92             parser = new OAMPFrameworkMessageParser(XMLBuilder.Instance().getDocumentBuilder());
93         }
94         catch (AceMessageException ex)
95         {
96             // print error message
97
AceLogger.Instance().log(AceLogger.ERROR, AceLogger.SYSTEM_LOG,
98             Thread.currentThread().getName()
99             + "- OAMPClient.requestReceived() -- Could not obtain XML parser: "
100             + ex.getMessage());
101             
102             return false;
103         }
104         
105         if (parser.parse(body, true) == false)
106         {
107             // print error message
108
AceLogger.Instance().log(AceLogger.ERROR, AceLogger.SYSTEM_LOG,
109             Thread.currentThread().getName()
110             + "- OAMPClient.requestReceived() -- Could not parse the received message: "
111             + parser.getErrorMessage());
112             
113             return false;
114         }
115         
116         // if not logged in, only allow registration request message through
117

118         if (loggedIn == false)
119         {
120             if (parser.isSystemMessage() == false)
121             {
122                 // print error message
123
AceLogger.Instance().log(AceLogger.ERROR,
124                 AceLogger.SYSTEM_LOG,
125                 "OAMPClient.requestReceived() -- Non-system message received before login, feature: "
126                 + parser.getFeatureName());
127                 
128                 return false;
129             }
130             
131             OAMPSystemMessageParser sysparser = new OAMPSystemMessageParser();
132             if (sysparser.parse(parser.getFirstChild(), true) == false)
133             {
134                 // print error message
135
AceLogger.Instance().log(AceLogger.ERROR,
136                 AceLogger.SYSTEM_LOG,
137                 "OAMPClient.requestReceived() -- Failure parsing system OAMP message before login, error: "
138                 + sysparser.getErrorMessage());
139                 
140                 return false;
141             }
142             
143             if ((sysparser.getMessage() instanceof RegistrationRequestMessage) == false)
144             {
145                 // print error message
146
AceLogger.Instance().log(AceLogger.ERROR,
147                 AceLogger.SYSTEM_LOG,
148                 "OAMPClient.requestReceived() -- Non-registration system OAMP message received before login, message type: "
149                 + sysparser.getMessage().messageType());
150                 
151                 return false;
152             }
153             
154             
155             AceNetworkAccess access = FeatureOAMP.Instance().getAccessInfo();
156             if (access != null) // specified in the config file
157
{
158                 if (access.match(clientHost) == false)
159                 {
160                     // print error message
161
AceLogger.Instance().log(AceLogger.WARNING, AceLogger.SYSTEM_LOG,
162                     Thread.currentThread().getName()
163                     + "- OAMPClient.requestReceived() -- Unauthorized access attempt from host "
164                     + clientHost);
165                     
166                     if (parent.sendResponseMessageToClient(request_id,
167                     AceHTTPMessage.FORBIDDEN,
168                     "You are not allowed to login from this location",
169                     "text/xml",
170                     null,
171                     true) == false)
172                     {
173                         // print error message
174
AceLogger.Instance().log(AceLogger.ERROR, AceLogger.SYSTEM_LOG,
175                         Thread.currentThread().getName()
176                         + "- OAMPClient.requestReceived() -- Error sending response message to the endpoint");
177                     }
178                     
179                     return false; // no point continuing
180
}
181             }
182         }
183                 
184         FeatureHandlerInterface handler = FeatureOAMPHandlerList.Instance().getHandler(parser.getFeatureName());
185         if (handler == null)
186         {
187             // print error message
188
AceLogger.Instance().log(AceLogger.ERROR,
189             AceLogger.SYSTEM_LOG,
190             "OAMPClient.requestReceived() -- Couldn't find handler for feature: "
191             + parser.getFeatureName());
192             
193             return false;
194         }
195         
196         if (handler.sendEvent(new OAMPMessageEvent(parent,
197         this, parser.getFirstChild(),
198         request_id, null)) == false)
199         {
200             // print log message
201
AceLogger.Instance().log(AceLogger.ERROR,
202             AceLogger.SYSTEM_LOG,
203             "OAMPClient.requestReceived() -- Couldn't send event to handler for feature: "
204             + parser.getFeatureName());
205             
206             return false;
207         }
208         
209         return true;
210     }
211     
212     private boolean checkRequestMessage(String JavaDoc content_type, String JavaDoc body)
213     {
214         if (body == null)
215         {
216             // print error message
217
AceLogger.Instance().log(AceLogger.WARNING, AceLogger.SYSTEM_LOG,
218             Thread.currentThread().getName()
219             + "- OAMPClient.checkRequestMessage() -- Request message does not have a body");
220             return false;
221         }
222         
223         if (body.length() <= 0)
224         {
225             // print error message
226
AceLogger.Instance().log(AceLogger.WARNING, AceLogger.SYSTEM_LOG,
227             Thread.currentThread().getName()
228             + "- OAMPClient.checkRequestMessage() -- Request message has a zero-length body");
229             return false;
230         }
231         
232         if (content_type.equalsIgnoreCase("text/xml") == false)
233         {
234             // print error message
235
AceLogger.Instance().log(AceLogger.WARNING, AceLogger.SYSTEM_LOG,
236             Thread.currentThread().getName()
237             + "- OAMPClient.checkRequestMessage() -- Content type of a request message is not text/xml");
238             return false;
239         }
240         return true;
241     }
242     
243     private boolean processOAMPMessageEvent(OAMPMessageEvent message)
244     {
245         if (message.isRequest() == true)
246         {
247             // send it to the client
248
if (parent.sendRequestMessageToClient(message.getRequestId(),
249             "text/xml",
250             OAMPFrameworkMessageParser.format(message.getFeatureName(),
251             message.getBody())) == false)
252             {
253                 // print error message
254
AceLogger.Instance().log(AceLogger.ERROR, AceLogger.SYSTEM_LOG,
255                 Thread.currentThread().getName()
256                 + "- OAMPClient.processOAMPMessageEvent() -- Couldn't send request message to endpoint");
257                 
258                 return false;
259             }
260         }
261         else // response message
262
{
263             OAMPMessageInterface parsed_msg = message.getBody();
264             if (parsed_msg != null)
265             {
266                 if ((parsed_msg instanceof RegistrationResponseMessage) == true)
267                 {
268                     return processRegistrationResponse((RegistrationResponseMessage)parsed_msg,
269                     message);
270                 }
271                 else
272                 {
273                     // send it to the client
274
if (parent.sendResponseMessageToClient(message.getRequestId(),
275                     message.getStatus(),
276                     message.getReason(),
277                     "text/xml",
278                     OAMPFrameworkMessageParser.format(message.getFeatureName(),
279                     parsed_msg)) == false)
280                     {
281                         // print error message
282
AceLogger.Instance().log(AceLogger.ERROR, AceLogger.SYSTEM_LOG,
283                         Thread.currentThread().getName()
284                         + "- OAMPClient.processOAMPMessageEvent() -- Couldn't send response message to endpoint");
285                         
286                         return false;
287                     }
288                 }
289             }
290             else // no message body present
291
{
292                 // send it to the client
293
if (parent.sendResponseMessageToClient(message.getRequestId(),
294                 message.getStatus(),
295                 message.getReason(),
296                 null,
297                 null) == false)
298                 {
299                     // print error message
300
AceLogger.Instance().log(AceLogger.ERROR, AceLogger.SYSTEM_LOG,
301                     Thread.currentThread().getName()
302                     + "- OAMPClient.processOAMPMessageEvent() -- Couldn't send response message to endpoint");
303                     
304                     
305                     return false;
306                 }
307             }
308         }
309         return true;
310     }
311     
312     private boolean processRegistrationResponse(RegistrationResponseMessage message,
313     OAMPMessageEvent event)
314     {
315         // send the message to the client
316
if (parent.sendResponseMessageToClient(event.getRequestId(),
317         event.getStatus(),
318         event.getReason(),
319         "text/xml",
320         OAMPFrameworkMessageParser.format(OAMPSystemMessageParser.OAMP_SYSTEM_FEATURE_NAME,
321         message)) == false)
322         {
323             // print error message
324
AceLogger.Instance().log(AceLogger.ERROR, AceLogger.SYSTEM_LOG,
325             Thread.currentThread().getName()
326             + "- OAMPClient.processRegistrationResponse() -- Couldn't send registration response message to endpoint");
327             
328             return false;
329         }
330         
331         if (event.getStatus() == AceHTTPMessage.OK)
332         {
333             loggedIn = true;
334         }
335         
336         return true;
337     }
338     
339     /** Getter for property accountInfo.
340      * @return Value of property accountInfo.
341      */

342     public com.quikj.application.web.oamp.plugin.AccountElement getAccountInfo()
343     {
344         return accountInfo;
345     }
346     
347     /** Setter for property accountInfo.
348      * @param accountInfo New value of property accountInfo.
349      */

350     public void setAccountInfo(com.quikj.application.web.oamp.plugin.AccountElement accountInfo)
351     {
352         this.accountInfo = accountInfo;
353     }
354     
355     public boolean isSuperUser()
356     {
357         if (accountInfo == null)
358         {
359             return false;
360         }
361         
362         return (accountInfo.getLevel() == RegistrationResponseMessage.LEVEL_SUPERUSER);
363     }
364     
365     public boolean isAdminLevel()
366     {
367         if (accountInfo == null)
368         {
369             return false;
370         }
371         
372         if (accountInfo.getLevel() == RegistrationResponseMessage.LEVEL_ADMIN)
373         {
374             return true;
375         }
376         
377         if (accountInfo.getLevel() == RegistrationResponseMessage.LEVEL_SUPERUSER)
378         {
379             return true;
380         }
381         
382         return false;
383     }
384     
385     public String JavaDoc getDomain()
386     {
387         if (accountInfo == null)
388         {
389             return "";
390         }
391         
392         return accountInfo.getDomain();
393     }
394     
395     public boolean canAdminFeature(String JavaDoc feature_name)
396     {
397         if (accountInfo == null)
398         {
399             return false;
400         }
401         
402         if (accountInfo.hasFeature(feature_name) == true)
403         {
404             if (accountInfo.getLevel() == RegistrationResponseMessage.LEVEL_ADMIN)
405             {
406                 return true;
407             }
408             
409             if (accountInfo.getLevel() == RegistrationResponseMessage.LEVEL_SUPERUSER)
410             {
411                 return true;
412             }
413         }
414         
415         return false;
416     }
417     
418     public boolean canAdminFeatures(String JavaDoc[] features)
419     {
420         if (accountInfo == null)
421         {
422             return false;
423         }
424         
425         if ((accountInfo.getLevel() != RegistrationResponseMessage.LEVEL_ADMIN) &&
426         (accountInfo.getLevel() != RegistrationResponseMessage.LEVEL_SUPERUSER))
427         {
428             return false;
429         }
430         
431         for (int i = 0; i < features.length; i++)
432         {
433             if (accountInfo.hasFeature(features[i]) == false)
434             {
435                 return false;
436             }
437         }
438         
439         return true;
440     }
441     
442     public int getSubscriberAccessLevel(String JavaDoc featurename)
443     {
444         if (accountInfo == null)
445         {
446             return NO_ACCESS;
447         }
448         
449         if (accountInfo.hasFeature(featurename) == true)
450         {
451             return FULL_ACCESS;
452         }
453         
454         FeatureHandlerInterface handler = FeatureOAMPHandlerList.Instance().getHandler(featurename);
455         if (handler == null)
456         {
457             return NO_ACCESS;
458         }
459         
460         ClientParms parms = handler.getClientParms();
461         if (parms == null)
462         {
463             return NO_ACCESS;
464         }
465         
466         if (parms.isDisplayToSubscriber() == true)
467         {
468             return TRIAL_ACCESS;
469         }
470         
471         return NO_ACCESS;
472     }
473     
474     public HTTPEndPoint getParent()
475     {
476         return parent;
477     }
478     
479     /** Getter for property loggedIn.
480      * @return Value of property loggedIn.
481      *
482      */

483     public boolean isLoggedIn()
484     {
485         return loggedIn;
486     }
487         
488     private String JavaDoc clientHost;
489     private HTTPEndPoint parent;
490     private boolean loggedIn = false;
491     
492     private AccountElement accountInfo = null;
493 }
494
Popular Tags