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 embed;
045
046    private final boolean managedProperties;
047
048    /**
049     * Standard constructor.
050     *
051     * @param preferTag the prefer tag
052     */
053    public LdpPreferTag(final PreferTag preferTag) {
054        super(preferTag);
055
056        final Optional<String> include = fromNullable(preferTag.getParams().get("include"));
057        final Optional<String> omit = fromNullable(preferTag.getParams().get("omit"));
058        final Optional<String> received = fromNullable(preferTag.getParams().get("received"));
059
060        final List<String> includes = asList(include.or(" ").split(" "));
061        final List<String> omits = asList(omit.or(" ").split(" "));
062
063        final boolean minimal = preferTag.getValue().equals("minimal") || received.or("").equals("minimal");
064
065        final boolean preferMinimalContainer = includes.contains(LDP_NAMESPACE + "PreferMinimalContainer") || minimal;
066
067        membership = (!preferMinimalContainer && !omits.contains(LDP_NAMESPACE + "PreferMembership")) ||
068                includes.contains(LDP_NAMESPACE + "PreferMembership");
069
070        containment = (!preferMinimalContainer && !omits.contains(LDP_NAMESPACE + "PreferContainment")) ||
071                includes.contains(LDP_NAMESPACE + "PreferContainment");
072
073        references = includes.contains(INBOUND_REFERENCES.toString());
074
075        embed = includes.contains(EMBED_CONTAINS.toString());
076
077        managedProperties = includes.contains(SERVER_MANAGED.toString())
078                || (!omits.contains(SERVER_MANAGED.toString()) && !minimal);
079    }
080
081    /**
082     * @return Whether this prefer tag demands membership triples.
083     */
084    public boolean prefersMembership() {
085        return membership;
086    }
087
088    /**
089     * @return Whether this prefer tag demands containment triples.
090     */
091    public boolean prefersContainment() {
092        return containment;
093    }
094
095    /**
096     * @return Whether this prefer tag demands references triples.
097     */
098    public boolean prefersReferences() {
099        return references;
100    }
101    /**
102     * @return Whether this prefer tag demands embedded triples.
103     */
104    public boolean prefersEmbed() {
105        return embed;
106    }
107    /**
108     * @return Whether this prefer tag demands server managed properties.
109     */
110    public boolean prefersServerManaged() {
111        return managedProperties;
112    }
113}