Example #1
0
  /**
   * Effectively handles a call with content negotiation of the response entity using an annotated
   * method.
   *
   * @param annotationInfo The annotation descriptor.
   * @param variant The response variant expected (can be null).
   * @return The response entity.
   * @throws ResourceException
   */
  private Representation doHandle(AnnotationInfo annotationInfo, Variant variant)
      throws ResourceException {
    Representation result = null;
    Class<?>[] parameterTypes = annotationInfo.getJavaInputTypes();

    // Invoke the annotated method and get the resulting object.
    Object resultObject = null;
    try {
      if (parameterTypes.length > 0) {
        List<Object> parameters = new ArrayList<Object>();
        Object parameter = null;

        for (Class<?> parameterType : parameterTypes) {
          if (Variant.class.equals(parameterType)) {
            parameters.add(variant);
          } else {
            if (getRequestEntity() != null
                && getRequestEntity().isAvailable()
                && getRequestEntity().getSize() != 0) {
              // Assume there is content to be read.
              // NB: it does not handle the case where the size is
              // unknown, but there is no content.
              parameter = toObject(getRequestEntity(), parameterType);

              if (parameter == null) {
                throw new ResourceException(Status.CLIENT_ERROR_UNSUPPORTED_MEDIA_TYPE);
              }
            } else {
              parameter = null;
            }

            parameters.add(parameter);
          }
        }

        resultObject = annotationInfo.getJavaMethod().invoke(this, parameters.toArray());
      } else {
        resultObject = annotationInfo.getJavaMethod().invoke(this);
      }
    } catch (IllegalArgumentException e) {
      throw new ResourceException(e);
    } catch (IllegalAccessException e) {
      throw new ResourceException(e);
    } catch (InvocationTargetException e) {
      if (e.getTargetException() instanceof ResourceException) {
        throw (ResourceException) e.getTargetException();
      }

      throw new ResourceException(e.getTargetException());
    }

    if (resultObject != null) {
      result = toRepresentation(resultObject, variant);
    }

    return result;
  }
  /**
   * Indicates if the current variant is equal to the given variant.
   *
   * @param other The other variant.
   * @return True if the current variant includes the other.
   */
  @Override
  public boolean equals(Object other) {
    boolean result = (other instanceof AnnotationInfo);

    if (result && (other != this)) {
      AnnotationInfo otherAnnotation = (AnnotationInfo) other;

      // Compare the method
      if (result) {
        result =
            ((getJavaMethod() == null) && (otherAnnotation.getJavaMethod() == null)
                || (getJavaMethod() != null)
                    && getJavaMethod().equals(otherAnnotation.getJavaMethod()));
      }

      // Compare the resource interface
      if (result) {
        result =
            ((getResourceClass() == null) && (otherAnnotation.getResourceClass() == null)
                || (getResourceClass() != null)
                    && getResourceClass().equals(otherAnnotation.getResourceClass()));
      }

      // Compare the Restlet method
      if (result) {
        result =
            ((getRestletMethod() == null) && (otherAnnotation.getRestletMethod() == null)
                || (getRestletMethod() != null)
                    && getRestletMethod().equals(otherAnnotation.getRestletMethod()));
      }

      // Compare the input annotation value
      if (result) {
        result =
            ((getInput() == null) && (otherAnnotation.getInput() == null)
                || (getInput() != null) && getInput().equals(otherAnnotation.getInput()));
      }

      // Compare the output annotation value
      if (result) {
        result =
            ((getOutput() == null) && (otherAnnotation.getOutput() == null)
                || (getOutput() != null) && getOutput().equals(otherAnnotation.getOutput()));
      }

      // Compare the query annotation value
      if (result) {
        result =
            ((getQuery() == null) && (otherAnnotation.getQuery() == null)
                || (getQuery() != null) && getQuery().equals(otherAnnotation.getQuery()));
      }
    }

    return result;
  }