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