001/*
002 * The contents of this file are subject to the license and copyright
003 * detailed in the LICENSE and NOTICE files at the root of the source
004 * tree.
005 */
006package org.fcrepo.http.commons.responses;
007
008import static javax.ws.rs.core.MediaType.TEXT_HTML_TYPE;
009import static javax.ws.rs.core.MediaType.TEXT_PLAIN;
010import static org.fcrepo.http.commons.domain.RDFMediaType.JSON_LD;
011import static org.fcrepo.http.commons.domain.RDFMediaType.N3;
012import static org.fcrepo.http.commons.domain.RDFMediaType.N3_ALT2;
013import static org.fcrepo.http.commons.domain.RDFMediaType.NTRIPLES;
014import static org.fcrepo.http.commons.domain.RDFMediaType.RDF_XML;
015import static org.fcrepo.http.commons.domain.RDFMediaType.TURTLE;
016import static org.slf4j.LoggerFactory.getLogger;
017
018import java.io.OutputStream;
019import java.lang.annotation.Annotation;
020import java.lang.reflect.Type;
021
022import javax.ws.rs.Produces;
023import javax.ws.rs.core.MediaType;
024import javax.ws.rs.core.MultivaluedMap;
025import javax.ws.rs.ext.MessageBodyWriter;
026import javax.ws.rs.ext.Provider;
027
028import org.slf4j.Logger;
029
030/**
031 * Provides serialization for streaming RDF results.
032 *
033 * @author ajs6f
034 * @since Nov 19, 2013
035 */
036@Provider
037@Produces({TURTLE, N3, N3_ALT2, RDF_XML, NTRIPLES, TEXT_PLAIN, JSON_LD})
038public class RdfStreamProvider implements MessageBodyWriter<RdfNamespacedStream> {
039
040    private static final Logger LOGGER = getLogger(RdfStreamProvider.class);
041
042    @Override
043    public boolean isWriteable(final Class<?> type, final Type genericType,
044            final Annotation[] annotations, final MediaType mediaType) {
045        LOGGER.debug(
046                "Checking to see if we can serialize type: {} to mimeType: {}",
047                type.getName(), mediaType.toString());
048        if (!RdfNamespacedStream.class.isAssignableFrom(type)) {
049            return false;
050        }
051        if (mediaType.equals(TEXT_HTML_TYPE)
052                || (mediaType.getType().equals("application") && mediaType
053                        .getSubtype().equals("html"))) {
054            LOGGER.debug("Was asked for an HTML mimeType, returning false.");
055            return false;
056        }
057        LOGGER.debug("Assuming that this is an attempt to retrieve RDF, returning true.");
058        return true;
059    }
060
061    @Override
062    public long getSize(final RdfNamespacedStream t, final Class<?> type,
063            final Type genericType, final Annotation[] annotations,
064            final MediaType mediaType) {
065        // We do not know how long the stream is
066        return -1;
067    }
068
069    @Override
070    public void writeTo(final RdfNamespacedStream nsStream, final Class<?> type,
071        final Type genericType, final Annotation[] annotations,
072        final MediaType mediaType,
073        final MultivaluedMap<String, Object> httpHeaders,
074        final OutputStream entityStream) {
075
076        LOGGER.debug("Serializing an RdfStream to mimeType: {}", mediaType);
077        final RdfStreamStreamingOutput streamOutput = new RdfStreamStreamingOutput(nsStream.stream,
078                nsStream.namespaces, mediaType);
079        streamOutput.write(entityStream);
080    }
081}