]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/java/org/opensrf/Stack.java
added a settings server client and test. made the global config object wrapped in...
[OpenSRF.git] / src / java / org / opensrf / Stack.java
1 package org.opensrf;
2 import org.opensrf.net.xmpp.XMPPMessage;
3 import org.opensrf.util.*;
4 import java.util.Date;
5 import java.util.List;
6 import java.util.Iterator;
7
8
9 public class Stack {
10
11     public static void processXMPPMessage(XMPPMessage msg) throws MethodException {
12
13         if(msg == null) return;
14
15         //System.out.println(msg.getBody());
16
17         /** fetch this session from the cache */
18         Session ses = Session.findCachedSession(msg.getThread());
19
20         if(ses == null) {
21             /** inbound client request, create a new server session */
22             return;
23         }
24
25         /** parse the JSON message body, which should result in a list of OpenSRF messages */
26         List msgList; 
27
28         try {
29             msgList = new JSONReader(msg.getBody()).readArray();
30         } catch(JSONException e) {
31             /** XXX LOG error */
32             e.printStackTrace();
33             return;
34         }
35
36         Iterator itr = msgList.iterator();
37
38         OSRFObject obj = null;
39         long start = new Date().getTime();
40
41         /** cycle through the messages and push them up the stack */
42         while(itr.hasNext()) {
43
44             /** Construct a Message object from the parsed generic OSRFObject */
45             obj = (OSRFObject) itr.next();
46
47             processOSRFMessage(
48                 ses, 
49                 new Message(
50                     obj.getInt("threadTrace"),
51                     obj.getString("type"),
52                     obj.get("payload")
53                 )
54             );
55         }
56
57         /** LOG the duration */
58     }
59
60     private static void processOSRFMessage(Session ses, Message msg) throws MethodException {
61         if( ses instanceof ClientSession ) 
62             processResponse((ClientSession) ses, msg);
63         else
64             processRequest((ServerSession) ses, msg);
65     }
66
67     /** 
68      * Process a server response
69      */
70     private static void processResponse(ClientSession session, Message msg) throws MethodException {
71         String type = msg.getType();
72
73         if(msg.RESULT.equals(type)) {
74             session.pushResponse(msg);
75             return;
76         }
77
78         if(msg.STATUS.equals(type)) {
79
80             OSRFObject obj = (OSRFObject) msg.getPayload();
81             Status stat = new Status(obj.getString("status"), obj.getInt("statusCode"));
82             int statusCode = stat.getStatusCode();
83             String status = stat.getStatus();
84
85             switch(statusCode) {
86                 case Status.COMPLETE:
87                     session.setRequestComplete(msg.getId());
88                     break;
89                 case Status.NOTFOUND: 
90                     session.setRequestComplete(msg.getId());
91                     throw new MethodException(status);
92             }
93         }
94     }
95
96     /**
97      * Process a client request
98      */
99     private static void processRequest(ServerSession session, Message msg) {
100     }
101 }