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