001/** 002 * Copyright 2014 DuraSpace, Inc. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.fcrepo.http.commons.domain; 018 019import static com.google.common.collect.Iterables.any; 020import static com.google.common.collect.Iterables.tryFind; 021import static org.fcrepo.http.commons.domain.PreferTag.emptyTag; 022 023import java.text.ParseException; 024import java.util.Set; 025import java.util.TreeSet; 026 027import org.glassfish.jersey.message.internal.HttpHeaderReader; 028 029import com.google.common.base.Predicate; 030 031/** 032 * JAX-RS HTTP parameter parser for the Prefer header 033 * 034 * @author cabeer 035 * @author ajs6f 036 */ 037public class SinglePrefer { 038 039 private final Set<PreferTag> preferTags = new TreeSet<>(); 040 041 /** 042 * Parse a Prefer: header 043 * 044 * @param inputValue 045 * @throws ParseException 046 */ 047 public SinglePrefer(final String header) throws ParseException { 048 preferTags.addAll(HttpHeaderReader.readList(PREFER_CREATOR, header)); 049 } 050 051 /** 052 * Does the Prefer: header have a return tag 053 * 054 * @return true if the header has a return tag 055 */ 056 public Boolean hasReturn() { 057 return any(preferTags(), getPreferTag("return")); 058 } 059 060 /** 061 * Does the Prefer: header have a return tag 062 * 063 * @return true if the header has a return tag 064 */ 065 public Boolean hasHandling() { 066 return any(preferTags(), getPreferTag("handling")); 067 } 068 069 /** 070 * Get the return tag, or a blank default, if none exists. 071 * 072 * @return return tag, or a blank default, if none exists 073 */ 074 public PreferTag getReturn() { 075 return tryFind(preferTags(), getPreferTag("return")).or(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 tryFind(preferTags(), getPreferTag("handling")).or(emptyTag()); 085 } 086 087 private static final HttpHeaderReader.ListElementCreator<PreferTag> PREFER_CREATOR = 088 new HttpHeaderReader.ListElementCreator<PreferTag>() { 089 090 @Override 091 public PreferTag create(final HttpHeaderReader reader) throws ParseException { 092 return new PreferTag(reader); 093 } 094 }; 095 096 private static <T extends PreferTag> Predicate<T> getPreferTag(final String tagName) { 097 return new Predicate<T>() { 098 099 @Override 100 public boolean apply(final T tag) { 101 return tag.getTag().equals(tagName); 102 } 103 }; 104 } 105 106 protected Set<PreferTag> preferTags() { 107 return preferTags; 108 } 109}