Permalink
Browse files

Adding IT for audit logging

  • Loading branch information...
escowles committed Jul 1, 2013
2 parents 5497546 + 5a20a03 commit 2ab0baf7e457e73cda3f0374e819d2185078ef91
@@ -1,3 +1,18 @@
/**
* Copyright 2013 DuraSpace, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.fcrepo.webdav;
@@ -23,7 +38,6 @@
* This class is almost entirely borrowed from
* {@link org.modeshape.web.jcr.webdav.DefaultContentMapper} except for the
* Fedora-specific behaviors.
*
*/
public class FedoraContentMapper implements ContentMapper {
@@ -61,7 +75,7 @@
private String newFolderPrimaryType;
private final Logger log = Logger.getLogger(getClass());
private final Logger logger = Logger.getLogger(getClass());
@Override
public void initialize(ServletContext servletContext) {
@@ -77,15 +91,18 @@ public void initialize(ServletContext servletContext) {
String newContentPrimaryType =
getParam(servletContext, INIT_NEW_CONTENT_PRIMARY_TYPE_NAME);
log.debug("FedoraContentMapper initial content primary types = " +
logger.debug("FedoraContentMapper initial content primary types = {}",
contentPrimaryTypes);
log.debug("FedoraContentMapper initial file primary types = " +
logger.debug("FedoraContentMapper initial file primary types = {}",
filePrimaryTypes);
log.debug("FedoraContentMapper initial new folder primary types = " +
logger.debug(
"FedoraContentMapper initial new folder primary types = {}",
newFolderPrimaryType);
log.debug("FedoraContentMapper initial new resource primary types = " +
logger.debug(
"FedoraContentMapper initial new resource primary types = {}",
newResourcePrimaryType);
log.debug("FedoraContentMapper initial new content primary types = " +
logger.debug(
"FedoraContentMapper initial new content primary types = {}",
newContentPrimaryType);
this.contentPrimaryTypes =
@@ -104,11 +121,13 @@ protected String getParam(ServletContext servletContext, String name) {
}
/**
* Returns an unmodifiable set containing the elements passed to this method
* Returns an unmodifiable set containing the elements passed in to this
* method
*
* @param elements a set of elements; may not be null
* @return an unmodifiable set containing all the elements in
* {@code elements}; never null
* @param elements
* a set of elements; may not be null
* @return an unmodifiable set containing all of the elements in
* {@code elements}; never null
*/
private static Set<String> setFor(String... elements) {
Set<String> set = new HashSet<String>(elements.length);
@@ -122,10 +141,10 @@ protected String getParam(ServletContext servletContext, String name) {
* substrings between the commas in the source string. The elements in the
* set will be {@link String#trim() trimmed}.
*
* @param commaDelimitedString input string; may not be null, but need not
* contain any commas
* @return an unmodifiable set whose elements are the trimmed substrings
* of the source string; never null
* @param commaDelimitedString
* input string; may not be null, but need not contain any commas
* @return an unmodifiable set whose elements are the trimmed substrings of
* the source string; never null
*/
private static Set<String> split(String commaDelimitedString) {
return setFor(commaDelimitedString.split("\\s*,\\s*"));
@@ -134,7 +153,9 @@ protected String getParam(ServletContext servletContext, String name) {
@Override
public InputStream getResourceContent(Node node)
throws RepositoryException {
if (!node.hasNode(CONTENT_NODE_NAME)) { return null; }
if (!node.hasNode(CONTENT_NODE_NAME)) {
return null;
}
return node.getProperty(CONTENT_NODE_NAME + "/" + DATA_PROP_NAME)
.getBinary().getStream();
}
@@ -150,7 +171,9 @@ public long getResourceLength(Node node) throws RepositoryException {
@Override
public Date getLastModified(Node node) throws RepositoryException {
if (!node.hasNode(CONTENT_NODE_NAME)) { return null; }
if (!node.hasNode(CONTENT_NODE_NAME)) {
return null;
}
return node.getProperty(CONTENT_NODE_NAME + "/" + MODIFIED_PROP_NAME)
.getDate().getTime();
@@ -162,31 +185,37 @@ public boolean isFolder(Node node) throws RepositoryException {
}
/**
* @param node the node to check
* @param node
* the node to check
* @return true if {@code node}'s primary type is one of the types in
* {@link #filePrimaryTypes}; may not be null
* @throws RepositoryException if an error occurs checking the node's
* primary type
* {@link #filePrimaryTypes}; may not be null
* @throws RepositoryException
* if an error occurs checking the node's primary type
*/
@Override
public boolean isFile(Node node) throws RepositoryException {
for (String nodeType : filePrimaryTypes) {
if (node.isNodeType(nodeType)) { return true; }
if (node.isNodeType(nodeType)) {
return true;
}
}
return false;
}
/**
* @param node the node to check
* @param node
* the node to check
* @return true if {@code node}'s primary type is one of the types in
* {@link #contentPrimaryTypes}; may not be null
* @throws RepositoryException if an error occurs checking the node's
* primary type
* {@link #contentPrimaryTypes}; may not be null
* @throws RepositoryException
* if an error occurs checking the node's primary type
*/
private boolean isContent(Node node) throws RepositoryException {
for (String nodeType : contentPrimaryTypes) {
if (node.isNodeType(nodeType)) { return true; }
if (node.isNodeType(nodeType)) {
return true;
}
}
return false;
@@ -195,8 +224,8 @@ private boolean isContent(Node node) throws RepositoryException {
@Override
public void createFile(Node parentNode, String fileName)
throws RepositoryException {
new Datastream(parentNode.getSession(), parentNode.getPath() +
"/" + fileName);
new Datastream(parentNode.getSession(), parentNode.getPath() + "/" +
fileName);
}
@@ -209,8 +238,10 @@ public void createFolder(Node parentNode, String folderName)
@Override
public long setContent(Node parentNode, String resourceName,
InputStream newContent, String contentType, String characterEncoding)
throws RepositoryException, IOException {
InputStream newContent, String contentType,
String characterEncoding) throws RepositoryException,
IOException {
Datastream ds = new Datastream(parentNode);
try {
ds.setContent(newContent, contentType, null, null, null);
@@ -0,0 +1,97 @@
/**
* Copyright 2013 DuraSpace, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.fcrepo.integration;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.PoolingClientConnectionManager;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class AuditIT {
/**
* The server port of the application, set as system property by
* maven-failsafe-plugin.
*/
private static final String SERVER_PORT = System.getProperty("test.port");
/**
* The context path of the application (including the leading "/"), set as
* system property by maven-failsafe-plugin.
*/
private static final String CONTEXT_PATH = System
.getProperty("test.context.path");
protected static final String HOSTNAME = "localhost";
protected static final String BASE_URL = "http://" + HOSTNAME + ":" +
SERVER_PORT;
protected static HttpClient client;
protected static final PoolingClientConnectionManager connectionManager =
new PoolingClientConnectionManager();
static {
connectionManager.setMaxTotal(Integer.MAX_VALUE);
connectionManager.setDefaultMaxPerRoute(5);
client = new DefaultHttpClient(connectionManager);
}
private Logger logger = LoggerFactory.getLogger(this.getClass());
@Before
public void setUp() throws Exception {
assertEquals(201, getStatus(new HttpPost(BASE_URL + CONTEXT_PATH +
"/rest/objects/fcr:new")));
}
@Test
public void test() throws SQLException {
Connection con = DriverManager.getConnection(
"jdbc:hsqldb:file:/tmp/audit.db;shutdown=true","sa","");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select count(*) from logging_event");
assertTrue(rs.next());
int rowCount = rs.getInt(1);
logger.warn("Audit events in db: " + rowCount);
assertTrue("No audit events found", rowCount > 0);
}
protected int getStatus(HttpUriRequest method)
throws ClientProtocolException, IOException {
return client.execute(method).getStatusLine().getStatusCode();
}
}
@@ -1,3 +1,18 @@
/**
* Copyright 2013 DuraSpace, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.fcrepo.integration;
@@ -1,3 +1,18 @@
/**
* Copyright 2013 DuraSpace, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.fcrepo.integration;
@@ -1,3 +1,18 @@
/**
* Copyright 2013 DuraSpace, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.fcrepo.integration;
import org.apache.http.client.ClientProtocolException;
@@ -1,9 +1,17 @@
/**
*
*/
/**
* @author Edwin Shin
* Copyright 2013 DuraSpace, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.fcrepo.integration;

0 comments on commit 2ab0baf

Please sign in to comment.