2 * Copyright © 2018 Keith Packard <keithp@keithp.com>
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.
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.
22 import org.altusmetrum.altoslib_13.*;
24 public class AltosMap {
26 public final static int port = 16717;
31 public String reason_string(int code) {
42 return "Request Timeout";
48 public void write_status(int status) {
49 System.out.printf("Status: %d %s\n", status, reason_string(status));
52 public void write_type(String type) {
53 System.out.printf("Content-Type: %s\n", type);
56 public void fail(int status, String reason) {
58 write_type("text/html");
59 System.out.printf("\n");
60 System.out.printf("<html>\n");
61 System.out.printf("<head><title>Map Fetch Failure</title></head>\n");
62 System.out.printf("<body>%s</body>\n", reason);
63 System.out.printf("</html>\n");
67 public void process() {
68 query_string = System.getenv("QUERY_STRING");
70 if (query_string == null)
71 fail(400, "Missing query string");
73 remote_addr = System.getenv("REMOTE_ADDR");
75 if (remote_addr == null)
76 fail(400, "Missing remote address");
78 String[] queries = query_string.split("&");
80 double lon = AltosLib.MISSING;
81 double lat = AltosLib.MISSING;
82 int zoom = AltosLib.MISSING;
85 for (String query : queries) {
86 String[] q = query.split("=");
90 if (name.equals("lon"))
91 lon = AltosParse.parse_double_net(value);
92 else if (name.equals("lat"))
93 lat = AltosParse.parse_double_net(value);
94 else if (name.equals("zoom"))
95 zoom = AltosParse.parse_int(value);
97 fail(400, String.format("Extra query param \"%s\"", query));
100 } catch (ParseException pe) {
101 fail(400, String.format("Invalid query: %s", pe.toString()));
104 if (lon == AltosLib.MISSING)
105 fail(400, "Missing longitude");
106 if (lat == AltosLib.MISSING)
107 fail(400, "Missing latitude");
108 if (zoom == AltosLib.MISSING)
109 fail(400, "Missing zoom");
112 Socket socket = new Socket(InetAddress.getLoopbackAddress(), port);
114 AltosJson request = new AltosJson();
116 request.put("lat", lat);
117 request.put("lon", lon);
118 request.put("zoom", zoom);
119 request.put("remote_addr", remote_addr);
121 Writer writer = new PrintWriter(socket.getOutputStream());
122 request.write(writer);
125 AltosJson reply = AltosJson.fromInputStream(socket.getInputStream());
127 int status = reply.get_int("status", 400);
130 fail(status, "Bad cache status");
132 String filename = reply.get_string("filename", null);
134 File file = new File(filename);
135 long length = file.length();
136 FileInputStream in = new FileInputStream(file);
137 String content_type = reply.get_string("content_type", null);
138 System.out.printf("Content-Type: %s\n", content_type);
139 System.out.printf("Content-Length: %d\n", file.length());
140 System.out.printf("\n");
141 byte[] buf = new byte[4096];
143 while ((bytes_read = in.read(buf)) > 0)
144 System.out.write(buf);
145 } catch (IOException ie) {
146 fail(404, String.format("IO Exception: %s", ie.toString()));
148 } catch (Exception e) {
149 fail(404, String.format("Exception %s", e.toString()));
156 public static void main(final String[] args) {
158 new AltosMap().process();