Skip to content

Commit

Permalink
Merge pull request #49 from mikedurbin/issue-48
Browse files Browse the repository at this point in the history
Updated fcrepo-java-client version and fcrepo4 version in integration…
  • Loading branch information
whikloj authored May 9, 2017
2 parents 3446103 + 1cae2e9 commit c6870a4
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 69 deletions.
7 changes: 4 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<parent>
<groupId>org.fcrepo</groupId>
<artifactId>fcrepo-parent</artifactId>
<version>4.5.0</version>
<version>4.7.2</version>
</parent>
<groupId>org.fcrepo.migration</groupId>
<artifactId>migration-utils</artifactId>
Expand All @@ -24,8 +24,8 @@
<slf4j.version>1.7.10</slf4j.version>
<junit.version>4.11</junit.version>
<fcrepo-build-tools.version>4.3.0</fcrepo-build-tools.version>
<fcrepo-java-client.version>0.1.2</fcrepo-java-client.version>
<fcrepo.version>4.4.1-SNAPSHOT</fcrepo.version>
<fcrepo-java-client.version>0.2.1</fcrepo-java-client.version>
<fcrepo.version>4.7.2</fcrepo.version>
</properties>

<scm>
Expand Down Expand Up @@ -401,6 +401,7 @@
<systemProperties>
<fcrepo.dynamic.jms.port>${fcrepo.dynamic.jms.port}</fcrepo.dynamic.jms.port>
<fcrepo.dynamic.stomp.port>${fcrepo.dynamic.stomp.port}</fcrepo.dynamic.stomp.port>
<fcrepo.modeshape.configuration>classpath:/config/file-simple/repository.json</fcrepo.modeshape.configuration>
</systemProperties>
</container>
</configuration>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ public StatelessFedora4Client(final String username, final String password, fina

private FcrepoClient getClient() {
try {
return new FcrepoClient(username, password, new URI(baseUri).toURL().getHost(), false);
return new FcrepoClient.FcrepoClientBuilder()
.credentials(username, password)
.authScope(new URI(baseUri).toURL().getHost()).build();
} catch (MalformedURLException | URISyntaxException e) {
throw new RuntimeException(e);
}
Expand Down Expand Up @@ -98,7 +100,7 @@ private String uriToPath(final String URI) {
@Override
public boolean exists(final String path) {
try {
return getClient().head(pathToURI(path)).getStatusCode() != 404;
return getClient().head(pathToURI(path)).perform().getStatusCode() != 404;
} catch (FcrepoOperationFailedException | URISyntaxException e) {
throw new RuntimeException(e);
}
Expand All @@ -107,7 +109,7 @@ public boolean exists(final String path) {
@Override
public void createResource(final String path) {
try {
assertSuccess(getClient().put(pathToURI(path), null, null));
assertSuccess(getClient().put(pathToURI(path)).perform());
} catch (FcrepoOperationFailedException | URISyntaxException e) {
throw new RuntimeException(e);
}
Expand All @@ -121,8 +123,10 @@ public String getRepositoryUrl() {
@Override
public void createOrUpdateRedirectNonRDFResource(final String path, final String url) {
try {
assertSuccess(getClient().put(pathToURI(path), null,
"message/external-body; access-type=URL; URL=\"" + url.toString() + "\""));

assertSuccess(getClient().put(pathToURI(path))
.body((InputStream) null, "message/external-body; access-type=URL; URL=\"" + url.toString() + "\"")
.perform());
} catch (FcrepoOperationFailedException | URISyntaxException e) {
throw new RuntimeException(e);
}
Expand All @@ -131,7 +135,7 @@ public void createOrUpdateRedirectNonRDFResource(final String path, final String
@Override
public void createOrUpdateNonRDFResource(final String path, final InputStream content, final String contentType) {
try {
assertSuccess(getClient().put(pathToURI(path), content, contentType));
assertSuccess(getClient().put(pathToURI(path)).body(content, contentType).perform());
} catch (FcrepoOperationFailedException | URISyntaxException e) {
throw new RuntimeException(e);
} finally {
Expand Down Expand Up @@ -170,7 +174,8 @@ public void createVersionSnapshot(final String path,final String versionId) {
@Override
public void updateResourceProperties(final String path, final String sparqlUpdate) {
try {
assertSuccess(getClient().patch(pathToURI(path), new ByteArrayInputStream(sparqlUpdate.getBytes("UTF-8"))));
assertSuccess(getClient().patch(pathToURI(path))
.body(new ByteArrayInputStream(sparqlUpdate.getBytes("UTF-8"))).perform());
} catch (FcrepoOperationFailedException | UnsupportedEncodingException | URISyntaxException e) {
throw new RuntimeException(e);
}
Expand All @@ -179,8 +184,8 @@ public void updateResourceProperties(final String path, final String sparqlUpdat
@Override
public void updateNonRDFResourceProperties(final String path, final String sparqlUpdate) {
try {
assertSuccess(getClient().patch(pathToURI(path + "/fcr:metadata"),
new ByteArrayInputStream(sparqlUpdate.getBytes("UTF-8"))));
assertSuccess(getClient().patch(pathToURI(path + "/fcr:metadata"))
.body(new ByteArrayInputStream(sparqlUpdate.getBytes("UTF-8"))).perform());
} catch (FcrepoOperationFailedException | UnsupportedEncodingException | URISyntaxException e) {
throw new RuntimeException(e);
}
Expand All @@ -190,7 +195,7 @@ public void updateNonRDFResourceProperties(final String path, final String sparq
public String createPlaceholder(final String path) {
if (path == null || path.length() == 0) {
try {
final FcrepoResponse r = getClient().post(new URI(baseUri), null, null);
final FcrepoResponse r = getClient().post(new URI(baseUri)).perform();
assertSuccess(r);
return uriToPath(r.getLocation().toString());
} catch (FcrepoOperationFailedException | URISyntaxException e) {
Expand All @@ -207,16 +212,18 @@ public String createNonRDFPlaceholder(final String path) {
if (!exists(path)) {
if (path == null || path.length() == 0) {
try {
final FcrepoResponse r = getClient().post(new URI(baseUri), null, "text/plain");
final FcrepoResponse r = getClient().post(new URI(baseUri))
.body((InputStream) null, "text/plain").perform();
assertSuccess(r);
return uriToPath(r.getLocation().toString());
} catch (FcrepoOperationFailedException | URISyntaxException e) {
throw new RuntimeException(e);
}
} else {
try {
assertSuccess(getClient().put(pathToURI(path), null, "text/xml"));
} catch (FcrepoOperationFailedException | URISyntaxException e) {
assertSuccess(getClient().put(pathToURI(path))
.body(new ByteArrayInputStream("".getBytes("UTF-8")), "text/xml").perform());
} catch (FcrepoOperationFailedException | URISyntaxException | UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
return path;
Expand All @@ -229,7 +236,7 @@ public String createNonRDFPlaceholder(final String path) {
@Override
public boolean isPlaceholder(final String path) {
try {
return getClient().get(pathToURI(path + "/fcr:versions"), null, null).getStatusCode() == 404;
return getClient().get(pathToURI(path + "/fcr:versions")).perform().getStatusCode() == 404;
} catch (FcrepoOperationFailedException | URISyntaxException e) {
throw new RuntimeException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,17 @@
*/
package org.fcrepo.migration.handlers;

import static com.hp.hpl.jena.graph.Node.ANY;
import static com.hp.hpl.jena.rdf.model.ModelFactory.createDefaultModel;
import static org.apache.jena.riot.RDFLanguages.contentTypeToLang;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.slf4j.LoggerFactory.getLogger;

import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;

import javax.ws.rs.core.MediaType;
import javax.xml.stream.XMLStreamException;

import org.apache.http.HttpEntity;
import com.hp.hpl.jena.graph.NodeFactory;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.update.GraphStore;
import com.hp.hpl.jena.update.GraphStoreFactory;
import junit.framework.Assert;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.jena.riot.Lang;
import org.fcrepo.client.FcrepoHttpClientBuilder;
import org.fcrepo.client.HttpMethods;
import org.apache.jena.atlas.web.ContentType;
import org.apache.jena.riot.RDFLanguages;
import org.fcrepo.client.FcrepoClient;
import org.fcrepo.client.FcrepoOperationFailedException;
import org.fcrepo.client.FcrepoResponse;
import org.fcrepo.migration.Fedora4Client;
import org.fcrepo.migration.MigrationIDMapper;
import org.fcrepo.migration.Migrator;
Expand All @@ -47,12 +35,16 @@
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.hp.hpl.jena.graph.NodeFactory;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.update.GraphStore;
import com.hp.hpl.jena.update.GraphStoreFactory;
import javax.xml.stream.XMLStreamException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;

import junit.framework.Assert;
import static com.hp.hpl.jena.graph.Node.ANY;
import static com.hp.hpl.jena.rdf.model.ModelFactory.createDefaultModel;
import static org.junit.Assert.assertTrue;
import static org.slf4j.LoggerFactory.getLogger;

/**
* @author mdurbin
Expand Down Expand Up @@ -98,7 +90,8 @@ public void testExample1DatastreamsWereCreated() {
}

@Test
public void testCustomPropertyMapping() throws ClientProtocolException, URISyntaxException, IOException {
public void testCustomPropertyMapping() throws ClientProtocolException, URISyntaxException, IOException,
FcrepoOperationFailedException {
final GraphStore g = getResourceTriples(idMapper.mapObjectPath("example:1"));
assertTrue("Unable to find mapped PID.",
g.contains(ANY, ANY, NodeFactory.createURI("http://fake/fake/pid"),
Expand All @@ -107,40 +100,24 @@ public void testCustomPropertyMapping() throws ClientProtocolException, URISynta
}

@Test
public void testDatastreamProperties() throws ClientProtocolException, URISyntaxException, IOException {
public void testDatastreamProperties() throws ClientProtocolException, URISyntaxException, IOException,
FcrepoOperationFailedException {
final GraphStore g = getResourceTriples(idMapper.mapDatastreamPath("example:1", "DS1") + "/fcr:metadata");
assertTrue("Unable to find datastream label in migrated resource RDF assertions.",
g.contains(ANY, ANY, NodeFactory.createURI("http://purl.org/dc/terms/title"),
NodeFactory.createLiteral("Example inline XML datastream")));
}

private GraphStore getResourceTriples(final String path) throws URISyntaxException,
ClientProtocolException, IOException {
final FcrepoHttpClientBuilder b = new FcrepoHttpClientBuilder(null, null, client.getRepositoryUrl());
try (final CloseableHttpClient c = b.build()) {
final HttpMethods method = HttpMethods.GET;
final URI uri = new URI(client.getRepositoryUrl() + path);
final HttpRequestBase request = method.createRequest(uri);
try (final CloseableHttpResponse response = c.execute(request)) {
return parseTriples(response.getEntity());
}
}
}

private static String getRdfSerialization(final HttpEntity entity) {
final MediaType mediaType = MediaType.valueOf(entity.getContentType().getValue());
final Lang lang = contentTypeToLang(mediaType.toString());
assertNotNull("Entity is not an RDF serialization", lang);
return lang.getName();
}

private static GraphStore parseTriples(final HttpEntity entity) throws IOException {
return parseTriples(entity.getContent(), getRdfSerialization(entity));
private GraphStore getResourceTriples(final String path) throws URISyntaxException, IOException,
FcrepoOperationFailedException {
final FcrepoClient c = new FcrepoClient.FcrepoClientBuilder().build();
final FcrepoResponse r = c.get(URI.create(client.getRepositoryUrl() + path)).perform();
return parseTriples(r.getBody(), r.getContentType());
}

private static GraphStore parseTriples(final InputStream content, final String contentType) {
final Model model = createDefaultModel();
model.read(content, "", contentType);
model.read(content, "", RDFLanguages.contentTypeToLang(ContentType.create(contentType)).getName());
return GraphStoreFactory.create(model);
}

Expand Down

0 comments on commit c6870a4

Please sign in to comment.