altos-mapd: Add geo-fencing for map requests. Add port and key arguments
[fw/altos] / map-server / altos-mapd / AltosMapdClient.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 altosmapd;
16
17 import java.net.*;
18 import java.util.*;
19 import java.util.concurrent.*;
20 import java.io.*;
21
22 import org.altusmetrum.altoslib_13.*;
23
24 public class AltosMapdClient extends Thread implements AltosMapStoreListener {
25         private Socket          socket;
26         private AltosJson       request;
27         private AltosJson       reply;
28
29         private void set_status(int status) {
30                 reply.put("status", status);
31         }
32
33         private void set_filename(String filename) {
34                 reply.put("filename", filename);
35
36         }
37
38         private void set_content_type(String content_type) {
39                 reply.put("content_type", content_type);
40         }
41
42         private String content_type(File file) {
43                 String content_type = "application/octet-stream";
44                 String basename = file.getName();
45                 if (basename.endsWith(".jpg"))
46                         content_type = "image/jpeg";
47                 if (basename.endsWith(".png"))
48                         content_type = "image/png";
49                 return content_type;
50         }
51
52         private void set_file(File file) {
53                 set_filename(file.getAbsolutePath());
54                 set_content_type(content_type(file));
55         }
56
57         private Semaphore store_ready;
58
59         public void notify_store(AltosMapStore map_store, int status) {
60                 if (status != AltosMapTile.fetching)
61                         store_ready.release();
62         }
63
64         public void run() {
65                 reply = new AltosJson();
66                 try {
67                         request = AltosJson.fromInputStream(socket.getInputStream());
68
69                         double  lat = request.get_double("lat", AltosLib.MISSING);
70                         double  lon = request.get_double("lon", AltosLib.MISSING);
71                         int     zoom = request.get_int("zoom", AltosLib.MISSING);
72                         String  addr = request.get_string("remote_addr", null);
73
74                         if (lat == AltosLib.MISSING ||
75                             lon == AltosLib.MISSING ||
76                             zoom == AltosLib.MISSING ||
77                             addr == null)
78                         {
79                                 set_status(400);
80                         } else if (!AltosMapd.check_lat_lon(lat, lon)) {
81                                 set_status(403);        /* Forbidden */
82                         } else {
83
84                                 store_ready = new Semaphore(0);
85
86                                 System.out.printf("Fetching tile for %g %g %d\n", lat, lon, zoom);
87
88                                 AltosMapStore   map_store = AltosMapStore.get(new AltosLatLon(lat, lon),
89                                                                               zoom,
90                                                                               AltosMapd.maptype,
91                                                                               AltosMapd.px_size,
92                                                                               AltosMapd.scale);
93                                 int status;
94
95                                 if (map_store == null) {
96                                         System.out.printf("no store?\n");
97                                         status = AltosMapTile.failed;
98                                 } else {
99                                         map_store.add_listener(this);
100
101                                         System.out.printf("Waiting for tile\n");
102
103                                         try {
104                                                 store_ready.acquire();
105                                         } catch (Exception ie) {
106                                         }
107
108                                         status = map_store.status();
109                                 }
110
111                                 if (status == AltosMapTile.fetched || status == AltosMapTile.loaded) {
112                                         set_status(200);
113                                         set_file(map_store.file);
114                                 } else if (status == AltosMapTile.failed) {
115                                         set_status(404);
116                                 } else if (status == AltosMapTile.fetching) {
117                                         set_status(408);
118                                 } else if (status == AltosMapTile.bad_request) {
119                                         set_status(400);
120                                 } else if (status == AltosMapTile.forbidden) {
121                                         set_status(403);
122                                 } else {
123                                         set_status(400);
124                                 }
125                         }
126                 } catch (Exception e) {
127                         System.out.printf("client exception %s\n", e.toString());
128                         e.printStackTrace(System.out);
129                         set_status(400);
130
131                 } finally {
132                         try {
133                                 Writer writer = new PrintWriter(socket.getOutputStream());
134                                 reply.write(writer);
135                                 writer.write('\n');
136                                 writer.flush();
137                         } catch (IOException ie) {
138                         }
139                         try {
140                                 socket.close();
141                         } catch (IOException ie) {
142                         }
143                         System.out.printf("client done\n");
144                 }
145         }
146
147         public AltosMapdClient(Socket socket) {
148                 this.socket = socket;
149                 start();
150         }
151 }