altos-mapd: Handle clients failing to send valid JSON
[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 int             http_status;
30
31         private void set_status(int status) {
32                 http_status = status;
33                 reply.put("status", status);
34         }
35
36         private void set_filename(String filename) {
37                 reply.put("filename", filename);
38
39         }
40
41         private void set_content_type(String content_type) {
42                 reply.put("content_type", content_type);
43         }
44
45         private String content_type(File file) {
46                 String content_type = "application/octet-stream";
47                 String basename = file.getName();
48                 if (basename.endsWith(".jpg"))
49                         content_type = "image/jpeg";
50                 if (basename.endsWith(".png"))
51                         content_type = "image/png";
52                 return content_type;
53         }
54
55         private void set_file(File file) {
56                 set_filename(file.getAbsolutePath());
57                 set_content_type(content_type(file));
58         }
59
60         private Semaphore store_ready;
61
62         public void notify_store(AltosMapStore map_store, int status) {
63                 if (status != AltosMapTile.fetching)
64                         store_ready.release();
65         }
66
67         public void run() {
68                 reply = new AltosJson();
69                 try {
70                         request = AltosJson.fromInputStream(socket.getInputStream());
71
72                         if (request == null) {
73                                 set_status(400);
74                                 System.out.printf("client failed %d\n", http_status);
75                         } else {
76
77                                 double  lat = request.get_double("lat", AltosLib.MISSING);
78                                 double  lon = request.get_double("lon", AltosLib.MISSING);
79                                 int     zoom = request.get_int("zoom", AltosLib.MISSING);
80                                 String  addr = request.get_string("remote_addr", null);
81
82                                 if (lat == AltosLib.MISSING ||
83                                     lon == AltosLib.MISSING ||
84                                     zoom == AltosLib.MISSING ||
85                                     addr == null)
86                                 {
87                                         set_status(400);
88                                 } else if (!AltosMapd.check_lat_lon(lat, lon)) {
89                                         set_status(403);        /* Forbidden */
90                                 } else {
91
92                                         store_ready = new Semaphore(0);
93
94                                         AltosMapStore   map_store = AltosMapStore.get(new AltosLatLon(lat, lon),
95                                                                                       zoom,
96                                                                                       AltosMapd.maptype,
97                                                                                       AltosMapd.px_size,
98                                                                                       AltosMapd.scale);
99                                         int status;
100
101                                         if (map_store == null) {
102                                                 status = AltosMapTile.failed;
103                                         } else {
104                                                 map_store.add_listener(this);
105
106                                                 try {
107                                                         store_ready.acquire();
108                                                 } catch (Exception ie) {
109                                                 }
110
111                                                 status = map_store.status();
112                                         }
113
114                                         if (status == AltosMapTile.fetched || status == AltosMapTile.loaded) {
115                                                 set_status(200);
116                                                 set_file(map_store.file);
117                                         } else if (status == AltosMapTile.failed) {
118                                                 set_status(404);
119                                         } else if (status == AltosMapTile.fetching) {
120                                                 set_status(408);
121                                         } else if (status == AltosMapTile.bad_request) {
122                                                 set_status(400);
123                                         } else if (status == AltosMapTile.forbidden) {
124                                                 set_status(403);
125                                         } else {
126                                                 set_status(400);
127                                         }
128                                 }
129                                 System.out.printf("%s: %.6f %.6f %d status %d\n",
130                                                   addr, lat, lon, zoom, http_status);
131                         }
132                 } catch (Exception e) {
133                         System.out.printf("client exception %s\n", e.toString());
134                         e.printStackTrace(System.out);
135                         set_status(400);
136
137                 } finally {
138                         try {
139                                 Writer writer = new PrintWriter(socket.getOutputStream());
140                                 reply.write(writer);
141                                 writer.write('\n');
142                                 writer.flush();
143                         } catch (IOException ie) {
144                         }
145                         try {
146                                 socket.close();
147                         } catch (IOException ie) {
148                         }
149                 }
150         }
151
152         public AltosMapdClient(Socket socket) {
153                 this.socket = socket;
154                 start();
155         }
156 }