Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorVioleta Georgieva2011-07-04 07:57:59 +0000
committerGlyn Normington2011-07-19 13:32:22 +0000
commitfb5f23e0c42ba97339d726cbfd6b92a0d31c85d4 (patch)
tree31e2277fc4c55204e3845228fa9bb6bfcca67172
parent20dd9588769725fcb5ae55277af89b38082b7f83 (diff)
downloadorg.eclipse.gemini.web.gemini-web-container-fb5f23e0c42ba97339d726cbfd6b92a0d31c85d4.tar.gz
org.eclipse.gemini.web.gemini-web-container-fb5f23e0c42ba97339d726cbfd6b92a0d31c85d4.tar.xz
org.eclipse.gemini.web.gemini-web-container-fb5f23e0c42ba97339d726cbfd6b92a0d31c85d4.zip
bug 351030: When one is using DefaultServlet of Tomcat to browse directory, one always has to see the file sizes.
-rw-r--r--org.eclipse.gemini.web.test/src/test/java/org/eclipse/gemini/web/test/tomcat/TomcatServletContainerTests.java29
-rw-r--r--org.eclipse.gemini.web.tomcat/src/main/java/org/eclipse/gemini/web/tomcat/internal/loading/BundleEntryAttributes.java93
-rw-r--r--org.eclipse.gemini.web.tomcat/src/test/java/org/eclipse/gemini/web/tomcat/internal/loading/BundleDirContextTests.java26
3 files changed, 119 insertions, 29 deletions
diff --git a/org.eclipse.gemini.web.test/src/test/java/org/eclipse/gemini/web/test/tomcat/TomcatServletContainerTests.java b/org.eclipse.gemini.web.test/src/test/java/org/eclipse/gemini/web/test/tomcat/TomcatServletContainerTests.java
index 787192d..c3aa799 100644
--- a/org.eclipse.gemini.web.test/src/test/java/org/eclipse/gemini/web/test/tomcat/TomcatServletContainerTests.java
+++ b/org.eclipse.gemini.web.test/src/test/java/org/eclipse/gemini/web/test/tomcat/TomcatServletContainerTests.java
@@ -102,8 +102,8 @@ public class TomcatServletContainerTests {
@Before
public void before() throws Exception {
this.bundleContext = TestFrameworkUtils.getBundleContextForTestClass(getClass());
- ServiceReference ref = bundleContext.getServiceReference(ServletContainer.class.getName());
- this.container = (ServletContainer) bundleContext.getService(ref);
+ ServiceReference<?> ref = this.bundleContext.getServiceReference(ServletContainer.class.getName());
+ this.container = (ServletContainer) this.bundleContext.getService(ref);
}
@Test
@@ -197,13 +197,13 @@ public class TomcatServletContainerTests {
try {
Bundle bundle = this.bundleContext.installBundle(LOCATION_WAR_WITH_TLD_FROM_DEPENDENCY);
-
+
try {
bundle.start();
WebApplicationHandle handle = this.container.createWebApplication("/war-with-tld-from-dependency", bundle);
this.container.startWebApplication(handle);
-
+
try {
String realPath = handle.getServletContext().getRealPath("/");
System.out.println(realPath);
@@ -236,7 +236,7 @@ public class TomcatServletContainerTests {
WebApplicationHandle handle = this.container.createWebApplication("/war-with-tld-from-dependency", bundle);
this.container.startWebApplication(handle);
-
+
try {
validateURL("http://localhost:8080/war-with-tld-from-dependency/test.jsp");
} finally {
@@ -342,6 +342,23 @@ public class TomcatServletContainerTests {
}
}
+ private void validateURLExpectedContent(String path, String... extectedContent) throws MalformedURLException, IOException {
+ URL url = new URL(path);
+ InputStream stream = url.openConnection().getInputStream();
+ assertNotNull(stream);
+ BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
+ StringBuilder stringBuilder = new StringBuilder();
+ String line = null;
+ while ((line = reader.readLine()) != null) {
+ stringBuilder.append(line);
+ }
+ String content = stringBuilder.toString();
+ System.out.println(content);
+ for (int j = 0; j < extectedContent.length; j++) {
+ assertTrue(content.contains(extectedContent[j]));
+ }
+ }
+
private void validateNotFound(String path) throws Exception {
URL url = new URL(path);
try {
@@ -492,7 +509,7 @@ public class TomcatServletContainerTests {
WebApplicationHandle handle = this.container.createWebApplication("/war-with-tld", bundle);
this.container.startWebApplication(handle);
try {
- validateURL("http://localhost:8080/war-with-tld");
+ validateURLExpectedContent("http://localhost:8080/war-with-tld", new String[] { "test.jsp", "0.2 kb" });
} finally {
this.container.stopWebApplication(handle);
bundle.uninstall();
diff --git a/org.eclipse.gemini.web.tomcat/src/main/java/org/eclipse/gemini/web/tomcat/internal/loading/BundleEntryAttributes.java b/org.eclipse.gemini.web.tomcat/src/main/java/org/eclipse/gemini/web/tomcat/internal/loading/BundleEntryAttributes.java
index f237881..43161bf 100644
--- a/org.eclipse.gemini.web.tomcat/src/main/java/org/eclipse/gemini/web/tomcat/internal/loading/BundleEntryAttributes.java
+++ b/org.eclipse.gemini.web.tomcat/src/main/java/org/eclipse/gemini/web/tomcat/internal/loading/BundleEntryAttributes.java
@@ -32,14 +32,14 @@ final class BundleEntryAttributes extends ResourceAttributes {
private static final long TIME_NOT_SET = -1L;
+ private static final long CONTENT_LENGTH_NOT_SET = -1L;
+
private static final long serialVersionUID = 7799793247259935763L;
private final transient BundleEntry bundleEntry;
private final String[] attrIds;
- private long lastModified = TIME_NOT_SET;
-
/**
* Creates a {@link BundleEntryAttributes} for the given {@link BundleEntry} and attribute identifier array.
*
@@ -52,8 +52,13 @@ final class BundleEntryAttributes extends ResourceAttributes {
this.attrIds = attrIds;
setCollection(this.bundleEntry.isDirectory());
getName();
- getLastModified();
- getCreation();
+ try {
+ URLConnection urlConnection = getBundleEntryURLConnection();
+ getLastModified(urlConnection);
+ getCreation(urlConnection);
+ getContentLength(urlConnection);
+ } catch (IOException e) {
+ }
}
/**
@@ -61,27 +66,35 @@ final class BundleEntryAttributes extends ResourceAttributes {
*/
@Override
public long getCreation() {
+ try {
+ return getCreation(getBundleEntryURLConnection());
+ } catch (IOException e) {
+ }
+ return TIME_NOT_SET;
+ }
+
+ private long getCreation(URLConnection urlConnection) {
long creation = TIME_NOT_SET;
if (attrPresent(CREATION_DATE) || attrPresent(ALTERNATE_CREATION_DATE)) {
creation = super.getCreation();
if (creation == TIME_NOT_SET) {
- try {
- URLConnection urlConnection = this.bundleEntry.getURL().openConnection();
- creation = urlConnection.getDate();
- if (creation == CREATION_DATE_UNKNOWN) {
- creation = determineLastModified();
- }
- setCreation(creation);
- } catch (IOException _) {
+ creation = determineDate(urlConnection);
+ if (creation == CREATION_DATE_UNKNOWN) {
+ creation = determineLastModified(urlConnection);
}
+ setCreation(creation);
}
}
return creation;
}
+ private long determineDate(URLConnection urlConnection) {
+ return urlConnection.getDate();
+ }
+
private boolean attrPresent(String attrId) {
if (this.attrIds == null) {
return true;
@@ -101,13 +114,21 @@ final class BundleEntryAttributes extends ResourceAttributes {
*/
@Override
public long getLastModified() {
+ try {
+ return getLastModified(getBundleEntryURLConnection());
+ } catch (IOException e) {
+ }
+ return TIME_NOT_SET;
+ }
+
+ private long getLastModified(URLConnection urlConnection) {
long lastModified = TIME_NOT_SET;
if (attrPresent(LAST_MODIFIED) || attrPresent(ALTERNATE_LAST_MODIFIED)) {
lastModified = super.getLastModified();
if (lastModified == TIME_NOT_SET) {
- lastModified = determineLastModified();
+ lastModified = determineLastModified(urlConnection);
if (lastModified != TIME_NOT_SET) {
setLastModified(lastModified);
@@ -118,15 +139,8 @@ final class BundleEntryAttributes extends ResourceAttributes {
return lastModified;
}
- private long determineLastModified() {
- if (this.lastModified == TIME_NOT_SET) {
- try {
- URLConnection urlConnection = this.bundleEntry.getURL().openConnection();
- this.lastModified = urlConnection.getLastModified();
- } catch (IOException _) {
- }
- }
- return this.lastModified;
+ private long determineLastModified(URLConnection urlConnection) {
+ return urlConnection.getLastModified();
}
/**
@@ -145,4 +159,39 @@ final class BundleEntryAttributes extends ResourceAttributes {
return name;
}
+ @Override
+ public long getContentLength() {
+ try {
+ return getContentLength(getBundleEntryURLConnection());
+ } catch (IOException e) {
+ }
+ return CONTENT_LENGTH_NOT_SET;
+ }
+
+ private long getContentLength(URLConnection urlConnection) {
+ long contentLength = CONTENT_LENGTH_NOT_SET;
+
+ if (attrPresent(CONTENT_LENGTH) || attrPresent(ALTERNATE_CONTENT_LENGTH)) {
+ contentLength = super.getContentLength();
+
+ if (contentLength == CONTENT_LENGTH_NOT_SET) {
+ contentLength = determineContentLength(urlConnection);
+
+ if (contentLength != CONTENT_LENGTH_NOT_SET) {
+ setContentLength(contentLength);
+ }
+ }
+ }
+
+ return contentLength;
+ }
+
+ private long determineContentLength(URLConnection urlConnection) {
+ return urlConnection.getContentLength();
+ }
+
+ private URLConnection getBundleEntryURLConnection() throws IOException {
+ return this.bundleEntry.getURL().openConnection();
+ }
+
}
diff --git a/org.eclipse.gemini.web.tomcat/src/test/java/org/eclipse/gemini/web/tomcat/internal/loading/BundleDirContextTests.java b/org.eclipse.gemini.web.tomcat/src/test/java/org/eclipse/gemini/web/tomcat/internal/loading/BundleDirContextTests.java
index 7ed3881..a0154f7 100644
--- a/org.eclipse.gemini.web.tomcat/src/test/java/org/eclipse/gemini/web/tomcat/internal/loading/BundleDirContextTests.java
+++ b/org.eclipse.gemini.web.tomcat/src/test/java/org/eclipse/gemini/web/tomcat/internal/loading/BundleDirContextTests.java
@@ -86,6 +86,16 @@ public class BundleDirContextTests {
checkDirectoryResourceType(attributes);
checkTimes(attributes);
+
+ checkContentLength(attributes);
+ }
+
+ private void checkContentLength(Attributes attributes) {
+ Attribute contentLength = attributes.get(org.apache.naming.resources.ResourceAttributes.CONTENT_LENGTH);
+ Assert.assertNotNull(contentLength);
+
+ contentLength = attributes.get(org.apache.naming.resources.ResourceAttributes.ALTERNATE_CONTENT_LENGTH);
+ Assert.assertNotNull(contentLength);
}
private void checkName(Attributes attributes, String expectedName) throws NamingException {
@@ -122,6 +132,8 @@ public class BundleDirContextTests {
checkNoResourceType(attributes);
checkTimes(attributes);
+
+ checkContentLength(attributes);
}
private void checkNoResourceType(Attributes attributes) {
@@ -152,8 +164,18 @@ public class BundleDirContextTests {
checkNoResourceType(attributes);
checkNoTimes(attributes);
+
+ checkNoContentLength(attributes);
}
-
+
+ private void checkNoContentLength(Attributes attributes) {
+ Attribute contentLength = attributes.get(org.apache.naming.resources.ResourceAttributes.CONTENT_LENGTH);
+ Assert.assertNull(contentLength);
+
+ contentLength = attributes.get(org.apache.naming.resources.ResourceAttributes.ALTERNATE_CONTENT_LENGTH);
+ Assert.assertNull(contentLength);
+ }
+
/**
* @throws NamingException
*/
@@ -186,6 +208,8 @@ public class BundleDirContextTests {
checkNoResourceType(attributes);
checkOnlyCreationDate(attributes);
+
+ checkNoContentLength(attributes);
}
private void checkOnlyCreationDate(Attributes attributes) {

Back to the top