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