Search in sources :

Example 1 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) {
        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 MediaTypeDescriptor

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

the class SimpleNameForMethod method simpleNameFor.

@Override
public String simpleNameFor(Object unwrapped, boolean noParams) throws TemplateModelException {
    if (unwrapped instanceof Entity) {
        List<? extends MediaTypeDescriptor> mediaTypes = ((Entity) unwrapped).getMediaTypes();
        for (MediaTypeDescriptor mediaType : mediaTypes) {
            if (this.jsonContext.getLabel().equals(mediaType.getSyntax())) {
                DataTypeReference dataType = mediaType.getDataType();
                unwrapped = this.jsonContext.findType(dataType);
                if (unwrapped == null) {
                    return "JavaScriptObject";
                }
            }
        }
    }
    if (unwrapped instanceof Entity) {
        return "JavaScriptObject";
    }
    return super.simpleNameFor(unwrapped, noParams);
}
Also used : MediaTypeDescriptor(com.webcohesion.enunciate.api.resources.MediaTypeDescriptor) Entity(com.webcohesion.enunciate.api.resources.Entity) DataTypeReference(com.webcohesion.enunciate.api.datatype.DataTypeReference)

Example 3 with MediaTypeDescriptor

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

the class QNameForMediaTypeMethod method exec.

public Object exec(List list) throws TemplateModelException {
    if (list.size() < 1) {
        throw new TemplateModelException("The QNameForType method must have a type mirror as a parameter.");
    }
    TemplateModel from = (TemplateModel) list.get(0);
    Object unwrapped = FreemarkerUtil.unwrap(from);
    if (unwrapped instanceof MediaTypeDescriptor) {
        MediaTypeDescriptor mt = (MediaTypeDescriptor) unwrapped;
        DataTypeReference typeReference = mt.getDataType();
        if (typeReference != null) {
            if (typeReference instanceof DataTypeReferenceImpl) {
                return ((DataTypeReferenceImpl) typeReference).getElementQName();
            } else if (associateJsonWithXml && mt.getMediaType().endsWith("json")) {
                DataType dataType = typeReference.getValue();
                if (dataType != null) {
                    XmlType knownType = this.context.getKnownType(dataType.getJavaElement());
                    if (knownType != null) {
                        return knownType.getQname();
                    }
                    TypeDefinition typeDefinition = this.context.findTypeDefinition(dataType.getJavaElement());
                    if (typeDefinition != null) {
                        return typeDefinition.getQname();
                    }
                }
            }
        }
    }
    return null;
}
Also used : MediaTypeDescriptor(com.webcohesion.enunciate.api.resources.MediaTypeDescriptor) TemplateModelException(freemarker.template.TemplateModelException) DataTypeReference(com.webcohesion.enunciate.api.datatype.DataTypeReference) DataTypeReferenceImpl(com.webcohesion.enunciate.modules.jaxb.api.impl.DataTypeReferenceImpl) DataType(com.webcohesion.enunciate.api.datatype.DataType) TemplateModel(freemarker.template.TemplateModel) XmlType(com.webcohesion.enunciate.modules.jaxb.model.types.XmlType) TypeDefinition(com.webcohesion.enunciate.modules.jaxb.model.TypeDefinition)

Example 4 with MediaTypeDescriptor

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

the class RequestEntityImpl method getMediaTypes.

@Override
public List<? extends MediaTypeDescriptor> getMediaTypes() {
    Set<String> consumes = this.requestMapping.getConsumesMediaTypes();
    ArrayList<MediaTypeDescriptor> mts = new ArrayList<MediaTypeDescriptor>(consumes.size());
    for (String mt : consumes) {
        boolean descriptorFound = false;
        DecoratedTypeMirror type = (DecoratedTypeMirror) this.entityParameter.getType();
        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.entityParameter.getType())));
            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) ArrayList(java.util.ArrayList)

Example 5 with MediaTypeDescriptor

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

the class FindBestDataTypeMethod method findBestDataType.

protected static DataTypeReference findBestDataType(List<? extends MediaTypeDescriptor> mediaTypes) {
    if (mediaTypes == null || mediaTypes.isEmpty()) {
        return null;
    }
    float highestQuality = Float.MIN_VALUE;
    for (MediaTypeDescriptor mediaTypeDescriptor : mediaTypes) {
        highestQuality = Math.max(highestQuality, mediaTypeDescriptor.getQualityOfSourceFactor());
    }
    // first filter out all the media types of lower quality:
    mediaTypes = new ArrayList<MediaTypeDescriptor>(mediaTypes);
    Iterator<? extends MediaTypeDescriptor> iterator = mediaTypes.iterator();
    while (iterator.hasNext()) {
        MediaTypeDescriptor mediaTypeDescriptor = iterator.next();
        if (mediaTypeDescriptor.getQualityOfSourceFactor() < highestQuality) {
            iterator.remove();
        }
    }
    // return the first JSON-based media type.
    for (MediaTypeDescriptor mediaTypeDescriptor : mediaTypes) {
        if (mediaTypeDescriptor.getSyntax() != null && mediaTypeDescriptor.getSyntax().toLowerCase().contains("json")) {
            return mediaTypeDescriptor.getDataType();
        }
    }
    // return the first text-based media type.
    for (MediaTypeDescriptor mediaTypeDescriptor : mediaTypes) {
        String mt = mediaTypeDescriptor.getMediaType();
        if (mt != null) {
            mt = mt.toLowerCase();
            if (mt.startsWith("text") || mt.endsWith("json") || mt.endsWith("xml")) {
                DataTypeReference dataType = mediaTypeDescriptor.getDataType();
                return dataType == null || dataType.getValue() == null ? GENERIC_STRING_BASED_DATATYPE_REFERENCE : dataType;
            }
        }
    }
    // didn't find any text-based media types; try any other media types.
    for (MediaTypeDescriptor mediaTypeDescriptor : mediaTypes) {
        if (mediaTypeDescriptor.getDataType() != null) {
            return mediaTypeDescriptor.getDataType();
        }
    }
    return null;
}
Also used : MediaTypeDescriptor(com.webcohesion.enunciate.api.resources.MediaTypeDescriptor) DataTypeReference(com.webcohesion.enunciate.api.datatype.DataTypeReference)

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