/** For debug - and possibly show the info, allow device selection. */ @Override public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { GeatteRegisterRequestInfo reqInfo = GeatteRegisterRequestInfo.processRequest(req, resp, getServletContext()); if (reqInfo == null) { return; } resp.setContentType("application/json"); JSONObject regs = new JSONObject(); try { regs.put("userEmail", reqInfo.getUserEmail()); JSONArray devices = new JSONArray(); for (DeviceInfo di : reqInfo.devices) { JSONObject dijson = new JSONObject(); dijson.put("key", di.getKey().toString()); dijson.put("deviceName", di.getDeviceName()); dijson.put("type", di.getType()); dijson.put("registrationId", di.getDeviceRegistrationID()); dijson.put("timestamp", di.getRegistrationTimestamp()); devices.put(dijson); } regs.put("devices", devices); PrintWriter out = resp.getWriter(); regs.write(out); } catch (JSONException e) { throw new IOException(e); } }
public static String getUpdatesSince(long sinceId) { // First try to pull the response from the cache. @SuppressWarnings("unchecked") Map<Long, String> cachedQueries = (Map<Long, String>) cache.get(CACHED_QUERIES_KEY); Long sinceIdObj = Long.valueOf(sinceId); if (cachedQueries == null) { cachedQueries = new HashMap<Long, String>(); } else if (cachedQueries.containsKey(sinceIdObj)) { log.info("Found query in the cache: " + sinceId); return cachedQueries.get(sinceIdObj); } // If we haven't cached this response, we must query for it. PersistenceManager pm = PMF.get().getPersistenceManager(); JSONArray resultArray = new JSONArray(); try { Query query = pm.newQuery(TrainUpdate.class); query.setOrdering("twitterId ASC"); if (sinceId >= 0) { query.setFilter("twitterId > " + sinceId); } @SuppressWarnings("unchecked") List<TrainUpdate> updates = (List<TrainUpdate>) query.execute(); for (TrainUpdate update : updates) { JSONObject updateJson = update.getJSON(); resultArray.put(updateJson); } // Append any updates that are stored in the cache to this result. @SuppressWarnings("unchecked") List<TrainUpdate> cachedUpdates = (List<TrainUpdate>) cache.get(CACHED_UPDATES_KEY); if (cachedUpdates != null) { log.info("Fetched cache with size of : " + cachedUpdates.size()); for (TrainUpdate update : cachedUpdates) { if (update.getTwitterId() > sinceId) { JSONObject updateJson = update.getJSON(); resultArray.put(updateJson); } } } } finally { pm.close(); } // Finally cache the response. String result = resultArray.toString(); cachedQueries.put(sinceIdObj, result); cache.put(CACHED_QUERIES_KEY, cachedQueries); return result; }
private String getList() { JSONObject rootObj = new JSONObject(); JSONArray array = new JSONArray(); try { List<ChannelBase> channels = ChannelDatabaseFactory.getPersistance().getList(); for (ChannelBase channel : channels) { JSONArray dataArray = new JSONArray(); dataArray.put(channel.getId()); dataArray.put(channel.getName()); dataArray.put(channel.getStreamUrl()); array.put(dataArray); } rootObj.put("aaData", array); } catch (Exception e) { } return rootObj.toString(); }
public static ArrayList<FriendModel> getFriends( String facebookId, String accessToken, boolean linked) { ArrayList<FriendModel> friendsArray = new ArrayList<FriendModel>(); DAO dao = new DAO(); // Add self if either not linked only or if self has already been linked if (!linked || dao.existsPlayer(facebookId)) { HttpURLConnection connection = null; try { URL url = new URL(GRAPH_PATH_BASE + facebookId + GRAPH_PATH_PART_SELF + accessToken); connection = (HttpURLConnection) url.openConnection(); BufferedReader rd = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8")); StringBuilder sb = new StringBuilder(); String line; while ((line = rd.readLine()) != null) { sb.append(line); } rd.close(); JSONObject data = new JSONObject(sb.toString()); FriendModel self = new FriendModel(facebookId, data.getString("name")); friendsArray.add(self); } catch (Exception e) { return null; } finally { if (connection != null) connection.disconnect(); } } // add rest of friends HttpURLConnection connection = null; try { URL url = new URL(GRAPH_PATH_BASE + facebookId + GRAPH_PATH_PART_FRIENDS + accessToken); connection = (HttpURLConnection) url.openConnection(); BufferedReader rd = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8")); StringBuilder sb = new StringBuilder(); String line; while ((line = rd.readLine()) != null) { sb.append(line); } rd.close(); JSONObject data = new JSONObject(sb.toString()); JSONArray friends = data.getJSONArray("data"); for (int i = 0; i < friends.length(); i++) { JSONObject friendJson = friends.getJSONObject(i); FriendModel friend = new FriendModel(friendJson.getString("id"), friendJson.getString("name")); if (!linked || dao.existsPlayer(friend.getId())) { friendsArray.add(friend); } } return friendsArray; } catch (Exception e) { return null; } finally { if (connection != null) connection.disconnect(); } }
private String getAvailableApps(Representation representation) { JSONArray array = new JSONArray(); Form form = new Form(representation); try { long id = 0; if (form.getFirstValue("id") != null) id = Long.parseLong(form.getFirstValue("id")); if (id != 0) { ChannelBase selected = ChannelDatabaseFactory.getPersistance().getSingle(id); for (ServiceBase service : selected.getServices()) { JSONArray dataArray = new JSONArray(); dataArray.put(service.getId()); dataArray.put(service.getName()); dataArray.put(service.getServiceUrl()); dataArray.put(true); array.put(dataArray); } List<ServiceBase> allServices = ServiceDatabaseFactory.getPersistance().getList(); for (ServiceBase service : allServices) { if (!selected.getServices().contains(service)) { JSONArray dataArray = new JSONArray(); dataArray.put(service.getId()); dataArray.put(service.getName()); dataArray.put(service.getServiceUrl()); dataArray.put(false); array.put(dataArray); } } } } catch (Exception e) { } return array.toString(); }