dhis2-devs team mailing list archive
-
dhis2-devs team
-
Mailing list archive
-
Message #15326
[Branch ~dhis2-devs-core/dhis2/trunk] Rev 5506: Implemented simplistic sms gateway for sending through http get to a bulk sms provider.
------------------------------------------------------------
revno: 5506
committer: Jo Størset <storset@xxxxxxxxx>
branch nick: dhis2
timestamp: Tue 2011-12-20 13:46:37 +0100
message:
Implemented simplistic sms gateway for sending through http get to a bulk sms provider.
added:
dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/adapter/Parameter.java
dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/adapter/Parameters.java
dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/adapter/ParametersMapXmlAdapter.java
dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/adapter/package-info.java
dhis-2/dhis-api/src/main/java/org/hisp/dhis/sms/config/GenericHttpGatewayConfig.java
dhis-2/dhis-api/src/test/java/org/hisp/dhis/sms/
dhis-2/dhis-api/src/test/java/org/hisp/dhis/sms/config/
dhis-2/dhis-api/src/test/java/org/hisp/dhis/sms/config/GenericHttpGatewayConfigTest.java
dhis-2/dhis-services/dhis-service-sms/src/main/java/org/hisp/dhis/sms/smslib/SimplisticHttpGetGateWay.java
modified:
dhis-2/dhis-api/src/main/java/org/hisp/dhis/sms/config/SmsConfiguration.java
dhis-2/dhis-services/dhis-service-sms/src/main/java/org/hisp/dhis/sms/outbound/SmsMessageSender.java
dhis-2/dhis-services/dhis-service-sms/src/main/java/org/hisp/dhis/sms/smslib/GateWayFactory.java
dhis-2/dhis-services/dhis-service-sms/src/main/java/org/hisp/dhis/sms/smslib/SmsLibService.java
dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/SmsConfigurationController.java
--
lp:dhis2
https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk
Your team DHIS 2 developers is subscribed to branch lp:dhis2.
To unsubscribe from this branch go to https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk/+edit-subscription
=== added file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/adapter/Parameter.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/adapter/Parameter.java 1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/adapter/Parameter.java 2011-12-20 12:46:37 +0000
@@ -0,0 +1,46 @@
+package org.hisp.dhis.common.adapter;
+
+import javax.xml.bind.annotation.XmlAttribute;
+import javax.xml.bind.annotation.XmlType;
+
+@XmlType(propOrder={"value", "key"})
+public class Parameter
+{
+
+ private String key;
+
+ private String value;
+
+ public Parameter()
+ {
+ }
+
+ public Parameter( String key, String value )
+ {
+ this.key = key;
+ this.value = value;
+ }
+
+ @XmlAttribute
+ public String getKey()
+ {
+ return key;
+ }
+
+ @XmlAttribute
+ public String getValue()
+ {
+ return value;
+ }
+
+ public void setKey( String key )
+ {
+ this.key = key;
+ }
+
+ public void setValue( String value )
+ {
+ this.value = value;
+ }
+
+}
=== added file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/adapter/Parameters.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/adapter/Parameters.java 1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/adapter/Parameters.java 2011-12-20 12:46:37 +0000
@@ -0,0 +1,33 @@
+package org.hisp.dhis.common.adapter;
+
+import java.util.List;
+
+import javax.xml.bind.annotation.XmlElement;
+
+public class Parameters
+{
+
+ List<Parameter> parameters;
+
+ public Parameters()
+ {
+ }
+
+ public Parameters( List<Parameter> parameters )
+ {
+ this.parameters = parameters;
+ }
+
+ @XmlElement( name = "parameter" )
+ public List<Parameter> getParameters()
+ {
+ return parameters;
+ }
+
+ public void setParameters( List<Parameter> parameters )
+ {
+ this.parameters = parameters;
+ }
+
+
+}
=== added file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/adapter/ParametersMapXmlAdapter.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/adapter/ParametersMapXmlAdapter.java 1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/adapter/ParametersMapXmlAdapter.java 2011-12-20 12:46:37 +0000
@@ -0,0 +1,36 @@
+package org.hisp.dhis.common.adapter;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.xml.bind.annotation.adapters.XmlAdapter;
+
+
+public class ParametersMapXmlAdapter extends XmlAdapter<Parameters, Map<String,String>> {
+
+ @Override
+ public Parameters marshal( Map<String, String> v )
+ throws Exception
+ {
+ ArrayList<Parameter> list = new ArrayList<Parameter>();
+
+ for ( Map.Entry<String,String> e : v.entrySet() ) {
+
+ list.add( new Parameter(e.getKey(), e.getValue()) );
+ }
+ return new Parameters(list);
+ }
+
+ @Override
+ public Map<String, String> unmarshal( Parameters v)
+ throws Exception
+ {
+ Map<String,String> map = new HashMap<String,String>();
+ for ( Parameter p : v.getParameters() ) {
+ map.put(p.getKey(), p.getValue());
+ }
+ return map;
+ }
+
+}
\ No newline at end of file
=== added file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/adapter/package-info.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/adapter/package-info.java 1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/adapter/package-info.java 2011-12-20 12:46:37 +0000
@@ -0,0 +1,43 @@
+/*
+ * Copyright (c) 2004-2010, University of Oslo
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ * * Redistributions of source code must retain the above copyright notice, this
+ * list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ * * Neither the name of the HISP project nor the names of its contributors may
+ * be used to endorse or promote products derived from this software without
+ * specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/**
+ * @author bobj
+ */
+
+@XmlSchema(
+ namespace = "http://dhis2.org/schema/dxf/2.0",
+ xmlns = {
+ @XmlNs(namespaceURI = "http://dhis2.org/schema/dxf/2.0", prefix = "d")
+ },
+ elementFormDefault = XmlNsForm.QUALIFIED)
+
+package org.hisp.dhis.common.adapter;
+
+import javax.xml.bind.annotation.XmlSchema;
+import javax.xml.bind.annotation.XmlNs;
+import javax.xml.bind.annotation.XmlNsForm;
=== added file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/sms/config/GenericHttpGatewayConfig.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/sms/config/GenericHttpGatewayConfig.java 1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/sms/config/GenericHttpGatewayConfig.java 2011-12-20 12:46:37 +0000
@@ -0,0 +1,89 @@
+package org.hisp.dhis.sms.config;
+
+import java.util.Map;
+
+import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
+
+import org.hisp.dhis.common.adapter.ParametersMapXmlAdapter;
+
+/*
+ * Copyright (c) 2011, University of Oslo
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ * * Redistributions of source code must retain the above copyright notice, this
+ * list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ * * Neither the name of the HISP project nor the names of its contributors may
+ * be used to endorse or promote products derived from this software without
+ * specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+public class GenericHttpGatewayConfig
+ extends SmsGatewayConfig
+{
+
+ private static final long serialVersionUID = 6340853488475760213L;
+
+ private String urlTemplate;
+
+ Map<String, String> parameters;
+
+ public GenericHttpGatewayConfig()
+ {
+ }
+
+ public GenericHttpGatewayConfig( String urlTemplate, Map<String, String> parameters )
+ {
+ this.urlTemplate = urlTemplate;
+ this.parameters = parameters;
+ }
+
+ public String getUrlTemplate()
+ {
+ return urlTemplate;
+ }
+
+ public void setUrlTemplate( String urlTemplate )
+ {
+ this.urlTemplate = urlTemplate;
+ }
+
+ @XmlJavaTypeAdapter( ParametersMapXmlAdapter.class )
+ public Map<String, String> getParameters()
+ {
+ return parameters;
+ }
+
+ public void setParameters( Map<String, String> parameters )
+ {
+ this.parameters = parameters;
+ }
+
+ @Override
+ public boolean isInbound()
+ {
+ return false;
+ }
+
+ @Override
+ public boolean isOutbound()
+ {
+ return true;
+ }
+
+}
=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/sms/config/SmsConfiguration.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/sms/config/SmsConfiguration.java 2011-12-15 11:41:17 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/sms/config/SmsConfiguration.java 2011-12-20 12:46:37 +0000
@@ -39,20 +39,21 @@
* Serializable configuration object for Sms.
*/
@XmlRootElement( name = "smsConfiguration" )
-public class SmsConfiguration implements Serializable
+public class SmsConfiguration
+ implements Serializable
{
private static final long serialVersionUID = 7460688383539123303L;
private String longNumber;
-
+
private List<SmsGatewayConfig> gateways;
-
+
private Integer pollingInterval;
private boolean enabled = false;
- public SmsConfiguration( )
+ public SmsConfiguration()
{
}
@@ -70,7 +71,7 @@
{
this.enabled = enabled;
}
-
+
public String getLongNumber()
{
return longNumber;
@@ -81,11 +82,11 @@
this.longNumber = longNumber;
}
- @XmlElementWrapper(name="gateways")
- @XmlElements({
- @XmlElement(name = "bulksms", type = BulkSmsGatewayConfig.class),
- @XmlElement(name = "clickatell", type = ClickatellGatewayConfig.class),
- @XmlElement(name = "modem", type = ModemGatewayConfig.class)})
+ @XmlElementWrapper( name = "gateways" )
+ @XmlElements( { @XmlElement( name = "bulksms", type = BulkSmsGatewayConfig.class ),
+ @XmlElement( name = "clickatell", type = ClickatellGatewayConfig.class ),
+ @XmlElement( name = "http", type = GenericHttpGatewayConfig.class ),
+ @XmlElement( name = "modem", type = ModemGatewayConfig.class ) } )
public List<SmsGatewayConfig> getGateways()
{
return gateways;
=== added directory 'dhis-2/dhis-api/src/test/java/org/hisp/dhis/sms'
=== added directory 'dhis-2/dhis-api/src/test/java/org/hisp/dhis/sms/config'
=== added file 'dhis-2/dhis-api/src/test/java/org/hisp/dhis/sms/config/GenericHttpGatewayConfigTest.java'
--- dhis-2/dhis-api/src/test/java/org/hisp/dhis/sms/config/GenericHttpGatewayConfigTest.java 1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-api/src/test/java/org/hisp/dhis/sms/config/GenericHttpGatewayConfigTest.java 2011-12-20 12:46:37 +0000
@@ -0,0 +1,115 @@
+package org.hisp.dhis.sms.config;
+
+import static org.junit.Assert.*;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+
+import java.io.IOException;
+import java.io.StringReader;
+import java.io.StringWriter;
+import java.io.Writer;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import javax.xml.bind.JAXBContext;
+import javax.xml.bind.JAXBException;
+import javax.xml.bind.Marshaller;
+import javax.xml.bind.Unmarshaller;
+
+import org.junit.Before;
+import org.junit.Test;
+
+/*
+ * Copyright (c) 2011, University of Oslo
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ * * Redistributions of source code must retain the above copyright notice, this
+ * list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ * * Neither the name of the HISP project nor the names of its contributors may
+ * be used to endorse or promote products derived from this software without
+ * specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+public class GenericHttpGatewayConfigTest
+{
+
+ private String urlTemplate = "http://bulksms.vsms.net:5567/eapi/submission/send_sms/2/2.0?username={username}&password={password}&source_id={sender}&message={message}&msisdn={recipient}";
+
+ private String bulk = "<bulksms><name>bulk</name><username>username</username><password>password</password></bulksms>";
+
+ private String click = "<clickatell><name>click</name><username>storset</username><password>dust2001</password><apiId>3304014</apiId></clickatell>";
+
+ private JAXBContext context;
+
+ private String urlString = "<urlTemplate>http://bulksms.vsms.net:5567/eapi/submission/send_sms/2/2.0?username={username}&password={password}&source_id={sender}&message={message}&msisdn={recipient}</urlTemplate>";
+
+ private String http = "<http><name>http</name>" + urlString + "<parameters>"
+ + "<parameter key=\"username\" value=\"storset\" /><parameter key=\"password\" value=\"dust2001\" />"
+ + "</parameters>" + "</http>";
+
+ @Before
+ public void setup()
+ throws JAXBException
+ {
+ context = JAXBContext.newInstance( SmsConfiguration.class );
+ }
+
+ @Test
+ public void testMarshalling()
+ throws IOException, JAXBException
+ {
+
+ Writer writer = new StringWriter();
+ Map<String, String> parameters = new HashMap<String, String>();
+ parameters.put( "username", "u1" );
+ parameters.put( "password", "p1" );
+ parameters.put( "sender", "s1" );
+
+ SmsGatewayConfig config = new GenericHttpGatewayConfig( urlTemplate, parameters );
+ SmsConfiguration smsConfiguration = new SmsConfiguration();
+ smsConfiguration.setGateways( Collections.singletonList( config ) );
+ Marshaller marshaller = context.createMarshaller();
+ marshaller.setProperty( Marshaller.JAXB_FORMATTED_OUTPUT, true );
+ marshaller.marshal( smsConfiguration, writer );
+
+ writer.flush();
+
+ assertTrue(writer.toString().contains( "<parameter key=\"username\" value=\"u1\"" ));
+ }
+
+ @Test
+ public void testUntmarshalling()
+ throws JAXBException
+ {
+ String xml = "<smsConfiguration xmlns=\"http://dhis2.org/schema/dxf/2.0\"><enabled>true</enabled><longNumber>DHIS2</longNumber>";
+ xml += "<gateways>" + bulk + click + http + "</gateways></smsConfiguration>";
+
+ Unmarshaller unmarshaller = context.createUnmarshaller();
+
+ SmsConfiguration config = (SmsConfiguration) unmarshaller.unmarshal( new StringReader( xml ) );
+
+ assertNotNull( config );
+ List<SmsGatewayConfig> gateways = config.getGateways();
+ assertNotNull( gateways );
+ assertEquals( 3, gateways.size() );
+ assertTrue( ((GenericHttpGatewayConfig)gateways.get( 2 )).getUrlTemplate().contains( "http://bulksms.vsms.net:5567/eapi/submission/send_sms/2/2.0" ) );
+ }
+}
=== modified file 'dhis-2/dhis-services/dhis-service-sms/src/main/java/org/hisp/dhis/sms/outbound/SmsMessageSender.java'
--- dhis-2/dhis-services/dhis-service-sms/src/main/java/org/hisp/dhis/sms/outbound/SmsMessageSender.java 2011-12-15 08:46:42 +0000
+++ dhis-2/dhis-services/dhis-service-sms/src/main/java/org/hisp/dhis/sms/outbound/SmsMessageSender.java 2011-12-20 12:46:37 +0000
@@ -38,11 +38,8 @@
import org.apache.commons.logging.LogFactory;
import org.hisp.dhis.message.MessageSender;
import org.hisp.dhis.sms.SmsServiceException;
-import org.hisp.dhis.sms.outbound.OutboundSms;
-import org.hisp.dhis.sms.outbound.OutboundSmsService;
import org.hisp.dhis.user.User;
import org.hisp.dhis.user.UserService;
-import org.springframework.beans.factory.annotation.Autowired;
public class SmsMessageSender
implements MessageSender
@@ -124,7 +121,7 @@
if ( sender != null )
name = sender.getUsername();
- text = "From " + name + ", " + subject + ": " + text;
+ text = "From " + name + " - " + subject + ": " + text;
// Simplistic cutoff 160 characters..
int length = text.length();
=== modified file 'dhis-2/dhis-services/dhis-service-sms/src/main/java/org/hisp/dhis/sms/smslib/GateWayFactory.java'
--- dhis-2/dhis-services/dhis-service-sms/src/main/java/org/hisp/dhis/sms/smslib/GateWayFactory.java 2011-12-15 20:51:45 +0000
+++ dhis-2/dhis-services/dhis-service-sms/src/main/java/org/hisp/dhis/sms/smslib/GateWayFactory.java 2011-12-20 12:46:37 +0000
@@ -3,6 +3,7 @@
import org.hisp.dhis.sms.SmsServiceException;
import org.hisp.dhis.sms.config.BulkSmsGatewayConfig;
import org.hisp.dhis.sms.config.ClickatellGatewayConfig;
+import org.hisp.dhis.sms.config.GenericHttpGatewayConfig;
import org.hisp.dhis.sms.config.ModemGatewayConfig;
import org.hisp.dhis.sms.config.SmsGatewayConfig;
import org.smslib.AGateway;
@@ -18,6 +19,8 @@
{
if ( config instanceof BulkSmsGatewayConfig )
return createBulkSmsGateway( (BulkSmsGatewayConfig) config );
+ else if ( config instanceof GenericHttpGatewayConfig )
+ return createSimplisticHttpGetGateway( (GenericHttpGatewayConfig) config );
else if ( config instanceof ClickatellGatewayConfig )
return createClickatellGateway( (ClickatellGatewayConfig) config );
else if ( config instanceof ModemGatewayConfig )
@@ -69,5 +72,12 @@
return gateway;
}
+ public AGateway createSimplisticHttpGetGateway( GenericHttpGatewayConfig c )
+ {
+ SimplisticHttpGetGateWay gateway = new SimplisticHttpGetGateWay( c.getName(), c.getUrlTemplate(), c.getParameters() );
+ gateway.setOutbound( true );
+ gateway.setInbound( false );
+ return gateway;
+ }
}
=== added file 'dhis-2/dhis-services/dhis-service-sms/src/main/java/org/hisp/dhis/sms/smslib/SimplisticHttpGetGateWay.java'
--- dhis-2/dhis-services/dhis-service-sms/src/main/java/org/hisp/dhis/sms/smslib/SimplisticHttpGetGateWay.java 1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-services/dhis-service-sms/src/main/java/org/hisp/dhis/sms/smslib/SimplisticHttpGetGateWay.java 2011-12-20 12:46:37 +0000
@@ -0,0 +1,123 @@
+package org.hisp.dhis.sms.smslib;
+
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.smslib.AGateway;
+import org.smslib.GatewayException;
+import org.smslib.OutboundMessage;
+import org.smslib.TimeoutException;
+import org.smslib.helper.Logger;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.ResponseEntity;
+import org.springframework.web.client.RestClientException;
+import org.springframework.web.client.RestTemplate;
+
+/**
+ * Simplistic http gateway sending smses through a get to a url constructed from the provided
+ * urlTemplate and map of static parameters.
+ * <p>This gateway is simplistic in that it can't evaluate the response from the provider, being most suitable as an
+ * example gateway. For production use a more robust gateway should be used implemented for the specific provider.
+ *
+ * <p>The gateway adds the following keys to the parameters:
+ * <ul>
+ * <li>recipient
+ * <li>message
+ * <li>sender - if available in the message
+ * </ul>
+ *
+ * An example usage with bulksms.com would be this template:<br/>
+ * http://bulksms.vsms.net:5567/eapi/submission/send_sms/2/2.0?username={username}&password={password}&message={message}&msisdn={recipient}<br/>
+ * With the following parameters provided:
+ * <ul>
+ * <li>username
+ * <li>password
+ * </ul>
+ *
+ */
+public class SimplisticHttpGetGateWay
+ extends AGateway
+{
+
+ private static final String SENDER = "sender";
+
+ private static final String RECIPIENT = "recipient";
+
+ private static final String MESSAGE = "message";
+
+ RestTemplate restTemplate = new RestTemplate();
+
+ private Map<String, String> parameters;
+
+ private String urlTemplate;
+
+ public SimplisticHttpGetGateWay( String id, String urlTemplate, Map<String, String> parameters)
+ {
+ super( id );
+ this.urlTemplate = urlTemplate;
+ this.parameters = parameters;
+
+ setAttributes( AGateway.GatewayAttributes.SEND | AGateway.GatewayAttributes.CUSTOMFROM
+ | AGateway.GatewayAttributes.BIGMESSAGES | AGateway.GatewayAttributes.FLASHSMS );
+ }
+
+ @Override
+ public void startGateway()
+ throws TimeoutException, GatewayException, IOException, InterruptedException
+ {
+ Logger.getInstance().logInfo( "Starting gateway.", null, getGatewayId() );
+ super.startGateway();
+ }
+
+ @Override
+ public void stopGateway()
+ throws TimeoutException, GatewayException, IOException, InterruptedException
+ {
+ Logger.getInstance().logInfo( "Stopping gateway.", null, getGatewayId() );
+ super.stopGateway();
+ }
+
+ @Override
+ public boolean sendMessage( OutboundMessage msg )
+ throws TimeoutException, GatewayException, IOException, InterruptedException
+ {
+ Logger.getInstance().logDebug( "Sending message " + msg, null, getGatewayId() );
+
+ Map<String, String> requestParameters = new HashMap<String, String>(parameters);
+
+ requestParameters.put( MESSAGE, msg.getText() );
+ requestParameters.put( RECIPIENT, msg.getRecipient() );
+ String sender = msg.getFrom();
+ if (sender != null) {
+ Logger.getInstance().logDebug( "Adding sender " + sender, null, getGatewayId() );
+ requestParameters.put( SENDER, sender );
+ }
+ try
+ {
+ ResponseEntity<String> response = restTemplate.getForEntity( urlTemplate, String.class, requestParameters );
+
+ if (response.getStatusCode().series() != HttpStatus.Series.SUCCESSFUL) {
+ Logger.getInstance().logWarn( "Couldn't send message, got response " + response, null, getGatewayId() );
+ return false;
+ }
+
+ }
+ catch ( RestClientException e )
+ {
+ Logger.getInstance().logWarn( "Couldn't send message " + msg, e, getGatewayId() );
+ return false;
+ }
+
+ return true;
+
+ }
+
+ @Override
+ public int getQueueSchedulingInterval()
+ {
+ // FIXME: ?
+ return 500;
+ }
+
+}
=== modified file 'dhis-2/dhis-services/dhis-service-sms/src/main/java/org/hisp/dhis/sms/smslib/SmsLibService.java'
--- dhis-2/dhis-services/dhis-service-sms/src/main/java/org/hisp/dhis/sms/smslib/SmsLibService.java 2011-12-15 20:41:45 +0000
+++ dhis-2/dhis-services/dhis-service-sms/src/main/java/org/hisp/dhis/sms/smslib/SmsLibService.java 2011-12-20 12:46:37 +0000
@@ -63,10 +63,12 @@
message.setFrom( longNumber );
}
+ boolean sent = false;
+
try
{
log.debug( "Sending message " + sms );
- getService().sendMessage( message );
+ sent = getService().sendMessage( message );
}
catch ( SMSLibException e )
{
@@ -92,6 +94,11 @@
}
}
+ if ( !sent )
+ {
+
+ }
+
}
@Override
@@ -118,10 +125,12 @@
log.info( "Loading configuration" );
reloadConfig();
- if (config.isEnabled()) {
- log.info( "Starting SmsLib" );
- startService();
- } else
+ if ( config.isEnabled() )
+ {
+ log.info( "Starting SmsLib" );
+ startService();
+ }
+ else
{
log.info( "Sms not enabled, won't start service" );
}
@@ -201,7 +210,7 @@
Service service = Service.getInstance();
service.setOutboundMessageNotification( new OutboundNotification() );
-
+
service.getGateways().clear();
// Add gateways
@@ -227,7 +236,7 @@
@Override
public void process( AGateway gateway, OutboundMessage msg )
{
- log.info( "Sent message through gateway " + gateway.getGatewayId() + ": " + msg);
+ log.info( "Sent message through gateway " + gateway.getGatewayId() + ": " + msg );
}
}
=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/SmsConfigurationController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/SmsConfigurationController.java 2011-12-16 21:52:08 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/SmsConfigurationController.java 2011-12-20 12:46:37 +0000
@@ -28,13 +28,17 @@
*/
import java.io.IOException;
+import java.util.Collections;
+import java.util.HashMap;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hisp.dhis.sms.SmsConfigurationManager;
+import org.hisp.dhis.sms.config.GenericHttpGatewayConfig;
import org.hisp.dhis.sms.config.SmsConfiguration;
+import org.hisp.dhis.sms.config.SmsGatewayConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
@@ -69,6 +73,21 @@
return "smsConfiguration";
}
+ @RequestMapping( value="test", method = RequestMethod.GET )
+ public String getTest( Model model )
+ {
+
+ SmsConfiguration smsConfiguration = new SmsConfiguration();
+
+ SmsGatewayConfig gatewayConfig = new GenericHttpGatewayConfig("http://storset.org/", new HashMap<String,String>());
+ smsConfiguration.setGateways( Collections.singletonList( gatewayConfig ) );
+
+ model.addAttribute( "model", smsConfiguration );
+
+ return "smsConfiguration";
+ }
+
+
//-------------------------------------------------------------------------------------------------------
// POST
//-------------------------------------------------------------------------------------------------------