// parse and set params public void setParams(String paramString) { if (paramString == null || paramString.isEmpty()) return; JsonReader jsonReader = null; try { jsonReader = javax.json.Json.createReader(new java.io.ByteArrayInputStream(paramString.getBytes())); JsonObject jsonObject = jsonReader.readObject(); jsonReader.close(); String thresholdString = jsonObject.getString("threshold", null); if (thresholdString != null && !thresholdString.isEmpty()) try { threshold = Float.parseFloat(thresholdString); } catch (Exception ex) { } JsonArray paramList = jsonObject.getJsonArray("params"); if (paramList != null) { int mlen = paramList.size(); for (int i = 0; i < mlen; i++) { JsonObject mobj = paramList.getJsonObject(i); params.put(mobj.getString("name"), mobj.getString("value")); } } } catch (Exception ex) { logger.log(Level.WARNING, "Error to parse alert subscription params: " + paramString, ex); } }
private void readJsonStream(InputStream istream, JsonObjectCallback callback) throws IOException, UnsupportedEncodingException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] buffer = new byte[8192]; int count; while ((count = istream.read(buffer)) > 0) { baos.write(buffer, 0, count); String s = new String(baos.toByteArray(), "UTF-8"); JsonReader reader = Json.createReader(new StringReader(s)); JsonObject json = null; try { json = reader.readObject(); } catch (JsonParsingException e) { // just ignore and continue continue; } baos.close(); baos = new ByteArrayOutputStream(); callback.callback(json); } }
@Override public List<Employee> readEmployees(String infile) { List<Employee> emps = new ArrayList<>(); Employee emp; try (InputStream is = new FileInputStream(infile); JsonReader reader = Json.createReader(is)) { JsonObject obj = reader.readObject(); JsonArray empArr = obj.getJsonArray("Employees"); for (JsonObject empObj : empArr.getValuesAs(JsonObject.class)) { emp = new Employee(); emp.setId(empObj.getInt("ID", -1)); emp.setFirstName(empObj.getString("FirstName", null)); emp.setLastName(empObj.getString("LastName", null)); emp.setDepartmentNumber(empObj.getInt("DepartmentNumber", -1)); emp.setLocationId(empObj.getInt("LocationID", -1)); if (emp.getId() > 0 && emp.getFirstName() != null && emp.getLastName() != null) emps.add(emp); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (JsonParsingException jpe) { System.err.println("Json file is malformed."); System.err.println(jpe.getMessage()); System.err.println(jpe.getStackTrace()); } return emps; }
/** * Parse a json message collection to create a Message * * @param arg json collection * @return Message */ public static Message readMessage(String arg) { JsonReader jsonReader = Json.createReader(new StringReader(arg)); JsonObject msgValues = jsonReader.readObject(); jsonReader.close(); if (null == msgValues.getJsonArray(ATTACHMENTS)) { return new Message( msgValues.getString(ID), msgValues.getString(SENDER), msgValues.getString(TOPIC), msgValues.getJsonNumber(TIMESTAMP).longValue(), msgValues.getString(CONTENT)); } else { JsonArray attachmentsJson = msgValues.getJsonArray(ATTACHMENTS); Byte[] attachmentByte = new Byte[attachmentsJson.size()]; for (int i = 0; i < attachmentByte.length; i++) { attachmentByte[i] = Byte.valueOf(attachmentsJson.getString(i)); } return new Message( msgValues.getString(ID), msgValues.getString(SENDER), msgValues.getString(TOPIC), msgValues.getJsonNumber(TIMESTAMP).longValue(), msgValues.getString(CONTENT), attachmentByte); } }
public JsonObject processInput(byte[] input) { ByteArrayInputStream byteInStream = new ByteArrayInputStream(input); JsonReader jsonReader = Json.createReader(byteInStream); JsonObject ob = jsonReader.readObject(); PieLogger.info(this.getClass(), String.format("ConnectionText: %s", ob.toString())); return ob; }
private FlickrPhoto buildInfo(JsonReader jsonReader) { JsonObject jobj = jsonReader.readObject(); JsonObject object = (JsonObject) jobj.get("photo"); String id = object.getString("id"); FlickrPhoto p = photos.getPhoto(id); if (p == null) { p = new FlickrPhoto(); p.id = id; photos.setPhoto(p); } p.id = id; // JsonArray array = (JsonArray) object.get("dates"); object = (JsonObject) object.get("dates"); logger.debug("date=" + object.getString("taken")); logger.debug( "dateTaken=" + FlickrPhotoDate.setDateInFlickrTextFormat(object.getString("taken"))); p.dateTaken = FlickrPhotoDate.setDateInFlickrTextFormat(object.getString("taken")); logger.debug("DateTaken=" + p.dateTaken); return p; }
@Override public Photo readFrom( Class<Photo> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException, WebApplicationException { try (JsonReader in = Json.createReader(entityStream)) { JsonObject jsonPhoto = in.readObject(); Photo p = new Photo(); p.setId(jsonPhoto.getInt("id")); p.setTitle(jsonPhoto.getString("title", null)); p.setUrl(jsonPhoto.getString("url", null)); p.setuComment(jsonPhoto.getString("uComment", null)); p.setDate(new Date(jsonPhoto.getJsonNumber("date").longValue())); User u = em.find(User.class, jsonPhoto.getString("uId")); p.setUser(u); return p; } catch (JsonException | ClassCastException ex) { throw new BadRequestException("Ongeldige JSON invoer"); } }
public ListItemReader<JsonObject> setResource() throws MalformedURLException { // Date date = new Date(); // String today= new SimpleDateFormat("yyyyMMdd").format(date); // local time이 일치하지 않으므로, 전날 종료된 경기정보 업데이트이므로 date -1 Calendar cal = Calendar.getInstance(); cal.add(Calendar.DATE, -1); String today = new SimpleDateFormat("yyyyMMdd").format(cal.getTime()); URL url = new URL("http://data.nba.com/data//json/nbacom/2015/gameline/" + today + "/games.json"); try (InputStream is = url.openStream(); JsonReader rdr = Json.createReader(is)) { JsonObject obj = rdr.readObject(); if (obj != null && obj.get("games") != null && obj.getJsonArray("games").size() > 0) { for (int idx = 0; idx < obj.getJsonArray("games").size(); idx++) { items.add(obj.getJsonArray("games").getJsonObject(idx)); } } } catch (IOException e) { e.printStackTrace(); } ListItemReader<JsonObject> reader = new ListItemReader<>(this.items); return reader; }
@Override public JsonObject getJsonObject() { try { try (JsonReader reader = Json.createReader(getResourceInputStream())) { return reader.readObject(); } } catch (Exception e) { throw new ResourceException("Error while getting the Json object", e); } }
@Test public void statusFor() { try (JsonReader reader = Json.createReader(getClass().getResourceAsStream("site2CantTest.json"))) { Mesh mesh = Mesh.from(reader.readObject()); assertThat(mesh.statusFor(2, 0, Mesh.CellHalf.INITIATED_BY_ROW), equalTo(3)); assertThat(mesh.statusFor(2, 0, Mesh.CellHalf.INITIATED_BY_COLUMN), equalTo(0)); assertThat(mesh.statusFor(0, 2, Mesh.CellHalf.INITIATED_BY_ROW), equalTo(0)); assertThat(mesh.statusFor(0, 2, Mesh.CellHalf.INITIATED_BY_COLUMN), equalTo(3)); } }
/** * @see org.jivesoftware.smack.PacketListener#processPacket(org.jivesoftware.smack.packet.Packet) */ @Override public void processPacket(Packet packet) { Message incomingMessage = (Message) packet; GcmPacketExtension gcmPacket = (GcmPacketExtension) incomingMessage.getExtension(GcmPacketExtension.GCM_NAMESPACE); String json = gcmPacket.getJson(); JsonReader jsonReader = Json.createReader(new StringReader(json)); JsonObject jsonObject = jsonReader.readObject(); Object messageType = jsonObject.get("message_type"); System.out.println(messageType); }
@Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setCharacterEncoding("UTF-8"); HttpSession user = req.getSession(); PrintWriter out = resp.getWriter(); SpejdResultAnalyser result = null; JsonObject response; JsonReader jsonReader = Json.createReader(new InputStreamReader(req.getInputStream(), StandardCharsets.ISO_8859_1)); JsonObject jsonObject = jsonReader.readObject(); jsonReader.close(); System.out.println("REQUEST"); System.out.println(jsonObject); String originalText = jsonObject.getString("text"); int space = jsonObject.getInt("space"); String xml = getXml(originalText, user.getId()); try { result = new SpejdResultAnalyser(xml.replace("SYSTEM \"xcesAnaIPI.dtd\"", "")); } catch (ParserConfigurationException | SAXException e) { out.println(e.toString()); } assert result != null; /** do analysis of xml spejd result */ SubstantivePairs pairs = result.doAnalysis().makePair(space); /** * create json response { text: ... xml: ... pairs: [ { subst1: ... subst2: ... quantity: ... }, * ...] } */ JsonArrayBuilder arrayBuilder = Json.createArrayBuilder(); for (SubstantivePair pair : pairs) { arrayBuilder.add( Json.createObjectBuilder() .add("subst1", pair.getSubst1()) .add("subst2", pair.getSubst2()) .add("quantity", pair.getQuantity())); } Charset.forName("UTF-8").encode(originalText); JsonArray array = arrayBuilder.build(); response = Json.createObjectBuilder() .add("text", originalText) .add("xml", xml) .add("pairs", array) .build(); System.out.println("RESPONSE"); System.out.println(response); out.print(response); }
/** * Parse and return the JsonObject for the payload * * @param a JsonObject for the parsed payload */ public JsonObject getParsedBody() { // We do not worry about concurrency here: processing any given message // is single threaded until queued (at which point we aren't looking // anymore) // We also are not using the more advanced streaming APIs, as the // messages the player service unpacks tend to be short and focused. if (jsonData == null) { JsonReader jsonReader = Json.createReader(new StringReader(messageData)); jsonData = jsonReader.readObject(); } return jsonData; }
public static boolean verify(String gRecaptchaResponse) throws IOException { if (gRecaptchaResponse == null || "".equals(gRecaptchaResponse)) { return false; } try { URL obj = new URL(url); HttpsURLConnection con = (HttpsURLConnection) obj.openConnection(); // add reuqest header con.setRequestMethod("POST"); con.setRequestProperty("User-Agent", USER_AGENT); con.setRequestProperty("Accept-Language", "en-US,en;q=0.5"); String postParams = "secret=" + secret + "&response=" + gRecaptchaResponse; // Send post request con.setDoOutput(true); DataOutputStream wr = new DataOutputStream(con.getOutputStream()); wr.writeBytes(postParams); wr.flush(); wr.close(); int responseCode = con.getResponseCode(); System.out.println("\nSending 'POST' request to URL : " + url); System.out.println("Post parameters : " + postParams); System.out.println("Response Code : " + responseCode); BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); String inputLine; StringBuilder response = new StringBuilder(); while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); // print result System.out.println(response.toString()); // parse JSON response and return 'success' value JsonReader jsonReader = Json.createReader(new StringReader(response.toString())); JsonObject jsonObject = jsonReader.readObject(); jsonReader.close(); return jsonObject.getBoolean("success"); } catch (Exception e) { return false; } }
/** * This methods print outs public posts informations of given user sets the plusPosts with all * those information. getPlusPosts() can be used to get the information. * * @param userName : user name for a Google plus user to crawl the posts * @return no return value. */ public void fetchUserPosts(String userName) { try { InputStream is = new URL( "https://www.googleapis.com/plus/v1/people/" + userName + "/activities/public?key=" + API_KEY) .openStream(); JsonReader rdr = Json.createReader(is); JsonObject obj = rdr.readObject(); UserPost userPost = new UserPost(); userPost.setUserId(userName); JsonArray results = obj.getJsonArray("items"); if (results != null) { System.out.println("===================================="); System.out.println(" Posts"); System.out.println("===================================="); for (JsonObject result : results.getValuesAs(JsonObject.class)) { String postTitle = result.getString("title"); String postID = result.getString("id"); String postDate = result.getString("published"); System.out.println("\nPost ID: " + postID); System.out.println("Post Title: " + postTitle); System.out.println("Post Date: " + postDate); userPost.setPostId(postID); userPost.setPostText(postTitle); SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); Date parsedDate = formatter.parse(postDate); userPost.setPostDate(parsedDate); userPost.setPlaceOfPost(""); getPlusPosts().add(userPost); System.out.println("___________________________________"); } } } catch (java.text.ParseException ex) { System.out.println(ex); } catch (IOException ex) { System.out.println(ex); } }
public SubDocument from(String subdocAsJson, SubDocType subDocType) { JsonReader reader = Json.createReader(new StringReader(subdocAsJson)); JsonObject jsonObject = reader.readObject(); SubDocument.Builder builder = new SubDocument.Builder(); for (Map.Entry<String, JsonValue> entry : jsonObject.entrySet()) { Object objectValue = jsonValueToObject(entry.getValue()); convertAttribute(builder, entry.getKey(), objectValue, subDocType); } return builder.build(); }
// Update the existing album with `id`. // // Display the modified album. private void actionPutAlbum(Router router, Request request, String id) { JsonReader reader = Json.createReader(new StringReader(request.body)); JsonObject album = reader.readObject(); for (JsonObject oalbum : catalog.getValuesAs(JsonObject.class)) { if (oalbum.getString("id").equals(id)) { JsonObjectBuilder builder = Json.createObjectBuilder(); for (String key : oalbum.keySet()) { builder.add(key, oalbum.get(key)); } for (String key : album.keySet()) { builder.add(key, album.get(key)); } // TODO add model layer and data management router.sendJsonResponse(200, "OK", builder.build()); return; } } // No album with this ID found, return error router.sendJsonError(404, "Not found"); }
/** * this method should be called after clicktracking test, in order to verify if expected events * were tracked * * @author Michal 'justnpT' Nowierski */ public void compareTrackedEventsTo(List<JsonObject> expectedEventsList) { executeScript(ClickTrackingScriptsProvider.EVENTS_CAPTURE_INSTALLATION); List<JsonObject> trackedEventsArrayList = new ArrayList<JsonObject>(); List<JsonObject> trackedEventsList; JavascriptExecutor js = (JavascriptExecutor) driver; // prepare list of tracked events Object event = js.executeScript("return selenium_popEvent()"); StringReader reader = new StringReader(event.toString()); JsonReader jsonReader = Json.createReader(reader); while (!(event == null)) { reader = new StringReader(event.toString()); jsonReader = Json.createReader(reader); trackedEventsArrayList.add(jsonReader.readObject()); // take next tracked event event = js.executeScript("return selenium_popEvent()"); } trackedEventsList = trackedEventsArrayList; ClickTrackingSupport support = new ClickTrackingSupport(); support.compare(expectedEventsList, trackedEventsList); }
public static void main(String[] args) { System.out.println("Starting client...."); String url = "http://127.0.0.1:8080/common/api/customers"; URI uri = URI.create(url); final Client client = ClientBuilder.newClient(); WebTarget webTarget = client.target(uri); Response response = webTarget.request(MediaType.APPLICATION_JSON).get(); // Se Response.Status.OK; if (response.getStatus() == 200) { StringReader stringReader = new StringReader(webTarget.request(MediaType.APPLICATION_JSON).get(String.class)); try (JsonReader jsonReader = Json.createReader(stringReader)) { // return jsonReader.readObject(); } } }
ObservableList<Question> getObservableList() throws IOException { String url = "http://api.stackexchange.com/2.2/search?tagged=javafx&site=stackoverflow"; URL host = new URL(url); JsonReader jr = Json.createReader(new GZIPInputStream(host.openConnection().getInputStream())); JsonObject jsonObject = jr.readObject(); JsonArray jsonArray = jsonObject.getJsonArray("items"); ObservableList<Question> answer = FXCollections.observableArrayList(); jsonArray .iterator() .forEachRemaining( (JsonValue e) -> { JsonObject obj = (JsonObject) e; JsonString name = obj.getJsonObject("owner").getJsonString("display_name"); JsonString quest = obj.getJsonString("title"); JsonNumber jsonNumber = obj.getJsonNumber("creation_date"); Question q = new Question(name.getString(), quest.getString(), jsonNumber.longValue() * 1000); answer.add(q); }); return answer; }
public static void parseVcap() throws Exception { if (URL != null && !URL.equals("")) { // If URL is already set, use it as is return; } // Otherwise parse URL and credentials from VCAP_SERVICES String serviceName = System.getenv("SERVICE_NAME"); if (serviceName == null || serviceName.length() == 0) { serviceName = SERVICE_NAME; } String vcapServices = System.getenv("VCAP_SERVICES"); if (vcapServices == null) { throw new Exception("VCAP_SERVICES not found in the environment"); } StringReader stringReader = new StringReader(vcapServices); JsonReader jsonReader = Json.createReader(stringReader); JsonObject vcap = jsonReader.readObject(); System.out.println("vcap: " + vcap); if (vcap.getJsonArray(serviceName) == null) { throw new Exception("Service " + serviceName + " not found in VCAP_SERVICES"); } if (USE_SSL) { URL = vcap.getJsonArray(serviceName) .getJsonObject(0) .getJsonObject("credentials") .getString("java_drda_url_ssl"); } else { URL = vcap.getJsonArray(serviceName) .getJsonObject(0) .getJsonObject("credentials") .getString("java_drda_url"); } System.out.println(URL); }
@Test public void getSites() { try (JsonReader reader = Json.createReader(getClass().getResourceAsStream("allWell.json"))) { Mesh mesh = Mesh.from(reader.readObject()); assertThat( mesh.getSites(), equalTo( Arrays.asList( "CERN-PROD LAT", "FZK-LCG2 LAT", "IN2P3-CC LAT", "INFN-T1 LAT", "KR-KISTI-GSDC-01 LAT", "NDGF-T1 LAT", "RAL-LCG2 LAT", "RRC-KI-T1 LAT", "SARA-MATRIX LAT", "TRIUMF-LCG2 LAT", "Taiwan-LCG2 LAT", "US-FNAL LT", "lhcperfmon-bnl", "pic LAT"))); } }
private static boolean loadATMSwitchConfig(String connId) throws Exception { InputStream is = null; boolean isLoadedFlag = false; ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); is = classLoader.getResource(atmSwitchConfigFileName).openStream(); if (is == null) { throw new Exception("Unable to load config file - " + atmSwitchConfigFileName); } try { JsonReader jsonReader = Json.createReader(is); JsonObject allConfigs = jsonReader.readObject(); JsonString logFileLocation = allConfigs.getJsonString(logFileLocationKey); if (logFileLocation == null) { throw new Exception(logFileLocationKey + " parameter missing in the configuration...."); } if ("".equals(connId)) logger = (new AsynchLogger(logFileLocation.toString() + "/ISOSysLog", Level.INFO)) .getLogger("ISOSysLog"); JsonArray serverConfigs = allConfigs.getJsonArray(serverConfigKey); if (serverConfigs == null) { throw new Exception( serverConfigKey + " parameter missing. No server Configurations specified...."); } logger.info("Initializing ATM Switch Configuration...."); if (manifestAttrs != null && "".equals(connId)) { logger.info( "Build version " + manifestAttrs.getValue("Implementation-Version") + " Build Date " + manifestAttrs.getValue("Build-Time")); } for (int i = 0; i < serverConfigs.size(); i++) { JsonObject serverConfig = serverConfigs.getJsonObject(i); JsonString host = serverConfig.getJsonString(serverHostKey); JsonNumber port = serverConfig.getJsonNumber(portKey); JsonNumber connTimeOut = serverConfig.getJsonNumber(connTimeOutKey); JsonNumber readTimeOut = serverConfig.getJsonNumber(readTimeOutKey); JsonNumber retry = serverConfig.getJsonNumber(retryKey); JsonNumber threadTimeOut = serverConfig.getJsonNumber(threadTimeOutKey); JsonString logLevelJson = serverConfig.getJsonString(loglevelKey); JsonNumber echoTimeInterval = serverConfig.getJsonNumber(echoTimeIntervalKey); JsonString uidFormat = serverConfig.getJsonString("uidFormat"); if (host == null || port == null) { logger.error( serverConfig + " Bad configuration for connection ( Host OR Port is missing ). Not Loading this connection...."); continue; } if (uidFormat == null) { logger.error( serverConfig + " Bad configuration for connection ( uidFormat is missing ). Not Loading this connection...."); continue; } Level level = null; if (logLevelJson == null) { level = Level.INFO; } else { String logLevel = logLevelJson.getString(); if (logLevel.equalsIgnoreCase("TRACE")) { level = Level.TRACE; } else if (logLevel.equalsIgnoreCase("DEBUG")) { level = Level.DEBUG; } else { level = Level.INFO; } } MessageUIDGenerator messageUIDGenerator = MessageUIDGeneratorFactory.getMessageUIDGenerator(uidFormat.toString()); if (!"".equals(connId)) { if (!connId.equalsIgnoreCase(host.getString() + "_" + port.intValue())) { continue; } } ATMSwitchTCPHandler.manualStopPool.put( host.getString() + "_" + String.valueOf(port.intValue()), Boolean.FALSE); ConnectionBean bean = new ConnectionBean( host.getString(), port.intValue(), (connTimeOut == null ? 8000 : connTimeOut.intValue()), (readTimeOut == null ? 8000 : readTimeOut.intValue()), (threadTimeOut == null ? 5000 : threadTimeOut.intValue()), (retry == null ? 3 : retry.intValue()), (echoTimeInterval == null ? 0 : echoTimeInterval.intValue())); ServerContainer container = new ServerContainer(bean, logFileLocation.getString(), level, messageUIDGenerator); try { if (container.connect(false)) { logger.info(bean.toString() + " Loaded...."); isLoadedFlag = true; } else { logger.warn(bean.toString() + " could not be loaded...."); } } catch (InterruptedException e) { logger.error( "( " + bean.getUniqueName() + " ,loadAllClientConnection(, ,) ) Interrupted Exception", e); } catch (ExecutionException e) { logger.error( "( " + bean.getUniqueName() + " ,loadAllClientConnection(, ,) ) ExecutionException", e); } catch (IOException e) { logger.error( "( " + bean.getUniqueName() + " ,loadAllClientConnection(, ,) ) IOException", e); } catch (Exception e) { logger.error( "( " + bean.getUniqueName() + " ,loadAllClientConnection(, ,) ) Exception", e); } } } finally { if (is != null) { try { is.close(); } catch (IOException e) { logger.error("IOException in closing the stream for JSON config property file....", e); } } } return isLoadedFlag; }
public void logController(String topic, MqttMessage mqttMessage) { // get deviceId String[] split = topic.split("log/"); String deviceId = split[1]; // parse log try { JsonReader reader = Json.createReader(new StringReader(mqttMessage.toString())); JsonObject jsonMessage = reader.readObject(); String logType = jsonMessage.getString("type"); switch (logType) { case LOG_NORMAL: int temperature = 0; int timer = jsonMessage.getInt("time"); try { temperature = jsonMessage.getInt("temperature"); } catch (Exception se1) { // @todo: warning break; } JsonString subDeviceId = jsonMessage.getJsonString("subId"); if (subDeviceId == null) sessionHandle.drawTemperature(deviceId, timer, temperature); else sessionHandle.drawTemperature(deviceId, timer, temperature, subDeviceId.toString()); break; case LOG_WARNING: timer = jsonMessage.getInt("time"); subDeviceId = jsonMessage.getJsonString("subId"); int subdeviceNo = jsonMessage.getInt("number"); String deviceState = jsonMessage.getString("value"); String msg; switch (subdeviceNo) { case 1: msg = ("0".equals(deviceState)) ? "Button release" : "Button clicked"; break; case 2: msg = ("0".equals(deviceState)) ? "Cửa cuốn được cuộn lên" : "Đang thả cửa cuốn"; break; default: msg = DEFAULT_ALERT_MSG; break; } if (subDeviceId == null) sessionHandle.doAlert(deviceId, timer, msg); else sessionHandle.doAlert(deviceId, timer, msg, subDeviceId.toString()); break; case LOG_CONTROL: String state = jsonMessage.getString("light"); subdeviceNo = jsonMessage.getInt("no"); sessionHandle.updateLightState(deviceId, subdeviceNo, state); break; case LOG_ACK: subdeviceNo = jsonMessage.getInt("number"); deviceState = jsonMessage.getString("value"); state = ("0".equals(deviceState)) ? "off" : "on"; sessionHandle.ackLightState(deviceId, subdeviceNo, state); break; case LOG_DEVICE_STATUS: JsonArray lstDevice = jsonMessage.getJsonArray("status"); System.out.println("Update status:"); for (int i = 0; i < lstDevice.size(); i++) { JsonObject device = (JsonObject) lstDevice.get(i); System.out.println(device.toString()); subdeviceNo = device.getInt("no"); int intState = device.getInt("value"); state = (intState == 0) ? "off" : "on"; sessionHandle.updateLightState(deviceId, subdeviceNo, state); } sessionHandle.finishUpdateDeviceStatus(); break; default: } } catch (Exception ex) { System.out.println("Parse error"); System.out.println(topic); System.out.println(mqttMessage); System.out.println(ex.getMessage()); } }
public static <T extends Factory> T objectFromJson(Reader reader, T prototype) throws ParseException { try (JsonReader jsonReader = Json.createReader(reader)) { return objectFromJson(jsonReader.readObject(), prototype); } }
/** * This methods print outs public profile informations of given user sets the plusProfile with all * those information. getPlusProfile() can be used to get the information. * * @param userName : user name of a Google plus user to crawl the public profile informations. * @return no return value */ public void startCrawling(String userName) { try { InputStream is = new URL("https://www.googleapis.com/plus/v1/people/" + userName + "?key=" + API_KEY) .openStream(); JsonReader rdr = Json.createReader(is); JsonObject obj = rdr.readObject(); // retrieve user's profile information System.out.println("\nGoogle+ Profile crawler started...\n"); System.out.println("____________________________________"); System.out.println(" Profile"); System.out.println("===================================="); String displayName = obj.getJsonString("displayName").getString(); System.out.println("Name: " + displayName); getPlusProfile().setName(displayName); String user_id = obj.getJsonString("id").getString(); System.out.println("ID: " + user_id); // getPlusProfile().setUserId(user_id);//number format user id getPlusProfile().setUserId(userName); if (obj.containsKey("gender")) { String gender = obj.getJsonString("gender").getString(); System.out.println("Gender: " + gender); getPlusProfile().setGender(gender); } else { System.out.println("Gender: " + ""); getPlusProfile().setGender(""); } getPlusProfile().setDateOfBirth(""); JsonString occupationJson = obj.getJsonString("occupation"); if (occupationJson != null) { String occupation = obj.getJsonString("occupation").getString(); System.out.println("Occupation: " + occupation); } // retrieve user's work and education information JsonArray results = obj.getJsonArray("placesLived"); if (results != null) { System.out.println("____________________________________"); System.out.println(" Locations Lived"); System.out.println("===================================="); int i = 0; for (JsonObject result : results.getValuesAs(JsonObject.class)) { String location = result.getString("value"); System.out.println(location); if (i == 0) getPlusProfile().setCurrentLocation(location); else getPlusProfile().setHomeLocation(location); i++; } if (i == 1) getPlusProfile().setHomeLocation(getPlusProfile().getCurrentLocation()); } else { System.out.println("Location info NOT AVAILABLE"); getPlusProfile().setCurrentLocation(""); getPlusProfile().setHomeLocation(""); } // retrieve user's work and education information Set<String> wrkEdu = new TreeSet<String>(); results = obj.getJsonArray("organizations"); if (results != null) { System.out.println("____________________________________"); System.out.println(" Work and Education"); System.out.println("===================================="); for (JsonObject result : results.getValuesAs(JsonObject.class)) { if (result.containsKey("name")) { String workEdu = result.getString("name"); System.out.println(workEdu); } else { wrkEdu.add(""); System.out.println("Company name NOT AVAILABLE"); } } getPlusProfile().setEmployer(wrkEdu); getPlusProfile().setEducation(wrkEdu); } else { System.out.println("Work and Education info NOT AVAILABLE"); getPlusProfile().setEmployer(wrkEdu); getPlusProfile().setEducation(wrkEdu); } List<String> languages = new ArrayList<String>(); getPlusProfile().setLanguages(languages); firefoxDriver = new FirefoxDriver(); firefoxDriver.get("https://plus.google.com/" + userName + "/about"); Thread.sleep(3000); // get list of circles List<WebElement> circle = firefoxDriver.findElements(By.className("bkb")); Set<String> circles = new TreeSet<String>(); if (circle.size() != 0) { String circleText = circle.get(0).getText(); System.out.println(circleText); String circleCountstr = circleText.split("\\s+")[0]; if (circleCountstr.contains(",")) { circleCountstr = circleCountstr.replace(",", ""); } int circleCount = Integer.parseInt(circleCountstr); System.out.println("Circle Count:" + circleCount); WebElement circleLink = firefoxDriver.findElement(By.xpath("//span[contains(text(),'" + circleText + "')]")); circleLink.click(); Thread.sleep(3000); WebElement friendListBox = firefoxDriver.findElement(By.className("G-q-B")); Robot robot = new Robot(); int loop = 0; while (loop < circleCount / 10) { if (circles.size() == circleCount) break; Thread.sleep(3000); List<WebElement> friendList = friendListBox.findElements(By.tagName("a")); for (WebElement friend : friendList) { System.out.println(friend.getText()); circles.add(friend.getText()); } int count = 0; while (count < 500) { robot.keyPress(java.awt.event.KeyEvent.VK_DOWN); count++; } loop++; } } List<String> friends = new ArrayList<String>(); for (String friend : circles) { friends.add(friend); } getPlusProfile().setFriends(friends); System.out.println( "_________________________________________________________________________________________"); System.out.println("Finish Crawling..."); closePlusWebDriverNode(); } catch (AWTException ex) { System.out.println("Problem in key press:" + ex); } catch (IOException ex) { System.out.println(ex); } catch (InterruptedException ex) { System.out.println(); } }
// Create a new album from a JsonRequest. // // Display the received album. private void actionPostAlbum(Router router, Request request, String id) { JsonReader reader = Json.createReader(new StringReader(request.body)); JsonObject album = reader.readObject(); // TODO add model layer and data management router.sendJsonResponse(201, "Created", album); }
private boolean isJsonResult(String response) { JsonReader jsonReader = Json.createReader(new StringReader(response)); JsonObject json = jsonReader.readObject(); return json.getString("realname").equals("Richard David James"); }
// 对接收的表单数据进行处理,并存入form @Path("/form/store") @POST @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.TEXT_PLAIN) public void StoreForm(String data, @Context HttpServletRequest request) throws UnsupportedEncodingException, IOException { String schema = ""; // 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 = ""; String focusTime = ""; String blurTime = ""; String text = ""; String type = ""; String id = ""; String name = ""; String costTime = ""; 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"); focusTime = jsonobj.getString("focusTime"); blurTime = jsonobj.getString("blurTime"); text = jsonobj.getString("text"); type = jsonobj.getString("type"); id = jsonobj.getString("id"); name = jsonobj.getString("name"); costTime = jsonobj.getString("costTime"); schema = "browser,os,url,loadTime,blurTime,id,name,focusTime,text,type,costTime,ip"; StringBuffer elebuf = new StringBuffer(); // 先存储头信息 elebuf.append(browser); elebuf.append(","); elebuf.append(os); elebuf.append(","); elebuf.append(url); elebuf.append(","); elebuf.append(loadtime); elebuf.append(","); elebuf.append(blurTime + ","); elebuf.append(id + ","); elebuf.append(name + ","); elebuf.append(focusTime + ","); elebuf.append(text + ","); elebuf.append(type + ","); elebuf.append(costTime + ","); elebuf.append(ip); formDAO.insertForm(schema, elebuf.toString()); // return "Post Data Success!"; }
// 对接收的(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!"; }