1 5 package org.exoplatform.services.communication.sms.provider.prodat; 6 7 import java.io.ByteArrayInputStream ; 8 import java.io.InputStream ; 9 import java.util.Iterator ; 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 ; 27 28 29 33 public class ProdatResponseImpl implements SmsResponse { 34 35 private SmsMonitorService _monitor; 36 private Provider _provider; 37 private Messages _messages; 38 private String _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 49 public Messages getMessages() { 50 return _messages; 51 } 52 53 56 public void setMessages(Messages messages) { 57 _messages = messages; 58 } 59 60 63 public String getResult() { 64 return _payload; 65 } 66 67 70 public void setResult(String payload) { 71 _payload = payload; 72 } 73 74 private void beautifyResponse() { 75 _payload = _payload.replaceAll("\\?>", "?>\n"); 77 _payload = _payload.replaceAll("><", ">\n<"); 78 _payload = _payload.substring(0, _payload.length() - 1); 79 } 80 81 private String select(XMLQueryingService service, Query query, String 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 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 is = new ByteArrayInputStream (_payload.getBytes()); 99 wft.init(new InputSource (is)); 100 query.setInput(wft); 101 102 String 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 hasErrorOccured = Boolean.FALSE; 117 for(Iterator i = _messages.iterator(); i.hasNext(); ) { 118 Message m = (Message) i.next(); 119 for(Iterator j = m.getRecipients().iterator(); j.hasNext(); ) { 120 Recipient r = (Recipient) j.next(); 121 Integer id = r.getId(); 122 123 String xpath = "/SESSION/MSGLST/MSG/STATUS[../ID="+id+"]/text()"; 124 String 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 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 |