Example #1
0
 public static <T extends ActionResult> T errorResult(Class<T> clazz, String message) {
   try {
     T result = clazz.newInstance();
     result.setSuccess(false);
     result.setMessage(message);
     return result;
   } catch (InstantiationException e) {
     return null;
   } catch (IllegalAccessException e) {
     return null;
   }
 }
  /**
   * This is the method to call when the timer expires. The timer will continue to reset until the
   * verification context is verified or the verification context expires. If the context is
   * verified, the status will be COMPLETE. If the context expires, the status will be FAILURE.
   *
   * @param scheduledTime Time in milliseconds when the timer should fire
   * @param actualTime Time in milliseconds
   * @return long - next scheduled time for firing; 0 implies the timer is complete
   */
  @Override
  public long timerFire(long scheduledTime, long actualTime) {
    if (getLogger().isTraceEnabled()) {
      getLogger().trace("The timer has fired for request " + request.getReqId());
    }
    T response = getResponseObject();
    Status status = null;
    String msg = null;
    long timerInterval = 0; // initialize to assume completion
    long timerResetValue = System.currentTimeMillis() + interval;

    response.setReqId(request.getReqId());
    response.setTimestamp(System.currentTimeMillis());
    if (!(--retries > -1)) {
      status = Status.FAILURE;
      msg = "The timer call has exceeded the maximum number of retries.";
      getLogger().error(msg);
      response.setStatus(status);
      response.setMessage(msg);
      responsePromise.complete(response);
    } else if (promise != null && promise.isCompleted()) {
      try {
        IHttpResponse httpResponse = (IHttpResponse) promise.get();
        int respStatus = httpResponse.getStatusCode();

        if (respStatus == 404 && retryOn404) {
          timerInterval = timerResetValue; // reset timer
          promise = run(); // refresh the response
        } else if (respStatus == 200) {
          String azureStatus = getStatus(httpResponse);
          try {
            if (desiredState.equalsIgnoreCase(azureStatus)) { // azureStatus could be null
              response = updateResponseObject(request, response, getJaxbObject(httpResponse));
              status = Status.COMPLETE;
              msg = "Success";
              response.setStatus(status);
              response.setMessage(msg);
              responsePromise.complete(response);
            } else if (isFailedState(azureStatus)) { // allows for short-circuit of failed calls
              status = Status.FAILURE;
              msg = "The status of " + azureStatus + " was returned which implies the call failed.";
              getLogger().error(msg);
              response.setStatus(status);
              response.setMessage(msg);
              responsePromise.complete(response);
            } else {
              timerInterval = timerResetValue; // reset timer
              promise = run(); // refresh the response
            }
          } catch (Exception e) {
            status = Status.FAILURE;
            msg = "An exception occurred while polling status.  " + e.getMessage();
            getLogger().error(msg, e);
            response.setStatus(status);
            response.setMessage(msg);
            responsePromise.complete(response);
          }
        } else {
          status = Status.FAILURE;
          msg = "Unexpected response status was returned by the API call. Status: " + respStatus;
          getLogger().error(msg);
          response.setStatus(status);
          response.setMessage(msg);
          responsePromise.complete(response);
        }
      } catch (Throwable e) {
        status = Status.FAILURE;
        msg = "An exception occurred while polling status.  " + e.getMessage();
        getLogger().error(msg, e);
        response.setStatus(status);
        response.setMessage(msg);
        responsePromise.complete(response);
      }
    } else if (promise == null) {
      status = Status.FAILURE;
      msg = "The HTTP Promise object is null.";
      getLogger().error(msg);
      response.setStatus(status);
      response.setMessage(msg);
      responsePromise.complete(response);
    } else {
      timerInterval = timerResetValue; // reset timer
    }

    // make sure this is called at the end so a new JAXB object will be computed next time fired
    clearJaxbObject();

    return timerInterval;
  }