dhis2-devs team mailing list archive
-
dhis2-devs team
-
Mailing list archive
-
Message #22231
[Branch ~dhis2-devs-core/dhis2/trunk] Rev 10657: add the Adnvancd HTTP gateway
------------------------------------------------------------
revno: 10657
committer: Long <Long@Long-Laptop>
branch nick: dhis2
timestamp: Mon 2013-04-22 13:42:29 +0700
message:
add the Adnvancd HTTP gateway
added:
dhis-2/dhis-services/dhis-service-sms/src/main/java/org/hisp/dhis/sms/smslib/AdvanceHttpPostGateWay.java
modified:
dhis-2/dhis-services/dhis-service-sms/src/main/java/org/hisp/dhis/sms/smslib/SimplisticHttpGetGateWay.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-services/dhis-service-sms/src/main/java/org/hisp/dhis/sms/smslib/AdvanceHttpPostGateWay.java'
--- dhis-2/dhis-services/dhis-service-sms/src/main/java/org/hisp/dhis/sms/smslib/AdvanceHttpPostGateWay.java 1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-services/dhis-service-sms/src/main/java/org/hisp/dhis/sms/smslib/AdvanceHttpPostGateWay.java 2013-04-22 06:42:29 +0000
@@ -0,0 +1,134 @@
+package org.hisp.dhis.sms.smslib;
+
+/*
+ * Copyright (c) 2004-2013, 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.
+ */
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.net.HttpURLConnection;
+import java.net.URL;
+import java.net.URLConnection;
+import java.net.URLEncoder;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Map;
+import org.apache.commons.lang.StringUtils;
+import org.smslib.AGateway;
+import org.smslib.GatewayException;
+import org.smslib.OutboundMessage;
+import org.smslib.TimeoutException;
+import org.smslib.helper.Logger;
+
+public class AdvanceHttpPostGateWay
+ extends AGateway
+{
+
+ private static final String SENDER = "sender";
+
+ private static final String RECIPIENT = "recipient";
+
+ private static final String MESSAGE = "message";
+
+ private Map<String, String> parameters;
+
+ private String urlTemplate;
+
+ public AdvanceHttpPostGateWay( String id, String urlTemplate, Map<String, String> parameters )
+ {
+ super( id );
+ this.urlTemplate = urlTemplate;
+ this.parameters = parameters;
+ }
+
+ @Override
+ public int sendMessages( Collection<OutboundMessage> outboundMessages )
+ throws TimeoutException, GatewayException, IOException, InterruptedException
+ {
+ Map<String, String> requestParameters = new HashMap<String, String>( parameters );
+ for ( OutboundMessage outboundMessage : outboundMessages )
+ {
+ requestParameters.put( RECIPIENT, outboundMessage.getRecipient() );
+ requestParameters.put( MESSAGE, outboundMessage.getText() );
+
+ String sender = outboundMessage.getFrom();
+ if ( sender != null )
+ {
+ Logger.getInstance().logDebug( "Adding sender " + sender, null, getGatewayId() );
+ requestParameters.put( SENDER, sender );
+ }
+ try
+ {
+ String urlString = urlTemplate;
+ for ( String key : requestParameters.keySet() )
+ {
+ if ( requestParameters.get( key ) != null )
+ {
+ urlString = StringUtils.replace( urlString, "{" + key + "}",
+ URLEncoder.encode( requestParameters.get( key ), "UTF-8" ) );
+ }
+ }
+ Logger.getInstance().logInfo( "RequestURL: " + urlString, null, getGatewayId() );
+ URL requestURL = new URL( urlString );
+ URLConnection conn = requestURL.openConnection();
+ BufferedReader reader = new BufferedReader( new InputStreamReader( conn.getInputStream() ) );
+ String line, response = "";
+ while ( (line = reader.readLine()) != null )
+ {
+ response += line;
+ }
+
+ HttpURLConnection httpConnection = (HttpURLConnection) conn;
+ if ( httpConnection.getResponseCode() != HttpURLConnection.HTTP_OK )
+ {
+ Logger.getInstance().logWarn( "Couldn't send message, got response " + response, null,
+ getGatewayId() );
+ return 0;
+ }
+
+ reader.close();
+
+ }
+ catch ( Exception e )
+ {
+ Logger.getInstance().logWarn( "Couldn't send message " + outboundMessage, e, getGatewayId() );
+ return 0;
+ }
+
+ return 1;
+
+ }
+ return super.sendMessages( outboundMessages );
+ }
+
+ @Override
+ public int getQueueSchedulingInterval()
+ {
+ return 0;
+ }
+
+}
=== modified 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 2013-03-08 12:21:34 +0000
+++ dhis-2/dhis-services/dhis-service-sms/src/main/java/org/hisp/dhis/sms/smslib/SimplisticHttpGetGateWay.java 2013-04-22 06:42:29 +0000
@@ -36,7 +36,6 @@
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
-
import org.apache.commons.lang.StringUtils;
import org.smslib.AGateway;
import org.smslib.GatewayException;
@@ -72,6 +71,7 @@
* </ul>
*
*/
+
public class SimplisticHttpGetGateWay
extends AGateway
{