diff --git a/kernel-impl/src/main/java/org/sakaiproject/content/impl/BaseContentService.java b/kernel-impl/src/main/java/org/sakaiproject/content/impl/BaseContentService.java index d16c1eb..6cc680d 100644 --- a/kernel-impl/src/main/java/org/sakaiproject/content/impl/BaseContentService.java +++ b/kernel-impl/src/main/java/org/sakaiproject/content/impl/BaseContentService.java @@ -11563,14 +11563,19 @@ SiteContentAdvisorProvider, SiteContentAdvisorTypeRegistry // return the body bytes byte[] rv = m_body; - if ((rv == null) && (m_contentLength > 0)) + if (rv == null) { // todo: try to get the body from the stream - - - // TODO: we do not store the body with the object, so as not to cache the body bytes -ggolden - rv = m_storage.getResourceBody(this); - // m_body = rv; + if (m_contentLength > 0) + { + rv = new byte[0]; + } + else + { + // TODO: we do not store the body with the object, so as not to cache the body bytes -ggolden + rv = m_storage.getResourceBody(this); + // m_body = rv; + } } return rv; diff --git a/kernel-impl/src/main/java/org/sakaiproject/content/impl/DbContentService.java b/kernel-impl/src/main/java/org/sakaiproject/content/impl/DbContentService.java index ab97527..2dab6e3 100644 --- a/kernel-impl/src/main/java/org/sakaiproject/content/impl/DbContentService.java +++ b/kernel-impl/src/main/java/org/sakaiproject/content/impl/DbContentService.java @@ -22,6 +22,7 @@ package org.sakaiproject.content.impl; +import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; @@ -1839,11 +1840,16 @@ public class DbContentService extends BaseContentService } else { - if (((BaseResourceEdit) resource).m_contentLength <= 0) + long length = ((BaseResourceEdit) resource).m_contentLength; + if (length <= 0) { - M_log.warn("getResourceBody(): non-positive content length: " + ((BaseResourceEdit) resource).m_contentLength + " id: " + if (length < 0) + { + M_log.warn("getResourceBody(): negative content length: " + length + " id: " + resource.getId()); - return null; + return null; + } + return new byte[0]; } // if we have been configured to use an external file system @@ -1957,11 +1963,16 @@ public class DbContentService extends BaseContentService } else { - if (((BaseResourceEdit) resource).m_contentLength <= 0) + long length = ((BaseResourceEdit) resource).m_contentLength; + if (length <= 0) { - M_log.warn("streamResourceBody(): non-positive content length: " + ((BaseResourceEdit) resource).m_contentLength + " id: " + if (length < 0) + { + M_log.warn("streamResourceBody(): negative content length: " + ((BaseResourceEdit) resource).m_contentLength + " id: " + resource.getId()); - return null; + return null; + } + return new ByteArrayInputStream(new byte[0]); } // if we have been configured to use an external file system @@ -1988,7 +1999,7 @@ public class DbContentService extends BaseContentService * * @param resource - * the resource for the stream It is a non-fatal error for the file not to be readible as long as the resource's expected length is - * zero. A zero length body is indicated by returning null. We check for the body length *after* we try to read the file. If the file + * zero. We check for the body length *after* we try to read the file. If the file * is readible, we simply read it and return it as the body. */ diff --git a/kernel-impl/src/test/java/org/sakaiproject/content/impl/test/ContentHostingServiceTest.java b/kernel-impl/src/test/java/org/sakaiproject/content/impl/test/ContentHostingServiceTest.java new file mode 100644 index 0000000..8fcb237 --- /dev/null +++ b/kernel-impl/src/test/java/org/sakaiproject/content/impl/test/ContentHostingServiceTest.java @@ -0,0 +1,88 @@ +package org.sakaiproject.content.impl.test; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; + +import junit.extensions.TestSetup; +import junit.framework.Test; +import junit.framework.TestSuite; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.sakaiproject.content.api.ContentHostingService; +import org.sakaiproject.content.api.ContentResource; +import org.sakaiproject.content.api.ContentResourceEdit; +import org.sakaiproject.exception.IdInvalidException; +import org.sakaiproject.exception.IdUnusedException; +import org.sakaiproject.exception.IdUsedException; +import org.sakaiproject.exception.InconsistentException; +import org.sakaiproject.exception.OverQuotaException; +import org.sakaiproject.exception.PermissionException; +import org.sakaiproject.exception.ServerOverloadException; +import org.sakaiproject.exception.TypeException; +import org.sakaiproject.test.SakaiKernelTestBase; +import org.sakaiproject.tool.api.Session; +import org.sakaiproject.tool.api.SessionManager; + +public class ContentHostingServiceTest extends SakaiKernelTestBase { + + private static final Log log = LogFactory.getLog(ContentHostingServiceTest.class); + + + public static Test suite() + { + TestSetup setup = new TestSetup(new TestSuite(ContentHostingServiceTest.class)) + { + protected void setUp() throws Exception + { + log.debug("starting oneTimeSetup"); + oneTimeSetup(null); + log.debug("finished oneTimeSetup"); + } + protected void tearDown() throws Exception + { + log.debug("starting tearDown"); + oneTimeTearDown(); + log.debug("finished tearDown"); + } + }; + return setup; + } + + + /** + * Checks the resources of zero bytes are handled correctly. + */ + public void testEmptyResources() throws Exception { + ContentHostingService ch = org.sakaiproject.content.cover.ContentHostingService.getInstance(); + SessionManager sm = org.sakaiproject.tool.cover.SessionManager.getInstance(); + Session session = sm.getCurrentSession(); + session.setUserEid("admin"); + session.setUserId("admin"); + ContentResourceEdit cr; + cr = ch.addResource("/emptyFileStreamed"); + cr.setContent(new ByteArrayInputStream(new byte[0])); + ch.commitResource(cr); + + cr = ch.addResource("/emptyFileArray"); + cr.setContent(new byte[0]); + ch.commitResource(cr); + + ContentResource resource; + InputStream stream; + resource = ch.getResource("/emptyFileStreamed"); + stream = resource.streamContent(); + assertEquals(0, stream.available()); + assertEquals(0, resource.getContentLength()); + assertEquals(0, resource.getContent().length); + + resource = ch.getResource("/emptyFileArray"); + stream = resource.streamContent(); + assertEquals(0, stream.available()); + assertEquals(0, resource.getContentLength()); + assertEquals(0, resource.getContent().length); + + + } +}