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