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