1 | /** |
---|
2 | * Copyright (c) 2007 Orkney, Inc. <http://www.orkney.co.jp/> |
---|
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 3 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, |
---|
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
12 | * GNU General Public License for more details. |
---|
13 | * |
---|
14 | * You should have received a copy of the GNU General Public License |
---|
15 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
16 | */ |
---|
17 | |
---|
18 | package jp.co.orkney.restlet; |
---|
19 | |
---|
20 | import java.io.IOException; |
---|
21 | import java.net.*; |
---|
22 | import java.sql.ResultSet; |
---|
23 | import java.sql.SQLException; |
---|
24 | |
---|
25 | import jp.co.orkney.restlet.geo.ClosestEdge; |
---|
26 | import jp.co.orkney.restlet.geo.DrivingDistance; |
---|
27 | import jp.co.orkney.restlet.geo.GeoAction; |
---|
28 | import jp.co.orkney.restlet.geo.ShortestPath; |
---|
29 | import jp.co.orkney.restlet.geo.TravelingSalesPerson; |
---|
30 | import jp.co.orkney.restlet.security.MD5SecurityHelper; |
---|
31 | import jp.co.orkney.restlet.security.SecurityHelper; |
---|
32 | import jp.co.orkney.restlet.util.DatabaseConnection; |
---|
33 | import jp.co.orkney.restlet.util.IOHelper; |
---|
34 | import jp.co.orkney.restlet.util.Log; |
---|
35 | import jp.co.orkney.restlet.util.Parameter; |
---|
36 | import jp.co.orkney.restlet.util.Configuration; |
---|
37 | |
---|
38 | import org.json.JSONException; |
---|
39 | import org.restlet.Component; |
---|
40 | import org.restlet.data.Form; |
---|
41 | import org.restlet.Restlet; |
---|
42 | import org.restlet.data.MediaType; |
---|
43 | import org.restlet.data.Method; |
---|
44 | import org.restlet.data.Protocol; |
---|
45 | import org.restlet.data.Request; |
---|
46 | import org.restlet.data.Response; |
---|
47 | import org.restlet.data.Status; |
---|
48 | |
---|
49 | import com.sun.org.apache.xalan.internal.xsltc.runtime.Hashtable; |
---|
50 | |
---|
51 | /** |
---|
52 | * <b>WebRouting class is the main class of WebRouting Service.</b> |
---|
53 | * <p> |
---|
54 | * WebRounting class provides a Restlet object to handle all the requests on the |
---|
55 | * server. Parses these GET or POST requests to know which provider, service, |
---|
56 | * input and output format are requested by user. After it the Restlet calls the |
---|
57 | * right method and draws the result. <br /> |
---|
58 | * <br /> |
---|
59 | * WebRouting Service step by step: |
---|
60 | * <ul> |
---|
61 | * <li>1. Parses XML configuration file</li> |
---|
62 | * <li>2. Launches a server with the specified port number</li> |
---|
63 | * <li>3. Creates the Restlet object</li> |
---|
64 | * <li>4. In case of requests on the server, parses this request to know all |
---|
65 | * requested information (provider, service, format, data,...)</li> |
---|
66 | * <li>5. Starts the requested service with data and configuration specified in |
---|
67 | * the request</li> |
---|
68 | * <li>6. Draws the result in the specified format</li> |
---|
69 | * </p> |
---|
70 | * |
---|
71 | * @author Matthieu Bilbille - Orkney Inc. |
---|
72 | * @version 1.0 |
---|
73 | * |
---|
74 | */ |
---|
75 | public class WebRouting |
---|
76 | { |
---|
77 | static final String CLOSEST_EDGE = "closest_edge"; |
---|
78 | static final String SHORTEST_PATH = "shortest_path"; |
---|
79 | static final String SHORTEST_WALK = "shortest_walk"; |
---|
80 | static final String DRIVING_DISTANCE = "driving_distance"; |
---|
81 | static final String TSP = "traveling_sales_person"; |
---|
82 | |
---|
83 | private String result = ""; |
---|
84 | private MediaType resultFormat; |
---|
85 | private Log log; |
---|
86 | private Configuration configuration; |
---|
87 | private DatabaseConnection connection; |
---|
88 | private IOHelper ioHelper; |
---|
89 | private boolean error; |
---|
90 | |
---|
91 | // This hashtable stores clientId/apiKey pairs |
---|
92 | private Hashtable keys; |
---|
93 | |
---|
94 | /** |
---|
95 | * Constructor WebRouting |
---|
96 | * |
---|
97 | * @throws Exception |
---|
98 | */ |
---|
99 | public WebRouting() throws Exception |
---|
100 | { |
---|
101 | Component component = new Component(); |
---|
102 | configuration = new Configuration(); |
---|
103 | configuration.parseXML(); |
---|
104 | component.getServers().add(Protocol.HTTP, Integer.parseInt(configuration.getPort())); |
---|
105 | log = configuration.createLog(); |
---|
106 | ioHelper = new IOHelper(configuration); |
---|
107 | keys = new Hashtable(); |
---|
108 | |
---|
109 | connection = new DatabaseConnection(configuration); |
---|
110 | |
---|
111 | Restlet restlet = new Restlet() |
---|
112 | { |
---|
113 | synchronized public void handle(Request request, Response response) |
---|
114 | { |
---|
115 | error = false; |
---|
116 | log.write("Restlet HANDLE", 2); |
---|
117 | |
---|
118 | try |
---|
119 | { |
---|
120 | configuration.parseURL(request.getResourceRef().getRemainingPart().split("\\?")[0]); |
---|
121 | } |
---|
122 | catch (Exception e1) |
---|
123 | { |
---|
124 | log.write("--ERROR: Wrong URL", 0); |
---|
125 | System.out.println(e1.getMessage()); |
---|
126 | error = true; |
---|
127 | response.setStatus(Status.SERVER_ERROR_INTERNAL); |
---|
128 | } |
---|
129 | if (configuration.askCapabilities() != -1) |
---|
130 | { |
---|
131 | response.setEntity(configuration.getCapabilities(), MediaType.TEXT_XML); |
---|
132 | } |
---|
133 | else |
---|
134 | { |
---|
135 | if (!error) |
---|
136 | { |
---|
137 | Form telForm = new Form(); |
---|
138 | // GET Request |
---|
139 | if (request.getMethod() == Method.GET) |
---|
140 | { |
---|
141 | telForm = new Form(request.getResourceRef().getRemainingPart().split("\\?")[1]); |
---|
142 | } |
---|
143 | // POST Request |
---|
144 | else if (request.getMethod() == Method.POST) |
---|
145 | { |
---|
146 | telForm = request.getEntityAsForm(); |
---|
147 | } |
---|
148 | else |
---|
149 | { |
---|
150 | error = true; |
---|
151 | response.setStatus(Status.SERVER_ERROR_INTERNAL); |
---|
152 | } |
---|
153 | |
---|
154 | if (!error) |
---|
155 | { |
---|
156 | int paramNum = configuration.getParameters().size(); |
---|
157 | |
---|
158 | if (configuration.getProvider().getAuthService() != null) |
---|
159 | { |
---|
160 | paramNum += configuration.getProvider().getAuthService().getParameters().size(); |
---|
161 | } |
---|
162 | |
---|
163 | try |
---|
164 | { |
---|
165 | for (int i = 0; i < paramNum; i++) |
---|
166 | { |
---|
167 | Parameter parameter = null; |
---|
168 | if (i < configuration.getParameters().size()) |
---|
169 | parameter = configuration.getParameters().get(i); |
---|
170 | else |
---|
171 | parameter = configuration.getProvider().getAuthService().getParameters().get( |
---|
172 | i - configuration.getParameters().size()); |
---|
173 | |
---|
174 | if (telForm.getNames().contains(parameter.getCodeName())) |
---|
175 | { |
---|
176 | parameter.setValue(URLDecoder.decode(telForm.getValues(parameter.getCodeName()).replaceAll("\\+", "\\%2B"), "UTF-8")); |
---|
177 | } |
---|
178 | else if (parameter.isRequired()) |
---|
179 | { |
---|
180 | error = true; |
---|
181 | response.setStatus(Status.SERVER_ERROR_INTERNAL); |
---|
182 | log.write("--ERROR: The parameter: \"" + parameter.getName() + "\" is required", 0); |
---|
183 | } |
---|
184 | |
---|
185 | } |
---|
186 | } |
---|
187 | catch (IOException e1) |
---|
188 | { |
---|
189 | log.write("--ERROR: Reading data failed", 0); |
---|
190 | response.setStatus(Status.SERVER_ERROR_INTERNAL); |
---|
191 | error = true; |
---|
192 | } |
---|
193 | |
---|
194 | if (configuration.getProvider().getAuthService() != null) |
---|
195 | { |
---|
196 | // Fill keys table |
---|
197 | try |
---|
198 | { |
---|
199 | connection.fillKeysTable(configuration.getProvider().getAuthService(), keys); |
---|
200 | } |
---|
201 | catch (SQLException e) |
---|
202 | { |
---|
203 | response.setStatus(Status.CLIENT_ERROR_FORBIDDEN); |
---|
204 | log.write("--ERROR: Authentication failed", 0); |
---|
205 | error = true; |
---|
206 | } |
---|
207 | |
---|
208 | String apiKey = configuration.getProvider().getAuthService().getParameter(Configuration.API_KEY).getValue(); |
---|
209 | String signature = configuration.getProvider().getAuthService().getParameter(Configuration.SIGNATURE).getValue(); |
---|
210 | String clientId = configuration.getProvider().getAuthService().getParameter(Configuration.CLIENT_ID).getValue(); |
---|
211 | |
---|
212 | try |
---|
213 | { |
---|
214 | //if (!SecurityHelper.checkAPIKey((String) keys.get(clientId.toUpperCase()), encKey, signature, clientId, log)) |
---|
215 | if (!MD5SecurityHelper.checkAPIKey(clientId, signature, apiKey, (String) keys.get(clientId), log)) |
---|
216 | { |
---|
217 | response.setStatus(Status.CLIENT_ERROR_FORBIDDEN); |
---|
218 | log.write("--ERROR: Authentication failed", 0); |
---|
219 | error = true; |
---|
220 | } |
---|
221 | } |
---|
222 | catch (Exception e) |
---|
223 | { |
---|
224 | response.setStatus(Status.CLIENT_ERROR_FORBIDDEN); |
---|
225 | log.write("--ERROR: Authentication failed", 0); |
---|
226 | log.getStackTrace(e); |
---|
227 | error = true; |
---|
228 | } |
---|
229 | |
---|
230 | } |
---|
231 | if (!error) |
---|
232 | { |
---|
233 | GeoAction action = null; |
---|
234 | |
---|
235 | // Choosing an action --> |
---|
236 | if (configuration.getService().getName().equals(CLOSEST_EDGE)) |
---|
237 | action = new ClosestEdge(configuration, connection, ioHelper, log); |
---|
238 | else if (configuration.getService().getName().equals(SHORTEST_PATH)) |
---|
239 | action = new ShortestPath(configuration, connection, ioHelper, log); |
---|
240 | else if (configuration.getService().getName().equals(SHORTEST_WALK)) |
---|
241 | action = new ShortestPath(configuration, connection, ioHelper, log); |
---|
242 | else if (configuration.getService().getName().equals(DRIVING_DISTANCE)) |
---|
243 | action = new DrivingDistance(configuration, connection, ioHelper, log); |
---|
244 | else if (configuration.getService().getName().equals(TSP)) |
---|
245 | action = new TravelingSalesPerson(configuration, connection, ioHelper, log); |
---|
246 | try |
---|
247 | { |
---|
248 | action.start(); |
---|
249 | } |
---|
250 | catch (NullPointerException e) |
---|
251 | { |
---|
252 | log.write("--ERROR: Invalid action " + action.getResult(), 0); |
---|
253 | log.write(e.getMessage(), 1); |
---|
254 | response.setStatus(Status.SERVER_ERROR_INTERNAL); |
---|
255 | error = true; |
---|
256 | } |
---|
257 | catch (SQLException e) |
---|
258 | { |
---|
259 | log.write("--ERROR: Exception \"SQLException\" to draw the result", 0); |
---|
260 | log.getStackTrace(e); |
---|
261 | response.setStatus(Status.SERVER_ERROR_INTERNAL); |
---|
262 | error = true; |
---|
263 | } |
---|
264 | catch (JSONException e) |
---|
265 | { |
---|
266 | log.write("--ERROR: Exception \"JSONException\" to draw the result", 0); |
---|
267 | log.write(e.getMessage(), 1); |
---|
268 | response.setStatus(Status.SERVER_ERROR_INTERNAL); |
---|
269 | error = true; |
---|
270 | } |
---|
271 | //log.write(action.getResult() + " started", 1); |
---|
272 | result = action.getResult(); |
---|
273 | //log.write(action.getResult() + " finished", 1); |
---|
274 | |
---|
275 | if(result=="") |
---|
276 | { |
---|
277 | response.setStatus(Status.CLIENT_ERROR_REQUESTED_RANGE_NOT_SATISFIABLE); |
---|
278 | error = true; |
---|
279 | } |
---|
280 | // OUTPUT FORMAT --> |
---|
281 | if (configuration.getFormatOut().getName().equals("html")) |
---|
282 | { |
---|
283 | resultFormat = MediaType.TEXT_HTML; |
---|
284 | } |
---|
285 | else if (configuration.getFormatOut().getName().equals("xml")) |
---|
286 | { |
---|
287 | resultFormat = MediaType.TEXT_XML; |
---|
288 | } |
---|
289 | else if (configuration.getFormatOut().getName().equals("gml")) |
---|
290 | { |
---|
291 | resultFormat = MediaType.TEXT_XML; |
---|
292 | } |
---|
293 | else if (configuration.getFormatOut().getName().equals("kml")) |
---|
294 | { |
---|
295 | resultFormat = MediaType.ALL; |
---|
296 | } |
---|
297 | else |
---|
298 | { |
---|
299 | resultFormat = MediaType.TEXT_PLAIN; |
---|
300 | }// <-- FIN |
---|
301 | |
---|
302 | if (!error) |
---|
303 | response.setEntity(result, resultFormat); |
---|
304 | } |
---|
305 | } |
---|
306 | } |
---|
307 | } |
---|
308 | /* |
---|
309 | if (error) |
---|
310 | { |
---|
311 | response.setStatus(Status.SERVER_ERROR_INTERNAL); |
---|
312 | } |
---|
313 | */ |
---|
314 | } |
---|
315 | }; |
---|
316 | component.getDefaultHost().attach("/", restlet); |
---|
317 | component.start(); |
---|
318 | } |
---|
319 | |
---|
320 | /** |
---|
321 | * Launches the WebRouting Service |
---|
322 | * |
---|
323 | * @throws Exception |
---|
324 | */ |
---|
325 | public static void main(String telArgs[]) throws Exception |
---|
326 | { |
---|
327 | new WebRouting(); |
---|
328 | } |
---|
329 | } |
---|