Skip to content

Commit

Permalink
DefaultFilesystemStoreImpl refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul Warren committed Jun 29, 2018
1 parent ff91903 commit 375fa43
Show file tree
Hide file tree
Showing 2 changed files with 354 additions and 206 deletions.
Expand Up @@ -47,33 +47,28 @@ public DefaultFilesystemStoreImpl(FileSystemResourceLoader loader,

@Override
public Resource getResource(SID id) {
return getResourceInternal(id);
String location = conversion.convert(id, String.class);
Resource resource = loader.getResource(location);
return resource;
}

@Override
public Resource getResource(S entity) {
Resource r = getResourceInternal(entity);
if (r != null) {
return r;
Resource resource = null;
if (conversion.canConvert(entity.getClass(), String.class)) {
String location = conversion.convert(entity, String.class);
resource = loader.getResource(location);
if (resource != null) {
return resource;
}
}

SID contentId = (SID) BeanUtils.getFieldWithAnnotation(entity, ContentId.class);
if (contentId == null) {
return null;
if (contentId != null) {
return getResource(contentId);
}
return getResourceInternal(contentId);
}

protected Resource getResourceInternal(S entity) {
String location = conversion.convert(entity, String.class);
Resource resource = loader.getResource(location);
return resource;
}

protected Resource getResourceInternal(SID id) {
String location = conversion.convert(id, String.class);
Resource resource = loader.getResource(location);
return resource;
return null;
}

@Override
Expand Down Expand Up @@ -127,14 +122,7 @@ public void setContent(S property, InputStream content) {
property.toString()), e);
}
finally {
try {
if (os != null) {
os.close();
}
}
catch (IOException ioe) {
// ignore
}
IOUtils.closeQuietly(os);
}

try {
Expand All @@ -149,47 +137,39 @@ public void setContent(S property, InputStream content) {
}

@Override
public InputStream getContent(S property) {
if (property == null)
return null;
SID contentId = (SID) BeanUtils.getFieldWithAnnotation(property, ContentId.class);
if (contentId == null)
public InputStream getContent(S entity) {
if (entity == null)
return null;

Resource resource = getResourceInternal(contentId);
Resource resource = getResource(entity);

try {
if (resource.exists()) {
if (resource != null && resource.exists()) {
return resource.getInputStream();
}
}
catch (IOException e) {
logger.error(String.format("Unexpected error getting content %s",
contentId.toString()), e);
logger.error(String.format("Unexpected error getting content for entity %s",
entity), e);
}

return null;
}

@Override
public void unsetContent(S property) {
if (property == null)
return;

SID contentId = (SID) BeanUtils.getFieldWithAnnotation(property, ContentId.class);
if (contentId == null)
public void unsetContent(S entity) {
if (entity == null)
return;

// delete any existing content object
Resource resource = getResourceInternal(contentId);
Resource resource = getResource(entity);

if (resource.exists() && resource instanceof DeletableResource) {
if (resource != null && resource.exists() && resource instanceof DeletableResource) {
((DeletableResource) resource).delete();
}

// reset content fields
unassociate(property);
BeanUtils.setFieldWithAnnotation(property, ContentLength.class, 0);
unassociate(entity);
BeanUtils.setFieldWithAnnotation(entity, ContentLength.class, 0);
}

private Object convertToExternalContentIdType(S property, Object contentId) {
Expand Down

0 comments on commit 375fa43

Please sign in to comment.