Search in sources :

Example 1 with MediaType

use of com.webcohesion.enunciate.modules.jaxrs.model.util.MediaType in project enunciate by stoicflame.

the class StatusCodeImpl method getMediaTypes.

@Override
public List<? extends MediaTypeDescriptor> getMediaTypes() {
    ArrayList<MediaTypeDescriptor> mts = new ArrayList<MediaTypeDescriptor>();
    DecoratedTypeMirror type = this.responseCode.getType();
    if (type != null) {
        ResourceMethod resourceMethod = this.responseCode.getResourceMethod();
        Set<MediaType> produces = resourceMethod.getProducesMediaTypes();
        for (com.webcohesion.enunciate.modules.jaxrs.model.util.MediaType mt : produces) {
            for (Syntax syntax : resourceMethod.getContext().getContext().getApiRegistry().getSyntaxes(registrationContext)) {
                MediaTypeDescriptor descriptor = syntax.findMediaTypeDescriptor(mt.getMediaType(), type);
                if (descriptor != null) {
                    mts.add(new MediaTypeDescriptorImpl(descriptor, mt, descriptor.getExample()));
                }
            }
        }
    }
    return mts;
}
Also used : DecoratedTypeMirror(com.webcohesion.enunciate.javac.decorations.type.DecoratedTypeMirror) ArrayList(java.util.ArrayList) MediaTypeDescriptor(com.webcohesion.enunciate.api.resources.MediaTypeDescriptor) MediaType(com.webcohesion.enunciate.modules.jaxrs.model.util.MediaType) MediaType(com.webcohesion.enunciate.modules.jaxrs.model.util.MediaType) Syntax(com.webcohesion.enunciate.api.datatype.Syntax) ResourceMethod(com.webcohesion.enunciate.modules.jaxrs.model.ResourceMethod)

Example 2 with MediaType

use of com.webcohesion.enunciate.modules.jaxrs.model.util.MediaType in project enunciate by stoicflame.

the class JaxrsModule method addReferencedDataTypeDefinitions.

/**
 * Add the referenced type definitions for the specified resource method.
 *
 * @param resourceMethod The resource method.
 */
protected void addReferencedDataTypeDefinitions(ResourceMethod resourceMethod, LinkedList<Element> contextStack) {
    if (AnnotationUtils.isIgnored(resourceMethod)) {
        return;
    }
    ResourceEntityParameter ep = resourceMethod.getEntityParameter();
    if (ep != null) {
        Set<com.webcohesion.enunciate.modules.jaxrs.model.util.MediaType> consumesMt = resourceMethod.getConsumesMediaTypes();
        Set<String> consumes = new TreeSet<>();
        for (MediaType mediaType : consumesMt) {
            consumes.add(mediaType.getMediaType());
        }
        contextStack.push(ep.getDelegate());
        TypeMirror type = ep.getType();
        contextStack.push(resourceMethod);
        try {
            for (MediaTypeDefinitionModule mediaTypeModule : this.mediaTypeModules) {
                mediaTypeModule.addDataTypeDefinitions(type, consumes, contextStack);
            }
        } catch (RuntimeException e) {
            if (e.getClass().getName().endsWith("CompletionFailure")) {
                throw new CompletionFailureException(contextStack, e);
            }
            throw e;
        } finally {
            contextStack.pop();
        }
    }
    ResourceRepresentationMetadata outputPayload = resourceMethod.getRepresentationMetadata();
    if (outputPayload != null) {
        TypeMirror type = outputPayload.getDelegate();
        Set<com.webcohesion.enunciate.modules.jaxrs.model.util.MediaType> producesMt = resourceMethod.getProducesMediaTypes();
        Set<String> produces = new TreeSet<>();
        for (MediaType mediaType : producesMt) {
            produces.add(mediaType.getMediaType());
        }
        contextStack.push(resourceMethod);
        try {
            for (MediaTypeDefinitionModule mediaTypeModule : this.mediaTypeModules) {
                mediaTypeModule.addDataTypeDefinitions(type, produces, contextStack);
            }
        } catch (RuntimeException e) {
            if (e.getClass().getName().endsWith("CompletionFailure")) {
                throw new CompletionFailureException(contextStack, e);
            }
            throw e;
        } finally {
            contextStack.pop();
        }
    }
    List<? extends ResponseCode> statusCodes = resourceMethod.getStatusCodes();
    if (statusCodes != null) {
        for (ResponseCode statusCode : statusCodes) {
            TypeMirror type = statusCode.getType();
            if (type != null) {
                Set<com.webcohesion.enunciate.modules.jaxrs.model.util.MediaType> producesMt = resourceMethod.getProducesMediaTypes();
                Set<String> produces = new TreeSet<>();
                for (MediaType mediaType : producesMt) {
                    produces.add(mediaType.getMediaType());
                }
                contextStack.push(resourceMethod);
                try {
                    for (MediaTypeDefinitionModule mediaTypeModule : this.mediaTypeModules) {
                        mediaTypeModule.addDataTypeDefinitions(type, produces, contextStack);
                    }
                } catch (RuntimeException e) {
                    if (e.getClass().getName().endsWith("CompletionFailure")) {
                        throw new CompletionFailureException(contextStack, e);
                    }
                    throw e;
                } finally {
                    contextStack.pop();
                }
            }
        }
    }
}
Also used : CompletionFailureException(com.webcohesion.enunciate.CompletionFailureException) TypeMirror(javax.lang.model.type.TypeMirror) MediaType(com.webcohesion.enunciate.modules.jaxrs.model.util.MediaType) com.webcohesion.enunciate.modules.jaxrs.model(com.webcohesion.enunciate.modules.jaxrs.model)

Aggregations

MediaType (com.webcohesion.enunciate.modules.jaxrs.model.util.MediaType)2 CompletionFailureException (com.webcohesion.enunciate.CompletionFailureException)1 Syntax (com.webcohesion.enunciate.api.datatype.Syntax)1 MediaTypeDescriptor (com.webcohesion.enunciate.api.resources.MediaTypeDescriptor)1 DecoratedTypeMirror (com.webcohesion.enunciate.javac.decorations.type.DecoratedTypeMirror)1 com.webcohesion.enunciate.modules.jaxrs.model (com.webcohesion.enunciate.modules.jaxrs.model)1 ResourceMethod (com.webcohesion.enunciate.modules.jaxrs.model.ResourceMethod)1 ArrayList (java.util.ArrayList)1 TypeMirror (javax.lang.model.type.TypeMirror)1