KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > services > communication > sms > provider > prodat > ProdatResponseImpl


1 /**
2  * Copyright 2001-2004 The eXo platform SARL All rights reserved.
3  * Please look at license.txt in info directory for more license detail.
4  **/

5 package org.exoplatform.services.communication.sms.provider.prodat;
6
7 import java.io.ByteArrayInputStream JavaDoc;
8 import java.io.InputStream JavaDoc;
9 import java.util.Iterator JavaDoc;
10
11 import org.exoplatform.container.PortalContainer;
12 import org.exoplatform.services.communication.sms.SmsMonitorService;
13 import org.exoplatform.services.communication.sms.common.ResponseException;
14 import org.exoplatform.services.communication.sms.model.*;
15 import org.exoplatform.services.communication.sms.provider.Provider;
16 import org.exoplatform.services.communication.sms.provider.SmsResponse;
17 import org.exoplatform.services.xml.querying.InvalidSourceException;
18 import org.exoplatform.services.xml.querying.InvalidStatementException;
19 import org.exoplatform.services.xml.querying.QueryRunTimeException;
20 import org.exoplatform.services.xml.querying.Statement;
21 import org.exoplatform.services.xml.querying.UniFormTransformationException;
22 import org.exoplatform.services.xml.querying.XMLQueryingService;
23 import org.exoplatform.services.xml.querying.impl.xtas.Query;
24 import org.exoplatform.services.xml.querying.impl.xtas.WellFormedUniFormTree;
25 import org.exoplatform.services.xml.querying.impl.xtas.XMLQueryingServiceImpl;
26 import org.xml.sax.InputSource JavaDoc;
27
28
29 /**
30  * @author Ove Ranheim (oranheim@yahoo.no)
31  * @since Jun 17, 2004 9:40:10 PM
32  */

33 public class ProdatResponseImpl implements SmsResponse {
34
35     private SmsMonitorService _monitor;
36     private Provider _provider;
37     private Messages _messages;
38     private String JavaDoc _payload;
39
40     public ProdatResponseImpl(Provider provider) {
41         _provider = provider;
42         PortalContainer manager = PortalContainer.getInstance();
43         _monitor = (SmsMonitorService) manager.getComponentInstanceOfType(SmsMonitorService.class);
44     }
45
46     /*
47      * @see org.exoplatform.services.communication.sms.provider.SmsResponse#getMessages()
48      */

49     public Messages getMessages() {
50         return _messages;
51     }
52
53     /*
54      * @see org.exoplatform.services.communication.sms.provider.SmsResponse#setMessages(org.exoplatform.services.communication.sms.model.Messages)
55      */

56     public void setMessages(Messages messages) {
57         _messages = messages;
58     }
59
60     /*
61      * @see org.exoplatform.services.communication.sms.provider.SmsResponse#getResult()
62      */

63     public String JavaDoc getResult() {
64         return _payload;
65     }
66
67     /*
68      * @see org.exoplatform.services.communication.sms.provider.SmsResponse#setResult(java.lang.String)
69      */

70     public void setResult(String JavaDoc payload) {
71         _payload = payload;
72     }
73     
74     private void beautifyResponse() {
75         // This eliminates the exception "Content not allowed in prolog"
76
_payload = _payload.replaceAll("\\?>", "?>\n");
77         _payload = _payload.replaceAll("><", ">\n<");
78         _payload = _payload.substring(0, _payload.length() - 1);
79     }
80
81     private String JavaDoc select(XMLQueryingService service, Query query, String JavaDoc xpath) throws InvalidStatementException, QueryRunTimeException, InvalidSourceException {
82         Statement qc = service.createStatementHelper().select(xpath);
83         query.prepare(qc);
84         query.execute();
85         return query.getResult().toString();
86     }
87     
88     /*
89      * @see org.exoplatform.services.communication.sms.provider.SmsResponse#translate()
90      */

91     public void translate() throws ResponseException {
92         try {
93             PortalContainer manager = PortalContainer.getInstance();
94             XMLQueryingService service = (XMLQueryingService) manager.getComponentInstanceOfType(XMLQueryingServiceImpl.class);
95             Query query = (Query) service.createQuery();
96             WellFormedUniFormTree wft = new WellFormedUniFormTree();
97             beautifyResponse();
98             InputStream JavaDoc is = new ByteArrayInputStream JavaDoc(_payload.getBytes());
99             wft.init(new InputSource JavaDoc(is));
100             query.setInput(wft);
101
102             // Update messages head
103
String JavaDoc logon = select(service, query, "/SESSION/LOGON/text()");
104             if ("OK".equals(logon)) {
105                 _messages.setLogonStatus(LogonStatus.SUCCESS);
106                 _messages.setReason("");
107             } else if ("FAIL".equals(logon)) {
108                 _messages.setLogonStatus(LogonStatus.FAILED);
109                 _messages.setReason(select(service, query, "/SESSION/REASON/text()"));
110                 _messages.errorOccured();
111             } else {
112                 throw new ResponseException("Unknown logon status received from the gateway");
113             }
114             if (_messages.hasErrorOccured()) return;
115             
116             Boolean JavaDoc hasErrorOccured = Boolean.FALSE;
117             for(Iterator JavaDoc i = _messages.iterator(); i.hasNext(); ) {
118                 Message m = (Message) i.next();
119                 for(Iterator JavaDoc j = m.getRecipients().iterator(); j.hasNext(); ) {
120                     Recipient r = (Recipient) j.next();
121                     Integer JavaDoc id = r.getId();
122                     
123                     String JavaDoc xpath = "/SESSION/MSGLST/MSG/STATUS[../ID="+id+"]/text()";
124                     String JavaDoc status = select(service, query, xpath);
125                     if ("OK".equals(status)) {
126                         r.setStatus(MessageStatus.COMPLETE);
127                         _monitor.incSuccessfulMessages();
128                     } else if ("FAIL".equals(status)) {
129                         r.setStatus(MessageStatus.FAILURE);
130                         hasErrorOccured = Boolean.TRUE;
131                         _monitor.incFailedMessages();
132                     }
133                     
134                     xpath = "/SESSION/MSGLST/MSG/INFO[../ID="+id+"]/text()";
135                     String JavaDoc info = select(service, query, xpath);
136                     r.setError(info);
137                 }
138             }
139             if (hasErrorOccured.booleanValue()) _messages.errorOccured();
140
141         } catch (InvalidStatementException e) {
142             throw new ResponseException(e);
143         } catch (InvalidSourceException e) {
144             throw new ResponseException(e);
145         } catch (QueryRunTimeException e) {
146             throw new ResponseException(e);
147         } catch (UniFormTransformationException e) {
148             throw new ResponseException(e);
149         }
150
151     }
152
153 }
154
155
Popular Tags