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