Skip to content

Commit

Permalink
More Checkstyle bs
Browse files Browse the repository at this point in the history
  • Loading branch information
ajs6f committed Jul 6, 2013
1 parent 95ecf12 commit 4c4e882
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 54 deletions.
109 changes: 58 additions & 51 deletions fcrepo-kernel/src/main/java/org/fcrepo/Datastream.java
Expand Up @@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.fcrepo;

import static com.codahale.metrics.MetricRegistry.name;
Expand Down Expand Up @@ -47,18 +48,20 @@

/**
* Abstraction for a Fedora datastream backed by a JCR node.
*
*
* @author ajs6f
* @date Feb 21, 2013
*/
public class Datastream extends FedoraResource implements FedoraJcrTypes {

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

static final Histogram contentSizeHistogram = getMetrics().histogram(name(Datastream.class, "content-size"));
static final Histogram contentSizeHistogram = getMetrics().histogram(
name(Datastream.class, "content-size"));

/**
* The JCR node for this datastream
*
* @param n an existing {@link Node}
*/
public Datastream(final Node n) {
Expand All @@ -67,18 +70,20 @@ public Datastream(final Node n) {

/**
* Create or find a FedoraDatastream at the given path
*
* @param session the JCR session to use to retrieve the object
* @param path the absolute path to the object
* @throws RepositoryException
*/
public Datastream(final Session session, final String path,
final String nodeType) throws RepositoryException {
final String nodeType) throws RepositoryException {
super(session, path, nodeType);
mixinTypeSpecificCrap();
}

/**
* Create or find a FedoraDatastream at the given path
*
* @param session the JCR session to use to retrieve the object
* @param path the absolute path to the object
* @throws RepositoryException
Expand All @@ -92,17 +97,17 @@ private void mixinTypeSpecificCrap() {
try {
if (node.isNew() || !hasMixin(node)) {
LOGGER.debug("Setting {} properties on a {} node...",
FEDORA_DATASTREAM, JcrConstants.NT_FILE);
FEDORA_DATASTREAM, JcrConstants.NT_FILE);
node.addMixin(FEDORA_DATASTREAM);

if (node.hasNode(JCR_CONTENT)) {
Node contentNode = node.getNode(JCR_CONTENT);
final Node contentNode = node.getNode(JCR_CONTENT);
decorateContentNode(contentNode);
}
}
} catch (RepositoryException ex) {
LOGGER.warn("Could not decorate {} with {} properties: {}" ,
JCR_CONTENT, FEDORA_DATASTREAM, ex);
} catch (final RepositoryException ex) {
LOGGER.warn("Could not decorate {} with {} properties: {}",
JCR_CONTENT, FEDORA_DATASTREAM, ex);
}
}

Expand All @@ -118,14 +123,14 @@ public InputStream getContent() throws RepositoryException {

/**
* Sets the content of this Datastream.
*
*
* @param content
* @throws RepositoryException
*/
public void setContent(final InputStream content, final String contentType,
final URI checksum,
PolicyDecisionPoint storagePolicyDecisionPoint)
throws RepositoryException, InvalidChecksumException {
final URI checksum,
final PolicyDecisionPoint storagePolicyDecisionPoint) throws RepositoryException,
InvalidChecksumException {

final Node contentNode =
findOrCreateChild(node, JCR_CONTENT, NT_RESOURCE);
Expand All @@ -138,57 +143,55 @@ public void setContent(final InputStream content, final String contentType,
contentNode.setProperty(JCR_MIME_TYPE, contentType);
}


LOGGER.debug("Created content node at path: {}", contentNode.getPath());

String hint = null;


if (storagePolicyDecisionPoint != null) {
hint = storagePolicyDecisionPoint.evaluatePolicies(node);
}

Binary binary = (Binary) getBinary(node, content, hint);
final Binary binary = (Binary) getBinary(node, content, hint);

/*
* This next line of code deserves explanation. If we chose for the
* simpler line:
* Property dataProperty = contentNode.setProperty(JCR_DATA,
* requestBodyStream);
* then the JCR would not block on the stream's completion, and we would
* return to the requester before the mutation to the repo had actually
* completed. So instead we use createBinary(requestBodyStream), because
* its contract specifies:
* "The passed InputStream is closed before this method returns either
* normally or because of an exception."
* which lets us block and not return until the job is done! The simpler
* code may still be useful to us for an asynchronous method that we
* develop later.
* simpler line: Property dataProperty =
* contentNode.setProperty(JCR_DATA, requestBodyStream); then the JCR
* would not block on the stream's completion, and we would return to
* the requester before the mutation to the repo had actually completed.
* So instead we use createBinary(requestBodyStream), because its
* contract specifies: "The passed InputStream is closed before this
* method returns either normally or because of an exception." which
* lets us block and not return until the job is done! The simpler code
* may still be useful to us for an asynchronous method that we develop
* later.
*/
final Property dataProperty = contentNode.setProperty(JCR_DATA, binary);

final String dsChecksum = binary.getHexHash();
if (checksum != null && !checksum.equals(ContentDigest.asURI("SHA-1", dsChecksum))) {
if (checksum != null &&
!checksum.equals(ContentDigest.asURI("SHA-1", dsChecksum))) {
LOGGER.debug("Failed checksum test");
throw new InvalidChecksumException("Checksum Mismatch of " +
dsChecksum + " and " + checksum);
dsChecksum + " and " + checksum);
}

decorateContentNode(contentNode);

LOGGER.debug("Created data property at path: {}",
dataProperty.getPath());
LOGGER.debug("Created data property at path: {}", dataProperty
.getPath());

}

/**
* Set the content of this datastream
*
* @param content
* @throws InvalidChecksumException
* @throws RepositoryException
*/
public void setContent(InputStream content) throws InvalidChecksumException,
RepositoryException {
public void setContent(final InputStream content)
throws InvalidChecksumException, RepositoryException {
setContent(content, null, null, null);
}

Expand All @@ -199,8 +202,8 @@ public void setContent(InputStream content) throws InvalidChecksumException,
public long getContentSize() {
try {
return node.getNode(JCR_CONTENT).getProperty(CONTENT_SIZE)
.getLong();
} catch (RepositoryException e) {
.getLong();
} catch (final RepositoryException e) {
LOGGER.error("Could not get contentSize() - " + e.getMessage());
}
// TODO Size is not stored, recalculate size?
Expand All @@ -209,25 +212,27 @@ public long getContentSize() {

/**
* Get the pre-calculated content digest for the binary payload
*
* @return a URI with the format algorithm:value
* @throws RepositoryException
*/
public URI getContentDigest() throws RepositoryException {
final Node contentNode = node.getNode(JCR_CONTENT);
try {
return new URI(contentNode.getProperty(CONTENT_DIGEST).getString());
} catch (RepositoryException e) {
} catch (final RepositoryException e) {
LOGGER.error("Could not get content digest: ", e);
} catch (URISyntaxException e) {
} catch (final URISyntaxException e) {
LOGGER.error("Could not get content digest: {}", e);
}
//TODO checksum not stored. recalculating checksum,
//however, this would defeat the purpose validating against the checksum
Binary binary = (Binary) contentNode.getProperty(JCR_DATA)
.getBinary();
String dsChecksum = binary.getHexHash();
// TODO checksum not stored. recalculating checksum,
// however, this would defeat the purpose validating against the
// checksum
final Binary binary =
(Binary) contentNode.getProperty(JCR_DATA).getBinary();
final String dsChecksum = binary.getHexHash();

return ContentDigest.asURI("SHA-1",dsChecksum);
return ContentDigest.asURI("SHA-1", dsChecksum);
}

/**
Expand All @@ -253,13 +258,14 @@ public FedoraObject getObject() throws RepositoryException {
*/
public String getMimeType() throws RepositoryException {
return node.hasNode(JCR_CONTENT) &&
node.getNode(JCR_CONTENT).hasProperty(JCR_MIME_TYPE) ?
node.getNode(JCR_CONTENT).getProperty(JCR_MIME_TYPE).getString() :
"application/octet-stream";
node.getNode(JCR_CONTENT).hasProperty(JCR_MIME_TYPE) ? node
.getNode(JCR_CONTENT).getProperty(JCR_MIME_TYPE).getString()
: "application/octet-stream";
}

/**
* Return the calculated size of the DS node
*
* @return
* @throws RepositoryException
*/
Expand All @@ -269,7 +275,7 @@ public long getSize() throws RepositoryException {

}

private void decorateContentNode(Node contentNode)
private void decorateContentNode(final Node contentNode)
throws RepositoryException {
if (contentNode == null) {
LOGGER.warn("{}/{} appears to be null!", JCR_CONTENT);
Expand All @@ -280,21 +286,22 @@ private void decorateContentNode(Node contentNode)
}

final Property dataProperty = contentNode.getProperty(JCR_DATA);
Binary binary = (Binary) dataProperty.getBinary();
final Binary binary = (Binary) dataProperty.getBinary();
final String dsChecksum = binary.getHexHash();

contentSizeHistogram.update(dataProperty.getLength());

contentNode.setProperty(CONTENT_SIZE, dataProperty.getLength());
contentNode.setProperty(CONTENT_DIGEST, ContentDigest.
asURI("SHA-1", dsChecksum).toString());
contentNode.setProperty(CONTENT_DIGEST, ContentDigest.asURI("SHA-1",
dsChecksum).toString());

LOGGER.debug("Decorated data property at path: " +
dataProperty.getPath());
dataProperty.getPath());
}

/**
* Check if the node has a fedora:datastream mixin
*
* @param node
* @return
* @throws RepositoryException
Expand Down
Expand Up @@ -170,9 +170,8 @@ public static void removeNodeProperty(final Node node,
* @return a PropertyType value
* @throws RepositoryException
*/
public static int
getPropertyType(final Node node, final String propertyName)
throws RepositoryException {
public static int getPropertyType(final Node node,
final String propertyName) throws RepositoryException {
final PropertyDefinition def =
getDefinitionForPropertyName(node, propertyName);

Expand Down

0 comments on commit 4c4e882

Please sign in to comment.