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.ldp;
017
018import static com.google.common.base.Optional.fromNullable;
019import static java.util.Arrays.asList;
020import static org.fcrepo.kernel.RdfLexicon.EMBED_CONTAINS;
021import static org.fcrepo.kernel.RdfLexicon.INBOUND_REFERENCES;
022import static org.fcrepo.kernel.RdfLexicon.LDP_NAMESPACE;
023import static org.fcrepo.kernel.RdfLexicon.SERVER_MANAGED;
024
025import java.util.List;
026
027import org.fcrepo.http.commons.domain.PreferTag;
028
029import com.google.common.base.Optional;
030
031/**
032 * A subclass of {@link PreferTag} that contemplates the possible preferences for Linked Data Platform requests.
033 *
034 * @author ajs6f
035 */
036public class LdpPreferTag extends PreferTag {
037
038    private final boolean membership;
039
040    private final boolean containment;
041
042    private final boolean references;
043
044    private final boolean preferMinimalContainer;
045
046    private final boolean embed;
047
048    private final boolean managedProperties;
049
050    /**
051     * Standard constructor.
052     *
053     * @param preferTag
054     */
055    public LdpPreferTag(final PreferTag preferTag) {
056        super(preferTag);
057
058        final Optional<String> include = fromNullable(preferTag.getParams().get("include"));
059        final Optional<String> omit = fromNullable(preferTag.getParams().get("omit"));
060        final Optional<String> received = fromNullable(preferTag.getParams().get("received"));
061
062        final List<String> includes = asList(include.or(" ").split(" "));
063        final List<String> omits = asList(omit.or(" ").split(" "));
064
065        final boolean minimal = preferTag.getValue().equals("minimal") || received.or("").equals("minimal");
066
067        preferMinimalContainer = includes.contains(LDP_NAMESPACE + "PreferMinimalContainer") || minimal;
068
069        membership = (!preferMinimalContainer && !omits.contains(LDP_NAMESPACE + "PreferMembership")) ||
070                includes.contains(LDP_NAMESPACE + "PreferMembership");
071
072        containment = (!preferMinimalContainer && !omits.contains(LDP_NAMESPACE + "PreferContainment")) ||
073                includes.contains(LDP_NAMESPACE + "PreferContainment");
074
075        references = includes.contains(INBOUND_REFERENCES.toString());
076
077        embed = includes.contains(EMBED_CONTAINS.toString());
078
079        managedProperties = includes.contains(SERVER_MANAGED.toString())
080                || (!omits.contains(SERVER_MANAGED.toString()) && !minimal);
081    }
082
083    /**
084     * @return Whether this prefer tag demands membership triples.
085     */
086    public boolean prefersMembership() {
087        return membership;
088    }
089
090    /**
091     * @return Whether this prefer tag demands containment triples.
092     */
093    public boolean prefersContainment() {
094        return containment;
095    }
096
097    /**
098     * @return Whether this prefer tag demands references triples.
099     */
100    public boolean prefersReferences() {
101        return references;
102    }
103    /**
104     * @return Whether this prefer tag demands embedded triples.
105     */
106    public boolean prefersEmbed() {
107        return embed;
108    }
109    /**
110     * @return Whether this prefer tag demands server managed properties.
111     */
112    public boolean prefersServerManaged() {
113        return managedProperties;
114    }
115}