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