/** * Get a feed object by its feed identifier * * @param feedid Id of the Cosm feed to retrieve * @return Feed object which corresponds to the id provided as the parameter * @throws CosmException If something goes wrong, or if the Feed was not found. */ public Feed getFeed(int feedid, boolean show_user) throws CosmException { try { HttpGet hr = null; if (show_user) { hr = new HttpGet( API_BASE_URL_V2 + API_RESOURCE_FEEDS + "/" + feedid + JSON_FILE_EXTENSION + "?show_user=true"); } else { hr = new HttpGet( API_BASE_URL_V2 + API_RESOURCE_FEEDS + "/" + feedid + JSON_FILE_EXTENSION + "?show_user=false"); } HttpResponse response = this.client.execute(hr); StatusLine statusLine = response.getStatusLine(); String body = this.client.getBody(response); if (statusLine.getStatusCode() == 200) { return CosmFactory.toFeed(body); } throw new CosmException(statusLine, body); } catch (IOException e) { throw new CosmException(IO_EXCEPTION_MESSAGE); } }
// get a datapoint public Datapoint getDatapoint(Integer feedid, String datastreamid, String at) throws CosmException { try { HttpGet request = new HttpGet( API_BASE_URL_V2 + API_RESOURCE_FEEDS + "/" + feedid + "/datastreams/" + datastreamid + "/datapoints/" + at + JSON_FILE_EXTENSION); HttpResponse response = this.client.execute(request); StatusLine statusLine = response.getStatusLine(); if (statusLine.getStatusCode() == 200) { return CosmFactory.toDatapoint(this.client.getBody(response)); } throw new HttpException(statusLine.toString()); } catch (Exception e) { e.printStackTrace(); throw new CosmException(e.getMessage()); } }
// get triggers public Trigger[] getTriggers() throws CosmException { try { HttpGet hr = new HttpGet(API_BASE_URL_V2 + "triggers" + JSON_FILE_EXTENSION); HttpResponse response = this.client.execute(hr); StatusLine statusLine = response.getStatusLine(); if (statusLine.getStatusCode() == 200) { return CosmFactory.toTriggers(this.client.getBody(response)); } throw new CosmException(response.getStatusLine().toString()); } catch (Exception e) { e.printStackTrace(); throw new CosmException("error in getTrigger"); } }
// get groups public Group[] getGroups() throws CosmException { try { HttpGet hr = new HttpGet(API_BASE_URL_V2 + "groups" + JSON_FILE_EXTENSION); HttpResponse response = this.client.execute(hr); StatusLine statusLine = response.getStatusLine(); if (statusLine.getStatusCode() == 200) { return CosmFactory.toGroups(this.client.getBody(response)); } throw new CosmException(response.getStatusLine().toString()); } catch (Exception e) { throw new CosmException("Caught exception in getGroups"); } }
// get group public Group getGroup(String groupid) throws CosmException { try { HttpGet hr = new HttpGet(API_BASE_URL_V2 + "groups/" + groupid + JSON_FILE_EXTENSION); HttpResponse response = this.client.execute(hr); StatusLine statusLine = response.getStatusLine(); if (statusLine.getStatusCode() == 200) { return CosmFactory.toGroup(this.client.getBody(response)); } throw new CosmException(response.getStatusLine().toString()); } catch (Exception e) { e.printStackTrace(); throw new CosmException("Caught exception in getGroup" + e.getMessage()); } }
// create group public Group createGroup(Group group) throws CosmException { try { HttpPost hr = new HttpPost(API_BASE_URL_V2 + "groups" + JSON_FILE_EXTENSION); hr.setEntity(new StringEntity(group.toJSONObject().toString())); HttpResponse response = this.client.execute(hr); StatusLine statusLine = response.getStatusLine(); if (statusLine.getStatusCode() == 201) { return CosmFactory.toGroup(this.client.getBody(response)); } throw new CosmException(response.getStatusLine().toString()); } catch (Exception e) { e.printStackTrace(); throw new CosmException("Caught exception in create Group" + e.getMessage()); } }
// listing all datapoints, historical queries public Datapoint[] getDatapoints( Integer feedid, String datastreamid, String start, String end, String duration, Integer interval, Boolean find_previous, Interval_type interval_type, int per_page, String timezone) throws CosmException { try { String url = API_BASE_URL_V2 + API_RESOURCE_FEEDS + "/" + feedid + "/datastreams/" + datastreamid + JSON_FILE_EXTENSION + "?"; boolean bAdd = false; if (start != null) { if (bAdd) url += '&'; url += "start=" + start; bAdd = true; } if (end != null) { if (bAdd) url += '&'; url += "end=" + end; bAdd = true; } if (duration != null) { if (bAdd) url += '&'; url += "duration=" + duration; bAdd = true; } if (interval != null) { if (bAdd) url += '&'; url += "interval=" + interval; bAdd = true; } if (find_previous != null) { if (bAdd) url += '&'; url += "find_previous=" + find_previous.toString(); bAdd = true; } if (interval_type != null) { if (bAdd) url += '&'; url += "interval_type=" + interval_type.toString(); bAdd = true; } if (timezone != null) { if (bAdd) url += '&'; url += "timezone=" + timezone; } if (bAdd) url += '&'; url += "per_page=" + per_page; ArrayList<Datapoint> datapoints = new ArrayList<Datapoint>(); boolean bContinue = true; int page = 1; while (bContinue) { HttpGet request = new HttpGet(url + "&page=" + page); HttpResponse response = this.client.execute(request); StatusLine statusLine = response.getStatusLine(); String body = this.client.getBody(response); if (statusLine.getStatusCode() == 200) { Datapoint[] newDatapoints = CosmFactory.toDatapoints(body); if (newDatapoints.length < per_page) { bContinue = false; } else { page++; } for (Datapoint datapoint : newDatapoints) { datapoints.add(datapoint); } } else { throw new CosmException(statusLine, body); } } return datapoints.toArray(new Datapoint[datapoints.size()]); } catch (CosmException e) { throw e; } catch (IOException e) { throw new CosmException("IO Exception when communicating with Cosm"); } }
public Waypoint[] getWaypoints( Integer feedid, String start, String end, String duration, Integer interval, Boolean find_previous, Interval_type interval_type, String timezone) throws CosmException { try { String url = API_BASE_URL_V2 + API_RESOURCE_FEEDS + "/" + feedid + JSON_FILE_EXTENSION + "?"; boolean bAdd = false; if (start != null) { if (bAdd) url += '&'; url += "start=" + start; bAdd = true; } if (end != null) { if (bAdd) url += '&'; url += "end=" + end; bAdd = true; } if (duration != null) { if (bAdd) url += '&'; url += "duration=" + duration; bAdd = true; } if (interval != null) { if (bAdd) url += '&'; url += "interval=" + interval; bAdd = true; } if (find_previous != null) { if (bAdd) url += '&'; url += "find_previous=" + find_previous.toString(); bAdd = true; } if (interval_type != null) { if (bAdd) url += '&'; url += "interval_type=" + interval_type.toString(); bAdd = true; } if (timezone != null) { if (bAdd) url += '&'; url += "timezone=" + timezone; } HttpGet request = new HttpGet(url); HttpResponse response = this.client.execute(request); StatusLine statusLine = response.getStatusLine(); String body = this.client.getBody(response); if (statusLine.getStatusCode() == 200) { Feed feed = CosmFactory.toFeed(body); Location location = feed.getLocation(); Waypoint[] waypoints = location.getWaypoints(); return waypoints; } throw new CosmException(statusLine, body); } catch (CosmException e) { throw e; } catch (NullPointerException e) { throw new CosmException(NULL_POINTER_EXCEPTION_MESSAGE); } catch (IOException e) { throw new CosmException(IO_EXCEPTION_MESSAGE); } }
/** * returns a list of feed objects based on a number of optional query parameters. If set to {@link * null}, a parameter is ignored. * * @param query Full text {@link String} search parameter. Should return any feeds matching this * string * @param content parameter of type {@link Content} describing the type of results * @param tag Returns feeds containing datastreams tagged with the search query * @param user Returns feeds created by the user specified. * @param units Returns feeds containing datastreams with units specified by the search query. * @param status Parameter of type {@link Status} * @param order Parameter of type {@link Order}. Used for ordering the results. * @param show_user Include user login and user level for each feed. {@link Boolean} with possible * values: true, false (default) * @param lat Used to find feeds located around this latitude. Used if ids/_datastreams_ are not * specified. * @param lon Used to find feeds located around this longitude. Used if ids/_datastreams_ are not * specified. * @param distance search radius * @param distance_units miles or kms * @return Array of {@link Feed} objects * @throws CosmException */ public Feed[] getFeeds( String query, Content content, String tag, String user, String units, Status status, Order order, Boolean show_user, Double lat, Double lon, Double distance, DistanceUnit distance_units) throws CosmException { String q = ""; boolean bAdd = false; if (query != null) { if (bAdd) q += '&'; q += "q=" + query; bAdd = true; } if (content != null) { if (bAdd) q += '&'; q += "content=" + content.toString(); bAdd = true; } if (tag != null) { if (bAdd) q += '&'; q += "tag=" + tag; bAdd = true; } if (user != null) { if (bAdd) q += '&'; q += "user="******"units=" + units; bAdd = true; } if (status != null) { if (bAdd) q += '&'; q += "status=" + status.toString(); bAdd = true; } if (order != null) { if (bAdd) q += '&'; q += "order=" + order.toString(); bAdd = true; } if (show_user != null) { if (bAdd) q += '&'; q += "show_user="******"lat=" + lat; bAdd = true; } if (lon != null) { if (bAdd) q += '&'; q += "lon=" + lon; bAdd = true; } if (distance != null) { if (bAdd) q += '&'; q += "distance=" + distance; bAdd = true; } if (distance_units != null) { if (bAdd) q += '&'; q += "distance_units=" + distance_units.toString(); bAdd = true; } try { URI uri = new URI( API_SCHEME, API_HOST, API_VERSION + API_RESOURCE_FEEDS + JSON_FILE_EXTENSION, q, null); HttpGet hr = new HttpGet(uri); HttpResponse response = this.client.execute(hr); StatusLine statusLine = response.getStatusLine(); String body = this.client.getBody(response); if (statusLine.getStatusCode() == 200) { return CosmFactory.toFeeds(body); } throw new CosmException(statusLine, body); } catch (IOException e) { throw new CosmException(IO_EXCEPTION_MESSAGE); } catch (URISyntaxException e) { throw new CosmException(URL_SYNTAX_EXCEPTION_MESSAGE); } }