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_CONTAINED;
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.PREFER_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                !omits.contains(PREFER_SERVER_MANAGED.toString()))
073                || includes.contains(LDP_NAMESPACE + "PreferContainment");
074
075        references = includes.contains(INBOUND_REFERENCES.toString());
076
077        embed = includes.contains(EMBED_CONTAINED.toString());
078
079        managedProperties = includes.contains(PREFER_SERVER_MANAGED.toString())
080                || (!omits.contains(PREFER_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}