Author: jvermillard
Date: Tue Feb 26 13:28:19 2008
New Revision: 631390

URL: http://svn.apache.org/viewvc?rev=631390&view=rev
Log:
moved file serving service to it's own module and deleted the files from 
examples module

Added:
    mina/asyncweb/trunk/fileservice/
    mina/asyncweb/trunk/fileservice/pom.xml
    mina/asyncweb/trunk/fileservice/src/
    mina/asyncweb/trunk/fileservice/src/main/
    mina/asyncweb/trunk/fileservice/src/main/java/
    mina/asyncweb/trunk/fileservice/src/main/java/org/
    mina/asyncweb/trunk/fileservice/src/main/java/org/apache/
    mina/asyncweb/trunk/fileservice/src/main/java/org/apache/asyncweb/
    mina/asyncweb/trunk/fileservice/src/main/java/org/apache/asyncweb/examples/
    
mina/asyncweb/trunk/fileservice/src/main/java/org/apache/asyncweb/examples/file/
    
mina/asyncweb/trunk/fileservice/src/main/java/org/apache/asyncweb/examples/file/cache/
    
mina/asyncweb/trunk/fileservice/src/main/java/org/apache/asyncweb/examples/file/cache/CachingPolicy.java
    
mina/asyncweb/trunk/fileservice/src/main/java/org/apache/asyncweb/examples/file/cache/SimpleCachingPolicy.java
    
mina/asyncweb/trunk/fileservice/src/main/java/org/apache/asyncweb/examples/file/fileloader/
    
mina/asyncweb/trunk/fileservice/src/main/java/org/apache/asyncweb/examples/file/fileloader/FileLoader.java
    
mina/asyncweb/trunk/fileservice/src/main/java/org/apache/asyncweb/examples/file/fileloader/MmapFileLoader.java
    
mina/asyncweb/trunk/fileservice/src/main/java/org/apache/asyncweb/examples/file/fileloader/SimpleFileLoader.java
    
mina/asyncweb/trunk/fileservice/src/main/java/org/apache/asyncweb/examples/file/index/
    
mina/asyncweb/trunk/fileservice/src/main/java/org/apache/asyncweb/examples/file/index/DefaultDirectoryIndexGenerator.java
    
mina/asyncweb/trunk/fileservice/src/main/java/org/apache/asyncweb/examples/file/index/DirectoryIndexGenerator.java
    
mina/asyncweb/trunk/fileservice/src/main/java/org/apache/asyncweb/examples/file/mimetype/
    
mina/asyncweb/trunk/fileservice/src/main/java/org/apache/asyncweb/examples/file/mimetype/MimeMap.java
    
mina/asyncweb/trunk/fileservice/src/main/java/org/apache/asyncweb/fileservice/
    
mina/asyncweb/trunk/fileservice/src/main/java/org/apache/asyncweb/fileservice/FileHttpService.java
    
mina/asyncweb/trunk/fileservice/src/main/java/org/apache/asyncweb/fileservice/FileMain.java
    mina/asyncweb/trunk/fileservice/src/main/resources/
    mina/asyncweb/trunk/fileservice/src/main/resources/log4j.properties
    mina/asyncweb/trunk/fileservice/src/main/resources/org/
    mina/asyncweb/trunk/fileservice/src/main/resources/org/apache/
    mina/asyncweb/trunk/fileservice/src/main/resources/org/apache/asyncweb/
    
mina/asyncweb/trunk/fileservice/src/main/resources/org/apache/asyncweb/fileservice/
    
mina/asyncweb/trunk/fileservice/src/main/resources/org/apache/asyncweb/fileservice/index/
    
mina/asyncweb/trunk/fileservice/src/main/resources/org/apache/asyncweb/fileservice/index/strings.properties
    mina/asyncweb/trunk/fileservice/src/test/
    mina/asyncweb/trunk/fileservice/src/test/java/
    mina/asyncweb/trunk/fileservice/src/test/java/org/
    mina/asyncweb/trunk/fileservice/src/test/java/org/apache/
    mina/asyncweb/trunk/fileservice/src/test/java/org/apache/asyncweb/
    
mina/asyncweb/trunk/fileservice/src/test/java/org/apache/asyncweb/fileservice/
    
mina/asyncweb/trunk/fileservice/src/test/java/org/apache/asyncweb/fileservice/fileloader/
    
mina/asyncweb/trunk/fileservice/src/test/java/org/apache/asyncweb/fileservice/fileloader/MmapFileLoaderTest.java
    
mina/asyncweb/trunk/fileservice/src/test/java/org/apache/asyncweb/fileservice/fileloader/SimpleFileLoaderTest.java
Removed:
    
mina/asyncweb/trunk/examples/src/main/java/org/apache/asyncweb/examples/file/
    mina/asyncweb/trunk/examples/src/main/resources/org/
    mina/asyncweb/trunk/examples/src/test/
Modified:
    
mina/asyncweb/trunk/client/src/main/java/org/apache/asyncweb/client/AsyncHttpClient.java
    mina/asyncweb/trunk/pom.xml

Modified: 
mina/asyncweb/trunk/client/src/main/java/org/apache/asyncweb/client/AsyncHttpClient.java
URL: 
http://svn.apache.org/viewvc/mina/asyncweb/trunk/client/src/main/java/org/apache/asyncweb/client/AsyncHttpClient.java?rev=631390&r1=631389&r2=631390&view=diff
==============================================================================
--- 
mina/asyncweb/trunk/client/src/main/java/org/apache/asyncweb/client/AsyncHttpClient.java
 (original)
+++ 
mina/asyncweb/trunk/client/src/main/java/org/apache/asyncweb/client/AsyncHttpClient.java
 Tue Feb 26 13:28:19 2008
@@ -26,13 +26,14 @@
 
 import javax.net.ssl.SSLContext;
 
