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.transform; 019 020import org.fcrepo.transform.transformations.LDPathTransform; 021import org.fcrepo.transform.transformations.SparqlQueryTransform; 022 023import javax.ws.rs.core.MediaType; 024 025import java.io.InputStream; 026import java.util.HashMap; 027import java.util.Map; 028import java.util.function.Function; 029 030import static org.apache.jena.riot.WebContent.contentTypeSPARQLQuery; 031import static org.fcrepo.transform.transformations.LDPathTransform.APPLICATION_RDF_LDPATH; 032 033/** 034 * Get a Transformation from a MediaType 035 * 036 * @author cbeer 037 * @author ajs6f 038 */ 039public class TransformationFactory { 040 041 @SuppressWarnings("rawtypes") 042 private final Map<String, Function<InputStream, Transformation>> mimeToTransform = new HashMap<>(); 043 044 /** 045 * Get a new TransformationFactory with the default classes 046 * @throws SecurityException if security exception occurred 047 */ 048 public TransformationFactory() { 049 mimeToTransform.put(contentTypeSPARQLQuery, SparqlQueryTransform::new); 050 mimeToTransform.put(APPLICATION_RDF_LDPATH, LDPathTransform::new); 051 } 052 053 /** 054 * Get a Transformation from a MediaType and an InputStream with 055 * the transform program 056 * @param <T> the transformation type 057 * @param contentType the content type 058 * @param inputStream the input stream 059 * @return T a Transformation 060 */ 061 @SuppressWarnings("unchecked") 062 public <T> Transformation<T> getTransform(final MediaType contentType, final InputStream inputStream) { 063 final String mimeType = contentType.toString(); 064 if (mimeToTransform.containsKey(mimeType)) { 065 return mimeToTransform.get(contentType.toString()).apply(inputStream); 066 } 067 throw new UnsupportedOperationException( 068 "No transform type exists for media type " + mimeType + "!"); 069 } 070}