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