@Override protected void doInit() throws ResourceException { super.doInit(); this.autoDescribing = true; }
/** * Returns the first annotation descriptor matching the given method. * * @param method * The method to match. * @param query * The query parameters. * @param entity * The request entity or null. * @return The annotation descriptor. * @throws IOException */ private MethodAnnotationInfo getAnnotation(Method method, Form query, Representation entity) throws IOException { if (isAnnotated()) { return AnnotationUtils.getInstance().getMethodAnnotation( getAnnotations(), method, query, entity, getMetadataService(), getConverterService()); } return null; }
Method method = getMethod(); setStatus(Status.CLIENT_ERROR_BAD_REQUEST, "No method specified"); } else { if (method.equals(Method.PUT)) { result = put(getRequestEntity()); } else if (method.equals(Method.PATCH)) { result = patch(getRequestEntity()); } else if (isExisting()) { if (method.equals(Method.GET)) { result = get(); } else if (method.equals(Method.POST)) { result = post(getRequestEntity()); } else if (method.equals(Method.DELETE)) { result = delete(); } else if (method.equals(Method.HEAD)) { result = head(); } else if (method.equals(Method.OPTIONS)) { result = options(); } else { result = doHandle(method, getQuery(), getRequestEntity()); doError(Status.CLIENT_ERROR_NOT_FOUND);
Representation result = null; if ((getVariants() != null) && (!getVariants().isEmpty())) { Variant preferredVariant = getPreferredVariant(getVariants()); doError(Status.CLIENT_ERROR_NOT_ACCEPTABLE); result = describeVariants(); } else { updateDimensions(); result = doHandle(preferredVariant); result = doHandle();
/** * Indicates the communication options available for this resource. This * method is only invoked if content negotiation has been disabled as * indicated by the {@link #isNegotiated()}, otherwise the {@link #options(Variant)} method is invoked.<br> * <br> * The default behavior is to set the response status to * {@link org.restlet.data.Status#CLIENT_ERROR_METHOD_NOT_ALLOWED}. * * @return The optional response entity. */ protected Representation options() throws ResourceException { Representation result = null; MethodAnnotationInfo annotationInfo; try { annotationInfo = getAnnotation(Method.OPTIONS); // Updates the list of allowed methods updateAllowedMethods(); if (annotationInfo != null) { result = doHandle(annotationInfo, null); } else { doError(Status.CLIENT_ERROR_METHOD_NOT_ALLOWED); } } catch (IOException e) { throw new ResourceException(e); } return result; }
/** * Returns the preferred variant among a list of available variants. The * selection is based on the client preferences using the * {@link org.restlet.service.ConnegService#getPreferredVariant(List, Request, org.restlet.service.MetadataService)} * method. * * @param variants * The available variants. * @return The preferred variant. */ protected Variant getPreferredVariant(List<Variant> variants) { Variant result = null; // If variants were found, select the best matching one if ((variants != null) && (!variants.isEmpty())) { result = getConnegService().getPreferredVariant(variants, getRequest(), getMetadataService()); } return result; }
if (!isExisting() && getMethod().isSafe()) { doError(Status.CLIENT_ERROR_NOT_FOUND); } else { try { if (isConditional()) { result = doConditionalHandle(); } else if (isNegotiated()) { result = doNegotiatedHandle(); } else { result = doHandle(); getResponse().setEntity(result); doCatch(t); } finally { if (Status.CLIENT_ERROR_METHOD_NOT_ALLOWED.equals(getStatus())) { updateAllowedMethods(); } else if (Status.SUCCESS_OK.equals(getStatus()) && (getResponseEntity() == null || !getResponseEntity() .isAvailable())) { getLogger() .fine("A response with a 200 (Ok) status should have an entity. " + "Changing the status to 204 (No content)."); setStatus(Status.SUCCESS_NO_CONTENT);
if (isAnnotated() && hasAnnotations()) { List<Variant> annoVariants = null; method = (Method.HEAD.equals(method)) ? Method.GET : method; for (AnnotationInfo annotationInfo : getAnnotations()) { try { if (annotationInfo instanceof MethodAnnotationInfo) { .isCompatible(method, getQuery(), getRequestEntity(), getMetadataService(), getConverterService())) { annoVariants = methodAnnotationInfo .getResponseVariants( getMetadataService(), getConverterService()); if ((getRequest().getEntity() != null) && getRequest().getEntity() .isAvailable()) { MediaType emt = getRequest() .getEntity().getMediaType(); List<MediaType> amts = getMetadataService() .getAllMediaTypes( methodAnnotationInfo getLogger().log(Level.FINE, "Unable to get variants from annotation", e);
if (getConditions().hasSome()) { RepresentationInfo resultInfo = null; if (isNegotiated()) { Variant preferredVariant = getPreferredVariant(getVariants(Method.GET)); && getConnegService().isStrict()) { doError(Status.CLIENT_ERROR_NOT_ACCEPTABLE); } else { resultInfo = doGetInfo(preferredVariant); resultInfo = doGetInfo(); if ((getStatus() == null) || (getStatus().isSuccess() && !Status.SUCCESS_NO_CONTENT .equals(getStatus()))) { doError(Status.CLIENT_ERROR_NOT_FOUND); } else { Status status = getConditions().getStatus(getMethod(), resultInfo); doError(status); } else { setStatus(status); Status status = getConditions().getStatus(getMethod(), resultInfo);
ServerResource resource) { MetadataService metadataService = resource.getMetadataService(); List<AnnotationInfo> annotations = resource.isAnnotated() ? AnnotationUtils .getInstance().getAnnotations(resource.getClass()) : null; resource.getMetadataService(), resource.getConverterService()); List<Variant> responseVariants = mai .getResponseVariants( resource.getMetadataService(), resource.getConverterService());
resource.setName(serverResource.getName()); resource.setDescription(serverResource.getDescription());
private static void completeOperationProduces(Operation operation, MethodAnnotationInfo mai, ServerResource sr, MetadataService metadataService) { if (metadataService != null) { try { List<Variant> responseVariants = mai.getResponseVariants( metadataService, sr.getConverterService()); if (responseVariants == null || responseVariants.isEmpty()) { if (mai.getJavaMethod().getReturnType() != Void.TYPE) { LOGGER.warning("Method has no response variant: " + mai.getJavaMethod()); } return; } // une representation per variant ? for (Variant variant : responseVariants) { if (variant.getMediaType() == null) { LOGGER.warning("Variant has no media type: " + variant); continue; } operation.getProduces().add( variant.getMediaType().getName()); } } catch (IOException e) { throw new ResourceException(e); } } }
/** * Apply a patch entity to the current representation of the resource * retrieved by calling {@link #get()}. By default, the * {@link ConverterService#applyPatch(Representation, Representation)} method is used and then the * {@link #put(Representation)} method called. * * @param entity * The patch entity to apply. * @return The optional result entity. * @throws ResourceException * @see <a href="https://tools.ietf.org/html/rfc5789">HTTP PATCH method</a> */ protected Representation patch(Representation entity) throws ResourceException { AnnotationInfo annotationInfo; try { annotationInfo = getAnnotation(Method.PATCH); if (annotationInfo != null) { return doHandle(Method.PATCH, getQuery(), entity); } else { // Default implementation return put(getConverterService().applyPatch(get(), entity)); // doError(Status.CLIENT_ERROR_METHOD_NOT_ALLOWED); } } catch (IOException e) { throw new ResourceException(e); } }
/** * Returns the annotation descriptors. * * @return The annotation descriptors. */ private List<AnnotationInfo> getAnnotations() { return isAnnotated() ? AnnotationUtils.getInstance().getAnnotations( getClass()) : null; }
result = doHandle(((VariantInfo) variant).getAnnotationInfo(), variant); } else { result = put(getConverterService().applyPatch(get(), entity), variant);
/** * Automatically describes a method by discovering the resource's * annotations. * */ private static void completeOperation(CollectInfo collectInfo, Operation operation, MethodAnnotationInfo mai, ServerResource sr, List<? extends IntrospectionHelper> introspectionHelper) { // Loop over the annotated Java methods MetadataService metadataService = sr.getMetadataService(); // Retrieve thrown classes completeOperationThrows(collectInfo, operation, mai, introspectionHelper); // Describe the input completeOperationInput(collectInfo, operation, mai, sr, introspectionHelper, metadataService); // Describe query parameters, if any. completeOperationQueryParameter(operation, mai); // Describe the success response completeOperationOutput(collectInfo, operation, mai, introspectionHelper); // Produces completeOperationProduces(operation, mai, sr, metadataService); }
/** * Indicates the communication options available for this resource. A * variant parameter is passed to indicate which representation should be * returned if any.<br> * <br> * This method is only invoked if content negotiation has been enabled as * indicated by the {@link #isNegotiated()}, otherwise the {@link #options()} method is invoked.<br> * <br> * The default behavior is to set the response status to * {@link org.restlet.data.Status#CLIENT_ERROR_METHOD_NOT_ALLOWED}.<br> * * @param variant * The variant of the response entity. * @return The optional response entity. * @see #get(Variant) */ protected Representation options(Variant variant) throws ResourceException { Representation result = null; // Updates the list of allowed methods updateAllowedMethods(); if (variant instanceof VariantInfo) { result = doHandle(((VariantInfo) variant).getAnnotationInfo(), variant); } else { doError(Status.CLIENT_ERROR_METHOD_NOT_ALLOWED); } return result; }
if (getAnnotation(method) != null) { MethodAnnotationInfo annotationInfo = getAnnotation(method, query, entity); result = doHandle(annotationInfo, null); } else { doError(Status.CLIENT_ERROR_UNSUPPORTED_MEDIA_TYPE); doError(Status.CLIENT_ERROR_METHOD_NOT_ALLOWED);
/** * Returns a full representation for a given variant. A variant parameter is * passed to indicate which representation should be returned if any.<br> * <br> * This method is only invoked if content negotiation has been enabled as * indicated by the {@link #isNegotiated()}, otherwise the {@link #get()} method is invoked.<br> * <br> * The default behavior is to set the response status to * {@link org.restlet.data.Status#CLIENT_ERROR_METHOD_NOT_ALLOWED}.<br> * * @param variant * The variant whose full representation must be returned. * @return The resource's representation. * @see #get(Variant) * @throws ResourceException */ protected Representation get(Variant variant) throws ResourceException { Representation result = null; if (variant instanceof VariantInfo) { result = doHandle(((VariantInfo) variant).getAnnotationInfo(), variant); } else { doError(Status.CLIENT_ERROR_METHOD_NOT_ALLOWED); } return result; }
((ServerResource) resource).updateAllowedMethods(); methodsList.addAll(((ServerResource) resource).getAllowedMethods());