Esempio n. 1
0
 /**
  * Add the HTTP header field associated with the provided key and value.
  *
  * @param key The header's key.
  * @param value The header's value.
  * @throws ParseException if anything goes wrong.
  */
 public void addHeader(final String key, final String value) throws ParseException {
   try {
     headers.put(key, value);
   } catch (JSONException ex) {
     Logger.getInstance().error("Unable to add header. Error: " + ex);
     throw new ParseException(
         ParseException.INVALID_JSON, ParseException.ERR_PREPARING_REQUEST, ex);
   }
 }
Esempio n. 2
0
 /**
  * Adds the default headers (e.g., {@link ParseConstants#HEADER_APPLICATION_ID} and {@link
  * ParseConstants#HEADER_CLIENT_KEY}) associated with Parse REST API calls.
  *
  * @param addJson If true, the corresponding content-type header field is also set.
  * @throws ParseException if anything goes wrong.
  */
 protected void setupDefaultHeaders(boolean addJson) throws ParseException {
   try {
     headers.put(ParseConstants.HEADER_APPLICATION_ID, Parse.getApplicationId());
     headers.put(ParseConstants.HEADER_CLIENT_KEY, Parse.getClientKey());
     if (addJson) {
       headers.put(ParseConstants.HEADER_CONTENT_TYPE, ParseConstants.CONTENT_TYPE_JSON);
     }
     if (!data.has(ParseConstants.FIELD_SESSION_TOKEN) && ParseUser.getCurrent() != null) {
       data.put(ParseConstants.FIELD_SESSION_TOKEN, ParseUser.getCurrent().getSessionToken());
     }
     if (data.has(ParseConstants.FIELD_SESSION_TOKEN)) {
       headers.put(
           ParseConstants.HEADER_SESSION_TOKEN,
           data.getString(ParseConstants.FIELD_SESSION_TOKEN));
     }
   } catch (JSONException ex) {
     throw new ParseException(
         ParseException.INVALID_JSON, ParseException.ERR_PREPARING_REQUEST, ex);
   }
 }
Esempio n. 3
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;
  }