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_14.*;
24 public class AltosMap {
26 public final static int port = 16717;
28 public final static String protocol_version = "1.0.0";
33 public String reason_string(int code) {
44 return "Request Timeout";
50 public void write_status(int status) {
51 System.out.printf("Status: %d %s\n", status, reason_string(status));
54 public void write_type(String type) {
55 System.out.printf("Content-Type: %s\n", type);
58 public void fail(int status, String reason) {
60 write_type("text/html");
61 System.out.printf("\n");
62 System.out.printf("<html>\n");
63 System.out.printf("<head><title>Map Fetch Failure</title></head>\n");
64 System.out.printf("<body>%s</body>\n", reason);
65 System.out.printf("</html>\n");
69 public void process() {
70 query_string = System.getenv("QUERY_STRING");
72 if (query_string == null)
73 fail(400, "Missing query string");
75 remote_addr = System.getenv("REMOTE_ADDR");
77 if (remote_addr == null)
78 fail(400, "Missing remote address");
80 String[] queries = query_string.split("&");
82 double lon = AltosLib.MISSING;
83 double lat = AltosLib.MISSING;
84 int zoom = AltosLib.MISSING;
85 String version = null;
88 for (String query : queries) {
89 String[] q = query.split("=");
93 if (name.equals("lon"))
94 lon = AltosParse.parse_double_net(value);
95 else if (name.equals("lat"))
96 lat = AltosParse.parse_double_net(value);
97 else if (name.equals("zoom"))
98 zoom = AltosParse.parse_int(value);
99 else if (name.equals("version"))
102 fail(400, String.format("Extra query param \"%s\"", query));
105 } catch (ParseException pe) {
106 fail(400, String.format("Invalid query: %s", pe.toString()));
109 if (version != null) {
110 System.out.printf("Content-Type: text/plain\n");
111 System.out.printf("\n");
112 System.out.printf("%s\n", protocol_version);
114 if (lon == AltosLib.MISSING)
115 fail(400, "Missing longitude");
116 if (lat == AltosLib.MISSING)
117 fail(400, "Missing latitude");
118 if (zoom == AltosLib.MISSING)
119 fail(400, "Missing zoom");
122 Socket socket = null;
125 while (tries < 10 && socket == null) {
127 socket = new Socket(InetAddress.getLoopbackAddress(), port);
128 } catch (IOException ie) {
134 AltosJson request = new AltosJson();
136 request.put("lat", lat);
137 request.put("lon", lon);
138 request.put("zoom", zoom);
139 request.put("remote_addr", remote_addr);
141 Writer writer = new PrintWriter(socket.getOutputStream());
142 request.write(writer);
145 AltosJson reply = AltosJson.fromInputStream(socket.getInputStream());
147 int status = reply.get_int("status", 400);
150 fail(status, "Bad cache status");
152 String filename = reply.get_string("filename", null);
154 File file = new File(filename);
155 long length = file.length();
156 FileInputStream in = new FileInputStream(file);
157 String content_type = reply.get_string("content_type", null);
158 System.out.printf("Content-Type: %s\n", content_type);
159 System.out.printf("Content-Length: %d\n", file.length());
160 System.out.printf("\n");
161 byte[] buf = new byte[4096];
163 while ((bytes_read = in.read(buf)) > 0)
164 System.out.write(buf);
165 } catch (IOException ie) {
166 fail(404, String.format("IO Exception: %s", ie.toString()));
168 } catch (Exception e) {
169 fail(404, String.format("Exception %s", e.toString()));
177 public static void main(final String[] args) {
179 new AltosMap().process();