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";
}
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;
}
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);
}
Aggregations