1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
|
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package scatours.smsgateway;
import java.io.ByteArrayInputStream;
import javax.jms.BytesMessage;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.xml.namespace.QName;
import javax.xml.stream.XMLStreamException;
import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMText;
import org.apache.axiom.om.impl.builder.StAXOMBuilder;
public class SMSGatewayImpl implements SMSGateway {
private final Session session;
private final MessageConsumer consumer;
private final MessageProducer producer;
public SMSGatewayImpl(Session session) throws JMSException {
this.session = session;
Destination requestDest = session.createQueue("SMSRequestQueue");
consumer = session.createConsumer(requestDest);
Destination responseDest = session.createQueue("SMSResponseQueue");
producer = session.createProducer(responseDest);
}
public void start() throws JMSException, XMLStreamException {
while (true) {
Message message = consumer.receive();
handleRequest(message);
}
}
private void handleRequest(Message inMessage) throws XMLStreamException, JMSException {
OMElement xml = parseXMLPayload(inMessage);
String fromNumber = getStringArg(xml, 0);
String toNumber = getStringArg(xml, 1);
String text = getStringArg(xml, 2);
boolean result = sendSMS(fromNumber, toNumber, text);
String responseXML = createXMLResponse(result);
TextMessage outMessage = createResponseMessage(inMessage.getJMSMessageID(), responseXML);
producer.send(outMessage);
}
private OMElement parseXMLPayload(Message inMessage) throws JMSException, XMLStreamException {
final byte[] msgData;
if (inMessage instanceof TextMessage) {
msgData = ((TextMessage)inMessage).getText().getBytes();
} else if (inMessage instanceof BytesMessage) {
BytesMessage bytesMessage = (BytesMessage)inMessage;
msgData = new byte[(int)bytesMessage.getBodyLength()];
bytesMessage.readBytes(msgData);
} else {
throw new JMSException("Unsupported JMS message type of " + inMessage.getClass().getName());
}
ByteArrayInputStream in = new ByteArrayInputStream(msgData);
StAXOMBuilder builder = new StAXOMBuilder(in);
OMElement doc = builder.getDocumentElement();
return doc;
}
private String getStringArg(OMElement doc, int i) {
QName argQName = new QName("arg" + i);
OMElement arg = doc.getFirstChildWithName(argQName);
if (arg == null) {
return null;
}
return arg.getText();
}
public boolean sendSMS(String fromNumber, String toNumber, String text) {
System.out.println("From: " + fromNumber);
System.out.println("To: " + toNumber);
System.out.println(text);
return true;
}
private String createXMLResponse(boolean result) {
OMFactory omFactory = OMAbstractFactory.getOMFactory();
OMElement response = omFactory.createOMElement("sendSMS", "http://smsgateway.scatours/", "ns2");
OMElement returnValue = omFactory.createOMElement("return", null);
OMText returnV = omFactory.createOMText(Boolean.toString(result));
returnValue.addChild(returnV);
response.addChild(returnValue);
return response.toString();
}
private TextMessage createResponseMessage(String correlationID, String responseXML) throws JMSException {
TextMessage outMessage = session.createTextMessage("sendSMS");
outMessage.setText(responseXML);
outMessage.setJMSCorrelationID(correlationID);
return outMessage;
}
}
|