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