Skip to content

Commit

Permalink
Export jcr/xml without binary contents by setting the parameter skipB…
Browse files Browse the repository at this point in the history
  • Loading branch information
lsitu authored and Andrew Woods committed Jul 24, 2014
1 parent f189fd0 commit a5a17ac
Show file tree
Hide file tree
Showing 7 changed files with 189 additions and 30 deletions.
Expand Up @@ -15,6 +15,7 @@
*/
package org.fcrepo.http.api;

import static java.lang.Boolean.parseBoolean;
import static javax.ws.rs.core.Response.ok;
import static org.slf4j.LoggerFactory.getLogger;

Expand Down Expand Up @@ -67,12 +68,16 @@ public class FedoraExport extends AbstractResource {
*
* @param pathList
* @param format
* @param skipBinary
* @param recurse
* @return object in the given format
*/
@GET
public Response exportObject(
@PathParam("path") final List<PathSegment> pathList,
@QueryParam("format") @DefaultValue("jcr/xml") final String format) {
@QueryParam("format") @DefaultValue("jcr/xml") final String format,
@QueryParam("skipBinary") @DefaultValue("true") final String skipBinary,
@QueryParam("recurse") @DefaultValue("false") final String recurse) {

final String path = toPath(pathList);

Expand All @@ -91,8 +96,10 @@ public void write(final OutputStream out)
try {
LOGGER.debug("Selecting from serializer map: {}", serializers);
LOGGER.debug("Retrieved serializer for format: {}", format);
serializer.serialize(objectService.getObject(
session, path), out);
serializer.serialize(objectService.getObject(session, path),
out,
parseBoolean(skipBinary),
parseBoolean(recurse));
LOGGER.debug("Successfully serialized object: {}", path);
} catch (final RepositoryException e) {
throw new WebApplicationException(e);
Expand Down
Expand Up @@ -34,6 +34,7 @@
import org.fcrepo.kernel.FedoraObject;
import org.fcrepo.kernel.services.ObjectService;
import org.fcrepo.serialization.FedoraObjectSerializer;
import org.fcrepo.serialization.JcrXmlSerializer;
import org.fcrepo.serialization.SerializerUtil;
import org.junit.Before;
import org.junit.Test;
Expand All @@ -56,6 +57,9 @@ public class FedoraExportTest {
@Mock
private FedoraObjectSerializer mockSerializer;

@Mock
private JcrXmlSerializer mockJcrXmlSerializer;

@Mock
private ObjectService mockObjects;

Expand All @@ -80,11 +84,39 @@ public void testExportObject() throws Exception {
when(mockObjects.getObject(mockSession, "/test/object")).thenReturn(
mockObject);
((StreamingOutput) testObj.exportObject(
createPathList("test", "object"), "fake-format").getEntity())
.write(new ByteArrayOutputStream());
verify(mockSerializer).serialize(eq(mockObject),
any(OutputStream.class));
createPathList("test", "object"), "fake-format",
"false", "false").getEntity()).write(new ByteArrayOutputStream());
verify(mockSerializer).serialize(eq(mockObject), any(OutputStream.class),
eq(Boolean.valueOf("false")), eq(Boolean.valueOf("false")));

}

@Test
public void testExportObjectSkipBinary() throws Exception {
final String skipBinary = "true";
when(mockSerializers.getSerializer(FedoraObjectSerializer.JCR_XML)).thenReturn(
mockJcrXmlSerializer);
when(mockObjects.getObject(mockSession, "/test/object")).thenReturn(
mockObject);
((StreamingOutput) testObj.exportObject(
createPathList("test", "object"), FedoraObjectSerializer.JCR_XML,
"false", skipBinary).getEntity()).write(new ByteArrayOutputStream());
verify(mockJcrXmlSerializer).serialize(eq(mockObject), any(OutputStream.class),
eq(Boolean.valueOf("false")), eq(Boolean.valueOf(skipBinary)));
}

@Test
public void testExportObjectNoRecurse() throws Exception {
final String noRecurse = "true";
when(mockSerializers.getSerializer(FedoraObjectSerializer.JCR_XML)).thenReturn(
mockJcrXmlSerializer);
when(mockObjects.getObject(mockSession, "/test/object")).thenReturn(
mockObject);
((StreamingOutput) testObj.exportObject(
createPathList("test", "object"), FedoraObjectSerializer.JCR_XML,
noRecurse, "false").getEntity()).write(new ByteArrayOutputStream());
verify(mockJcrXmlSerializer).serialize(eq(mockObject),
any(OutputStream.class), eq(Boolean.valueOf(noRecurse)),
eq(Boolean.valueOf("false")));
}
}
Expand Up @@ -16,6 +16,8 @@
package org.fcrepo.integration.http.api;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import java.io.IOException;

Expand All @@ -26,6 +28,7 @@
import org.apache.http.entity.StringEntity;
import org.apache.http.util.EntityUtils;
import org.junit.Test;
import org.modeshape.common.util.Base64;

/**
* <p>FedoraExportIT class.</p>
Expand Down Expand Up @@ -122,7 +125,7 @@ public void shouldFailToImportOverExistingNode() throws IOException {
// export it
logger.debug("Attempting to export: " + objName);
final HttpGet getObjMethod =
new HttpGet(serverAddress + objName + "/fcr:export");
new HttpGet(serverAddress + objName + "/fcr:export?skipBinary=false&recurse=true");
final HttpResponse response = client.execute(getObjMethod);
assertEquals(200, response.getStatusLine().getStatusCode());
final String content = EntityUtils.toString(response.getEntity());
Expand Down Expand Up @@ -153,4 +156,65 @@ public void shouldExportUsingTheRepositoryWideApi() throws IOException {

}

@Test
public void shouldExportObjectWithNoBinary() throws IOException {
final String objName = getRandomUniquePid();
final String binaryValue = "stuff";
// set up the object
client.execute(postObjMethod(objName));
client.execute(postDSMethod(objName, "testDS", binaryValue));

// export it
logger.debug("Attempting to export: " + objName);
final HttpGet getObjMethod =
new HttpGet(serverAddress + objName + "/fcr:export?recurse=true");
HttpResponse response = client.execute(getObjMethod);
assertEquals("application/xml", response.getEntity().getContentType()
.getValue());
assertEquals(200, response.getStatusLine().getStatusCode());
logger.debug("Successfully exported: " + objName);
final String content = EntityUtils.toString(response.getEntity());
logger.debug("Found exported object: " + content);
final char[] base64Value = Base64.encodeBytes(binaryValue.getBytes("UTF-8")).toCharArray();
assertFalse(content.indexOf(String.valueOf(base64Value)) >= 0);

// Contains the binary value otherwise
final HttpGet getObjWithBinaryMethod = new HttpGet(
serverAddress + objName + "/fcr:export?recurse=true&skipBinary=false");
response = client.execute(getObjWithBinaryMethod);
assertEquals("application/xml", response.getEntity().getContentType()
.getValue());
assertEquals(200, response.getStatusLine().getStatusCode());
assertTrue(EntityUtils.toString(response.getEntity()).indexOf(String.valueOf(base64Value)) >= 0);
}

@Test
public void shouldExportObjectRecurse() throws IOException {
final String objName = getRandomUniquePid();
final String childName = "testDS";
final String binaryValue = "stuff";
// set up the object
client.execute(postObjMethod(objName));
client.execute(postDSMethod(objName, childName, binaryValue));
// export it
logger.debug("Attempting to export: " + objName);
final HttpGet getObjMethod =
new HttpGet(serverAddress + objName + "/fcr:export");
HttpResponse response = client.execute(getObjMethod);
assertEquals("application/xml", response.getEntity().getContentType()
.getValue());
assertEquals(200, response.getStatusLine().getStatusCode());
logger.debug("Successfully exported: " + objName);
final String content = EntityUtils.toString(response.getEntity());
logger.debug("Found exported object: " + content);
assertFalse(content.indexOf("sv:name=\"" + childName + "\"") > 0);

// Contains the child node otherwise
final HttpGet getObjWithBinaryMethod = new HttpGet(serverAddress + objName + "/fcr:export?recurse=true");
response = client.execute(getObjWithBinaryMethod);
assertEquals("application/xml", response.getEntity().getContentType()
.getValue());
assertEquals(200, response.getStatusLine().getStatusCode());
assertTrue(EntityUtils.toString(response.getEntity()).indexOf("sv:name=\"" + childName + "\"") > 0);
}
}
Expand Up @@ -48,7 +48,9 @@ public abstract class BaseFedoraObjectSerializer implements

@Override
public abstract void serialize(final FedoraObject obj,
final OutputStream out) throws RepositoryException, IOException;
final OutputStream out,
final boolean skipBinary,
final boolean recurse) throws RepositoryException, IOException;

@Override
public abstract void deserialize(final Session session, final String path,
Expand Down
Expand Up @@ -34,7 +34,8 @@
*/
@Component
public interface FedoraObjectSerializer {

// Key for jcr/xml serialization
final String JCR_XML = "jcr/xml";
/**
* Get the key for the serializer (that will be
* used at the REST API to identify the format)
Expand All @@ -50,16 +51,18 @@ public interface FedoraObjectSerializer {
String getMediaType();

/**
* Serialize a FedoraObject into some format, and write it
* to the given OutputStream
* Serialize a FedoraObject into some format with options for recurse
* and skipBinary, and write it to the given OutputStream
*
* @param obj
* @param out
* @param skipBinary
* @param recurse
* @throws RepositoryException
* @throws IOException
*/
void serialize(final FedoraObject obj, final OutputStream out)
throws RepositoryException, IOException;
void serialize(final FedoraObject obj, final OutputStream out, final boolean skipBinary, final boolean recurse)
throws RepositoryException, IOException;

/**
* Read the given InputStream and de-serialize the content
Expand Down
Expand Up @@ -37,7 +37,7 @@ public class JcrXmlSerializer extends BaseFedoraObjectSerializer {

@Override
public String getKey() {
return "jcr/xml";
return JCR_XML;
}

@Override
Expand All @@ -46,10 +46,24 @@ public String getMediaType() {
}

@Override
public void serialize(final FedoraObject obj, final OutputStream out)
throws RepositoryException, IOException {
/**
* Serialize JCR/XML with options for recurse and skipBinary.
* @param obj
* @param out
* @param skipBinary
* @param recurse
* @throws RepositoryException
* @throws IOException
*/
public void serialize(final FedoraObject obj,
final OutputStream out,
final boolean skipBinary,
final boolean recurse)
throws RepositoryException, IOException {
final Node node = obj.getNode();
node.getSession().exportSystemView(node.getPath(), out, false, false);
// jcr/xml export system view implemented for noRecurse:
// exportSystemView(String absPath, OutputStream out, boolean skipBinary, boolean noRecurse)
node.getSession().exportSystemView(node.getPath(), out, skipBinary, !recurse);
}

@Override
Expand Down
Expand Up @@ -13,12 +13,15 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.fcrepo.serialization.jcrxml;
package org.fcrepo.serialization;

import org.mockito.Mock;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.mockito.MockitoAnnotations.initMocks;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
Expand All @@ -29,30 +32,64 @@
import javax.jcr.Session;

import org.fcrepo.kernel.FedoraObject;
import org.fcrepo.serialization.JcrXmlSerializer;
import org.junit.Before;
import org.junit.Test;

/**
* <p>JcrXmlSerializerTest class.</p>
*
* @author cbeer
* @author lsitu
*/
public class JcrXmlSerializerTest {
@Mock
Session mockSession;

@Test
public void testSerialize() throws Exception {
final Session mockSession = mock(Session.class);
final Node mockNode = mock(Node.class);
final FedoraObject mockObject = mock(FedoraObject.class);
@Mock
Node mockNode;

@Mock
FedoraObject mockObject;

private String testPath = "/path/to/node";
@Before
public void setUp() throws Exception {
initMocks(this);
when(mockObject.getNode()).thenReturn(mockNode);
when(mockNode.getSession()).thenReturn(mockSession);
when(mockNode.getPath()).thenReturn("/path/to/node");
when(mockNode.getPath()).thenReturn(testPath);
}

@Test
public void testSerialize() throws Exception {

final OutputStream os = new ByteArrayOutputStream();

new JcrXmlSerializer().serialize(mockObject, os, false, false);

verify(mockSession).exportSystemView(testPath, os, false, !false);
}

@Test
public void testSerializeWithSkipBinary() throws Exception {

final boolean skipBinary = true;
final OutputStream os = new ByteArrayOutputStream();

new JcrXmlSerializer().serialize(mockObject, os, skipBinary, false);

verify(mockSession).exportSystemView(testPath, os, skipBinary, !false);
}

@Test
public void testSerializeWithOptions() throws Exception {

final boolean skipBinary = true;
final boolean recurse = true;
final OutputStream os = new ByteArrayOutputStream();

new JcrXmlSerializer().serialize(mockObject, os);
new JcrXmlSerializer().serialize(mockObject, os, skipBinary, recurse);

verify(mockSession).exportSystemView("/path/to/node", os, false, false);
verify(mockSession).exportSystemView(testPath, os, skipBinary, !recurse);
}

@Test
Expand All @@ -68,7 +105,7 @@ public void testDeserialize() throws Exception {

@Test
public void testGetKey() {
assertEquals("jcr/xml", new JcrXmlSerializer().getKey());
assertEquals(FedoraObjectSerializer.JCR_XML, new JcrXmlSerializer().getKey());
}

@Test
Expand Down

0 comments on commit a5a17ac

Please sign in to comment.