]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/java/org/opensrf/Stack.java
LP1827055 Remove Python libs, install bits, and docs
[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
62         Logger.debug("received id=" + msg.getId() + 
63             " type=" + msg.getType() + " payload=" + msg.getPayload());
64
65         if( ses instanceof ClientSession ) 
66             processResponse((ClientSession) ses, msg);
67         else
68             processRequest((ServerSession) ses, msg);
69     }
70
71     /** 
72      * Process a server response
73      */
74     private static void processResponse(ClientSession session, Message msg) throws MethodException {
75         String type = msg.getType();
76
77         if(msg.RESULT.equals(type)) {
78             session.pushResponse(msg);
79             return;
80         }
81
82         if(msg.STATUS.equals(type)) {
83
84             OSRFObject obj = (OSRFObject) msg.getPayload();
85             Status stat = new Status(obj.getString("status"), obj.getInt("statusCode"));
86             int statusCode = stat.getStatusCode();
87             String status = stat.getStatus();
88
89             switch(statusCode) {
90                 case Status.COMPLETE:
91                     session.setRequestComplete(msg.getId());
92                     break;
93                 case Status.NOTFOUND: 
94                     session.setRequestComplete(msg.getId());
95                     throw new MethodException(status);
96             }
97         }
98     }
99
100     /**
101      * Process a client request
102      */
103     private static void processRequest(ServerSession session, Message msg) {
104     }
105 }