예제 #1
0
  @PUT
  @Consumes(MediaType.APPLICATION_JSON)
  public Response addUpdateListItem(InputStream incomingData) {
    StringBuilder listBuilder = new StringBuilder();
    try {
      BufferedReader in = new BufferedReader(new InputStreamReader(incomingData));
      String line = null;
      while ((line = in.readLine()) != null) {
        listBuilder.append(line);
      }
    } catch (Exception e) {
      System.out.println("Error Parsing :- ");
    }

    try {
      JSONObject json = new JSONObject(listBuilder.toString());
      Iterator<String> iterator = json.keys();
      ListItem item =
          new ListItem(
              (String) json.get(iterator.next()),
              (String) json.get(iterator.next()),
              Boolean.parseBoolean((String) json.get(iterator.next())));
      int index = list.indexOf(item);
      item = list.get(index);
      list.remove(index);
      item.setTitle((String) json.get(iterator.next()));
      item.setBody((String) json.get(iterator.next()));
      list.add(index, item);
    } catch (JSONException e) {
      e.printStackTrace();
    }
    // return HTTP response 200 in case of success
    return Response.status(200).entity(listBuilder.toString()).build();
  }
예제 #2
0
 @DELETE
 @Produces(value = "text/plain")
 @Path("/{title}/{body}/{done}")
 public String delete(
     @PathParam("title") String title,
     @PathParam("body") String body,
     @PathParam("done") String done) {
   System.out.println("Incoming values   : " + title + " " + body + " " + done);
   Boolean doneFlag = Boolean.parseBoolean(done);
   ListItem deleteItem = new ListItem(title, body, doneFlag);
   list.remove(deleteItem);
   return deleteItem.toString();
 }
예제 #3
0
  @PUT
  @Path("{title}/{body}/{isDone}")
  @Produces(value = "text/plain")
  @Consumes(MediaType.TEXT_PLAIN)
  public Response toggleStatusListItem(
      @PathParam("title") String title,
      @PathParam("body") String body,
      @PathParam("isDone") boolean isDone) {
    // Find your Account Sid and Token at twilio.com/user/account
    ListItem item = new ListItem(title, body, isDone);
    boolean done = isDone ? false : true;
    int index = list.indexOf(item);
    item.setDone(done);
    list.remove(index);
    list.add(index, item);

    if (isDone == Boolean.FALSE) {

      String ACCOUNT_SID = "AC96d0a9024203de2a4177624c789fad17";
      String AUTH_TOKEN = "919a89967cc484a35a1a0cfd0555dd57";

      TwilioRestClient client = new TwilioRestClient(ACCOUNT_SID, AUTH_TOKEN);
      try {
        // Build a filter for the MessageList
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("Body", "Task : " + body + "is complete."));
        params.add(new BasicNameValuePair("To", "+14086270378"));
        params.add(new BasicNameValuePair("From", "+18316847487"));

        MessageFactory messageFactory = client.getAccount().getMessageFactory();
        Message message = messageFactory.create(params);
        System.out.println(message.getSid());
        return Response.status(200).entity(body.toString()).build();
      } catch (TwilioRestException e) {
        System.out.println(e.getErrorMessage());
      }
    }
    return Response.status(500).entity(body.toString()).build();
  }