Ejemplo n.º 1
0
  /**
   * Create a Parse API URL using the provided data.
   *
   * @param endPoint The end point
   * @param objectId The optional objectId
   * @return The Parse API URL of the format {@code https://api.parse.com/<endpoint>[/<objectId>]}.
   */
  protected static String getUrl(final String endPoint, final String objectId) {
    String url = Parse.getParseAPIUrl(endPoint) + (objectId != null ? "/" + objectId : "");

    if (LOGGER.isDebugEnabled()) {
      LOGGER.debug("Request URL: " + url);
    }

    return url;
  }
Ejemplo n.º 2
0
  /**
   * Performs this ParseCommand by issuing a synchronous network request.
   *
   * @return The response received if the request was successful.
   * @throws ParseException if anything goes wrong.
   */
  public ParseResponse perform() throws ParseException {

    if (LOGGER.isDebugEnabled()) {
      LOGGER.debug("Data to be sent: " + data.toString());
    }

    long commandStart = System.currentTimeMillis();
    final ParseResponse response = new ParseResponse();
    final ConnectionRequest request = createConnectionRequest(response);
    setUpRequest(request);

    if (progressCallback != null) {
      NetworkManager.getInstance()
          .addProgressListener(
              new ActionListener() {

                public void actionPerformed(ActionEvent evt) {
                  if (evt instanceof NetworkEvent) {
                    final NetworkEvent networkEvent = (NetworkEvent) evt;
                    if (request.equals(networkEvent.getConnectionRequest())) {
                      int progressPercentage = networkEvent.getProgressPercentage();
                      if (progressPercentage >= 0) {
                        progressCallback.done(progressPercentage);
                      }
                    }
                  }
                }
              });
    }

    Iterator keys = headers.keys();
    while (keys.hasNext()) {
      final String key = (String) keys.next();

      try {
        request.addRequestHeader(key, (String) headers.get(key));
      } catch (JSONException ex) {
        Logger.getInstance().error("Error parsing header '" + key + "' + Error: " + ex);
        throw new ParseException(
            ParseException.INVALID_JSON, ParseException.ERR_PREPARING_REQUEST, ex);
      }
    }

    keys = data.keys();
    while (keys.hasNext()) {
      final String key = (String) keys.next();
      if (!REQUEST_BODY_KEY.equals(key)) {
        try {
          request.addArgument(key, data.get(key).toString());
        } catch (JSONException ex) {
          Logger.getInstance()
              .error("Error parsing key '" + key + "' in command data. Error: " + ex);
          throw new ParseException(
              ParseException.INVALID_JSON, ParseException.ERR_PREPARING_REQUEST, ex);
        }
      }
    }
    NetworkManager.getInstance().addToQueueAndWait(request);
    response.extractResponseData(request);
    long commandReceived = System.currentTimeMillis();

    if (LOGGER.isDebugEnabled()) {
      LOGGER.debug(
          "Parse "
              + request.getHttpMethod()
              + " Command took "
              + (commandReceived - commandStart)
              + " milliseconds\n");
    }

    return response;
  }