private String getRestMethod(JMethod method) throws UnableToCompleteException { String restMethod = null; if (method.getAnnotation(DELETE.class) != null) { restMethod = METHOD_DELETE; } else if (method.getAnnotation(GET.class) != null) { restMethod = METHOD_GET; } else if (method.getAnnotation(HEAD.class) != null) { restMethod = METHOD_HEAD; } else if (method.getAnnotation(OPTIONS.class) != null) { restMethod = METHOD_OPTIONS; } else if (method.getAnnotation(POST.class) != null) { restMethod = METHOD_POST; } else if (method.getAnnotation(PUT.class) != null) { restMethod = METHOD_PUT; } else if (method.getAnnotation(JSONP.class) != null) { restMethod = METHOD_JSONP; } else { restMethod = method.getName(); if (!REST_METHODS.contains(restMethod)) { getLogger() .log( ERROR, "Invalid rest method. It must either have a lower case rest method name or have a javax rs method annotation: " + method.getReadableDeclaration()); throw new UnableToCompleteException(); } } return restMethod; }
static { REST_METHODS.add(METHOD_DELETE); REST_METHODS.add(METHOD_GET); REST_METHODS.add(METHOD_HEAD); REST_METHODS.add(METHOD_OPTIONS); REST_METHODS.add(METHOD_POST); REST_METHODS.add(METHOD_PUT); REST_METHODS.add(METHOD_JSONP); }
// 对接收的(click,mouseover,scroll)数据进行处理,并存入events @Path("/events/store") @POST @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.TEXT_PLAIN) public void StoreEvents(String data, @Context HttpServletRequest request) throws UnsupportedEncodingException, IOException { String schema = "browser,os,url,ip,loadtime,time,element,id,text,semantics,event,left,top,height,width"; HashSet<String> hs_log = new HashSet<String>(); // System.out.println(data); // 读取获得的json数据 JsonReader reader = Json.createReader(new StringReader(data)); JsonObject jsonobj = reader.readObject(); reader.close(); // 获取来源IP,读取url,time,events等信息 String ip = ""; String browser = ""; String os = ""; String url = ""; String loadtime = ""; ip = getRemoteHost(request); browser = jsonobj.getString("browser"); os = jsonobj.getString("os"); url = jsonobj.getString("url").split("\\?")[0]; url = url.replace("http://", "").replace("HTTP://", "").replace("https://", ""); loadtime = jsonobj.getString("loadtime"); JsonArray events = jsonobj.getJsonArray("events"); // 整理数据 for (int i = 0; i < events.size(); i++) { JsonObject obj = events.getJsonObject(i); StringBuffer elebuf = new StringBuffer(); // 先存储头信息 elebuf.append(browser); elebuf.append(","); elebuf.append(os); elebuf.append(","); elebuf.append(url); elebuf.append(","); elebuf.append(ip); elebuf.append(","); elebuf.append(loadtime); elebuf.append(","); // 各个事件的信息 elebuf.append(obj.getString("time")); elebuf.append(","); elebuf.append(obj.getString("element")); elebuf.append(","); try { elebuf.append(obj.getString("id")); } catch (Exception e) { elebuf.append(""); } elebuf.append(","); elebuf.append(obj.getString("text")); elebuf.append(","); // 在这里可以引入SVM标注,标注后在存入数据库 // 或者可以读取数据在标注 elebuf.append(""); // semantics,标注后更新 elebuf.append(","); elebuf.append(obj.getString("event")); elebuf.append(","); elebuf.append(obj.getString("left")); elebuf.append(","); elebuf.append(obj.getString("top")); elebuf.append(","); elebuf.append(obj.getString("height")); elebuf.append(","); elebuf.append(obj.getString("width")); // 添加set中,去重 hs_log.add(elebuf.toString()); } eventsDAO.insertEvents(schema, hs_log); // return "Post Data Success!"; }