Search in sources :

Example 11 with MediaTypeDescriptor

use of com.webcohesion.enunciate.api.resources.MediaTypeDescriptor in project enunciate by stoicflame.

the class MediaTypeForMethod method exec.

/**
 * Returns the qname of the element that has the first parameter as the namespace, the second as the element.
 *
 * @param list The arguments.
 * @return The qname.
 */
public Object exec(List list) throws TemplateModelException {
    if (list.size() < 1) {
        throw new TemplateModelException("The MediaTypeForMethod must have a entity as a parameter.");
    }
    TemplateModel from = (TemplateModel) list.get(0);
    Object unwrapped = DeepUnwrap.unwrap(from);
    if (unwrapped instanceof Entity) {
        List<? extends MediaTypeDescriptor> mediaTypes = ((Entity) unwrapped).getMediaTypes();
        if (mediaTypes != null && !mediaTypes.isEmpty()) {
            for (MediaTypeDescriptor mediaType : mediaTypes) {
                if (mediaType.getMediaType().contains("xml")) {
                    return mediaType.getMediaType();
                }
            }
        }
    }
    return "application/xml";
}
Also used : MediaTypeDescriptor(com.webcohesion.enunciate.api.resources.MediaTypeDescriptor) TemplateModelException(freemarker.template.TemplateModelException) Entity(com.webcohesion.enunciate.api.resources.Entity) TemplateModel(freemarker.template.TemplateModel)

Example 12 with MediaTypeDescriptor

use of com.webcohesion.enunciate.api.resources.MediaTypeDescriptor in project enunciate by stoicflame.

the class UniqueMediaTypesForMethod method exec.

public Object exec(List list) throws TemplateModelException {
    if (list.size() < 1) {
        throw new TemplateModelException("The uniqueMediaTypesFor method must have a parameter.");
    }
    TemplateModel from = (TemplateModel) list.get(0);
    Object unwrapped = FreemarkerUtil.unwrap(from);
    HashMap<String, MediaTypeDescriptor> uniqueMediaTypes = new HashMap<String, MediaTypeDescriptor>();
    if (unwrapped instanceof Entity) {
        Entity entity = (Entity) unwrapped;
        List<? extends MediaTypeDescriptor> mts = entity.getMediaTypes();
        if (mts != null) {
            for (MediaTypeDescriptor mt : mts) {
                uniqueMediaTypes.put(mt.getMediaType(), mt);
            }
        }
    }
    ArrayList<MediaTypeDescriptor> orderedTypes = new ArrayList<>(uniqueMediaTypes.values());
    Collections.sort(orderedTypes, (m1, m2) -> {
        String syntax1 = m1.getSyntax() == null ? "" : m1.getSyntax();
        String syntax2 = m2.getSyntax() == null ? "" : m2.getSyntax();
        return syntax1.compareTo(syntax2);
    });
    return orderedTypes;
}
Also used : MediaTypeDescriptor(com.webcohesion.enunciate.api.resources.MediaTypeDescriptor) TemplateModelException(freemarker.template.TemplateModelException) Entity(com.webcohesion.enunciate.api.resources.Entity) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) TemplateModel(freemarker.template.TemplateModel)

Example 13 with MediaTypeDescriptor

use of com.webcohesion.enunciate.api.resources.MediaTypeDescriptor in project enunciate by stoicflame.

the class ResponseEntityImpl method getMediaTypes.

@Override
public List<? extends MediaTypeDescriptor> getMediaTypes() {
    Set<String> produces = requestMapping.getProducesMediaTypes();
    ArrayList<MediaTypeDescriptor> mts = new ArrayList<MediaTypeDescriptor>(produces.size());
    for (String mt : produces) {
        boolean descriptorFound = false;
        DecoratedTypeMirror type = (DecoratedTypeMirror) this.responseMetadata.getDelegate();
        for (Syntax syntax : this.requestMapping.getContext().getContext().getApiRegistry().getSyntaxes(this.registrationContext)) {
            MediaTypeDescriptor descriptor = syntax.findMediaTypeDescriptor(mt, type);
            if (descriptor != null) {
                mts.add(new MediaTypeDescriptorImpl(descriptor, loadExample(syntax, descriptor)));
                descriptorFound = true;
            }
        }
        if (!descriptorFound) {
            CustomMediaTypeDescriptor descriptor = new CustomMediaTypeDescriptor(mt);
            descriptor.setDataType(new CustomDataTypeReference(BaseType.fromType(this.responseMetadata.getDelegate())));
            CustomSyntax syntax = new CustomSyntax(descriptor);
            descriptor.setExample(loadExample(syntax, descriptor));
            mts.add(descriptor);
        }
    }
    return mts;
}
Also used : MediaTypeDescriptor(com.webcohesion.enunciate.api.resources.MediaTypeDescriptor) DecoratedTypeMirror(com.webcohesion.enunciate.javac.decorations.type.DecoratedTypeMirror)

Example 14 with MediaTypeDescriptor

use of com.webcohesion.enunciate.api.resources.MediaTypeDescriptor 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) {
        RequestMapping requestMapping = this.responseCode.getRequestMapping();
        Set<String> produces = requestMapping.getProducesMediaTypes();
        for (String mt : produces) {
            for (Syntax syntax : requestMapping.getContext().getContext().getApiRegistry().getSyntaxes(this.registrationContext)) {
                MediaTypeDescriptor descriptor = syntax.findMediaTypeDescriptor(mt, type);
                if (descriptor != null) {
                    mts.add(descriptor);
                }
            }
        }
    }
    return mts;
}
Also used : MediaTypeDescriptor(com.webcohesion.enunciate.api.resources.MediaTypeDescriptor) DecoratedTypeMirror(com.webcohesion.enunciate.javac.decorations.type.DecoratedTypeMirror) ArrayList(java.util.ArrayList) Syntax(com.webcohesion.enunciate.api.datatype.Syntax) RequestMapping(com.webcohesion.enunciate.modules.spring_web.model.RequestMapping)

Aggregations

MediaTypeDescriptor (com.webcohesion.enunciate.api.resources.MediaTypeDescriptor)14 Entity (com.webcohesion.enunciate.api.resources.Entity)6 DecoratedTypeMirror (com.webcohesion.enunciate.javac.decorations.type.DecoratedTypeMirror)6 ArrayList (java.util.ArrayList)6 TemplateModel (freemarker.template.TemplateModel)5 TemplateModelException (freemarker.template.TemplateModelException)5 DataTypeReference (com.webcohesion.enunciate.api.datatype.DataTypeReference)4 Syntax (com.webcohesion.enunciate.api.datatype.Syntax)2 HashMap (java.util.HashMap)2 DataType (com.webcohesion.enunciate.api.datatype.DataType)1 Example (com.webcohesion.enunciate.api.datatype.Example)1 DocumentationExample (com.webcohesion.enunciate.metadata.DocumentationExample)1 DataTypeReferenceImpl (com.webcohesion.enunciate.modules.jaxb.api.impl.DataTypeReferenceImpl)1 TypeDefinition (com.webcohesion.enunciate.modules.jaxb.model.TypeDefinition)1 XmlType (com.webcohesion.enunciate.modules.jaxb.model.types.XmlType)1 ResourceMethod (com.webcohesion.enunciate.modules.jaxrs.model.ResourceMethod)1 MediaType (com.webcohesion.enunciate.modules.jaxrs.model.util.MediaType)1 RequestMapping (com.webcohesion.enunciate.modules.spring_web.model.RequestMapping)1