Skip to content

Commit f094b69

Browse files
committed
- added (not fully tested) SOS credential connection capability
1 parent 5a384e2 commit f094b69

8 files changed

Lines changed: 27 additions & 6 deletions

File tree

50 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.

SweLib/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ android {
99
defaultConfig {
1010
minSdkVersion 21
1111
targetSdkVersion 28
12-
versionCode 3
13-
versionName '1.0.4'
12+
versionCode 4
13+
versionName '1.0.5'
1414
}
1515
buildTypes {
1616
release {

SweLib/src/main/java/org/sofwerx/ogc/sos/HttpHelper.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.sofwerx.ogc.sos;
22

3+
import android.util.Base64;
34
import android.util.Log;
45

56
import java.io.BufferedReader;
@@ -9,13 +10,14 @@
910
import java.io.OutputStream;
1011
import java.io.OutputStreamWriter;
1112
import java.io.StringWriter;
13+
import java.io.UnsupportedEncodingException;
1214
import java.net.HttpURLConnection;
1315
import java.net.URL;
1416

1517
import javax.net.ssl.HttpsURLConnection;
1618

1719
public class HttpHelper {
18-
public static String post(String serverURL, String body) throws IOException {
20+
public static String post(String serverURL, String username, String password, String body) throws IOException {
1921
if (serverURL == null)
2022
throw new IOException("Cannot connect to a null server URL");
2123
if (body == null)
@@ -36,6 +38,14 @@ public static String post(String serverURL, String body) throws IOException {
3638
conn.setRequestMethod("POST");
3739
conn.setRequestProperty("Content-Type","application/soap+xml");
3840
conn.setDoInput(true);
41+
if ((username != null) && (password != null)) {
42+
try {
43+
byte[] data = (username + ":" + password).getBytes("UTF-8");
44+
String encoded = new String(Base64.encode(data, Base64.DEFAULT));
45+
conn.setRequestProperty("Authorization", "Basic " + encoded);
46+
} catch (UnsupportedEncodingException ignore) {
47+
}
48+
}
3949
//conn.setDoOutput(true);
4050
conn.setInstanceFollowRedirects(false);
4151

SweLib/src/main/java/org/sofwerx/ogc/sos/SosService.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ public class SosService implements SosMessageListener {
2222
private Handler handler;
2323
private SosMessageListener listener;
2424
private String serverURL;
25+
private String username;
26+
private String password;
2527
private Context context;
2628
private SosSensor sosSensor;
2729
private SosIpcTransceiver transceiver;
@@ -31,20 +33,25 @@ public class SosService implements SosMessageListener {
3133
private boolean sosHttpBroadcast;
3234

3335
/**
34-
* Creates a new SOS Service
36+
* Creates a new SosService
3537
* @param context
3638
* @param sosSensor
39+
* @param sosServerURL
40+
* @param username
41+
* @param password
3742
* @param turnOn true == the service will start running immediately; false means the service will initiate but will not start sending/receiving
3843
* @param enableIpcBroadcast
3944
*/
40-
public SosService(Context context, SosSensor sosSensor, String sosServerURL, final boolean turnOn, final boolean enableIpcBroadcast) {
45+
public SosService(Context context, SosSensor sosSensor, String sosServerURL, String username, String password, final boolean turnOn, final boolean enableIpcBroadcast) {
4146
if (context == null)
4247
Log.e(SosIpcTransceiver.TAG,"SosService should not be passed a null context");
4348
this.context = context;
4449
this.sosSensor = sosSensor;
4550
if (context instanceof SosMessageListener)
4651
listener = (SosMessageListener)context;
4752
this.serverURL = sosServerURL;
53+
this.username = username;
54+
this.password = password;
4855
sosThread = new HandlerThread("SosService") {
4956
@Override
5057
protected void onLooperPrepared() {
@@ -59,6 +66,10 @@ protected void onLooperPrepared() {
5966
SosIpcTransceiver.setChannel(DEFAULT_SWE_CHANNEL);
6067
}
6168

69+
public SosService(Context context, SosSensor sosSensor, String sosServerURL, final boolean turnOn, final boolean enableIpcBroadcast) {
70+
this(context, sosSensor, sosServerURL, null, null, turnOn, enableIpcBroadcast);
71+
}
72+
6273
/**
6374
* Toggles between on (active/running/transmitting and receiving) and off (paused)
6475
* @param on true = on/active/transmitting and receiving
@@ -120,7 +131,7 @@ private void broadcast(AbstractSosOperation operation) {
120131
if ((serverURL != null) && sosHttpBroadcast) {
121132
Log.d(SosIpcTransceiver.TAG,"Broadcasting SOS operation to "+serverURL);
122133
try {
123-
String result = HttpHelper.post(serverURL,SosIpcTransceiver.toString(operation.toXML()));
134+
String result = HttpHelper.post(serverURL, username, password,SosIpcTransceiver.toString(operation.toXML()));
124135
AbstractSosOperation responseOperation = AbstractSosOperation.newFromXmlString(result);
125136
if (responseOperation == null) {
126137
Log.e(SosIpcTransceiver.TAG,"Unable to parse response from server: "+result);

0 commit comments

Comments
 (0)