예제 #1
0
 /**
  * Produce a JsonObject by combining a JsonArray of names with the values of this JsonArray.
  *
  * @param names A JsonArray containing a list of key strings. These will be paired with the
  *     values.
  * @return A JsonObject, or null if there are no names or if this JsonArray has no values.
  * @throws JsonException If any of the names are null.
  */
 public JsonObject toJsonObject(JsonArray names) {
   if (names == null || names.length() == 0 || length() == 0) {
     return null;
   }
   JsonObject jo = new JsonObject();
   for (int i = 0; i < names.length(); i += 1) {
     jo.put(names.getString(i), this.opt(i));
   }
   return jo;
 }
예제 #2
0
  /**
   * Write the contents of the JsonArray as JSON text to a writer. For compactness, no whitespace is
   * added.
   *
   * <p>Warning: This method assumes that the data structure is acyclical.
   *
   * @return The writer.
   * @throws JsonException
   */
  public Writer write(Writer writer) {
    try {
      boolean b = false;
      int len = length();

      writer.write('[');

      for (int i = 0; i < len; i += 1) {
        if (b) {
          writer.write(',');
        }
        Object v = this.myArrayList.get(i);
        if (v instanceof JsonObject) {
          ((JsonObject) v).write(writer);
        } else if (v instanceof JsonArray) {
          ((JsonArray) v).write(writer);
        } else {
          writer.write(JsonObject.valueToString(v));
        }
        b = true;
      }
      writer.write(']');
      return writer;
    } catch (IOException e) {
      throw new JsonException(e);
    }
  }
예제 #3
0
  /**
   * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
   *
   * @param request servlet request
   * @param response servlet response
   * @throws ServletException if a servlet-specific error occurs
   * @throws IOException if an I/O error occurs
   */
  protected void processRequest(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    response.setContentType("text/json;charset=UTF-8");
    PrintWriter out = response.getWriter();
    try {
      // THIS is for NOKIA application
      Logger.getLogger(FBPlacesServlet.class.getName())
          .log(Level.SEVERE, "Oops !!! Somebody called " + FBPlacesServlet.class.getName());

      if (HttpUtils.isEmptyAny(request, "latitude", "longitude", "distance")) {
        response.sendError(HttpServletResponse.SC_BAD_REQUEST);
      } else {
        String latitude = request.getParameter("latitude");
        String longitude = request.getParameter("longitude");
        String distance = request.getParameter("distance");
        int limit = NumberUtils.getInt(request.getParameter("limit"), 30);

        FacebookClient facebookClient =
            FacebookUtils.getFacebookClient(Commons.getProperty(Property.fb_app_token));

        String query = request.getParameter("q");
        JsonObject placesSearch = null;

        if (query != null && query.length() > 0) {
          placesSearch =
              facebookClient.fetchObject(
                  "search",
                  JsonObject.class,
                  Parameter.with("type", "place"),
                  Parameter.with("center", latitude + "," + longitude),
                  Parameter.with("distance", distance),
                  Parameter.with("q", query),
                  Parameter.with("limit", limit));
        } else {
          placesSearch =
              facebookClient.fetchObject(
                  "search",
                  JsonObject.class,
                  Parameter.with("type", "place"),
                  Parameter.with("center", latitude + "," + longitude),
                  Parameter.with("distance", distance),
                  Parameter.with("limit", limit));
        }

        JsonArray data = placesSearch.getJsonArray("data");

        ArrayList<Object> jsonArray = new ArrayList<Object>();
        String output = "";

        if (request.getParameter("version") != null
            && request.getParameter("version").equals("3")) {

          for (int i = 0; i < data.length(); i++) {
            Map<String, Object> jsonObject = new HashMap<String, Object>();
            JsonObject place = (JsonObject) data.get(i);
            jsonObject.put("name", place.getString("name"));
            jsonObject.put("url", place.getString("id"));

            Map<String, String> desc = new HashMap<String, String>();
            if (place.has("category")) {
              desc.put("category", place.getString("category"));
            }
            JsonObject location = place.getJsonObject("location");
            Iterator<?> iter = location.sortedKeys();
            while (iter.hasNext()) {
              String next = (String) iter.next();
              if (!(next.equals("latitude") || next.equals("longitude"))) {
                desc.put(next, location.getString(next));
              }
            }
            jsonObject.put("desc", desc);
            jsonObject.put("lat", MathUtils.normalizeE6(location.getDouble("latitude")));
            jsonObject.put("lng", MathUtils.normalizeE6(location.getDouble("longitude")));
            jsonArray.add(jsonObject);
          }

          JSONObject json = new JSONObject().put("ResultSet", jsonArray);
          output = json.toString();

        } else if (request.getParameter("version") != null
            && request.getParameter("version").equals("2")) {

          for (int i = 0; i < data.length(); i++) {
            Map<String, Object> jsonObject = new HashMap<String, Object>();
            JsonObject place = (JsonObject) data.get(i);
            jsonObject.put("name", place.getString("name"));
            jsonObject.put("desc", place.getString("id"));
            JsonObject location = place.getJsonObject("location");
            jsonObject.put("lat", MathUtils.normalizeE6(location.getDouble("latitude")));
            jsonObject.put("lng", MathUtils.normalizeE6(location.getDouble("longitude")));
            jsonArray.add(jsonObject);
          }

          JSONObject json = new JSONObject().put("ResultSet", jsonArray);
          output = json.toString();

        } else {
          // data
          for (int i = 0; i < data.length(); i++) {
            Map<String, Object> jsonObject = new HashMap<String, Object>();
            JsonObject place = (JsonObject) data.get(i);
            if (place.has("name")) {
              jsonObject.put("name", place.getString("name"));
            } else {
              jsonObject.put("name", place.getString("id"));
            }
            jsonObject.put("id", place.getString("id"));
            JsonObject location = place.getJsonObject("location");
            jsonObject.put("lat", MathUtils.normalizeE6(location.getDouble("latitude")));
            jsonObject.put("lng", MathUtils.normalizeE6(location.getDouble("longitude")));
            jsonArray.add(jsonObject);
          }

          JSONObject json = new JSONObject().put("data", jsonArray);
          output = json.toString();
        }

        out.println(output);
      }
    } catch (Exception ex) {
      Logger.getLogger(FBPlacesServlet.class.getName()).log(Level.SEVERE, ex.getMessage(), ex);
      if (ex instanceof FacebookOAuthException) {
        response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
      } else {
        response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
      }
    } finally {
      out.close();
    }
  }