83bc7cea6d27578435e2fe663af5b63b92a77cff
[fw/altos] / map-server / altos-map / AltosMap.java
1 /*
2  * Copyright © 2018 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  */
14
15 package altosmap;
16
17 import java.net.*;
18 import java.io.*;
19 import java.util.*;
20 import java.text.*;
21
22 import org.altusmetrum.altoslib_13.*;
23
24 public class AltosMap {
25
26         public final static int port = 16717;
27
28         String  query_string;
29         String  remote_addr;
30
31         public String reason_string(int code) {
32                 switch (code) {
33                 case 200:
34                         return "OK";
35                 case 400:
36                         return "Bad Request";
37                 case 403:
38                         return "Forbidden";
39                 case 404:
40                         return "Not Found";
41                 case 408:
42                         return "Request Timeout";
43                 default:
44                         return "Failure";
45                 }
46         }
47
48         public void write_status(int status) {
49                 System.out.printf("Status: %d %s\n", status, reason_string(status));
50         }
51
52         public void write_type(String type) {
53                 System.out.printf("Content-Type: %s\n", type);
54         }
55
56         public void fail(int status, String reason) {
57                 write_status(status);
58                 write_type("text/html");
59                 System.out.printf("<body>%s</body>\n", reason);
60                 System.exit(1);
61         }
62
63         public void process() {
64                 query_string = System.getenv("QUERY_STRING");
65
66                 if (query_string == null)
67                         fail(400, "Missing query string");
68
69                 remote_addr = System.getenv("REMOTE_ADDR");
70
71                 if (remote_addr == null)
72                         fail(400, "Missing remote address");
73
74                 String[] queries = query_string.split("&");
75
76                 double  lon = AltosLib.MISSING;
77                 double  lat = AltosLib.MISSING;
78                 int     zoom = AltosLib.MISSING;
79
80                 try {
81                         for (String query : queries) {
82                                 String[] q = query.split("=");
83                                 if (q.length >= 2) {
84                                         String name = q[0];
85                                         String value = q[1];
86                                         if (name.equals("lon"))
87                                                 lon = AltosParse.parse_double_net(value);
88                                         else if (name.equals("lat"))
89                                                 lat = AltosParse.parse_double_net(value);
90                                         else if (name.equals("zoom"))
91                                                 zoom = AltosParse.parse_int(value);
92                                         else
93                                                 fail(400, String.format("Extra query param \"%s\"", query));
94                                 }
95                         }
96                 } catch (ParseException pe) {
97                         fail(400, String.format("Invalid query: %s", pe.toString()));
98                 }
99
100                 if (lon == AltosLib.MISSING)
101                         fail(400, "Missing longitude");
102                 if (lat == AltosLib.MISSING)
103                         fail(400, "Missing latitude");
104                 if (zoom == AltosLib.MISSING)
105                         fail(400, "Missing zoom");
106
107                 try {
108                         Socket  socket = new Socket(InetAddress.getLoopbackAddress(), port);
109
110                         AltosJson       request = new AltosJson();
111
112                         request.put("lat", lat);
113                         request.put("lon", lon);
114                         request.put("zoom", zoom);
115                         request.put("remote_addr", remote_addr);
116
117                         Writer writer = new PrintWriter(socket.getOutputStream());
118                         request.write(writer);
119                         writer.flush();
120
121                         AltosJson       reply = AltosJson.fromInputStream(socket.getInputStream());
122
123                         int status = reply.get_int("status", 400);
124
125                         if (status != 200)
126                                 fail(status, "Bad cache status");
127
128                         String filename = reply.get_string("filename", null);
129                         try {
130                                 File file = new File(filename);
131                                 long length = file.length();
132                                 FileInputStream in = new FileInputStream(file);
133                                 String content_type = reply.get_string("content_type", null);
134                                 System.out.printf("Content-Type: %s\n", content_type);
135                                 System.out.printf("Content-Length: %d\n", file.length());
136                                 byte[] buf = new byte[4096];
137                                 int bytes_read;
138                                 while ((bytes_read = in.read(buf)) > 0)
139                                         System.out.write(buf);
140                         } catch (IOException ie) {
141                                 fail(404, String.format("IO Exception: %s", ie.toString()));
142                         }
143                 } catch (Exception e) {
144                         fail(404, String.format("Exception %s", e.toString()));
145                 }
146         }
147
148         public AltosMap() {
149         }
150
151         public static void main(final String[] args) {
152
153                 new AltosMap().process();
154
155         }
156 }