Search in sources :

Example 6 with Entity

use of com.webcohesion.enunciate.api.resources.Entity 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 7 with Entity

use of com.webcohesion.enunciate.api.resources.Entity 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 8 with Entity

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

the class FindBestDataTypeMethod method exec.

public Object exec(List list) throws TemplateModelException {
    if (list.size() < 1) {
        throw new TemplateModelException("The responsesOf method must have a parameter.");
    }
    TemplateModel from = (TemplateModel) list.get(0);
    Object unwrapped = FreemarkerUtil.unwrap(from);
    if (unwrapped instanceof Entity) {
        return findBestDataType((Entity) unwrapped);
    }
    throw new TemplateModelException("No responses for: " + unwrapped);
}
Also used : TemplateModelException(freemarker.template.TemplateModelException) Entity(com.webcohesion.enunciate.api.resources.Entity) TemplateModel(freemarker.template.TemplateModel)

Aggregations

Entity (com.webcohesion.enunciate.api.resources.Entity)8 MediaTypeDescriptor (com.webcohesion.enunciate.api.resources.MediaTypeDescriptor)6 TemplateModel (freemarker.template.TemplateModel)6 TemplateModelException (freemarker.template.TemplateModelException)6 DataTypeReference (com.webcohesion.enunciate.api.datatype.DataTypeReference)3 HashMap (java.util.HashMap)2 Example (com.webcohesion.enunciate.api.datatype.Example)1 Method (com.webcohesion.enunciate.api.resources.Method)1 StatusCode (com.webcohesion.enunciate.api.resources.StatusCode)1 ArrayList (java.util.ArrayList)1