Author: jvermillard
Date: Wed Feb 13 08:34:21 2008
New Revision: 627515

URL: http://svn.apache.org/viewvc?rev=627515&view=rev
Log:
request with directory URI without '/' return a 301 moved permanently
fixed the directory indexer (added parent directory link) and cleaned some code

Added:
    mina/asyncweb/trunk/examples/src/main/resources/org/
    mina/asyncweb/trunk/examples/src/main/resources/org/apache/
    mina/asyncweb/trunk/examples/src/main/resources/org/apache/asyncweb/
    
mina/asyncweb/trunk/examples/src/main/resources/org/apache/asyncweb/examples/
    
mina/asyncweb/trunk/examples/src/main/resources/org/apache/asyncweb/examples/file/
    
mina/asyncweb/trunk/examples/src/main/resources/org/apache/asyncweb/examples/file/index/
    
mina/asyncweb/trunk/examples/src/main/resources/org/apache/asyncweb/examples/file/index/strings.properties
Modified:
    
mina/asyncweb/trunk/examples/src/main/java/org/apache/asyncweb/examples/file/FileHttpService.java
    
mina/asyncweb/trunk/examples/src/main/java/org/apache/asyncweb/examples/file/index/DefaultDirectoryIndexGenerator.java

Modified: 
mina/asyncweb/trunk/examples/src/main/java/org/apache/asyncweb/examples/file/FileHttpService.java
URL: 
http://svn.apache.org/viewvc/mina/asyncweb/trunk/examples/src/main/java/org/apache/asyncweb/examples/file/FileHttpService.java?rev=627515&r1=627514&r2=627515&view=diff
==============================================================================
--- 
mina/asyncweb/trunk/examples/src/main/java/org/apache/asyncweb/examples/file/FileHttpService.java
 (original)
+++ 
mina/asyncweb/trunk/examples/src/main/java/org/apache/asyncweb/examples/file/FileHttpService.java
 Wed Feb 13 08:34:21 2008
@@ -71,6 +71,7 @@
         if (baseUrl == null || basePath == null)
             throw new InvalidParameterException("Null parameters");
         File f = new File(basePath);
+        
         if (!(f.isDirectory() && f.exists()))
             throw new InvalidParameterException("The base path [ " + basePath
                     + " ] is not a valid path.");
