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.kernel.modeshape.services; 019 020import org.fcrepo.kernel.api.services.ExternalContentService; 021 022import com.google.common.annotations.VisibleForTesting; 023import org.apache.http.HttpResponse; 024import org.apache.http.client.methods.HttpGet; 025import org.apache.http.conn.HttpClientConnectionManager; 026import org.apache.http.impl.client.CloseableHttpClient; 027import org.apache.http.impl.client.HttpClients; 028import org.springframework.stereotype.Component; 029 030import javax.inject.Inject; 031import java.io.IOException; 032import java.io.InputStream; 033import java.net.URI; 034 035/** 036 * @author cabeer 037 */ 038@Component 039public class ExternalContentServiceImpl implements ExternalContentService { 040 041 @Inject 042 private HttpClientConnectionManager connManager; 043 044 /** 045 * Retrieve the content at the URI using the global connection pool. 046 * @param sourceUri the source uri 047 * @return the content at the URI using the global connection pool 048 * @throws IOException if IO exception occurred 049 */ 050 @SuppressWarnings("resource") 051 @Override 052 public InputStream retrieveExternalContent(final URI sourceUri) throws IOException { 053 final HttpGet httpGet = new HttpGet(sourceUri); 054 final CloseableHttpClient client = getCloseableHttpClient(); 055 final HttpResponse response = client.execute(httpGet); 056 return response.getEntity().getContent(); 057 } 058 059 @VisibleForTesting 060 protected CloseableHttpClient getCloseableHttpClient() { 061 return HttpClients.createMinimal(connManager); 062 } 063 064 @VisibleForTesting 065 protected void setConnManager(final HttpClientConnectionManager connManager) { 066 this.connManager = connManager; 067 } 068}