001/* 002 * Licensed to DuraSpace under one or more contributor license agreements. 003 * See the NOTICE file distributed with this work for additional information 004 * regarding copyright ownership. 005 * 006 * DuraSpace licenses this file to you under the Apache License, 007 * Version 2.0 (the "License"); you may not use this file except in 008 * compliance with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package org.fcrepo.http.commons.responses; 019 020import static javax.ws.rs.core.MediaType.TEXT_HTML_TYPE; 021import static javax.ws.rs.core.MediaType.TEXT_PLAIN; 022import static org.fcrepo.http.commons.domain.RDFMediaType.JSON_LD; 023import static org.fcrepo.http.commons.domain.RDFMediaType.N3; 024import static org.fcrepo.http.commons.domain.RDFMediaType.N3_ALT2; 025import static org.fcrepo.http.commons.domain.RDFMediaType.NTRIPLES; 026import static org.fcrepo.http.commons.domain.RDFMediaType.RDF_XML; 027import static org.fcrepo.http.commons.domain.RDFMediaType.TURTLE; 028import static org.fcrepo.http.commons.domain.RDFMediaType.TURTLE_X; 029import static org.slf4j.LoggerFactory.getLogger; 030 031import java.io.OutputStream; 032import java.lang.annotation.Annotation; 033import java.lang.reflect.Type; 034 035import javax.ws.rs.Produces; 036import javax.ws.rs.core.MediaType; 037import javax.ws.rs.core.MultivaluedMap; 038import javax.ws.rs.ext.MessageBodyWriter; 039import javax.ws.rs.ext.Provider; 040 041import org.slf4j.Logger; 042 043/** 044 * Provides serialization for streaming RDF results. 045 * 046 * @author ajs6f 047 * @since Nov 19, 2013 048 */ 049@Provider 050@Produces({TURTLE, N3, N3_ALT2, RDF_XML, NTRIPLES, TEXT_PLAIN, TURTLE_X, JSON_LD}) 051public class RdfStreamProvider implements MessageBodyWriter<RdfNamespacedStream> { 052 053 private static final Logger LOGGER = getLogger(RdfStreamProvider.class); 054 055 @Override 056 public boolean isWriteable(final Class<?> type, final Type genericType, 057 final Annotation[] annotations, final MediaType mediaType) { 058 LOGGER.debug( 059 "Checking to see if we can serialize type: {} to mimeType: {}", 060 type.getName(), mediaType.toString()); 061 if (!RdfNamespacedStream.class.isAssignableFrom(type)) { 062 return false; 063 } 064 if (mediaType.equals(TEXT_HTML_TYPE) 065 || (mediaType.getType().equals("application") && mediaType 066 .getSubtype().equals("html"))) { 067 LOGGER.debug("Was asked for an HTML mimeType, returning false."); 068 return false; 069 } 070 LOGGER.debug("Assuming that this is an attempt to retrieve RDF, returning true."); 071 return true; 072 } 073 074 @Override 075 public long getSize(final RdfNamespacedStream t, final Class<?> type, 076 final Type genericType, final Annotation[] annotations, 077 final MediaType mediaType) { 078 // We do not know how long the stream is 079 return -1; 080 } 081 082 @Override 083 public void writeTo(final RdfNamespacedStream nsStream, final Class<?> type, 084 final Type genericType, final Annotation[] annotations, 085 final MediaType mediaType, 086 final MultivaluedMap<String, Object> httpHeaders, 087 final OutputStream entityStream) { 088 089 LOGGER.debug("Serializing an RdfStream to mimeType: {}", mediaType); 090 final RdfStreamStreamingOutput streamOutput = new RdfStreamStreamingOutput(nsStream.stream, 091 nsStream.namespaces, mediaType); 092 streamOutput.write(entityStream); 093 } 094}