-import org.apache.mina.common.ConnectFuture;
-import org.apache.mina.common.IoSession;
-import org.apache.mina.filter.codec.ProtocolCodecFilter;
 import org.apache.asyncweb.common.HttpCodecFactory;
 import org.apache.asyncweb.common.HttpRequest;
 import org.apache.asyncweb.common.MutableHttpRequest;
+import org.apache.mina.common.ConnectFuture;
+import org.apache.mina.common.IoSession;
+import org.apache.mina.filter.codec.ProtocolCodecFilter;
 import org.apache.mina.filter.ssl.SslFilter;
+import org.apache.mina.transport.socket.SocketConnector;
 import org.apache.mina.transport.socket.nio.NioSocketConnector;
 
 public class AsyncHttpClient {
@@ -41,8 +42,6 @@
 
     public static String DEFAULT_SSL_PROTOCOL = "TLS";
 
-    private URI uri;
-
     private boolean followRedirects = true;
 
     private int connectionTimeout = DEFAULT_CONNECTION_TIMEOUT;
@@ -51,44 +50,41 @@
 
     private IoSession session;
 
-    private AsyncHttpClientCallback callback;
-
-    public AsyncHttpClient(URI url, AsyncHttpClientCallback callback) {
-        this.uri = url;
-        this.callback = callback;
+    private SocketConnector connector;
+    
+    public AsyncHttpClient(SocketConnector connector) {
+       this.connector = connector;
+       connector.getFilterChain().addLast("protocolFilter",
+                new ProtocolCodecFilter(new HttpCodecFactory()));
     }
 
-    public void connect() throws Exception {
-        NioSocketConnector connector = new NioSocketConnector();
-
+    public void connect(URI url, AsyncHttpClientCallback callback) throws 
Exception {
         connector.setConnectTimeout(connectionTimeout);
 
-        String scheme = uri.getScheme();
-        int port = uri.getPort();
-        if (scheme.toLowerCase().equals("https")) {
-            SslFilter filter = new SslFilter(createClientSSLContext());
-            filter.setUseClientMode(true);
-            connector.getFilterChain().addLast("SSL", filter);
-            if (port == -1) {
-                port = 443;
-            }
-        }
+        String scheme = url.getScheme();
+        int port = url.getPort();
+        
         if (scheme.toLowerCase().equals("http") && port == -1) {
             port = 80;
+        } else if (scheme.toLowerCase().equals("https") && port == -1) {
+               port = 443;
         }
-
-        connector.getFilterChain().addLast("protocolFilter",
-                new ProtocolCodecFilter(new HttpCodecFactory()));
         connector.setHandler(new HttpIoHandler(callback));
-        ConnectFuture future = connector.connect(new InetSocketAddress(uri
+        ConnectFuture future = connector.connect(new InetSocketAddress(url
                 .getHost(), port));
         future.awaitUninterruptibly();
         if (!future.isConnected()) {
-            throw new IOException("Cannot connect to " + uri.toString());
+            throw new IOException("Cannot connect to " + url.toString());
         }
         session = future.getSession();
+        // now add the good filters
+        if (scheme.toLowerCase().equals("https")) {
+            SslFilter filter = new SslFilter(createClientSSLContext());
+            filter.setUseClientMode(true);
+            session.getFilterChain().addLast("SSL", filter);
+        }
     }
-
+    
     public void disconnect() {
         if (session != null && session.isConnected()) {
             session.close();

Added: mina/asyncweb/trunk/fileservice/pom.xml
URL: 
http://svn.apache.org/viewvc/mina/asyncweb/trunk/fileservice/pom.xml?rev=631390&view=auto
==============================================================================
--- mina/asyncweb/trunk/fileservice/pom.xml (added)
+++ mina/asyncweb/trunk/fileservice/pom.xml Tue Feb 26 13:28:19 2008
@@ -0,0 +1,38 @@
+<?xml version="1.0" encoding="ISO-8859-1"?>
+<!-- 
+       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.
+-->
+<project> 
+  <modelVersion>4.0.0</modelVersion>
+  <parent>
+    <groupId>org.apache.asyncweb</groupId>
+    <artifactId>asyncweb-parent</artifactId>
+    <version>0.9.0-SNAPSHOT</version>
+  </parent>
+  <artifactId>asyncweb-fileservice</artifactId>
+  <packaging>jar</packaging>
+  <name>Apache Asyncweb File Service</name>
+
+  <dependencies>
+    <dependency>
+      <groupId>${groupId}</groupId>
+      <artifactId>asyncweb-server</artifactId>
+    </dependency>
+  </dependencies>
+
+</project>

Added: 
mina/asyncweb/trunk/fileservice/src/main/java/org/apache/asyncweb/examples/file/cache/CachingPolicy.java
URL: 
http://svn.apache.org/viewvc/mina/asyncweb/trunk/fileservice/src/main/java/org/apache/asyncweb/examples/file/cache/CachingPolicy.java?rev=631390&view=auto
==============================================================================
--- 
mina/asyncweb/trunk/fileservice/src/main/java/org/apache/asyncweb/examples/file/cache/CachingPolicy.java
 (added)
+++ 
mina/asyncweb/trunk/fileservice/src/main/java/org/apache/asyncweb/examples/file/cache/CachingPolicy.java
 Tue Feb 26 13:28:19 2008
@@ -0,0 +1,47 @@
+/*
+ *  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.
+ *
+ */
+package org.apache.asyncweb.examples.file.cache;
+
+import java.io.File;
+
+import org.apache.asyncweb.common.HttpRequest;
+import org.apache.asyncweb.common.MutableHttpResponse;
+/**
+ * 
+ * Caching strategies, under design class :)
+ * @author The Apache MINA Project ([EMAIL PROTECTED])
+ *
+ */
+public interface CachingPolicy {
+    /**
+     * Is the file is cachable or we just return no-cache directives
+     * @param requestedFile
+     * @param request
+     * @return
+     */
+    public boolean isCacheable(File requestedFile, HttpRequest request);
+    /**
+     * Test if it's a cache hit from file infos and headers infos
+     * @param requestedFile
+     * @param request
+     * @return
+     */
+    public boolean testAndSetCacheHit(File requestedFile, HttpRequest request, 
MutableHttpResponse response);
+}

Added: 
mina/asyncweb/trunk/fileservice/src/main/java/org/apache/asyncweb/examples/file/cache/SimpleCachingPolicy.java
URL: 
http://svn.apache.org/viewvc/mina/asyncweb/trunk/fileservice/src/main/java/org/apache/asyncweb/examples/file/cache/SimpleCachingPolicy.java?rev=631390&view=auto
==============================================================================
--- 
mina/asyncweb/trunk/fileservice/src/main/java/org/apache/asyncweb/examples/file/cache/SimpleCachingPolicy.java
 (added)
+++ 
mina/asyncweb/trunk/fileservice/src/main/java/org/apache/asyncweb/examples/file/cache/SimpleCachingPolicy.java
 Tue Feb 26 13:28:19 2008
@@ -0,0 +1,60 @@
+/*
+ *  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.
+ *
+ */
+
+package org.apache.asyncweb.examples.file.cache;
+
+import java.io.File;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.Locale;
+import java.util.TimeZone;
+
+import org.apache.asyncweb.common.HttpRequest;
+import org.apache.asyncweb.common.MutableHttpResponse;
+
+/**
+ * Very simple caching based on last modification date
+ * @author The Apache MINA Project ([EMAIL PROTECTED])
+ */
+public class SimpleCachingPolicy implements CachingPolicy {
+
+    /* formatter for strings like : "Last-Modified  Mon, 17 Dec 2007 00:12:30 
GMT" */
+    private SimpleDateFormat sdf;
+
+    public SimpleCachingPolicy() {
+        sdf = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.US);
+        sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
+    }
+
+    public boolean isCacheable(File requestedFile, HttpRequest request) {
+        return true;
+    }
+
+    public boolean testAndSetCacheHit(File requestedFile, HttpRequest request,
+            MutableHttpResponse response) {
+
+        long last = requestedFile.lastModified();
+        long maxAge = System.currentTimeMillis() - last;
+        response.setHeader("Cache-Control", "max-age=" + maxAge);
+        response.setHeader("Last-Modified", sdf.format(new Date(last)));
+        return true;
+    }
+
+}

Added: 
mina/asyncweb/trunk/fileservice/src/main/java/org/apache/asyncweb/examples/file/fileloader/FileLoader.java
URL: 
http://svn.apache.org/viewvc/mina/asyncweb/trunk/fileservice/src/main/java/org/apache/asyncweb/examples/file/fileloader/FileLoader.java?rev=631390&view=auto
==============================================================================
--- 
mina/asyncweb/trunk/fileservice/src/main/java/org/apache/asyncweb/examples/file/fileloader/FileLoader.java
 (added)
+++ 
mina/asyncweb/trunk/fileservice/src/main/java/org/apache/asyncweb/examples/file/fileloader/FileLoader.java
 Tue Feb 26 13:28:19 2008
@@ -0,0 +1,40 @@
+/*
+ *  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.
+ *
+ */
+
+package org.apache.asyncweb.examples.file.fileloader;
+
+import java.io.File;
+import java.io.IOException;
+
+import org.apache.mina.common.IoBuffer;
+
+/**
+ * Interface for providing IoBuffer to File serving services.
+ * @author The Apache MINA Project ([EMAIL PROTECTED])
+ */
+public interface FileLoader {
+       
+       /**
+        * Provide an IoBuffer from a given File
+        * @param file the file to provide
+        * @return a buffer representing the file
+        */
+       IoBuffer loadFile(File file) throws IOException;
+}

Added: 
mina/asyncweb/trunk/fileservice/src/main/java/org/apache/asyncweb/examples/file/fileloader/MmapFileLoader.java
URL: 
http://svn.apache.org/viewvc/mina/asyncweb/trunk/fileservice/src/main/java/org/apache/asyncweb/examples/file/fileloader/MmapFileLoader.java?rev=631390&view=auto
==============================================================================
--- 
mina/asyncweb/trunk/fileservice/src/main/java/org/apache/asyncweb/examples/file/fileloader/MmapFileLoader.java
 (added)
+++ 
mina/asyncweb/trunk/fileservice/src/main/java/org/apache/asyncweb/examples/file/fileloader/MmapFileLoader.java
 Tue Feb 26 13:28:19 2008
@@ -0,0 +1,51 @@
+/*
+ *  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.
+ *
+ */
+package org.apache.asyncweb.examples.file.fileloader;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.RandomAccessFile;
+import java.nio.MappedByteBuffer;
+import java.nio.channels.FileChannel;
+
+import org.apache.mina.common.IoBuffer;
+
+/**
+ * A file loader, mammping files to memory, supposed to be efficient 
+ * on relativly large files.
+ * 
+ * @author The Apache MINA Project ([EMAIL PROTECTED])
+ */
+public class MmapFileLoader implements FileLoader {
+       
+       public IoBuffer loadFile(File file) throws IOException {
+        RandomAccessFile raf = new RandomAccessFile(file, "r");
+        
+        FileChannel fc = raf.getChannel();
+        
+        MappedByteBuffer buffer = fc.map(FileChannel.MapMode.READ_ONLY, 0, 
fc.size());
+        
+        // it's mmaped we can close the file descriptor
+        fc.close();
+        
+        // the mapped byte buffer will be garbage collected
+        return IoBuffer.wrap(buffer);
+       }
+}
\ No newline at end of file

Added: 
mina/asyncweb/trunk/fileservice/src/main/java/org/apache/asyncweb/examples/file/fileloader/SimpleFileLoader.java
URL: 
http://svn.apache.org/viewvc/mina/asyncweb/trunk/fileservice/src/main/java/org/apache/asyncweb/examples/file/fileloader/SimpleFileLoader.java?rev=631390&view=auto
==============================================================================
--- 
mina/asyncweb/trunk/fileservice/src/main/java/org/apache/asyncweb/examples/file/fileloader/SimpleFileLoader.java
 (added)
+++ 
mina/asyncweb/trunk/fileservice/src/main/java/org/apache/asyncweb/examples/file/fileloader/SimpleFileLoader.java
 Tue Feb 26 13:28:19 2008
@@ -0,0 +1,52 @@
+/*
+ *  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.
+ *
+ */
+package org.apache.asyncweb.examples.file.fileloader;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.RandomAccessFile;
+import java.nio.channels.FileChannel;
+import java.security.InvalidParameterException;
+
+import org.apache.mina.common.IoBuffer;
+
+/**
+ * A simple file loader, supposed to be efficient on relativly small 
+ * files.
+ * 
+ * @author The Apache MINA Project ([EMAIL PROTECTED])
+ */
+public class SimpleFileLoader implements FileLoader {
+
+       public IoBuffer loadFile(File file) throws IOException {
+               if(file.length()> Integer.MAX_VALUE)
+                               throw new InvalidParameterException("File 
"+file+" is too big for SimpleFileLoader try MmapFileLoader");
+               IoBuffer buffer=IoBuffer.allocate((int)file.length());
+               RandomAccessFile raf = new RandomAccessFile(file, "r");
+        FileChannel fc = raf.getChannel();
+        fc.read(buffer.buf());
+               buffer.flip();
+               fc.close();
+               raf.close();
+               return buffer;
+       }
+       
+
+}

Added: 
mina/asyncweb/trunk/fileservice/src/main/java/org/apache/asyncweb/examples/file/index/DefaultDirectoryIndexGenerator.java
URL: 
http://svn.apache.org/viewvc/mina/asyncweb/trunk/fileservice/src/main/java/org/apache/asyncweb/examples/file/index/DefaultDirectoryIndexGenerator.java?rev=631390&view=auto
==============================================================================
--- 
mina/asyncweb/trunk/fileservice/src/main/java/org/apache/asyncweb/examples/file/index/DefaultDirectoryIndexGenerator.java
 (added)
+++ 
mina/asyncweb/trunk/fileservice/src/main/java/org/apache/asyncweb/examples/file/index/DefaultDirectoryIndexGenerator.java
 Tue Feb 26 13:28:19 2008
@@ -0,0 +1,113 @@
+/*
+ *  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.
+ *
+ */
+package org.apache.asyncweb.examples.file.index;
+
+import java.io.File;
+import java.io.UnsupportedEncodingException;
+import java.util.Date;
+
+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();
+
+        StringBuilder html = new StringBuilder(1024);
+        html.append("<html><head><title>");
+        html.append("AsyncWeb Server - ");
+        html.append(directory.getName());
+        html.append("</title><style><!--");
+        CSS.appendTo(html).append("--></style>");
+        html.append("</head></body>\n");
+        html.append("<H1>Index of : ").append(directory.getName()).append(
+                "</H1>\n");
+        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()) {
+                html.append("/");
+            }
+            html.append("\">");
+            html.append(file.getName());
+            if (file.isDirectory()) {
+                html.append("/");
+            }
+            html.append("</a></td><td>");
+            html.append(getType(file));
+            html.append("</td><td>");
+            html.append(file.length());
+            html.append("</td><td>");
+            html.append(new Date(file.lastModified()));
+            html.append("</td></tr>\n");
+        }
+        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());
+
+        // TODO: Need to sort this out when we start dealing with character 
encodings
+        try {
+            byte[] bytes = html.toString().getBytes("US-ASCII");
+            out.put(bytes);
+        } catch (UnsupportedEncodingException e) {
+            throw new RuntimeException(e);
+        }
+
+        return out;
+    }
+
+    private String getType(File file) {
+        if (file.isDirectory()) {
+            return "DIR";
+        }
+        
+        String extension = MimeMap.getExtension(file.getName());
+        
+        if (extension == null) {
+            return "";
+        } else {
+            return extension.toLowerCase();
+        }
+    }
+
+}

Added: 
mina/asyncweb/trunk/fileservice/src/main/java/org/apache/asyncweb/examples/file/index/DirectoryIndexGenerator.java
URL: 
http://svn.apache.org/viewvc/mina/asyncweb/trunk/fileservice/src/main/java/org/apache/asyncweb/examples/file/index/DirectoryIndexGenerator.java?rev=631390&view=auto
==============================================================================
--- 
mina/asyncweb/trunk/fileservice/src/main/java/org/apache/asyncweb/examples/file/index/DirectoryIndexGenerator.java
 (added)
+++ 
mina/asyncweb/trunk/fileservice/src/main/java/org/apache/asyncweb/examples/file/index/DirectoryIndexGenerator.java
 Tue Feb 26 13:28:19 2008
@@ -0,0 +1,39 @@
+/*
+ *  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.
+ *
+ */
+package org.apache.asyncweb.examples.file.index;
+
+import java.io.File;
+
+import org.apache.mina.common.IoBuffer;
+
+/**
+ * Directory index page generator.
+ * @author The Apache MINA Project ([EMAIL PROTECTED])
+ *
+ */
+public interface DirectoryIndexGenerator {
+
+    /**
+     * 
+     * @param directory the directory base of the file index
+     * @return buffer containing the generated index
+     */
+    IoBuffer generateIndex(File directory);
+}

Added: 
mina/asyncweb/trunk/fileservice/src/main/java/org/apache/asyncweb/examples/file/mimetype/MimeMap.java
URL: 
http://svn.apache.org/viewvc/mina/asyncweb/trunk/fileservice/src/main/java/org/apache/asyncweb/examples/file/mimetype/MimeMap.java?rev=631390&view=auto
==============================================================================
--- 
mina/asyncweb/trunk/fileservice/src/main/java/org/apache/asyncweb/examples/file/mimetype/MimeMap.java
 (added)
+++ 
mina/asyncweb/trunk/fileservice/src/main/java/org/apache/asyncweb/examples/file/mimetype/MimeMap.java
 Tue Feb 26 13:28:19 2008
@@ -0,0 +1,206 @@
+/*
+ *  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.
+ */
+
+package org.apache.asyncweb.examples.file.mimetype;
+
+import java.net.*;
+import java.util.*;
+
+/**
+ * A mime type map that implements the java.net.FileNameMap interface.
+ *
+ * @author James Duncan Davidson [EMAIL PROTECTED]
+ * @author Jason Hunter [EMAIL PROTECTED]
+ * @author The Apache MINA Project ([EMAIL PROTECTED])
+ */
+public class MimeMap implements FileNameMap {
+
+    public static Hashtable<String, String> defaultMap = new Hashtable<String, 
String>(
+            101);
+    static {
+        defaultMap.put("txt", "text/plain");
+        defaultMap.put("html", "text/html");
+        defaultMap.put("htm", "text/html");
+        defaultMap.put("gif", "image/gif");
+        defaultMap.put("jpg", "image/jpeg");
+        defaultMap.put("jpe", "image/jpeg");
+        defaultMap.put("jpeg", "image/jpeg");
+        defaultMap.put("png", "image/png");
+        defaultMap.put("java", "text/plain");
+        defaultMap.put("body", "text/html");
+        defaultMap.put("rtx", "text/richtext");
+        defaultMap.put("tsv", "text/tab-separated-values");
+        defaultMap.put("etx", "text/x-setext");
+        defaultMap.put("ps", "application/x-postscript");
+        defaultMap.put("class", "application/java");
+        defaultMap.put("csh", "application/x-csh");
+        defaultMap.put("sh", "application/x-sh");
+        defaultMap.put("tcl", "application/x-tcl");
+        defaultMap.put("tex", "application/x-tex");
+        defaultMap.put("texinfo", "application/x-texinfo");
+        defaultMap.put("texi", "application/x-texinfo");
+        defaultMap.put("t", "application/x-troff");
+        defaultMap.put("tr", "application/x-troff");
+        defaultMap.put("roff", "application/x-troff");
+        defaultMap.put("man", "application/x-troff-man");
+        defaultMap.put("me", "application/x-troff-me");
+        defaultMap.put("ms", "application/x-wais-source");
+        defaultMap.put("src", "application/x-wais-source");
+        defaultMap.put("zip", "application/zip");
+        defaultMap.put("bcpio", "application/x-bcpio");
+        defaultMap.put("cpio", "application/x-cpio");
+        defaultMap.put("gtar", "application/x-gtar");
+        defaultMap.put("shar", "application/x-shar");
+        defaultMap.put("sv4cpio", "application/x-sv4cpio");
+        defaultMap.put("sv4crc", "application/x-sv4crc");
+        defaultMap.put("tar", "application/x-tar");
+        defaultMap.put("ustar", "application/x-ustar");
+        defaultMap.put("dvi", "application/x-dvi");
+        defaultMap.put("hdf", "application/x-hdf");
+        defaultMap.put("latex", "application/x-latex");
+        defaultMap.put("bin", "application/octet-stream");
+        defaultMap.put("oda", "application/oda");
+        defaultMap.put("pdf", "application/pdf");
+        defaultMap.put("ps", "application/postscript");
+        defaultMap.put("eps", "application/postscript");
+        defaultMap.put("ai", "application/postscript");
+        defaultMap.put("rtf", "application/rtf");
+        defaultMap.put("nc", "application/x-netcdf");
+        defaultMap.put("cdf", "application/x-netcdf");
+        defaultMap.put("cer", "application/x-x509-ca-cert");
+        defaultMap.put("exe", "application/octet-stream");
+        defaultMap.put("gz", "application/x-gzip");
+        defaultMap.put("Z", "application/x-compress");
+        defaultMap.put("z", "application/x-compress");
+        defaultMap.put("hqx", "application/mac-binhex40");
+        defaultMap.put("mif", "application/x-mif");
+        defaultMap.put("ief", "image/ief");
+        defaultMap.put("tiff", "image/tiff");
+        defaultMap.put("tif", "image/tiff");
+        defaultMap.put("ras", "image/x-cmu-raster");
+        defaultMap.put("pnm", "image/x-portable-anymap");
+        defaultMap.put("pbm", "image/x-portable-bitmap");
+        defaultMap.put("pgm", "image/x-portable-graymap");
+        defaultMap.put("ppm", "image/x-portable-pixmap");
+        defaultMap.put("rgb", "image/x-rgb");
+        defaultMap.put("xbm", "image/x-xbitmap");
+        defaultMap.put("xpm", "image/x-xpixmap");
+        defaultMap.put("xwd", "image/x-xwindowdump");
+        defaultMap.put("au", "audio/basic");
+        defaultMap.put("snd", "audio/basic");
+        defaultMap.put("aif", "audio/x-aiff");
+        defaultMap.put("aiff", "audio/x-aiff");
+        defaultMap.put("aifc", "audio/x-aiff");
+        defaultMap.put("wav", "audio/x-wav");
+        defaultMap.put("mpeg", "video/mpeg");
+        defaultMap.put("mpg", "video/mpeg");
+        defaultMap.put("mpe", "video/mpeg");
+        defaultMap.put("qt", "video/quicktime");
+        defaultMap.put("mov", "video/quicktime");
+        defaultMap.put("avi", "video/x-msvideo");
+        defaultMap.put("movie", "video/x-sgi-movie");
+        defaultMap.put("avx", "video/x-rad-screenplay");
+        defaultMap.put("wrl", "x-world/x-vrml");
+        defaultMap.put("mpv2", "video/mpeg2");
+
+        /* Add XML related MIMEs */
+
+        defaultMap.put("xml", "text/xml");
+        defaultMap.put("xsl", "text/xml");
+        defaultMap.put("svg", "image/svg+xml");
+        defaultMap.put("svgz", "image/svg+xml");
+        defaultMap.put("wbmp", "image/vnd.wap.wbmp");
+        defaultMap.put("wml", "text/vnd.wap.wml");
+        defaultMap.put("wmlc", "application/vnd.wap.wmlc");
+        defaultMap.put("wmls", "text/vnd.wap.wmlscript");
+        defaultMap.put("wmlscriptc", "application/vnd.wap.wmlscriptc");
+    }
+
+    private Hashtable<String, String> map = new Hashtable<String, String>();
+
+    /**
+     * Add a custom content type for a given file extension
+     * @param extn file extension (without the dot)
+     * @param type the mime-type associated
+     */
+    public void addContentType(String extn, String type) {
+        map.put(extn, type.toLowerCase());
+    }
+
+    /**
+     * get list of the extensions added to the map
+     * @return
+     */
+    public Enumeration<String> getExtensions() {
+        return map.keys();
+    }
+
+    /**
+     * Return the content type for a given extension.
+     * @param extn
+     * @return mime-type
+     */
+    public String getContentType(String extn) {
+        String type = (String) map.get(extn.toLowerCase());
+        
+        if (type == null) {
+            type = (String) defaultMap.get(extn);
+        }
+        return type;
+    }
+
+    public void removeContentType(String extn) {
+        map.remove(extn.toLowerCase());
+    }
+
+    /** Get extension of file, without fragment id
+     */
+    public static String getExtension(String fileName) {
+        // play it safe and get rid of any fragment id
+        // that might be there
+        int length = fileName.length();
+
+        int newEnd = fileName.lastIndexOf('#');
+        
+        if (newEnd == -1) {
+            newEnd = length;
+        }
+        // Instead of creating a new string.
+        //         if (i != -1) {
+        //             fileName = fileName.substring(0, i);
+        //         }
+        int i = fileName.lastIndexOf('.', newEnd);
+        if (i != -1) {
+            return fileName.substring(i + 1, newEnd);
+        } else {
+            // no extension, no content type
+            return null;
+        }
+    }
+
+    public String getContentTypeFor(String fileName) {
+        String extn = getExtension(fileName);
+        
+        if (extn != null) {
+            return getContentType(extn);
+        } else {
+            // no extension, no content type
+            return null;
+        }
+    }
+
+}
\ No newline at end of file

Added: 
mina/asyncweb/trunk/fileservice/src/main/java/org/apache/asyncweb/fileservice/FileHttpService.java
URL: 
http://svn.apache.org/viewvc/mina/asyncweb/trunk/fileservice/src/main/java/org/apache/asyncweb/fileservice/FileHttpService.java?rev=631390&view=auto
==============================================================================
--- 
mina/asyncweb/trunk/fileservice/src/main/java/org/apache/asyncweb/fileservice/FileHttpService.java
 (added)
+++ 
mina/asyncweb/trunk/fileservice/src/main/java/org/apache/asyncweb/fileservice/FileHttpService.java
 Tue Feb 26 13:28:19 2008
@@ -0,0 +1,221 @@
+/*
+ *  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.
+ *
+ */
+
+package org.apache.asyncweb.fileservice;
+
+import java.io.File;
+import java.io.FilenameFilter;
+import java.net.URI;
+import java.security.InvalidParameterException;
+import java.util.regex.Pattern;
+
+import org.apache.asyncweb.common.DefaultHttpResponse;
+import org.apache.asyncweb.common.HttpResponseStatus;
+import org.apache.asyncweb.common.MutableHttpResponse;
+import org.apache.asyncweb.examples.file.cache.CachingPolicy;
+import org.apache.asyncweb.examples.file.cache.SimpleCachingPolicy;
+import org.apache.asyncweb.examples.file.fileloader.FileLoader;
+import org.apache.asyncweb.examples.file.fileloader.SimpleFileLoader;
+import org.apache.asyncweb.examples.file.index.DefaultDirectoryIndexGenerator;
+import org.apache.asyncweb.examples.file.index.DirectoryIndexGenerator;
+import org.apache.asyncweb.examples.file.mimetype.MimeMap;
+import org.apache.asyncweb.server.HttpService;
+import org.apache.asyncweb.server.HttpServiceContext;
+import org.apache.mina.common.IoBuffer;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * An HTTP service, serving files from the filesystem.
+ *
+ * @author The Apache MINA Project ([EMAIL PROTECTED])
+ */
+public class FileHttpService implements HttpService {
+    private static final Logger LOG = LoggerFactory
+            .getLogger(FileHttpService.class);
+
+    private String baseUrl;
+
+    private String basePath;
+
+    private CachingPolicy cachingPolicy = new SimpleCachingPolicy();
+
+    private MimeMap mimeMap = new MimeMap();
+
+    private FilenameFilter indexFileFilter;
+
+    private DirectoryIndexGenerator indexGenerator = new 
DefaultDirectoryIndexGenerator();
+    
+    private FileLoader fileLoader = new SimpleFileLoader();
+
+    public FileHttpService(String baseUrl, String basePath,
+            String directoryIndexPattern) {
+        this.baseUrl = baseUrl;
+        this.basePath = basePath;
+        this.indexFileFilter = new RegExpFilenameFilter(Pattern
+                .compile(directoryIndexPattern));
+
+        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.");
+        }
+    }
+
+    public FileHttpService(String baseUrl, String basePath) {
+        this(baseUrl, basePath, "index.html");
+    }
+
+    public void handleRequest(HttpServiceContext context) throws Exception {
+        URI uri = context.getRequest().getRequestUri();
+        String path = uri.getPath();
+        
+        LOG.info("Handling file request : {} from 
{}",uri,context.getRemoteAddress());
+
+        if (!path.startsWith(baseUrl)) {
+            // error the requested URL is not in the base URL
+            //TODO : find the good exception to throw
+            throw new InvalidParameterException("Wrong URL");
+        }
+
+        MutableHttpResponse response = new DefaultHttpResponse();
+
+        path = path.substring(baseUrl.length());
+        File f = new File(basePath + File.separator + path);
+
+        if (f.isDirectory()) {
+
+            // is the request finishing by the '/' character ?
+            String urlStr = uri.toString();
+
+            if (urlStr.charAt(urlStr.length() - 1) != '/') {
+
+                LOG.debug("Redirecting {} to {}/", urlStr, 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());
+
+            // caching processing
+            if (cachingPolicy == null
+                    || !cachingPolicy.isCacheable(f, context.getRequest())) {
+                response.setHeader("Pragma", "no-cache");
+                response.setHeader("Cache-Control", "no-cache");
+            } else {
+                cachingPolicy.testAndSetCacheHit(f, context.getRequest(),
+                        response);
+            }
+
+            // setting mime-type based on the mime-map
+
+            String contentType = mimeMap.getContentType(MimeMap.getExtension(f
+                    .getName()));
+
+            if (contentType != null)
+                response.setHeader("Content-Type", contentType);
+
+            response.setStatus(HttpResponseStatus.OK);
+
+            IoBuffer buffer=fileLoader.loadFile(f);
+            
+            response.setContent(buffer);
+            
+        } else {
+            // the file is not found, we send the famous 404 error
+            response.setStatus(HttpResponseStatus.NOT_FOUND);
+            response.setStatusReasonPhrase("File \"" + path + "\"");
+            LOG.warn("The file {} is not found",f.getAbsolutePath());
+        }
+        context.commitResponse(response);
+
+    }
+
+    public CachingPolicy getCachingPolicy() {
+        return cachingPolicy;
+    }
+
+    public void setCachingPolicy(CachingPolicy cachingPolicy) {
+        this.cachingPolicy = cachingPolicy;
+    }
+
+    public void start() {
+        // nothing to do there
+    }
+
+    public void stop() {
+        // nothing to do there
+    }
+
+    private class RegExpFilenameFilter implements FilenameFilter {
+        private Pattern pattern;
+
+        public RegExpFilenameFilter(Pattern pattern) {
+            this.pattern = pattern;
+        }
+
+        public boolean accept(File dir, String name) {
+            return pattern.matcher(name).matches();
+        }
+
+    }
+
+       public FileLoader getFileLoader() {
+               return fileLoader;
+       }
+
+       public void setFileLoader(FileLoader fileLoader) {
+               this.fileLoader = fileLoader;
+       }
+}

Added: 
mina/asyncweb/trunk/fileservice/src/main/java/org/apache/asyncweb/fileservice/FileMain.java
URL: 
http://svn.apache.org/viewvc/mina/asyncweb/trunk/fileservice/src/main/java/org/apache/asyncweb/fileservice/FileMain.java?rev=631390&view=auto
==============================================================================
--- 
mina/asyncweb/trunk/fileservice/src/main/java/org/apache/asyncweb/fileservice/FileMain.java
 (added)
+++ 
mina/asyncweb/trunk/fileservice/src/main/java/org/apache/asyncweb/fileservice/FileMain.java
 Tue Feb 26 13:28:19 2008
@@ -0,0 +1,57 @@
+/*
+ *  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.
+ *
+ */
+package org.apache.asyncweb.fileservice;
+
+import org.apache.asyncweb.server.BasicServiceContainer;
+import org.apache.asyncweb.server.HttpServiceHandler;
+import org.apache.asyncweb.server.resolver.PatternMatchResolver;
+import org.apache.asyncweb.server.transport.mina.DefaultHttpIoHandler;
+import org.apache.asyncweb.server.transport.mina.MinaTransport;
+
+public class FileMain {
+
+    /**
+     * Starting an AsyncWeb server on port 9021, service files from ./data to 
base url /static
+     */
+    public static void main(String[] args) throws Exception {
+        // Setup the default container with a service handler that contains 
the helloWorldService
+        BasicServiceContainer container = new BasicServiceContainer();
+        HttpServiceHandler handler = new HttpServiceHandler();
+        
+        handler.addHttpService("fileExample", new FileHttpService("/static",
+                "./data"));
+        
+        container.addServiceFilter(handler);
+
+        PatternMatchResolver resolver = new PatternMatchResolver();
+        resolver.addPatternMapping("/static/.*", "fileExample");
+        handler.setServiceResolver(resolver);
+
+        // Create the mina transport and enable the container with it
+        MinaTransport transport = new MinaTransport();
+        container.addTransport(transport);
+        DefaultHttpIoHandler ioHandler = new DefaultHttpIoHandler();
+        ioHandler.setReadIdle(10);
+        transport.setIoHandler(ioHandler);
+
+        // Fire it up and go
+        container.start();
+    }
+}

Added: mina/asyncweb/trunk/fileservice/src/main/resources/log4j.properties
URL: 
http://svn.apache.org/viewvc/mina/asyncweb/trunk/fileservice/src/main/resources/log4j.properties?rev=631390&view=auto
==============================================================================
--- mina/asyncweb/trunk/fileservice/src/main/resources/log4j.properties (added)
+++ mina/asyncweb/trunk/fileservice/src/main/resources/log4j.properties Tue Feb 
26 13:28:19 2008
@@ -0,0 +1,24 @@
+#
+#  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.
+#
+log4j.rootLogger=INFO, stdout
+log4j.logger.org.safehaus.asyncweb=INFO
+
+log4j.appender.stdout=org.apache.log4j.ConsoleAppender
+log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
+log4j.appender.stdout.layout.ConversionPattern=[%d{HH:mm:ss}] %5p 
[%-26.26c{1}] %m%n

Added: 
mina/asyncweb/trunk/fileservice/src/main/resources/org/apache/asyncweb/fileservice/index/strings.properties
URL: 
http://svn.apache.org/viewvc/mina/asyncweb/trunk/fileservice/src/main/resources/org/apache/asyncweb/fileservice/index/strings.properties?rev=631390&view=auto
==============================================================================
--- 
mina/asyncweb/trunk/fileservice/src/main/resources/org/apache/asyncweb/fileservice/index/strings.properties
 (added)
+++ 
mina/asyncweb/trunk/fileservice/src/main/resources/org/apache/asyncweb/fileservice/index/strings.properties
 Tue Feb 26 13:28:19 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

Added: 
mina/asyncweb/trunk/fileservice/src/test/java/org/apache/asyncweb/fileservice/fileloader/MmapFileLoaderTest.java
URL: 
http://svn.apache.org/viewvc/mina/asyncweb/trunk/fileservice/src/test/java/org/apache/asyncweb/fileservice/fileloader/MmapFileLoaderTest.java?rev=631390&view=auto
==============================================================================
--- 
mina/asyncweb/trunk/fileservice/src/test/java/org/apache/asyncweb/fileservice/fileloader/MmapFileLoaderTest.java
 (added)
+++ 
mina/asyncweb/trunk/fileservice/src/test/java/org/apache/asyncweb/fileservice/fileloader/MmapFileLoaderTest.java
 Tue Feb 26 13:28:19 2008
@@ -0,0 +1,48 @@
+package org.apache.asyncweb.fileservice.fileloader;
+
+import static org.junit.Assert.assertTrue;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.util.Random;
+
+import org.apache.asyncweb.examples.file.fileloader.FileLoader;
+import org.apache.asyncweb.examples.file.fileloader.MmapFileLoader;
+import org.apache.mina.common.IoBuffer;
+import org.junit.Test;
+
+public class MmapFileLoaderTest {
+       
+       private static final int TEMP_SIZE=1024*100+3; // ~100K file
+       
+       @Test
+       public void testLoadFile() throws Exception {
+               // generate temp file
+               File tempFile=File.createTempFile("dummy",null);
+               tempFile.deleteOnExit();
+               FileOutputStream fos=new FileOutputStream(tempFile);
+               Random rng=new Random();
+               
+               byte[] data = new byte[TEMP_SIZE];
+               rng.nextBytes(data);
+               fos.write(data);
+               fos.close();
+               
+               FileLoader sfl=new MmapFileLoader();
+               IoBuffer buffer=sfl.loadFile(tempFile);
+
+               assertTrue(buffer.remaining()==TEMP_SIZE);
+               
+               for(int i=0;i<TEMP_SIZE;i++) {
+                       assertTrue(data[i]==buffer.get());
+               }
+               
+               buffer=sfl.loadFile(tempFile);
+
+               assertTrue(buffer.remaining()==TEMP_SIZE);
+               
+               for(int i=0;i<TEMP_SIZE;i++) {
+                       assertTrue(data[i]==buffer.get());
+               }
+       }
+}

Added: 
mina/asyncweb/trunk/fileservice/src/test/java/org/apache/asyncweb/fileservice/fileloader/SimpleFileLoaderTest.java
URL: 
http://svn.apache.org/viewvc/mina/asyncweb/trunk/fileservice/src/test/java/org/apache/asyncweb/fileservice/fileloader/SimpleFileLoaderTest.java?rev=631390&view=auto
==============================================================================
--- 
mina/asyncweb/trunk/fileservice/src/test/java/org/apache/asyncweb/fileservice/fileloader/SimpleFileLoaderTest.java
 (added)
+++ 
mina/asyncweb/trunk/fileservice/src/test/java/org/apache/asyncweb/fileservice/fileloader/SimpleFileLoaderTest.java
 Tue Feb 26 13:28:19 2008
@@ -0,0 +1,47 @@
+package org.apache.asyncweb.fileservice.fileloader;
+
+import static org.junit.Assert.*;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.util.Random;
+
+import org.apache.asyncweb.examples.file.fileloader.SimpleFileLoader;
+import org.apache.mina.common.IoBuffer;
+import org.junit.Test;
+
+public class SimpleFileLoaderTest {
+
+       private static final int TEMP_SIZE=1024*100+3; // ~100K file
+       
+       @Test
+       public void testLoadFile() throws Exception {
+               // generate temp file
+               File tempFile=File.createTempFile("dummy",null);
+               tempFile.deleteOnExit();
+               FileOutputStream fos=new FileOutputStream(tempFile);
+               Random rng=new Random();
+               
+               byte[] data = new byte[TEMP_SIZE];
+               rng.nextBytes(data);
+               fos.write(data);
+               fos.close();
+               
+               SimpleFileLoader sfl=new SimpleFileLoader();
+               IoBuffer buffer=sfl.loadFile(tempFile);
+
+               assertTrue(buffer.remaining()==TEMP_SIZE);
+               
+               for(int i=0;i<TEMP_SIZE;i++) {
+                       assertTrue(data[i]==buffer.get());
+               }
+               
+               buffer=sfl.loadFile(tempFile);
+
+               assertTrue(buffer.remaining()==TEMP_SIZE);
+               
+               for(int i=0;i<TEMP_SIZE;i++) {
+                       assertTrue(data[i]==buffer.get());
+               }
+       }
+}

Modified: mina/asyncweb/trunk/pom.xml
URL: 
http://svn.apache.org/viewvc/mina/asyncweb/trunk/pom.xml?rev=631390&r1=631389&r2=631390&view=diff
==============================================================================
--- mina/asyncweb/trunk/pom.xml (original)
+++ mina/asyncweb/trunk/pom.xml Tue Feb 26 13:28:19 2008
@@ -131,6 +131,7 @@
     <module>client</module>
     <module>spring</module>
     <module>examples</module>
+    <module>fileservice</module>
   </modules>
 
   <build>


Reply via email to