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 org.fcrepo.http.commons.domain.RDFMediaType.APPLICATION_LINK_FORMAT; 009 010import java.io.OutputStream; 011import java.io.PrintWriter; 012import java.lang.annotation.Annotation; 013import java.lang.reflect.Type; 014import java.nio.charset.StandardCharsets; 015 016import javax.ws.rs.Produces; 017import javax.ws.rs.WebApplicationException; 018import javax.ws.rs.core.MediaType; 019import javax.ws.rs.core.MultivaluedMap; 020import javax.ws.rs.ext.MessageBodyWriter; 021import javax.ws.rs.ext.Provider; 022 023/** 024 * Writer for application/link-format bodies. 025 * 026 * @author whikloj 027 * @since 2017-10-25 028 */ 029@Provider 030@Produces(APPLICATION_LINK_FORMAT) 031public class LinkFormatProvider implements MessageBodyWriter<LinkFormatStream> { 032 033 @Override 034 public boolean isWriteable(final Class<?> type, final Type genericType, final Annotation[] annotations, 035 final MediaType mediaType) { 036 return LinkFormatStream.class.isAssignableFrom(type); 037 } 038 039 @Override 040 public long getSize(final LinkFormatStream links, final Class<?> type, final Type genericType, 041 final Annotation[] annotations, final MediaType mediaType) { 042 return -1; 043 } 044 045 @Override 046 public void writeTo(final LinkFormatStream links, final Class<?> type, final Type genericType, 047 final Annotation[] annotations, final MediaType mediaType, final MultivaluedMap<String, Object> httpHeaders, 048 final OutputStream entityStream) 049 throws WebApplicationException { 050 051 final PrintWriter writer = new PrintWriter(entityStream, false, StandardCharsets.UTF_8); 052 links.getStream().forEach(l -> { 053 writer.println(l.toString() + ","); 054 }); 055 writer.close(); 056 } 057 058}