Author: jvermillard
Date: Tue Feb 5 03:34:28 2008
New Revision: 618611
URL: http://svn.apache.org/viewvc?rev=618611&view=rev
Log:
Simple Asyncweb HTTP service for serving static files from disk.
Caching is under construction.
Added:
mina/asyncweb/trunk/examples/src/main/java/org/apache/asyncweb/examples/file/
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/FileMain.java
mina/asyncweb/trunk/examples/src/main/java/org/apache/asyncweb/examples/file/cache/
mina/asyncweb/trunk/examples/src/main/java/org/apache/asyncweb/examples/file/cache/CachingPolicy.java
Added:
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=618611&view=auto
==============================================================================
---
mina/asyncweb/trunk/examples/src/main/java/org/apache/asyncweb/examples/file/FileHttpService.java
(added)
+++
mina/asyncweb/trunk/examples/src/main/java/org/apache/asyncweb/examples/file/FileHttpService.java
Tue Feb 5 03:34:28 2008
@@ -0,0 +1,130 @@
+/*
+ * 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;
+
+import java.io.File;
+import java.io.RandomAccessFile;
+import java.net.URI;
+import java.nio.channels.FileChannel;
+import java.security.InvalidParameterException;
+
+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.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 = null;
+
+ public FileHttpService(String baseUrl, String basePath) {
+ this.baseUrl = baseUrl;
+ this.basePath = basePath;
+ 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 void handleRequest(HttpServiceContext context) throws Exception {
+ URI uri = context.getRequest().getRequestUri();
+ String path = uri.getPath();
+ LOG.info("handling file request : " + uri);
+ 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");
+ }
+
+ path = path.substring(baseUrl.length());
+ File f = new File(basePath + File.separator + path);
+ System.err.println(f.getAbsolutePath());
+
+ MutableHttpResponse response = new DefaultHttpResponse();
+ 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.setStatus(HttpResponseStatus.OK);
+
+ // TODO : set mime-type
+
+ FileChannel fileChannel = (new RandomAccessFile(f, "r"))
+ .getChannel();
+
+ // TODO : well it's quite explosive on big files, need to change
the API
+ IoBuffer buffer = IoBuffer.allocate((int) fileChannel.size());
+ fileChannel.read(buffer.buf());
+
+ buffer.flip();
+ 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 [ " + f.getAbsolutePath() + " ] is not found");
+ }
+ 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
+ }
+
+}
Added:
mina/asyncweb/trunk/examples/src/main/java/org/apache/asyncweb/examples/file/FileMain.java
URL:
http://svn.apache.org/viewvc/mina/asyncweb/trunk/examples/src/main/java/org/apache/asyncweb/examples/file/FileMain.java?rev=618611&view=auto
==============================================================================
---
mina/asyncweb/trunk/examples/src/main/java/org/apache/asyncweb/examples/file/FileMain.java
(added)
+++
mina/asyncweb/trunk/examples/src/main/java/org/apache/asyncweb/examples/file/FileMain.java
Tue Feb 5 03:34:28 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.examples.file;
+
+import org.apache.asyncweb.examples.helloworld.HelloWorldHttpService;
+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") );
+ handler.addHttpService( "helloWorldExample", new
HelloWorldHttpService() );
+ 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/examples/src/main/java/org/apache/asyncweb/examples/file/cache/CachingPolicy.java
URL:
http://svn.apache.org/viewvc/mina/asyncweb/trunk/examples/src/main/java/org/apache/asyncweb/examples/file/cache/CachingPolicy.java?rev=618611&view=auto
==============================================================================
---
mina/asyncweb/trunk/examples/src/main/java/org/apache/asyncweb/examples/file/cache/CachingPolicy.java
(added)
+++
mina/asyncweb/trunk/examples/src/main/java/org/apache/asyncweb/examples/file/cache/CachingPolicy.java
Tue Feb 5 03:34:28 2008
@@ -0,0 +1,46 @@
+/*
+ * 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;
+/**
+ *
+ * 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);
+}