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("<body>%s</body>\n", reason);
63 public void process() {
64 query_string = System.getenv("QUERY_STRING");
66 if (query_string == null)
67 fail(400, "Missing query string");
69 remote_addr = System.getenv("REMOTE_ADDR");
71 if (remote_addr == null)
72 fail(400, "Missing remote address");
74 String[] queries = query_string.split("&");
76 double lon = AltosLib.MISSING;
77 double lat = AltosLib.MISSING;
78 int zoom = AltosLib.MISSING;
81 for (String query : queries) {
82 String[] q = query.split("=");
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);
93 fail(400, String.format("Extra query param \"%s\"", query));
96 } catch (ParseException pe) {
97 fail(400, String.format("Invalid query: %s", pe.toString()));
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");
108 Socket socket = new Socket(InetAddress.getLoopbackAddress(), port);
110 AltosJson request = new AltosJson();
112 request.put("lat", lat);
113 request.put("lon", lon);
114 request.put("zoom", zoom);
115 request.put("remote_addr", remote_addr);
117 Writer writer = new PrintWriter(socket.getOutputStream());
118 request.write(writer);
121 AltosJson reply = AltosJson.fromInputStream(socket.getInputStream());
123 int status = reply.get_int("status", 400);
126 fail(status, "Bad cache status");
128 String filename = reply.get_string("filename", null);
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];
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()));
143 } catch (Exception e) {
144 fail(404, String.format("Exception %s", e.toString()));
151 public static void main(final String[] args) {
153 new AltosMap().process();