use of org.apache.stanbol.entityhub.web.ModelWriter in project stanbol by apache.
the class EntityWriter method writeTo.
@Override
public void writeTo(Entity entity, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException {
//check for wildcard
if (mediaType.isWildcardType() && mediaType.isWildcardSubtype()) {
mediaType = ModelWriter.DEFAULT_MEDIA_TYPE;
}
String charset = mediaType.getParameters().get("charset");
if (charset == null) {
charset = ModelWriter.DEFAULT_CHARSET;
mediaType = mediaType.withCharset(ModelWriter.DEFAULT_CHARSET);
httpHeaders.putSingle(HttpHeaders.CONTENT_TYPE, mediaType.toString());
}
Iterator<ServiceReference> refs = writerRegistry.getModelWriters(getMatchType(mediaType), entity.getRepresentation().getClass()).iterator();
ModelWriter writer = null;
MediaType selectedMediaType = null;
while ((writer == null || selectedMediaType == null) && refs.hasNext()) {
writer = writerRegistry.getService(refs.next());
if (writer != null) {
if (mediaType.isWildcardType() || mediaType.isWildcardSubtype()) {
selectedMediaType = writer.getBestMediaType(mediaType);
} else {
selectedMediaType = mediaType;
}
}
}
selectedMediaType = selectedMediaType.withCharset(charset);
httpHeaders.putSingle(HttpHeaders.CONTENT_TYPE, mediaType.toString());
if (writer == null || selectedMediaType == null) {
throw new WebApplicationException("Unable to serialize " + entity.getClass().getName() + " to " + mediaType);
}
log.debug("serialize Entity with {} data with ModelWriter {}", entity.getRepresentation().getClass().getName(), writer.getClass().getName());
writer.write(entity, entityStream, selectedMediaType);
}
use of org.apache.stanbol.entityhub.web.ModelWriter in project stanbol by apache.
the class ModelWriterTracker method addingService.
@Override
public Object addingService(ServiceReference reference) {
Object service = super.addingService(reference);
Set<MediaType> mediaTypes = parseMediaTypes(((ModelWriter) service).supportedMediaTypes());
Class<? extends Representation> nativeType = ((ModelWriter) service).getNativeType();
if (!mediaTypes.isEmpty()) {
lock.writeLock().lock();
try {
for (MediaType mediaType : mediaTypes) {
addModelWriter(nativeType, mediaType, reference);
}
} finally {
lock.writeLock().unlock();
}
return service;
} else {
//ignore this service
return null;
}
}
use of org.apache.stanbol.entityhub.web.ModelWriter in project stanbol by apache.
the class ModelWriterTracker method removedService.
@Override
public void removedService(ServiceReference reference, Object service) {
if (service != null) {
Set<MediaType> mediaTypes = parseMediaTypes(((ModelWriter) service).supportedMediaTypes());
Class<? extends Representation> nativeType = ((ModelWriter) service).getNativeType();
if (!mediaTypes.isEmpty()) {
lock.writeLock().lock();
try {
for (MediaType mediaType : mediaTypes) {
removeModelWriter(nativeType, mediaType, reference);
}
} finally {
lock.writeLock().unlock();
}
}
}
super.removedService(reference, service);
}
use of org.apache.stanbol.entityhub.web.ModelWriter in project stanbol by apache.
the class ResultListWriter method writeTo.
@Override
public void writeTo(QueryResultList<?> list, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException {
//check for wildcard
if (mediaType.isWildcardType() && mediaType.isWildcardSubtype()) {
mediaType = ModelWriter.DEFAULT_MEDIA_TYPE;
}
String charset = mediaType.getParameters().get("charset");
if (charset == null) {
charset = ModelWriter.DEFAULT_CHARSET;
mediaType = mediaType.withCharset(charset);
httpHeaders.putSingle(HttpHeaders.CONTENT_TYPE, mediaType.toString());
}
Class<? extends Representation> nativeType;
if (list.isEmpty()) {
//for empty lists
//the type does not matter
nativeType = null;
} else if (Representation.class.isAssignableFrom(list.getType())) {
nativeType = ((Representation) list.iterator().next()).getClass();
} else if (Entity.class.isAssignableFrom(list.getType())) {
nativeType = ((Entity) list.iterator().next()).getRepresentation().getClass();
} else {
//only a list of string ids
nativeType = null;
}
Iterator<ServiceReference> refs = writerRegistry.getModelWriters(getMatchType(mediaType), nativeType).iterator();
ModelWriter writer = null;
MediaType selectedMediaType = null;
while ((writer == null || selectedMediaType == null) && refs.hasNext()) {
writer = writerRegistry.getService(refs.next());
if (writer != null) {
if (mediaType.isWildcardType() || mediaType.isWildcardSubtype()) {
selectedMediaType = writer.getBestMediaType(mediaType);
} else {
selectedMediaType = mediaType;
}
}
}
selectedMediaType = selectedMediaType.withCharset(charset);
httpHeaders.putSingle(HttpHeaders.CONTENT_TYPE, mediaType.toString());
if (writer == null || selectedMediaType == null) {
throw new WebApplicationException("Unable to serialize ResultList with " + list.getType() + " (nativeType: " + nativeType + ") to " + mediaType);
}
log.debug("serialize ResultList of {} with ModelWriter {}", nativeType, writer.getClass().getName());
writer.write(list, entityStream, selectedMediaType);
}
use of org.apache.stanbol.entityhub.web.ModelWriter in project stanbol by apache.
the class RepresentationWriter method writeTo.
@Override
public void writeTo(Representation rep, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException {
//check for wildcard
if (mediaType.isWildcardType() && mediaType.isWildcardSubtype()) {
mediaType = ModelWriter.DEFAULT_MEDIA_TYPE;
}
String charset = mediaType.getParameters().get("charset");
if (charset == null) {
charset = ModelWriter.DEFAULT_CHARSET;
mediaType = mediaType.withCharset(charset);
httpHeaders.putSingle(HttpHeaders.CONTENT_TYPE, mediaType.toString());
}
Iterator<ServiceReference> refs = writerRegistry.getModelWriters(getMatchType(mediaType), rep.getClass()).iterator();
ModelWriter writer = null;
MediaType selectedMediaType = null;
while ((writer == null || selectedMediaType == null) && refs.hasNext()) {
writer = writerRegistry.getService(refs.next());
if (writer != null) {
if (mediaType.isWildcardType() || mediaType.isWildcardSubtype()) {
selectedMediaType = writer.getBestMediaType(mediaType);
} else {
selectedMediaType = mediaType;
}
}
}
selectedMediaType = selectedMediaType.withCharset(charset);
httpHeaders.putSingle(HttpHeaders.CONTENT_TYPE, mediaType.toString());
if (writer == null || selectedMediaType == null) {
throw new WebApplicationException("Unable to serialize " + rep.getClass().getName() + " to " + mediaType);
}
log.debug("serialize {} with ModelWriter {}", rep.getClass().getName(), writer.getClass().getName());
writer.write(rep, entityStream, selectedMediaType);
}
Aggregations