001/**
002 * Copyright 2015 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 */
016package org.fcrepo.http.commons.domain;
017
018import static com.google.common.collect.Iterables.any;
019import static com.google.common.collect.Iterables.tryFind;
020import static org.fcrepo.http.commons.domain.PreferTag.emptyTag;
021
022import java.text.ParseException;
023import java.util.Set;
024import java.util.TreeSet;
025
026import org.glassfish.jersey.message.internal.HttpHeaderReader;
027
028import com.google.common.base.Predicate;
029
030/**
031 * JAX-RS HTTP parameter parser for the Prefer header
032 *
033 * @author cabeer
034 * @author ajs6f
035 */
036public class SinglePrefer {
037
038    private final Set<PreferTag> preferTags = new TreeSet<>();
039
040    /**
041     * Parse a Prefer: header
042     *
043     * @param header the header
044     * @throws ParseException if parse exception occurred
045     */
046    public SinglePrefer(final String header) throws ParseException {
047        preferTags.addAll(HttpHeaderReader.readList(PREFER_CREATOR, header));
048    }
049
050    /**
051     * Does the Prefer: header have a return tag
052     *
053     * @return true if the header has a return tag
054     */
055    public Boolean hasReturn() {
056        return any(preferTags(), getPreferTag("return"));
057    }
058
059    /**
060     * Does the Prefer: header have a return tag
061     *
062     * @return true if the header has a return tag
063     */
064    public Boolean hasHandling() {
065        return any(preferTags(), getPreferTag("handling"));
066    }
067
068    /**
069     * Get the return tag, or a blank default, if none exists.
070     *
071     * @return return tag, or a blank default, if none exists
072     */
073    public PreferTag getReturn() {
074        return tryFind(preferTags(), getPreferTag("return")).or(emptyTag());
075    }
076
077    /**
078     * Get the return tag, or a blank default, if none exists.
079     *
080     * @return return tag, or a blank default, if none exists
081     */
082    public PreferTag getHandling() {
083        return tryFind(preferTags(), getPreferTag("handling")).or(emptyTag());
084    }
085
086    private static final HttpHeaderReader.ListElementCreator<PreferTag> PREFER_CREATOR =
087            new HttpHeaderReader.ListElementCreator<PreferTag>() {
088
089                @Override
090                public PreferTag create(final HttpHeaderReader reader) throws ParseException {
091                    return new PreferTag(reader);
092                }
093            };
094
095    private static <T extends PreferTag> Predicate<T> getPreferTag(final String tagName) {
096        return new Predicate<T>() {
097
098            @Override
099            public boolean apply(final T tag) {
100                return tag.getTag().equals(tagName);
101            }
102        };
103    }
104
105    protected Set<PreferTag> preferTags() {
106        return preferTags;
107    }
108}