Skip to content
This repository has been archived by the owner on Jan 3, 2019. It is now read-only.

Commit

Permalink
Removing hard-coded repository URL -- all repository URLs now come fr…
Browse files Browse the repository at this point in the history
…om JMS messages (or the reindex servlet's baseURI parameter)
  • Loading branch information
escowles committed Jul 16, 2014
1 parent 886bbea commit f76d835
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 55 deletions.
Expand Up @@ -23,6 +23,7 @@
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.DefaultRedirectStrategy;
Expand All @@ -36,7 +37,10 @@
import javax.jms.MessageListener;
import java.io.Reader;
import java.net.URI;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import static com.google.common.base.Suppliers.memoize;
Expand Down Expand Up @@ -65,11 +69,7 @@ public class IndexerGroup implements MessageListener {

private static final Logger LOGGER = getLogger(IndexerGroup.class);

private final String repositoryURL;

private final Set<Indexer<Object>> indexers;

private final DefaultHttpClient httpClient;
protected final Set<Indexer<Object>> indexers;

private Set<String> reindexed;

Expand Down Expand Up @@ -130,19 +130,63 @@ public class IndexerGroup implements MessageListener {

private static final Reader EMPTY_CONTENT = null;

private final String fedoraUsername;
private final String fedoraPassword;
private final Map<String,DefaultHttpClient> clients;
private final DefaultHttpClient defaultClient;

/**
* Default constructor.
**/
public IndexerGroup(final String repositoryURL,
final Set<Indexer<Object>> indexers,
public IndexerGroup(final Set<Indexer<Object>> indexers,
final String fedoraUsername,
final String fedoraPassword) {
this(repositoryURL, indexers, createHttpClient(repositoryURL, fedoraUsername, fedoraPassword));
this.fedoraUsername = fedoraUsername;
this.fedoraPassword = fedoraPassword;

LOGGER.debug("Creating IndexerGroup: {}", this);
this.indexers = indexers;
this.clients = new HashMap<>();
this.defaultClient = null;
}

protected static DefaultHttpClient createHttpClient(final String repositoryURL,
final String fedoraUsername,
final String fedoraPassword) {
/**
* Constructor with provided default HttpClient instance.
**/
public IndexerGroup(final Set<Indexer<Object>> indexers, final DefaultHttpClient httpClient) {
LOGGER.debug("Creating IndexerGroup: {}", this);
this.indexers = indexers;
this.clients = new HashMap<>();
this.fedoraUsername = null;
this.fedoraPassword = null;
this.defaultClient = httpClient;
}

protected DefaultHttpClient httpClient(final String repositoryURL) {
// try to find existing client
if ( clients.size() > 0 ) {
for ( final Iterator<String> it = clients.keySet().iterator(); it.hasNext(); ) {
final String base = it.next();
if ( repositoryURL.startsWith(base) ) {
return clients.get(base);
}
}
}

if ( defaultClient != null ) {
return defaultClient;
}

// if no existing client matched, create a new one
final String baseURL;
if ( repositoryURL.indexOf("/rest/") > 0 ) {
baseURL = repositoryURL.substring(0, repositoryURL.indexOf("/rest/") + 6);
} else if ( repositoryURL.indexOf("/",8) > 0 ) {
baseURL = repositoryURL.substring(0, repositoryURL.indexOf("/",8) + 1);
} else {
baseURL = repositoryURL;
}

final PoolingClientConnectionManager connMann = new PoolingClientConnectionManager();
connMann.setMaxTotal(MAX_VALUE);
connMann.setDefaultMaxPerRoute(MAX_VALUE);
Expand All @@ -155,41 +199,17 @@ protected static DefaultHttpClient createHttpClient(final String repositoryURL,
if (!isBlank(fedoraUsername) && !isBlank(fedoraPassword)) {
LOGGER.debug("Adding BASIC credentials to client for repo requests.");

final URI fedoraUri = URI.create(repositoryURL);
final URI fedoraUri = URI.create(baseURL);
final CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(new AuthScope(fedoraUri.getHost(), fedoraUri.getPort()),
new UsernamePasswordCredentials(fedoraUsername, fedoraPassword));

httpClient.setCredentialsProvider(credsProvider);
}

clients.put(baseURL, httpClient);
return httpClient;
}

/**
* Constructor
*/
public IndexerGroup(final String repositoryURL,
final Set<Indexer<Object>> indexers,
final DefaultHttpClient httpClient) {
LOGGER.debug("Creating IndexerGroup: {}", this);

assert (null != repositoryURL);
this.repositoryURL = repositoryURL;

assert (indexers.size() > 0);
this.indexers = indexers;

assert (null != httpClient);
this.httpClient = httpClient;
}

/**
* Get repository URL.
**/
public String getRepositoryURL() {
return repositoryURL;
}

/**
* Handle a JMS message representing an object update or deletion event.
Expand Down Expand Up @@ -225,6 +245,7 @@ public void onMessage(final Message message) {
**/
private void index( final String uri, final String eventType ) {
final Boolean removal = REMOVAL_EVENT_TYPE.equals(eventType);
final HttpClient httpClient = httpClient(uri);
LOGGER.debug("It is {} that this is a removal operation.", removal);
final Supplier<Model> rdfr =
memoize(new RdfRetriever(uri, httpClient));
Expand Down Expand Up @@ -312,10 +333,10 @@ private void index( final String uri, final String eventType ) {
/**
* Reindex all content in the repository by retrieving the root resource
* and recursively reindexing all indexable child resources.
* @param baseURI Repository base URI (e.g., http://localhost:8080/rest/).
**/
public void reindex() {
reindexed = new HashSet<>();
reindexURI( getRepositoryURL(), true );
public void reindex( final String baseURI ) {
reindex( baseURI, true );
}

/**
Expand All @@ -341,7 +362,7 @@ private void reindexURI( final String uri, final boolean recursive ) {
// check for children (rdf should be cached...)
if ( recursive ) {
final Supplier<Model> rdfr
= memoize(new RdfRetriever(uri, httpClient));
= memoize(new RdfRetriever(uri, httpClient(uri)));
final Model model = rdfr.get();
final NodeIterator children = model.listObjectsOfProperty( HAS_CHILD );
while ( children.hasNext() ) {
Expand Down
Expand Up @@ -66,7 +66,7 @@ public class IndexerGroupTest {

private IndexerGroup indexerGroup;

private String repoUrl;
private String repoUrl = "http://example.org:80";

private DefaultHttpClient httpClient;

Expand All @@ -80,23 +80,22 @@ public void setUp() {
initMocks(this);
httpClient = PowerMockito.mock(DefaultHttpClient.class);

repoUrl = "http://example.org:80";

indexers = new HashSet<>();
indexers.add(indexer);

indexerGroup = new IndexerGroup(repoUrl, indexers, httpClient);
indexerGroup = new IndexerGroup(indexers, httpClient);
}

@Test
public void testSanityConstructor() {
indexerGroup = new IndexerGroup(repoUrl, indexers, "user", "pass");
assertEquals(repoUrl, indexerGroup.getRepositoryURL());
indexerGroup = new IndexerGroup(indexers, "user", "pass");
assertEquals(indexers, indexerGroup.indexers);
}

@Test
public void testCreateHttpClient() {
final DefaultHttpClient client = IndexerGroup.createHttpClient(repoUrl, "user", "pass");
public void testHttpClient() {
final IndexerGroup indexer = new IndexerGroup(null, "user", "pass");
final DefaultHttpClient client = indexer.httpClient("http://example.org:80");
assertNotNull(client);

final CredentialsProvider provider = client.getCredentialsProvider();
Expand Down Expand Up @@ -146,7 +145,7 @@ public void testRDFIndexablePropertyUpdateMessage() throws Exception {
public void testReindex() throws Exception {
mockContent("", true, null);
when(indexer.getIndexerType()).thenReturn(Indexer.IndexerType.RDF);
indexerGroup.reindex();
indexerGroup.reindex(repoUrl);
verify(indexer,atLeastOnce()).update(eq(repoUrl), any());
}

Expand Down
Expand Up @@ -131,7 +131,7 @@ public void testIndexerGroupReindex() throws Exception {
testIndexer.clear();

// reindex everything
indexerGroup.reindex();
indexerGroup.reindex(serverAddress);

// records should be reindexed
synchronized (testIndexer) {
Expand Down
Expand Up @@ -50,7 +50,6 @@

<!-- Message Driven POJO (MDP) that manages individual indexers -->
<bean id="indexerGroup" class="org.fcrepo.indexer.IndexerGroup">
<constructor-arg name="repositoryURL" value="http://localhost:${test.port:8080}"/>
<constructor-arg name="indexers">
<set value-type="org.fcrepo.indexer.Indexer">
<ref bean="testIndexer"/>
Expand Down
Expand Up @@ -58,8 +58,8 @@ public void doPost(final HttpServletRequest request, final HttpServletResponse r
throws ServletException {
final String recurParam = request.getParameter("recursive");
final boolean recursive = (recurParam == null || recurParam.equals("true"));
final String path = request.getPathInfo();
indexer.reindex( indexer.getRepositoryURL() + path, recursive );
final String baseURI = request.getParameter("baseURI");
indexer.reindex( baseURI, recursive );

try {
response.setContentType("text/plain");
Expand Down
Expand Up @@ -40,7 +40,6 @@

<!-- Message Driven POJO (MDP) that manages individual indexers -->
<bean id="indexerGroup" class="org.fcrepo.indexer.IndexerGroup">
<constructor-arg name="repositoryURL" value="http://${fcrepo.host:localhost}:${fcrepo.port:8080}${fcrepo.context:/}rest" />
<constructor-arg name="indexers">
<set>
<ref bean="fileSerializer"/>
Expand Down
Expand Up @@ -18,11 +18,16 @@
import static org.junit.Assert.assertEquals;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.junit.Test;

Expand Down Expand Up @@ -50,11 +55,17 @@ public class FedoraIndexerIT {
private static final String serverAddress = "http://" + HOSTNAME + ":" +
SERVER_PORT + CONTEXT_PATH;

private static final String repoAddress = "http://" + HOSTNAME + ":" +
SERVER_PORT + "/f4/rest/";

private static HttpClient client = new DefaultHttpClient();

@Test
public void testReindex() throws IOException {
final HttpPost reindex = new HttpPost(serverAddress + "/reindex/");
final List<NameValuePair> params = new ArrayList<>();
params.add(new BasicNameValuePair("baseURI", repoAddress));
reindex.setEntity(new UrlEncodedFormEntity(params));
final HttpResponse response = client.execute(reindex);
assertEquals(200, response.getStatusLine().getStatusCode());
//substring required for OS specific differences
Expand Down

0 comments on commit f76d835

Please sign in to comment.