Пример #1
0
  /** 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);
    }
  }
Пример #2
0
  private boolean doRegisterIOSToUrban(DeviceInfo deviceInfo) {
    try {
      URL url =
          new URL(
              "https://go.urbanairship.com/api/device_tokens/"
                  + deviceInfo.getDeviceRegistrationID());

      HttpURLConnection connection = (HttpURLConnection) url.openConnection();
      connection.setRequestMethod("PUT");
      connection.setDoOutput(true);

      String appKey = Config.URBAN_APP_KEY;
      String appMasterSecret = Config.URBAN_APP_MASTERSECRET;

      String authString = appKey + ":" + appMasterSecret;
      String authStringBase64 = Base64.encode(authString.getBytes());
      authStringBase64 = authStringBase64.trim();

      connection.setRequestProperty("Content-type", "application/json");
      connection.setRequestProperty("Authorization", "Basic " + authStringBase64);

      JSONObject object = new JSONObject();
      if (deviceInfo.getDeviceName() != null && !deviceInfo.getDeviceName().isEmpty()) {
        object.put("alias", deviceInfo.getDeviceName());
      }
      object.put("tz", "America/Los_Angeles");

      //	    String jsonBodyString = "{\"alias\": \""+ deviceInfo.getDeviceName() +
      //	    "\", \"tz\": \"America/Los_Angeles\" }";

      OutputStreamWriter osw = new OutputStreamWriter(connection.getOutputStream());
      osw.write(object.toString());
      osw.close();

      int responseCode = connection.getResponseCode();
      log.log(
          Level.INFO,
          "RegisterServlet.doRegisterIOSToUrban() : registering to Urban OK, resp = "
              + responseCode);
      return true;
    } catch (Exception e) {
      log.log(
          Level.WARNING, "RegisterServlet.doRegisterIOSToUrban() : Error registering to Urban.", e);
    }
    return false;
  }
Пример #3
0
  @SuppressWarnings("unchecked")
  public void doGet(HttpServletRequest req, HttpServletResponse res) {
    String name = checkNull(req.getParameter("name"));
    List<Addresses> results;

    if (!name.isEmpty()) {
      results = ADDRESSDao.INSTANCE.getAddressByBuilding(name);
    } else {
      results = ADDRESSDao.INSTANCE.listAddresses();
    }

    if (!results.isEmpty()) {
      Iterator<Addresses> itr = results.iterator();
      List addList = new LinkedList();
      while (itr.hasNext()) {
        Map item = new HashMap();
        Addresses address = itr.next();
        item.put("id", address.getId());
        item.put("buildingname", address.getBuildingName());
        item.put("block", address.getBlock());
        item.put("streetname", address.getStreetName());
        item.put("unitno", address.getUnitNo());
        item.put("postalcode", address.getPostalCode());
        addList.add(item);
      }

      System.out.println("List of addresses: " + addList);

      res.setContentType("text/plain");

      try {
        JSONObject JAdd = new JSONObject().put("addresses", addList);
        String myString = JAdd.toString();
        res.getWriter().println(myString);
      } catch (IOException e) {
        e.printStackTrace();
      } catch (JSONException e) {
        e.printStackTrace();
      }
    } else
      try {
        res.getWriter().println("No such item ");
      } catch (IOException e) {
        e.printStackTrace();
      }
  }
Пример #4
0
 public String getParameter(String name) {
   if (jsonParams != null) {
     return jsonParams.optString(name, null);
   } else {
     String res[] = parameterMap.get(name);
     if (res == null || res.length == 0) {
       return null;
     }
     return res[0];
   }
 }
Пример #5
0
  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();
    }
  }
  public void processRequest(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException {
    //		PrintWriter out = resp.getWriter();
    //	    resp.setContentType("text/plain");

    String signedReq = req.getParameter("signed_request");
    if (signedReq == null) {
      System.out.println("ERROR: Unable to retrieve signed_request parameter");

    } else {
      int count = 0;
      String payload = null;

      // The parameter contains encoded signature and payload separated by Ô.Õ
      StringTokenizer st = new StringTokenizer(signedReq, ".");

      // Retrieve payload (Note: encoded signature is used for internal verification and it is
      // optional)
      while (st.hasMoreTokens()) {
        if (count == 1) {
          payload = st.nextToken();
          break;
        } else st.nextToken();

        count++;
      }

      // Initialize Base64 decoder
      Base64 dec = new Base64();

      // Replace special character in payload as indicated by FB
      payload = payload.replace("-", "+").replace("_", "/").trim();

      // Decode payload
      try {
        byte[] decodedPayload = dec.decode(payload.getBytes());
        payload = new String(decodedPayload, "UTF8");
        //	            out.println("payload");
        //	            out.println(payload);
      } catch (IOException e) {
        System.out.println("ERROR: Unable to perform Base64 Decode");
      }

      // JSON Decode - payload
      try {
        UserAccount acct = new UserAccount();
        JSONObject payloadObject = new JSONObject(payload);
        String facebookId = "" + payloadObject.get("user_id"); // Retrieve user ID
        String oauthToken = "" + payloadObject.get("oauth_token"); // Retrieve oauth token

        acct.setFacebookId(facebookId);
        acct.setOauthToken(oauthToken);

        String regData = "" + payloadObject.get("registration");

        JSONObject payloadObject2 = new JSONObject(regData);
        String name = "" + payloadObject2.get("name");
        String email = "" + payloadObject2.get("email");
        String language = "" + payloadObject2.get("language");
        String method = "" + payloadObject2.get("method");
        String phone = "" + payloadObject2.get("phone");
        String loc = "" + payloadObject2.get("location");
        String send1 = "" + payloadObject2.get("send-1");
        String send2 = "" + payloadObject2.get("send-2");
        String send3 = "" + payloadObject2.get("send-3");

        acct.setName(name);
        acct.setEmail(email);
        acct.setLanguage(language);
        acct.setMethod(method);
        acct.setPhone(phone);
        acct.setSend1(send1);
        acct.setSend2(send2);
        acct.setSend3(send3);

        JSONObject payloadObject3 = new JSONObject(loc);
        String locD = "" + payloadObject3.get("name");

        acct.setLocation(locD);
        acct.setCreated(getCurrentDateTime());

        DataAct da = new DataAct();
        da.insertNewUser(acct);
      } catch (JSONException e) {
        e.printStackTrace();
        // out.println("JSON Decode - payload Error");
      } catch (ServiceException e) {
        // TODO Auto-generated catch block
        // Tout.println("ServiceException Error");
        e.printStackTrace();
      } catch (Exception e) {
        // TODO Auto-generated catch block
        // Tout.println("Exception Error");
        e.printStackTrace();
      }
    }

    resp.sendRedirect("index.html");
  }