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