Search in sources :

Example 1 with ModelWriter

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);
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) MediaType(javax.ws.rs.core.MediaType) ModelWriter(org.apache.stanbol.entityhub.web.ModelWriter) ServiceReference(org.osgi.framework.ServiceReference)

Example 2 with ModelWriter

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;
    }
}
Also used : MediaType(javax.ws.rs.core.MediaType) ModelWriter(org.apache.stanbol.entityhub.web.ModelWriter)

Example 3 with ModelWriter

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);
}
Also used : MediaType(javax.ws.rs.core.MediaType) ModelWriter(org.apache.stanbol.entityhub.web.ModelWriter)

Example 4 with ModelWriter

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);
}
Also used : Entity(org.apache.stanbol.entityhub.servicesapi.model.Entity) WebApplicationException(javax.ws.rs.WebApplicationException) MediaType(javax.ws.rs.core.MediaType) Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation) ModelWriter(org.apache.stanbol.entityhub.web.ModelWriter) ServiceReference(org.osgi.framework.ServiceReference)

Example 5 with ModelWriter

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);
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) MediaType(javax.ws.rs.core.MediaType) ModelWriter(org.apache.stanbol.entityhub.web.ModelWriter) ServiceReference(org.osgi.framework.ServiceReference)

Aggregations

MediaType (javax.ws.rs.core.MediaType)6 ModelWriter (org.apache.stanbol.entityhub.web.ModelWriter)6 WebApplicationException (javax.ws.rs.WebApplicationException)3 ServiceReference (org.osgi.framework.ServiceReference)3 Entity (org.apache.stanbol.entityhub.servicesapi.model.Entity)1 Representation (org.apache.stanbol.entityhub.servicesapi.model.Representation)1