1+ package org .sofwerx .torgi .ogc ;
2+
3+ import android .util .Log ;
4+
5+ import org .apache .http .conn .util .InetAddressUtils ;
6+ import org .json .JSONException ;
7+ import org .json .JSONObject ;
8+ import org .sofwerx .torgi .gnss .helper .GeoPackageSatDataHelper ;
9+ import org .sofwerx .torgi .service .TorgiService ;
10+
11+ import java .io .IOException ;
12+ import java .net .InetAddress ;
13+ import java .net .NetworkInterface ;
14+ import java .util .ArrayList ;
15+ import java .util .Enumeration ;
16+ import java .util .HashMap ;
17+ import java .util .List ;
18+ import java .util .Map ;
19+
20+ import fi .iki .elonen .NanoHTTPD ;
21+
22+ public class LiteWebServer {
23+ private final static String TAG = "TORGI.WS" ;
24+ private WebServer server ;
25+ private final TorgiService torgiService ;
26+ private final static int PORT = 8080 ;
27+
28+ public LiteWebServer (TorgiService torgiService ) {
29+ this .torgiService = torgiService ;
30+
31+ server = new WebServer ();
32+ try {
33+ server .start ();
34+ } catch (IOException ioe ) {
35+ Log .w (TAG , "SOS web server could not start." );
36+ }
37+ torgiService .notifyOfWebServer (getLocalIpAddress ()+":" + PORT );
38+ }
39+
40+ public void stop () {
41+ if (server != null )
42+ server .stop ();
43+ }
44+
45+ private class WebServer extends NanoHTTPD {
46+ public WebServer () {
47+ super (PORT );
48+ }
49+
50+ public Response serve (IHTTPSession session ) {
51+ final Map <String , String > map = new HashMap <String , String >();
52+ Method method = session .getMethod ();
53+ Log .d (TAG ,"Method: " +method .name ());
54+ if (Method .PUT .equals (method ) || Method .POST .equals (method )) {
55+ try {
56+ session .parseBody (map );
57+ // get the POST body
58+ if (map .containsKey ("postData" )) {
59+ String data = map .get ("postData" );
60+ Log .d (TAG ,"Data = " +data );
61+ if (data != null ) {
62+ JSONObject obj = null ;
63+ try {
64+ obj = new JSONObject (data );
65+ } catch (JSONException e ) {
66+ if (data .indexOf ('{' ) > -1 ) {
67+ try {
68+ obj = new JSONObject (data .substring (data .indexOf ('{' )));
69+ } catch (JSONException ignore ) {}
70+ }
71+ }
72+ if (obj != null ) {
73+ String operation = obj .optString ("request" ,null );
74+ if (operation != null ) {
75+ if ("GetObservation" .equalsIgnoreCase (operation )) {
76+ ArrayList <GeoPackageSatDataHelper > measurements =
77+ torgiService .getGeoPackageRecorder ().getGnssMeasurementsSatDataBlocking (System .currentTimeMillis ()-1000l *10l ,System .currentTimeMillis ());
78+ if (measurements == null )
79+ return newFixedLengthResponse ("{}" ); //TODO send back a better empty description
80+ else {
81+ String out = SOSHelper .getObservation (measurements );
82+ return newFixedLengthResponse (out );
83+ }
84+ }
85+ }
86+ }
87+ }
88+ }
89+ } catch (IOException ioe ) {
90+ return newFixedLengthResponse (Response .Status .INTERNAL_ERROR , MIME_PLAINTEXT , "SERVER INTERNAL ERROR: IOException: " + ioe .getMessage ());
91+ } catch (ResponseException re ) {
92+ return newFixedLengthResponse (re .getStatus (), MIME_PLAINTEXT , re .getMessage ());
93+ }
94+ }
95+
96+ Map <String , List <String >> decodedQueryParameters = decodeParameters (session .getQueryParameterString ());
97+
98+ StringBuilder sb = new StringBuilder ();
99+ sb .append ("<html><head><title>TORGI Debug</title></head><body>" );
100+ sb .append ("<h1>TORGI did not understand what it received</h1>" );
101+ sb .append ("<p><blockquote><b>URI</b> = " ).append (String .valueOf (session .getUri ())).append ("<br />" );
102+ sb .append ("<b>Method</b> = " ).append (String .valueOf (session .getMethod ())).append ("</blockquote></p>" );
103+ sb .append ("<h3>Headers</h3><p><blockquote>" ).append (toString (session .getHeaders ())).append ("</blockquote></p>" );
104+ sb .append ("<h3>Parms</h3><p><blockquote>" ).append (toString (session .getParms ())).append ("</blockquote></p>" );
105+ sb .append ("<h3>Parms (multi values?)</h3><p><blockquote>" ).append (toString (decodedQueryParameters )).append ("</blockquote></p>" );
106+ try {
107+ Map <String , String > files = new HashMap <String , String >();
108+ session .parseBody (files );
109+ sb .append ("<h3>Files</h3><p><blockquote>" ).append (toString (files )).append ("</blockquote></p>" );
110+ } catch (Exception e ) {
111+ e .printStackTrace ();
112+ }
113+ sb .append ("</body></html>" );
114+ return newFixedLengthResponse (sb .toString ());
115+ }
116+
117+ private String toString (Map <String , ? extends Object > map ) {
118+ if (map .size () == 0 ) {
119+ return "" ;
120+ }
121+ return unsortedList (map );
122+ }
123+
124+ private String unsortedList (Map <String , ? extends Object > map ) {
125+ StringBuilder sb = new StringBuilder ();
126+ sb .append ("<ul>" );
127+ for (Map .Entry <String , ? extends Object > entry : map .entrySet ()) {
128+ listItem (sb , entry );
129+ }
130+ sb .append ("</ul>" );
131+ return sb .toString ();
132+ }
133+ }
134+
135+ private void listItem (StringBuilder sb , Map .Entry <String , ? extends Object > entry ) {
136+ sb .append ("<li><code><b>" ).append (entry .getKey ()).append ("</b> = " ).append (entry .getValue ()).append ("</code></li>" );
137+ }
138+
139+ private String getLocalIpAddress () {
140+ try {
141+ for (Enumeration <NetworkInterface > en = NetworkInterface .getNetworkInterfaces (); en .hasMoreElements ();) {
142+ NetworkInterface intf = en .nextElement ();
143+ for (Enumeration <InetAddress > enumIpAddr = intf .getInetAddresses (); enumIpAddr .hasMoreElements ();) {
144+ InetAddress inetAddress = enumIpAddr .nextElement ();
145+ if (!inetAddress .isLoopbackAddress () && InetAddressUtils .isIPv4Address (inetAddress .getHostAddress ())) {
146+ String ip = inetAddress .getHostAddress ().toString ();
147+ Log .d (TAG ,"Using IP address " +ip );
148+ return ip ;
149+ }
150+ }
151+ }
152+ } catch (Exception ex ) {
153+ Log .e (TAG , ex .toString ());
154+ }
155+ return null ;
156+ }
157+ }
0 commit comments