001/**
002 * Copyright 2015 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 */
016package org.fcrepo.http.commons.responses;
017
018import org.openrdf.rio.RDFFormat;
019import org.openrdf.rio.WriterConfig;
020import org.openrdf.rio.helpers.JSONLDMode;
021import org.openrdf.rio.helpers.JSONLDSettings;
022import org.slf4j.Logger;
023
024import javax.ws.rs.core.MediaType;
025import java.util.Map;
026
027import static org.slf4j.LoggerFactory.getLogger;
028
029/**
030 * This class creates {@link org.openrdf.rio.WriterConfig}s based on provided {@link javax.ws.rs.core.MediaType}s
031 *
032 * @author awoods
033 * @since 7/7/2015.
034 */
035public class WriterConfigHelper {
036
037    private static final Logger LOGGER = getLogger(WriterConfigHelper.class);
038
039    public static final String PROFILE_KEY = "profile";
040    public static final String PROFILE_COMPACT = "http://www.w3.org/ns/json-ld#compacted";
041    public static final String PROFILE_FLATTEN = "http://www.w3.org/ns/json-ld#flattened";
042    public static final String PROFILE_EXPAND = "http://www.w3.org/ns/json-ld#expanded";
043
044    private WriterConfigHelper() {
045        // no public constructor
046    }
047
048    /**
049     * This method returns a {@link org.openrdf.rio.WriterConfig} based on the arg {@link javax.ws.rs.core.MediaType}
050     *
051     * @param mediaType that will serve as the basis for the returned WriterConfig
052     * @return default or configured WriterConfig
053     */
054    public static WriterConfig apply(final MediaType mediaType) {
055        final WriterConfig config = new WriterConfig();
056        if (!mediaType.isCompatible(MediaType.valueOf(RDFFormat.JSONLD.getDefaultMIMEType()))) {
057            return config;
058        }
059
060        final Map<String, String> params = mediaType.getParameters();
061        if (null != params && params.containsKey(PROFILE_KEY)) {
062            final String profile = params.get(PROFILE_KEY);
063            switch (profile) {
064                case PROFILE_COMPACT:
065                    config.set(JSONLDSettings.JSONLD_MODE, JSONLDMode.COMPACT);
066                    break;
067                case PROFILE_FLATTEN:
068                    config.set(JSONLDSettings.JSONLD_MODE, JSONLDMode.FLATTEN);
069                    break;
070                case PROFILE_EXPAND:
071                    config.set(JSONLDSettings.JSONLD_MODE, JSONLDMode.EXPAND);
072                    break;
073                default:
074                    LOGGER.debug("No profile found in {}", mediaType);
075            }
076        }
077
078        return config;
079    }
080}