[927] | 1 | // Copyright 2012 Georg-August-Universität Göttingen, Germany |
---|
| 2 | // |
---|
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
---|
| 4 | // you may not use this file except in compliance with the License. |
---|
| 5 | // You may obtain a copy of the License at |
---|
| 6 | // |
---|
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
---|
| 8 | // |
---|
| 9 | // Unless required by applicable law or agreed to in writing, software |
---|
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
---|
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
| 12 | // See the License for the specific language governing permissions and |
---|
| 13 | // limitations under the License. |
---|
| 14 | |
---|
[857] | 15 | package de.ugoe.cs.autoquest.htmlmonitor; |
---|
| 16 | |
---|
[879] | 17 | import java.io.BufferedReader; |
---|
[1019] | 18 | import java.io.FileNotFoundException; |
---|
[857] | 19 | import java.io.IOException; |
---|
[879] | 20 | import java.io.InputStream; |
---|
[857] | 21 | import java.io.InputStreamReader; |
---|
[879] | 22 | import java.io.PrintWriter; |
---|
[857] | 23 | import java.net.MalformedURLException; |
---|
| 24 | import java.net.URL; |
---|
| 25 | import java.util.ArrayList; |
---|
| 26 | import java.util.List; |
---|
| 27 | import java.util.Map; |
---|
| 28 | import java.util.Set; |
---|
| 29 | |
---|
| 30 | import javax.servlet.ServletException; |
---|
| 31 | import javax.servlet.http.HttpServletRequest; |
---|
| 32 | import javax.servlet.http.HttpServletResponse; |
---|
| 33 | |
---|
| 34 | import org.json.simple.JSONArray; |
---|
| 35 | import org.json.simple.JSONObject; |
---|
| 36 | import org.json.simple.JSONValue; |
---|
| 37 | import org.json.simple.parser.ParseException; |
---|
| 38 | import org.mortbay.jetty.servlet.DefaultServlet; |
---|
| 39 | |
---|
[1019] | 40 | import de.ugoe.cs.util.FileTools; |
---|
[857] | 41 | import de.ugoe.cs.util.console.Console; |
---|
| 42 | |
---|
| 43 | /** |
---|
| 44 | * <p> |
---|
[879] | 45 | * the servlet deployed in the web server that receives all client messages and returns the client |
---|
| 46 | * java script. The messages are parsed, validated, and forwarded to the provided message listener. |
---|
| 47 | * If a message is not valid, it is discarded. If an event in a message is not valid, it is |
---|
| 48 | * discarded. Messages are only received via the POST HTTP method. The GET HTTP method is only |
---|
| 49 | * implemented for returning the client java script. |
---|
[857] | 50 | * </p> |
---|
| 51 | * |
---|
| 52 | * @author Patrick Harms |
---|
| 53 | */ |
---|
[871] | 54 | class HtmlMonitorServlet extends DefaultServlet { |
---|
[857] | 55 | |
---|
| 56 | /** */ |
---|
| 57 | private static final long serialVersionUID = 1L; |
---|
| 58 | |
---|
[879] | 59 | /** */ |
---|
| 60 | private static final boolean DO_TRACE = false; |
---|
| 61 | |
---|
[857] | 62 | /** |
---|
[1019] | 63 | * <p> |
---|
| 64 | * Name and path of the robot filter. |
---|
| 65 | * </p> |
---|
| 66 | */ |
---|
| 67 | private static final String ROBOTFILTERFILE = "data/robots/robotfilter.txt"; |
---|
| 68 | |
---|
| 69 | /** |
---|
| 70 | * <p> |
---|
| 71 | * Field that contains a regular expression that matches all robots |
---|
| 72 | * contained in {@link #ROBOTFILTERFILE}. |
---|
| 73 | * </p> |
---|
| 74 | */ |
---|
| 75 | private String robotRegex = null; |
---|
| 76 | |
---|
| 77 | /** |
---|
[871] | 78 | * the message listener to forward received messages to. |
---|
[857] | 79 | */ |
---|
[873] | 80 | private transient HtmlMonitorMessageListener messageListener; |
---|
[857] | 81 | |
---|
| 82 | /** |
---|
| 83 | * <p> |
---|
[871] | 84 | * initializes the servlet with the message listener to which all events shall be forwarded |
---|
[857] | 85 | * </p> |
---|
| 86 | * |
---|
[871] | 87 | * @param messageListener the message listener that shall receive all client events |
---|
[857] | 88 | */ |
---|
| 89 | HtmlMonitorServlet(HtmlMonitorMessageListener messageListener) { |
---|
| 90 | this.messageListener = messageListener; |
---|
[1019] | 91 | try { |
---|
| 92 | loadRobotRegex(); |
---|
| 93 | } |
---|
| 94 | catch (Exception e) { |
---|
| 95 | Console.println |
---|
| 96 | ("robot filtering disabled: could not parse robot filter file " + ROBOTFILTERFILE); |
---|
| 97 | } |
---|
[857] | 98 | } |
---|
| 99 | |
---|
| 100 | /* (non-Javadoc) |
---|
[879] | 101 | * @see org.mortbay.jetty.servlet.DefaultServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse) |
---|
| 102 | */ |
---|
| 103 | @Override |
---|
| 104 | protected void doGet(HttpServletRequest request, HttpServletResponse response) |
---|
| 105 | throws ServletException, IOException |
---|
| 106 | { |
---|
| 107 | if ((request.getPathInfo() != null) && |
---|
| 108 | (request.getPathInfo().endsWith("/script/autoquest-htmlmonitor.js"))) |
---|
| 109 | { |
---|
[884] | 110 | BufferedReader reader = null; |
---|
[879] | 111 | |
---|
| 112 | try { |
---|
[884] | 113 | InputStream script = this.getClass().getClassLoader().getResourceAsStream |
---|
[879] | 114 | ("autoquest-htmlmonitor.js"); |
---|
[1019] | 115 | |
---|
[879] | 116 | if (script == null) { |
---|
| 117 | Console.printerrln("could not read autoquest-htmlmonitor.js from classpath"); |
---|
| 118 | } |
---|
| 119 | else { |
---|
[884] | 120 | reader = new BufferedReader(new InputStreamReader(script, "UTF-8")); |
---|
[879] | 121 | PrintWriter output = response.getWriter(); |
---|
| 122 | String line; |
---|
| 123 | |
---|
| 124 | while ((line = reader.readLine()) != null) { |
---|
| 125 | output.println(line); |
---|
| 126 | } |
---|
| 127 | |
---|
| 128 | output.close(); |
---|
| 129 | } |
---|
| 130 | } |
---|
| 131 | catch (Exception e) { |
---|
| 132 | Console.printerrln("could not read autoquest-htmlmonitor.js from classpath: " + e); |
---|
| 133 | Console.logException(e); |
---|
| 134 | } |
---|
| 135 | finally { |
---|
[884] | 136 | if (reader != null) { |
---|
| 137 | reader.close(); |
---|
[879] | 138 | } |
---|
| 139 | } |
---|
| 140 | } |
---|
| 141 | } |
---|
| 142 | |
---|
| 143 | /* (non-Javadoc) |
---|
[857] | 144 | * @see org.mortbay.jetty.servlet.DefaultServlet#doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse) |
---|
| 145 | */ |
---|
| 146 | @Override |
---|
| 147 | protected void doPost(HttpServletRequest request, HttpServletResponse response) |
---|
| 148 | throws ServletException, IOException |
---|
| 149 | { |
---|
| 150 | Object value = null; |
---|
| 151 | try { |
---|
[1019] | 152 | //InputStream requestInputStream = dumpStreamContent(request.getInputStream()); |
---|
[998] | 153 | InputStream requestInputStream = request.getInputStream(); |
---|
[1019] | 154 | |
---|
[872] | 155 | value = JSONValue.parseWithException |
---|
[998] | 156 | (new InputStreamReader(requestInputStream, "UTF-8")); |
---|
[857] | 157 | |
---|
| 158 | if (!(value instanceof JSONObject)) { |
---|
| 159 | Console.printerrln("incoming data is not of the expected type --> discarding it"); |
---|
| 160 | } |
---|
| 161 | else { |
---|
| 162 | handleJSONObject((JSONObject) value); |
---|
| 163 | } |
---|
| 164 | } |
---|
| 165 | catch (ParseException e) { |
---|
| 166 | Console.printerrln |
---|
| 167 | ("could not parse incoming data --> discarding it (" + e.toString() + ")"); |
---|
| 168 | } |
---|
| 169 | } |
---|
| 170 | |
---|
| 171 | /** |
---|
| 172 | * <p> |
---|
[871] | 173 | * processes a received JSON object and validates it. If the message is ok, it is forwarded |
---|
| 174 | * to the message listener |
---|
[857] | 175 | * </p> |
---|
| 176 | * |
---|
[871] | 177 | * @param object the JSON object that contains a client message |
---|
[857] | 178 | */ |
---|
| 179 | private void handleJSONObject(JSONObject object) { |
---|
[879] | 180 | if (DO_TRACE) { |
---|
| 181 | dumpJSONObject(object, ""); |
---|
| 182 | } |
---|
[857] | 183 | |
---|
| 184 | JSONObject message = assertValue(object, "message", JSONObject.class); |
---|
| 185 | |
---|
| 186 | if (message == null) { |
---|
| 187 | Console.printerrln("incoming data is no valid message --> discarding it"); |
---|
| 188 | } |
---|
| 189 | else { |
---|
| 190 | HtmlClientInfos clientInfos = extractClientInfos(message); |
---|
| 191 | |
---|
| 192 | if (clientInfos == null) { |
---|
| 193 | Console.printerrln |
---|
| 194 | ("incoming message does not contain valid client infos --> discarding it"); |
---|
| 195 | } |
---|
[1019] | 196 | else if (isRobot(clientInfos.getUserAgent())) { |
---|
| 197 | Console.printerrln |
---|
| 198 | ("ignoring robot " + clientInfos.getUserAgent()); |
---|
| 199 | } |
---|
[857] | 200 | else { |
---|
[1019] | 201 | HtmlPageElement guiStructure = extractHtmlPageElements(message, clientInfos); |
---|
[857] | 202 | HtmlEvent[] events = extractHtmlEvents(message, clientInfos); |
---|
[1019] | 203 | |
---|
[857] | 204 | if (events == null) { |
---|
| 205 | Console.printerrln |
---|
[998] | 206 | ("incoming message does not contain valid events --> discarding it"); |
---|
[857] | 207 | } |
---|
| 208 | else { |
---|
[1019] | 209 | messageListener.handleMessage(clientInfos, guiStructure, events); |
---|
[857] | 210 | } |
---|
| 211 | } |
---|
| 212 | } |
---|
| 213 | } |
---|
| 214 | |
---|
| 215 | /** |
---|
| 216 | * <p> |
---|
[871] | 217 | * tries to extract the client infos out of the received JSON object. If this is not fully |
---|
| 218 | * possible, an appropriate message is dumped and the whole message is discarded (the method |
---|
| 219 | * return null). |
---|
[857] | 220 | * </p> |
---|
| 221 | * |
---|
[871] | 222 | * @param message the message to parse the client infos from |
---|
| 223 | * |
---|
| 224 | * @return the client infos, if the message is valid in this respect, or null if not |
---|
[857] | 225 | */ |
---|
| 226 | private HtmlClientInfos extractClientInfos(JSONObject message) { |
---|
| 227 | HtmlClientInfos clientInfos = null; |
---|
| 228 | |
---|
| 229 | JSONObject infos = assertValue(message, "clientInfos", JSONObject.class); |
---|
| 230 | |
---|
| 231 | if (infos != null) { |
---|
| 232 | String clientId = assertValue((JSONObject) infos, "clientId", String.class); |
---|
| 233 | String userAgent = assertValue((JSONObject) infos, "userAgent", String.class); |
---|
| 234 | URL url = assertValue((JSONObject) infos, "url", URL.class); |
---|
| 235 | String title = assertValue((JSONObject) infos, "title", String.class); |
---|
| 236 | |
---|
| 237 | if (clientId == null) { |
---|
| 238 | Console.printerrln("client infos do not contain a valid client id"); |
---|
| 239 | } |
---|
| 240 | else if (userAgent == null) { |
---|
| 241 | Console.printerrln("client infos do not contain a valid user agent"); |
---|
| 242 | } |
---|
| 243 | else if (url == null) { |
---|
| 244 | Console.printerrln("client infos do not contain a valid URL"); |
---|
| 245 | } |
---|
| 246 | else if (title == null) { |
---|
| 247 | Console.printerrln("client infos do not contain a valid title"); |
---|
| 248 | } |
---|
| 249 | else { |
---|
| 250 | clientInfos = new HtmlClientInfos(clientId, userAgent, url, title); |
---|
| 251 | } |
---|
| 252 | } |
---|
| 253 | |
---|
| 254 | return clientInfos; |
---|
| 255 | } |
---|
| 256 | |
---|
| 257 | /** |
---|
| 258 | * <p> |
---|
[871] | 259 | * tries to extract the events out of the received JSON object. If this is not fully |
---|
| 260 | * possible, an appropriate message is dumped and the errorprone event is discarded. If no |
---|
| 261 | * valid event is found, the whole message is discarded. |
---|
[857] | 262 | * </p> |
---|
| 263 | * |
---|
[871] | 264 | * @param object the message to parse the events from |
---|
| 265 | * @param clientInfos the infos about the client that send the events |
---|
| 266 | * |
---|
| 267 | * @return the valid events stored in the message, or null if there are none |
---|
[857] | 268 | */ |
---|
| 269 | private HtmlEvent[] extractHtmlEvents(JSONObject object, HtmlClientInfos clientInfos) { |
---|
| 270 | List<HtmlEvent> events = null; |
---|
| 271 | |
---|
| 272 | JSONArray eventArray = assertValue(object, "events", JSONArray.class); |
---|
| 273 | |
---|
| 274 | if (eventArray != null) { |
---|
| 275 | events = new ArrayList<HtmlEvent>(); |
---|
| 276 | |
---|
[1019] | 277 | HtmlPageElement server = getServerElement(clientInfos); |
---|
| 278 | HtmlPageElement page = getPageElementRepresentingWebPage(clientInfos, server); |
---|
| 279 | |
---|
[857] | 280 | for (int i = 0; i < eventArray.size(); i++) { |
---|
| 281 | Object eventObj = eventArray.get(i); |
---|
| 282 | if (!(eventObj instanceof JSONObject)) { |
---|
| 283 | Console.printerrln("event number " + (i + 1) + " is not a valid event object"); |
---|
| 284 | } |
---|
| 285 | else { |
---|
| 286 | Long time = assertValue(((JSONObject) eventObj), "time", Long.class); |
---|
| 287 | String path = assertValue(((JSONObject) eventObj), "path", String.class); |
---|
| 288 | String eventType = |
---|
| 289 | assertValue(((JSONObject) eventObj), "eventType", String.class); |
---|
| 290 | Integer[] coordinates = |
---|
| 291 | assertValue(((JSONObject) eventObj), "coordinates", Integer[].class); |
---|
| 292 | Integer key = assertValue(((JSONObject) eventObj), "key", Integer.class); |
---|
[942] | 293 | Integer[] scrollPosition = |
---|
| 294 | assertValue(((JSONObject) eventObj), "scrollPosition", Integer[].class); |
---|
| 295 | String selectedValue = |
---|
| 296 | assertValue(((JSONObject) eventObj), "selectedValue", String.class); |
---|
[857] | 297 | |
---|
[998] | 298 | if (eventType == null) { |
---|
| 299 | Console.printerrln("event number " + (i + 1) + " has no valid event type"); |
---|
[857] | 300 | } |
---|
[998] | 301 | else if (time == null) { |
---|
| 302 | Console.printerrln(eventType + " event has no valid timestamp"); |
---|
| 303 | } |
---|
[857] | 304 | else if (path == null) { |
---|
[998] | 305 | Console.printerrln(eventType + " event has no valid path"); |
---|
[857] | 306 | } |
---|
| 307 | else if ((coordinates != null) && (coordinates.length != 2)) { |
---|
[998] | 308 | Console.printerrln(eventType + " event has no valid coordinates"); |
---|
[857] | 309 | } |
---|
| 310 | else if (checkEventParameterCombinations |
---|
[942] | 311 | (eventType, coordinates, key, scrollPosition, selectedValue)) |
---|
[857] | 312 | { |
---|
[1019] | 313 | path = page.getPath() + path; |
---|
| 314 | events.add(new HtmlEvent(clientInfos, time, path, eventType, |
---|
| 315 | coordinates, key, scrollPosition, selectedValue)); |
---|
[857] | 316 | } |
---|
| 317 | else { |
---|
[998] | 318 | Console.printerrln(eventType + " event has no valid parameter combination"); |
---|
[857] | 319 | } |
---|
| 320 | } |
---|
| 321 | } |
---|
| 322 | |
---|
| 323 | } |
---|
| 324 | |
---|
| 325 | if ((events != null) && (events.size() > 0)) { |
---|
| 326 | return events.toArray(new HtmlEvent[events.size()]); |
---|
| 327 | } |
---|
| 328 | else { |
---|
| 329 | return null; |
---|
| 330 | } |
---|
| 331 | } |
---|
| 332 | |
---|
| 333 | /** |
---|
| 334 | * <p> |
---|
[1019] | 335 | * TODO: comment |
---|
| 336 | * </p> |
---|
| 337 | * |
---|
| 338 | * @param message |
---|
| 339 | * @param clientInfos |
---|
| 340 | * @return |
---|
| 341 | */ |
---|
| 342 | private HtmlPageElement extractHtmlPageElements(JSONObject object, |
---|
| 343 | HtmlClientInfos clientInfos) |
---|
| 344 | { |
---|
| 345 | HtmlPageElement server = getServerElement(clientInfos); |
---|
| 346 | HtmlPageElement page = getPageElementRepresentingWebPage(clientInfos, server); |
---|
| 347 | |
---|
| 348 | JSONObject jsonPageElement = assertValue(object, "guiModel", JSONObject.class); |
---|
| 349 | page.addChild(convert(jsonPageElement, page.getPath())); |
---|
| 350 | |
---|
| 351 | return server; |
---|
| 352 | } |
---|
| 353 | |
---|
| 354 | /** |
---|
| 355 | * <p> |
---|
| 356 | * TODO: comment |
---|
| 357 | * </p> |
---|
| 358 | * |
---|
| 359 | * @param clientInfos |
---|
| 360 | * @return |
---|
| 361 | */ |
---|
| 362 | private HtmlPageElement getServerElement(HtmlClientInfos clientInfos) { |
---|
| 363 | String id = clientInfos.getUrl().getHost(); |
---|
| 364 | if (clientInfos.getUrl().getPort() > -1) { |
---|
| 365 | id += ":" + clientInfos.getUrl().getPort(); |
---|
| 366 | } |
---|
| 367 | |
---|
| 368 | return new HtmlPageElement(null, "server", id, 0); |
---|
| 369 | } |
---|
| 370 | |
---|
| 371 | /** |
---|
| 372 | * <p> |
---|
| 373 | * TODO: comment |
---|
| 374 | * </p> |
---|
| 375 | * |
---|
| 376 | * @param clientInfos |
---|
| 377 | */ |
---|
| 378 | private HtmlPageElement getPageElementRepresentingWebPage(HtmlClientInfos clientInfos, |
---|
| 379 | HtmlPageElement server) |
---|
| 380 | { |
---|
| 381 | String id = clientInfos.getUrl().getPath(); |
---|
| 382 | |
---|
| 383 | if (clientInfos.getUrl().getQuery() != null) { |
---|
| 384 | id += "?" + clientInfos.getUrl().getQuery(); |
---|
| 385 | } |
---|
| 386 | |
---|
| 387 | HtmlPageElement page = |
---|
| 388 | new HtmlPageElement(server.getPath(), "document", id, clientInfos.getTitle(), 0); |
---|
| 389 | |
---|
| 390 | server.addChild(page); |
---|
| 391 | |
---|
| 392 | return page; |
---|
| 393 | } |
---|
| 394 | |
---|
| 395 | /** |
---|
| 396 | * <p> |
---|
| 397 | * TODO: comment |
---|
| 398 | * </p> |
---|
| 399 | * |
---|
| 400 | * @param jsonPageElement |
---|
| 401 | * @return |
---|
| 402 | */ |
---|
| 403 | private HtmlPageElement convert(JSONObject jsonPageElement, String parentPath) { |
---|
| 404 | HtmlPageElement result = null; |
---|
| 405 | |
---|
| 406 | if (jsonPageElement != null) { |
---|
| 407 | String tagName = assertValue(jsonPageElement, "tagName", String.class); |
---|
| 408 | String id = assertValue(jsonPageElement, "id", String.class); |
---|
| 409 | Integer index = assertValue(jsonPageElement, "index", Integer.class); |
---|
| 410 | |
---|
| 411 | result = new HtmlPageElement(parentPath, tagName, id, index); |
---|
| 412 | |
---|
| 413 | JSONArray childElements = assertValue(jsonPageElement, "children", JSONArray.class); |
---|
| 414 | |
---|
| 415 | if (childElements != null) { |
---|
| 416 | Object jsonChild; |
---|
| 417 | |
---|
| 418 | for (int i = 0; i < childElements.size(); i++) { |
---|
| 419 | jsonChild = childElements.get(i); |
---|
| 420 | if (!(jsonChild instanceof JSONObject)) { |
---|
| 421 | Console.printerrln("child " + (i + 1) + " of HTML page element " + tagName + |
---|
| 422 | " is no valid HTML page element"); |
---|
| 423 | } |
---|
| 424 | else { |
---|
| 425 | result.addChild(convert((JSONObject) jsonChild, result.getPath())); |
---|
| 426 | } |
---|
| 427 | } |
---|
| 428 | } |
---|
| 429 | |
---|
| 430 | } |
---|
| 431 | |
---|
| 432 | return result; |
---|
| 433 | } |
---|
| 434 | |
---|
| 435 | /** |
---|
| 436 | * <p> |
---|
[942] | 437 | * validates if for the given event type the parameter combination of coordinates, key, |
---|
| 438 | * scroll position, and selected value is valid. As an example, an onclick event should |
---|
| 439 | * usually not have an associated scroll position. |
---|
[857] | 440 | * </p> |
---|
| 441 | * |
---|
[871] | 442 | * @param eventType the type of the event |
---|
| 443 | * @param coordinates the coordinates of the event |
---|
| 444 | * @param key the key of the event |
---|
| 445 | * @param scrollPosition the scroll position of the event |
---|
[942] | 446 | * @param selectedValue the value selected through a specific event |
---|
[871] | 447 | * |
---|
| 448 | * @return true, if the combination of the parameters is valid, false else |
---|
[857] | 449 | */ |
---|
| 450 | private boolean checkEventParameterCombinations(String eventType, |
---|
| 451 | Integer[] coordinates, |
---|
| 452 | Integer key, |
---|
[942] | 453 | Integer[] scrollPosition, |
---|
| 454 | String selectedValue) |
---|
[857] | 455 | { |
---|
| 456 | boolean result = false; |
---|
| 457 | |
---|
| 458 | if ("onscroll".equals(eventType)) { |
---|
[942] | 459 | if ((coordinates == null) && (key == null) && |
---|
| 460 | (scrollPosition != null) && (selectedValue == null)) |
---|
| 461 | { |
---|
[857] | 462 | result = true; |
---|
| 463 | } |
---|
| 464 | else { |
---|
| 465 | Console.printerrln(eventType + " event has invalid parameters"); |
---|
| 466 | } |
---|
| 467 | } |
---|
| 468 | else if ("onclick".equals(eventType) || "ondblclick".equals(eventType)) { |
---|
[942] | 469 | if ((coordinates != null) && (key == null) && |
---|
| 470 | (scrollPosition == null) && (selectedValue == null)) |
---|
| 471 | { |
---|
[857] | 472 | result = true; |
---|
| 473 | } |
---|
| 474 | else { |
---|
| 475 | Console.printerrln(eventType + " event has invalid parameters"); |
---|
| 476 | } |
---|
| 477 | } |
---|
[942] | 478 | else if ("onchange".equals(eventType)) { |
---|
| 479 | if ((coordinates == null) && (key == null) && |
---|
| 480 | (scrollPosition == null) && (selectedValue != null)) |
---|
| 481 | { |
---|
| 482 | result = true; |
---|
| 483 | } |
---|
| 484 | else { |
---|
| 485 | Console.printerrln(eventType + " event has invalid parameters"); |
---|
| 486 | } |
---|
| 487 | } |
---|
[857] | 488 | else if ("onkeypress".equals(eventType) || "onkeydown".equals(eventType) || |
---|
| 489 | "onkeyup".equals(eventType)) |
---|
| 490 | { |
---|
[942] | 491 | if ((coordinates == null) && (key != null) && |
---|
| 492 | (scrollPosition == null) && (selectedValue == null)) |
---|
| 493 | { |
---|
[857] | 494 | result = true; |
---|
| 495 | } |
---|
| 496 | else { |
---|
| 497 | Console.printerrln(eventType + " event has invalid parameters"); |
---|
| 498 | } |
---|
| 499 | } |
---|
| 500 | else if ("onfocus".equals(eventType) || "onmouseout".equals(eventType) || |
---|
[998] | 501 | "onmousemove".equals(eventType) || "onload".equals(eventType) || |
---|
| 502 | "onunload".equals(eventType) || "onbeforeunload".equals(eventType) || |
---|
| 503 | "onpagehide".equals(eventType) || "onpageshow".equals(eventType) || |
---|
| 504 | "onabort".equals(eventType) || "onsubmit".equals(eventType) || |
---|
| 505 | "onplaying".equals(eventType) || "onpause".equals(eventType) || |
---|
| 506 | "ontimeupdate".equals(eventType) || "onerror".equals(eventType) || |
---|
| 507 | "onundo".equals(eventType) || "onreset".equals(eventType) || |
---|
| 508 | "onselect".equals(eventType)) |
---|
[857] | 509 | { |
---|
[942] | 510 | if ((coordinates == null) && (key == null) && |
---|
| 511 | (scrollPosition == null) && (selectedValue == null)) |
---|
| 512 | { |
---|
[857] | 513 | result = true; |
---|
| 514 | } |
---|
| 515 | else { |
---|
| 516 | Console.printerrln(eventType + " event has invalid parameters"); |
---|
| 517 | } |
---|
| 518 | } |
---|
| 519 | else { |
---|
| 520 | Console.printerrln("'" + eventType + "' is not a valid event type"); |
---|
| 521 | } |
---|
| 522 | |
---|
| 523 | return result; |
---|
| 524 | } |
---|
| 525 | |
---|
| 526 | /** |
---|
| 527 | * <p> |
---|
[871] | 528 | * converts a value in the provided object matching the provided key to the provided type. If |
---|
| 529 | * there is no value with the key or if the value can not be transformed to the provided type, |
---|
| 530 | * the method returns null. |
---|
[857] | 531 | * </p> |
---|
| 532 | * |
---|
[871] | 533 | * @param object the object to read the value from |
---|
| 534 | * @param key the key of the value |
---|
| 535 | * @param clazz the type to which the value should be transformed |
---|
| 536 | * |
---|
| 537 | * @return the value or null if either the value does not exist or if it can not be transformed |
---|
| 538 | * to the expected type |
---|
[857] | 539 | */ |
---|
| 540 | @SuppressWarnings("unchecked") |
---|
| 541 | private <T> T assertValue(JSONObject object, String key, Class<T> clazz) { |
---|
| 542 | Object value = object.get(key); |
---|
| 543 | T result = null; |
---|
| 544 | |
---|
| 545 | if (clazz.isInstance(value)) { |
---|
| 546 | result = (T) value; |
---|
| 547 | } |
---|
| 548 | else if (value instanceof String) { |
---|
| 549 | if (URL.class.equals(clazz)) { |
---|
| 550 | try { |
---|
| 551 | result = (T) new URL((String) value); |
---|
| 552 | } |
---|
| 553 | catch (MalformedURLException e) { |
---|
| 554 | e.printStackTrace(); |
---|
| 555 | Console.printerrln("retrieved malformed URL for key '" + key + "': " + value + |
---|
| 556 | " (" + e.toString() + ")"); |
---|
| 557 | } |
---|
| 558 | } |
---|
| 559 | else if ((int.class.equals(clazz)) || (Integer.class.equals(clazz))) { |
---|
| 560 | try { |
---|
[872] | 561 | result = (T) Integer.valueOf(Integer.parseInt((String) value)); |
---|
[857] | 562 | } |
---|
| 563 | catch (NumberFormatException e) { |
---|
| 564 | Console.printerrln |
---|
| 565 | ("retrieved malformed integer for key '" + key + "': " + value); |
---|
| 566 | } |
---|
| 567 | } |
---|
| 568 | else if ((long.class.equals(clazz)) || (Long.class.equals(clazz))) { |
---|
| 569 | try { |
---|
[872] | 570 | result = (T) Long.valueOf(Long.parseLong((String) value)); |
---|
[857] | 571 | } |
---|
| 572 | catch (NumberFormatException e) { |
---|
| 573 | Console.printerrln |
---|
| 574 | ("retrieved malformed long for key '" + key + "': " + value); |
---|
| 575 | } |
---|
| 576 | } |
---|
| 577 | } |
---|
| 578 | else if (value instanceof Long) { |
---|
| 579 | if ((int.class.equals(clazz)) || (Integer.class.equals(clazz))) { |
---|
| 580 | result = (T) (Integer) ((Long) value).intValue(); |
---|
| 581 | } |
---|
| 582 | } |
---|
| 583 | else if (value instanceof JSONArray) { |
---|
| 584 | if ((int[].class.equals(clazz)) || (Integer[].class.equals(clazz))) { |
---|
| 585 | Integer[] resultArray = new Integer[((JSONArray) value).size()]; |
---|
| 586 | boolean allCouldBeParsed = true; |
---|
| 587 | |
---|
| 588 | for (int i = 0; i < ((JSONArray) value).size(); i++) { |
---|
| 589 | try { |
---|
| 590 | if (((JSONArray) value).get(i) instanceof Long) { |
---|
| 591 | resultArray[i] = (int) (long) (Long) ((JSONArray) value).get(i); |
---|
| 592 | } |
---|
| 593 | else if (((JSONArray) value).get(i) instanceof String) { |
---|
| 594 | try { |
---|
| 595 | resultArray[i] = |
---|
| 596 | (int) Long.parseLong((String) ((JSONArray) value).get(i)); |
---|
| 597 | } |
---|
| 598 | catch (NumberFormatException e) { |
---|
| 599 | Console.printerrln |
---|
| 600 | ("retrieved malformed integer array for key '" + key + "': " + |
---|
| 601 | value); |
---|
| 602 | |
---|
| 603 | allCouldBeParsed = false; |
---|
| 604 | break; |
---|
| 605 | } |
---|
| 606 | } |
---|
| 607 | else { |
---|
| 608 | Console.printerrln |
---|
| 609 | ("can not handle type of value in expected integer array '" + key + |
---|
| 610 | "': " + value); |
---|
| 611 | } |
---|
| 612 | } |
---|
| 613 | catch (ClassCastException e) { |
---|
| 614 | e.printStackTrace(); |
---|
| 615 | Console.printerrln("expected integer array for key '" + key + |
---|
| 616 | "' but it was something else: " + value); |
---|
| 617 | |
---|
| 618 | allCouldBeParsed = false; |
---|
| 619 | break; |
---|
| 620 | } |
---|
| 621 | } |
---|
| 622 | |
---|
| 623 | if (allCouldBeParsed) { |
---|
| 624 | result = (T) resultArray; |
---|
| 625 | } |
---|
| 626 | } |
---|
| 627 | } |
---|
| 628 | |
---|
| 629 | return result; |
---|
| 630 | } |
---|
| 631 | |
---|
| 632 | /** |
---|
| 633 | * <p> |
---|
[1019] | 634 | * Checks whether an agent is a robot. |
---|
| 635 | * </p> |
---|
| 636 | * |
---|
| 637 | * @param agent |
---|
| 638 | * agent that is checked |
---|
| 639 | * @return true, if the agent is a robot; false otherwise |
---|
| 640 | */ |
---|
| 641 | private boolean isRobot(String agent) { |
---|
| 642 | return agent.matches(robotRegex); |
---|
| 643 | } |
---|
| 644 | |
---|
| 645 | /** |
---|
| 646 | * <p> |
---|
| 647 | * Reads {@link #ROBOTFILTERFILE} and creates a regular expression that |
---|
| 648 | * matches all the robots defined in the file. The regular expression is |
---|
| 649 | * stored in the field {@link #robotRegex}. |
---|
| 650 | * </p> |
---|
| 651 | * |
---|
| 652 | * @throws IOException |
---|
| 653 | * thrown if there is a problem reading the robot filter |
---|
| 654 | * @throws FileNotFoundException |
---|
| 655 | * thrown if the robot filter is not found |
---|
| 656 | */ |
---|
| 657 | private void loadRobotRegex() throws IOException, FileNotFoundException { |
---|
| 658 | String[] lines = FileTools.getLinesFromFile(ROBOTFILTERFILE); |
---|
| 659 | StringBuilder regex = new StringBuilder(); |
---|
| 660 | for (int i = 0; i < lines.length; i++) { |
---|
| 661 | regex.append("(.*" + lines[i] + ".*)"); |
---|
| 662 | if (i != lines.length - 1) { |
---|
| 663 | regex.append('|'); |
---|
| 664 | } |
---|
| 665 | } |
---|
| 666 | robotRegex = regex.toString(); |
---|
| 667 | } |
---|
| 668 | |
---|
| 669 | /** |
---|
| 670 | * <p> |
---|
[998] | 671 | * convenience method for dumping the content of a stream and returning a new stream |
---|
| 672 | * containing the same data. |
---|
| 673 | * </p> |
---|
| 674 | * |
---|
| 675 | * @param inputStream the stream to be dumped and copied |
---|
| 676 | * @return the copy of the stream |
---|
| 677 | * |
---|
| 678 | * @throws IOException if the stream can not be read |
---|
| 679 | */ |
---|
[1019] | 680 | /* private InputStream dumpStreamContent(ServletInputStream inputStream) throws IOException { |
---|
[998] | 681 | List<Byte> bytes = new ArrayList<Byte>(); |
---|
| 682 | int buf; |
---|
| 683 | |
---|
| 684 | while ((buf = inputStream.read()) >= 0) { |
---|
| 685 | bytes.add((byte) buf); |
---|
| 686 | } |
---|
| 687 | |
---|
| 688 | byte[] byteArray = new byte[bytes.size()]; |
---|
| 689 | for (int i = 0; i < bytes.size(); i++) { |
---|
| 690 | byteArray[i] = bytes.get(i); |
---|
| 691 | } |
---|
| 692 | |
---|
| 693 | System.out.println(new String(byteArray, "UTF-8")); |
---|
| 694 | |
---|
| 695 | return new ByteArrayInputStream(byteArray); |
---|
| 696 | }*/ |
---|
| 697 | |
---|
| 698 | /** |
---|
| 699 | * <p> |
---|
[871] | 700 | * convenience method for dumping an object to std out. If the object is a JSON object, it is |
---|
| 701 | * deeply analyzed and its internal structure is dumped as well. |
---|
[857] | 702 | * </p> |
---|
| 703 | * |
---|
[871] | 704 | * @param object the object to dump |
---|
| 705 | * @param indent the indentation to be used. |
---|
[857] | 706 | */ |
---|
| 707 | private void dumpJSONObject(Object object, String indent) { |
---|
| 708 | if (object instanceof JSONArray) { |
---|
| 709 | boolean arrayContainsJSONObjects = false; |
---|
| 710 | for (Object arrayElem : (JSONArray) object) { |
---|
| 711 | if (arrayElem instanceof JSONObject) { |
---|
| 712 | arrayContainsJSONObjects = true; |
---|
| 713 | break; |
---|
| 714 | } |
---|
| 715 | } |
---|
| 716 | |
---|
| 717 | if (arrayContainsJSONObjects) { |
---|
| 718 | System.out.println(); |
---|
| 719 | System.out.print(indent); |
---|
| 720 | System.out.println('['); |
---|
| 721 | System.out.print(indent); |
---|
| 722 | System.out.print(' '); |
---|
| 723 | } |
---|
| 724 | else { |
---|
| 725 | System.out.print(' '); |
---|
| 726 | System.out.print('['); |
---|
| 727 | } |
---|
| 728 | |
---|
| 729 | int index = 0; |
---|
| 730 | for (Object arrayElem : (JSONArray) object) { |
---|
| 731 | if (index++ > 0) { |
---|
| 732 | System.out.print(","); |
---|
| 733 | if (arrayContainsJSONObjects) { |
---|
| 734 | System.out.println(); |
---|
| 735 | System.out.print(indent); |
---|
| 736 | } |
---|
| 737 | |
---|
| 738 | System.out.print(' '); |
---|
| 739 | } |
---|
| 740 | |
---|
| 741 | dumpJSONObject(arrayElem, indent + " "); |
---|
| 742 | } |
---|
| 743 | |
---|
| 744 | if (arrayContainsJSONObjects) { |
---|
| 745 | System.out.println(); |
---|
| 746 | System.out.print(indent); |
---|
| 747 | } |
---|
| 748 | |
---|
| 749 | System.out.print(']'); |
---|
| 750 | } |
---|
| 751 | else if (object instanceof JSONObject) { |
---|
| 752 | System.out.println(" {"); |
---|
| 753 | |
---|
| 754 | @SuppressWarnings("unchecked") |
---|
| 755 | Set<Map.Entry<?,?>> entrySet = ((JSONObject) object).entrySet(); |
---|
| 756 | |
---|
| 757 | int index = 0; |
---|
| 758 | for (Map.Entry<?,?> entry : entrySet) { |
---|
| 759 | if (index++ > 0) { |
---|
| 760 | System.out.println(","); |
---|
| 761 | } |
---|
| 762 | System.out.print(indent); |
---|
| 763 | System.out.print(" \""); |
---|
| 764 | System.out.print(entry.getKey()); |
---|
| 765 | System.out.print("\":"); |
---|
| 766 | dumpJSONObject(entry.getValue(), indent + " "); |
---|
| 767 | } |
---|
| 768 | |
---|
| 769 | System.out.println(); |
---|
| 770 | System.out.print(indent); |
---|
| 771 | System.out.print('}'); |
---|
| 772 | } |
---|
| 773 | else { |
---|
| 774 | System.out.print('"'); |
---|
| 775 | System.out.print(object); |
---|
| 776 | System.out.print('"'); |
---|
| 777 | } |
---|
| 778 | } |
---|
| 779 | |
---|
| 780 | } |
---|