001/*
002 * Licensed to DuraSpace under one or more contributor license agreements.
003 * See the NOTICE file distributed with this work for additional information
004 * regarding copyright ownership.
005 *
006 * DuraSpace licenses this file to you under the Apache License,
007 * Version 2.0 (the "License"); you may not use this file except in
008 * compliance with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.fcrepo.http.commons.domain.ldp;
019
020import static java.util.Arrays.asList;
021import static java.util.Optional.ofNullable;
022import static org.fcrepo.kernel.api.RdfLexicon.EMBED_CONTAINS;
023import static org.fcrepo.kernel.api.RdfLexicon.INBOUND_REFERENCES;
024import static org.fcrepo.kernel.api.RdfLexicon.LDP_NAMESPACE;
025import static org.fcrepo.kernel.api.RdfLexicon.SERVER_MANAGED;
026
027import java.util.List;
028import java.util.Optional;
029
030import org.fcrepo.http.commons.domain.PreferTag;
031
032/**
033 * A subclass of {@link PreferTag} that contemplates the possible preferences for Linked Data Platform requests.
034 *
035 * @author ajs6f
036 */
037public class LdpPreferTag extends PreferTag {
038
039    private final boolean membership;
040
041    private final boolean containment;
042
043    private final boolean references;
044
045    private final boolean embed;
046
047    private final boolean managedProperties;
048
049    /**
050     * Standard constructor.
051     *
052     * @param preferTag the prefer tag
053     */
054    public LdpPreferTag(final PreferTag preferTag) {
055        super(preferTag);
056
057        final Optional<String> include = ofNullable(preferTag.getParams().get("include"));
058        final Optional<String> omit = ofNullable(preferTag.getParams().get("omit"));
059        final Optional<String> received = ofNullable(preferTag.getParams().get("received"));
060
061        final List<String> includes = asList(include.orElse(" ").split(" "));
062        final List<String> omits = asList(omit.orElse(" ").split(" "));
063
064        final boolean minimal = preferTag.getValue().equals("minimal") || received.orElse("").equals("minimal");
065
066        final boolean preferMinimalContainer = includes.contains(LDP_NAMESPACE + "PreferMinimalContainer") || minimal;
067
068        membership = (!preferMinimalContainer && !omits.contains(LDP_NAMESPACE + "PreferMembership")) ||
069                includes.contains(LDP_NAMESPACE + "PreferMembership");
070
071        containment = (!preferMinimalContainer && !omits.contains(LDP_NAMESPACE + "PreferContainment")) ||
072                includes.contains(LDP_NAMESPACE + "PreferContainment");
073
074        references = includes.contains(INBOUND_REFERENCES.toString());
075
076        embed = includes.contains(EMBED_CONTAINS.toString());
077
078        managedProperties = includes.contains(SERVER_MANAGED.toString())
079                || (!omits.contains(SERVER_MANAGED.toString()) && !minimal);
080    }
081
082    /**
083     * @return Whether this prefer tag demands membership triples.
084     */
085    public boolean prefersMembership() {
086        return membership;
087    }
088
089    /**
090     * @return Whether this prefer tag demands containment triples.
091     */
092    public boolean prefersContainment() {
093        return containment;
094    }
095
096    /**
097     * @return Whether this prefer tag demands references triples.
098     */
099    public boolean prefersReferences() {
100        return references;
101    }
102    /**
103     * @return Whether this prefer tag demands embedded triples.
104     */
105    public boolean prefersEmbed() {
106        return embed;
107    }
108    /**
109     * @return Whether this prefer tag demands server managed properties.
110     */
111    public boolean prefersServerManaged() {
112        return managedProperties;
113    }
114}