@@ -85,6 +86,7 @@
         URI uri = context.getRequest().getRequestUri();
         String path = uri.getPath();
         LOG.info("Handling file request : [ " + uri+" ] from [ 
"+context.getRemoteAddress()+" ]");
+        
         if (!path.startsWith(baseUrl)) {
             // error the requested URL is not in the base URL
             //TODO : find the good exception to throw
@@ -96,24 +98,43 @@
         path = path.substring(baseUrl.length());
         File f = new File(basePath + File.separator + path);
 
-        if (f.exists() && ( indexGenerator!=null ||!f.isDirectory() ) ) {
+        if (f.isDirectory()) {
             
-            if (f.isDirectory()) {
-                // search for index file
-                String[] indexes=f.list(indexFileFilter);
-                if(indexes.length==0) {
-                    if(indexGenerator!=null) {
-                        IoBuffer indexResponse = 
indexGenerator.generateIndex(f); 
-                        indexResponse.flip();
-                        response.setContent(indexResponse);
-                        response.setHeader("Content-Type","text/html");
-                        response.setStatus(HttpResponseStatus.OK);
-                        context.commitResponse(response);
-                        return;
-                    }
-                } else
-                    f=new File(f.getAbsolutePath()+File.separator+indexes[0]);
+            // is the request finishing by the '/' character ?
+            String urlStr=uri.toString();
+            
+            if (urlStr.charAt(urlStr.length()-1)!='/' ) {
+                
+                if(LOG.isDebugEnabled())
+                    LOG.debug("Redirecting "+urlStr+" to "+urlStr+"/");
+                
+                // send back the good URI
+                response.setStatus(HttpResponseStatus.MOVED_PERMANENTLY);
+                response.setHeader("Location", urlStr+"/");
+                context.commitResponse(response);
+                return;
             }
+           
+            // search for index file
+            String[] indexes=f.list(indexFileFilter);
+            if(indexes.length==0) {
+                LOG.info("Serving directory index for [ " + 
f.getAbsolutePath() + " ]");
+                if(indexGenerator!=null) {
+                    // create a directory index page
+                    IoBuffer indexResponse = indexGenerator.generateIndex(f); 
+                    indexResponse.flip();
+                    response.setContent(indexResponse);
+                    response.setHeader("Content-Type","text/html");
+                    response.setStatus(HttpResponseStatus.OK);
+                    context.commitResponse(response);
+                    return;
+                }
+            } else
+                // just serve the index file (ex:index.html) 
+                f=new File(f.getAbsolutePath()+File.separator+indexes[0]);
+            
+        }
+        if (f.exists() && !f.isDirectory() ) {
             
             LOG.info("Serving file [ " + f.getAbsolutePath() + " ]");
 

Modified: 
mina/asyncweb/trunk/examples/src/main/java/org/apache/asyncweb/examples/file/index/DefaultDirectoryIndexGenerator.java
URL: 
http://svn.apache.org/viewvc/mina/asyncweb/trunk/examples/src/main/java/org/apache/asyncweb/examples/file/index/DefaultDirectoryIndexGenerator.java?rev=627515&r1=627514&r2=627515&view=diff
==============================================================================
--- 
mina/asyncweb/trunk/examples/src/main/java/org/apache/asyncweb/examples/file/index/DefaultDirectoryIndexGenerator.java
 (original)
+++ 
mina/asyncweb/trunk/examples/src/main/java/org/apache/asyncweb/examples/file/index/DefaultDirectoryIndexGenerator.java
 Wed Feb 13 08:34:21 2008
@@ -25,10 +25,19 @@
 
 import org.apache.asyncweb.examples.file.mimetype.MimeMap;
 import org.apache.asyncweb.server.errorReporting.CSS;
+import org.apache.asyncweb.server.util.StringBundle;
 import org.apache.mina.common.IoBuffer;
 
+/**
+ * A simply directory index page generator, generating page in the error page 
style.
+ * @author The Apache MINA Project ([EMAIL PROTECTED])
+ */
 public class DefaultDirectoryIndexGenerator implements DirectoryIndexGenerator 
{
 
+    private static final StringBundle bundle = StringBundle
+            .getBundle(DefaultDirectoryIndexGenerator.class.getPackage()
+                    .getName());
+
     public IoBuffer generateIndex(File directory) {
         File[] files = directory.listFiles();
 
@@ -38,19 +47,28 @@
         html.append(directory.getName());
         html.append("</title><style><!--");
         CSS.appendTo(html).append("--></style>");
-        html.append("</head></body>");
+        html.append("</head></body>\n");
         html.append("<H1>Index of : ").append(directory.getName()).append(
                 "</H1>\n");
-        html
-                .append("<table 
cellpadding=\"5\"><tr><th>Name</th><th>Type</th><th>Size</th><th>Last-modified</th></tr>");
+        html.append("<table cellpadding=\"5\"><tr><th>");
+        html.append(bundle.getString("nameHeader")).append("</th><th>");
+        html.append(bundle.getString("typeHeader")).append("</th><th>");
+        html.append(bundle.getString("sizeHeader")).append("</th><th>");
+        html.append(bundle.getString("lastModifiedHeader"))
+                .append("</th></tr>\n");
+
+        // back to parent directory
+        html.append("<tr><td><a href=\"..\">").append(
+                bundle.getString("parentDirectory"));
+        html.append("</a></td><td>DIR</td><td></td><td></td></tr>\n");
 
         for (File file : files) {
             html.append("<tr><td><a href=\"").append(file.getName());
-            if(file.isDirectory())
+            if (file.isDirectory())
                 html.append("/");
             html.append("\">");
             html.append(file.getName());
-            if(file.isDirectory())
+            if (file.isDirectory())
                 html.append("/");
             html.append("</a></td><td>");
             html.append(getType(file));
@@ -58,9 +76,9 @@
             html.append(file.length());
             html.append("</td><td>");
             html.append(new Date(file.lastModified()));
-            html.append("</td></tr>");
+            html.append("</td></tr>\n");
         }
-        html.append("</table>");
+        html.append("</table>\n");
         html.append("<HR size=\"1\" noshade=\"noshade\">");
         html.append("<H2>AsyncWeb Server</H2></body></html>");
         IoBuffer out = IoBuffer.allocate(html.length());
@@ -75,12 +93,12 @@
 
         return out;
     }
-    
+
     private String getType(File file) {
-        if(file.isDirectory())
+        if (file.isDirectory())
             return "DIR";
         String extension = MimeMap.getExtension(file.getName());
-        if(extension==null)
+        if (extension == null)
             return "";
         else
             return extension.toLowerCase();

Added: 
mina/asyncweb/trunk/examples/src/main/resources/org/apache/asyncweb/examples/file/index/strings.properties
URL: 
http://svn.apache.org/viewvc/mina/asyncweb/trunk/examples/src/main/resources/org/apache/asyncweb/examples/file/index/strings.properties?rev=627515&view=auto
==============================================================================
--- 
mina/asyncweb/trunk/examples/src/main/resources/org/apache/asyncweb/examples/file/index/strings.properties
 (added)
+++ 
mina/asyncweb/trunk/examples/src/main/resources/org/apache/asyncweb/examples/file/index/strings.properties
 Wed Feb 13 08:34:21 2008
@@ -0,0 +1,23 @@
+#
+#  Licensed to the Apache Software Foundation (ASF) under one
+#  or more contributor license agreements.  See the NOTICE file
+#  distributed with this work for additional information
+#  regarding copyright ownership.  The ASF licenses this file
+#  to you 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.
+#
+nameHeader=Name
+typeHeader=Type
+sizeHeader=Size
+lastModifiedHeader=Last modified
+parentDirectory=Parent Directory
\ No newline at end of file


Reply via email to