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.domain; 007 008import static java.util.Arrays.stream; 009import static java.util.stream.Collectors.toSet; 010import static org.fcrepo.http.commons.domain.PreferTag.emptyTag; 011 012import java.util.Set; 013import java.util.TreeSet; 014 015/** 016 * JAX-RS HTTP parameter parser for the Prefer header 017 * 018 * @author cabeer 019 * @author ajs6f 020 * @author acoburn 021 */ 022public class SinglePrefer { 023 024 private final Set<PreferTag> preferTags = new TreeSet<>(); 025 026 /** 027 * Parse a Prefer: header 028 * 029 * @param header the header 030 */ 031 public SinglePrefer(final String header) { 032 preferTags.addAll(stream(header.split(",")) 033 .map(PreferTag::new) 034 .collect(toSet())); 035 } 036 037 /** 038 * Does the Prefer: header have a return tag 039 * 040 * @return true if the header has a return tag 041 */ 042 public Boolean hasReturn() { 043 return preferTags().stream().map(PreferTag::getTag).anyMatch("return"::equals); 044 } 045 046 /** 047 * Does the Prefer: header have a return tag 048 * 049 * @return true if the header has a return tag 050 */ 051 public Boolean hasHandling() { 052 return preferTags().stream().map(PreferTag::getTag).anyMatch("handling"::equals); 053 } 054 055 /** 056 * Get the return tag, or a blank default, if none exists. 057 * 058 * @return return tag, or a blank default, if none exists 059 */ 060 public PreferTag getReturn() { 061 return preferTags().stream() 062 .filter(x -> x.getTag().equals("return")) 063 .findFirst().orElse(emptyTag()); 064 } 065 066 /** 067 * Get the return tag, or a blank default, if none exists. 068 * 069 * @return return tag, or a blank default, if none exists 070 */ 071 public PreferTag getHandling() { 072 return preferTags().stream() 073 .filter(x -> x.getTag().equals("handling")) 074 .findFirst().orElse(emptyTag()); 075 } 076 077 protected Set<PreferTag> preferTags() { 078 return preferTags; 079 } 080}