/** * Returns a modifiable list of exposed variants for the given method. You can declare variants * manually by updating the result list , by overriding this method. By default, the variants will * be provided based on annotated methods. * * @param method The method. * @return The modifiable list of variants. */ private List<Variant> getVariants(Method method) { List<Variant> result = this.variants; if (result == null) { result = new ArrayList<Variant>(); // Add annotation-based variants in priority if (isAnnotated() && hasAnnotations()) { List<Variant> annoVariants = null; for (AnnotationInfo annotationInfo : getAnnotations()) { if (annotationInfo.isCompatible( method, getRequestEntity(), getMetadataService(), getConverterService())) { annoVariants = annotationInfo.getResponseVariants(getMetadataService(), getConverterService()); if (annoVariants != null) { for (Variant v : annoVariants) { result.add(new VariantInfo(v, annotationInfo)); } } } } } this.variants = result; } return result; }
/** * 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; }
/** * Invoked when the list of allowed methods needs to be updated. The {@link #getAllowedMethods()} * or the {@link #setAllowedMethods(Set)} methods should be used. The default implementation lists * the annotated methods. */ public void updateAllowedMethods() { getAllowedMethods().clear(); List<AnnotationInfo> annotations = getAnnotations(); if (annotations != null) { for (AnnotationInfo annotationInfo : annotations) { if (!getAllowedMethods().contains(annotationInfo.getRestletMethod())) { getAllowedMethods().add(annotationInfo.getRestletMethod()); } } } }
/** * 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; }