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