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