Author: jvermillard
Date: Thu Feb 14 00:56:48 2008
New Revision: 627684
URL: http://svn.apache.org/viewvc?rev=627684&view=rev
Log:
fixed logging statement and code style on he good Alex advice.
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/FileMain.java
mina/asyncweb/trunk/examples/src/main/java/org/apache/asyncweb/examples/file/cache/SimpleCachingPolicy.java
mina/asyncweb/trunk/examples/src/main/java/org/apache/asyncweb/examples/file/index/DefaultDirectoryIndexGenerator.java
mina/asyncweb/trunk/examples/src/main/java/org/apache/asyncweb/examples/file/mimetype/MimeMap.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=627684&r1=627683&r2=627684&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
Thu Feb 14 00:56:48 2008
@@ -60,24 +60,31 @@
private MimeMap mimeMap = new MimeMap();
private FilenameFilter indexFileFilter;
-
+
private DirectoryIndexGenerator indexGenerator = new
DefaultDirectoryIndexGenerator();
-
- public FileHttpService(String baseUrl, String basePath, String
directoryIndexPattern) {
+
+ 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)
+ 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()))
+
+ if (!(f.isDirectory() && f.exists())) {
+
throw new InvalidParameterException("The base path [ " + basePath
+ " ] is not a valid path.");
+ }
+
cachingPolicy = new SimpleCachingPolicy();
}
-
+
public FileHttpService(String baseUrl, String basePath) {
this(baseUrl, basePath, "index.html");
}
@@ -85,8 +92,9 @@
public void handleRequest(HttpServiceContext context) throws Exception {
URI uri = context.getRequest().getRequestUri();
String path = uri.getPath();
- LOG.info("Handling file request : [ " + uri+" ] from [
"+context.getRemoteAddress()+" ]");
+ 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
@@ -99,44 +107,46 @@
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)!='/' ) {
-
- if(LOG.isDebugEnabled())
- LOG.debug("Redirecting "+urlStr+" to "+urlStr+"/");
-
+ 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+"/");
+ 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) {
+ 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);
+ IoBuffer indexResponse = indexGenerator.generateIndex(f);
indexResponse.flip();
response.setContent(indexResponse);
- response.setHeader("Content-Type","text/html");
+ response.setHeader("Content-Type", "text/html");
response.setStatus(HttpResponseStatus.OK);
context.commitResponse(response);
return;
}
- } else
+ } else {
// just serve the index file (ex:index.html)
- f=new File(f.getAbsolutePath()+File.separator+indexes[0]);
-
+ f = new File(f.getAbsolutePath() + File.separator +
indexes[0]);
+ }
+
}
- if (f.exists() && !f.isDirectory() ) {
-
- LOG.info("Serving file [ " + f.getAbsolutePath() + " ]");
+ if (f.exists() && !f.isDirectory()) {
+
+ LOG.info("Serving file {}",f.getAbsolutePath());
// caching processing
if (cachingPolicy == null
@@ -149,7 +159,7 @@
}
// setting mime-type based on the mime-map
-
+
String contentType = mimeMap.getContentType(MimeMap.getExtension(f
.getName()));
@@ -162,21 +172,21 @@
.getChannel();
// TODO : well it's quite explosive on big files, need to change
the API
-
+
int fileSize = (int) fileChannel.size();
IoBuffer responseBuffer = IoBuffer.allocate(fileSize);
-
+
fileChannel.read(responseBuffer.buf());
fileChannel.close();
-
+
responseBuffer.flip();
response.setContent(responseBuffer);
-
+
} 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");
+ LOG.warn("The file {} is not found",f.getAbsolutePath());
}
context.commitResponse(response);
@@ -200,14 +210,14 @@
private class RegExpFilenameFilter implements FilenameFilter {
private Pattern pattern;
-
+
public RegExpFilenameFilter(Pattern pattern) {
- this.pattern=pattern;
+ this.pattern = pattern;
}
public boolean accept(File dir, String name) {
return pattern.matcher(name).matches();
}
-
+
}
}
Modified:
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=627684&r1=627683&r2=627684&view=diff
==============================================================================
---
mina/asyncweb/trunk/examples/src/main/java/org/apache/asyncweb/examples/file/FileMain.java
(original)
+++
mina/asyncweb/trunk/examples/src/main/java/org/apache/asyncweb/examples/file/FileMain.java
Thu Feb 14 00:56:48 2008
@@ -27,29 +27,34 @@
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
- {
+ 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 );
+
+ 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 );
+ handler.setServiceResolver(resolver);
// Create the mina transport and enable the container with it
MinaTransport transport = new MinaTransport();
- container.addTransport( transport );
+ container.addTransport(transport);
DefaultHttpIoHandler ioHandler = new DefaultHttpIoHandler();
- ioHandler.setReadIdle( 10 );
- transport.setIoHandler( ioHandler );
+ ioHandler.setReadIdle(10);
+ transport.setIoHandler(ioHandler);
// Fire it up and go
container.start();
Modified:
mina/asyncweb/trunk/examples/src/main/java/org/apache/asyncweb/examples/file/cache/SimpleCachingPolicy.java
URL:
http://svn.apache.org/viewvc/mina/asyncweb/trunk/examples/src/main/java/org/apache/asyncweb/examples/file/cache/SimpleCachingPolicy.java?rev=627684&r1=627683&r2=627684&view=diff
==============================================================================
---
mina/asyncweb/trunk/examples/src/main/java/org/apache/asyncweb/examples/file/cache/SimpleCachingPolicy.java
(original)
+++
mina/asyncweb/trunk/examples/src/main/java/org/apache/asyncweb/examples/file/cache/SimpleCachingPolicy.java
Thu Feb 14 00:56:48 2008
@@ -37,22 +37,23 @@
/* 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 = 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)));
+ 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;
}
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=627684&r1=627683&r2=627684&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
Thu Feb 14 00:56:48 2008
@@ -64,12 +64,14 @@
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));
html.append("</td><td>");
@@ -95,13 +97,17 @@
}
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
+ } else {
return extension.toLowerCase();
+ }
}
}
Modified:
mina/asyncweb/trunk/examples/src/main/java/org/apache/asyncweb/examples/file/mimetype/MimeMap.java
URL:
http://svn.apache.org/viewvc/mina/asyncweb/trunk/examples/src/main/java/org/apache/asyncweb/examples/file/mimetype/MimeMap.java?rev=627684&r1=627683&r2=627684&view=diff
==============================================================================
---
mina/asyncweb/trunk/examples/src/main/java/org/apache/asyncweb/examples/file/mimetype/MimeMap.java
(original)
+++
mina/asyncweb/trunk/examples/src/main/java/org/apache/asyncweb/examples/file/mimetype/MimeMap.java
Thu Feb 14 00:56:48 2008
@@ -156,8 +156,10 @@
*/
public String getContentType(String extn) {
String type = (String) map.get(extn.toLowerCase());
- if (type == null)
+
+ if (type == null) {
type = (String) defaultMap.get(extn);
+ }
return type;
}
@@ -173,8 +175,10 @@
int length = fileName.length();
int newEnd = fileName.lastIndexOf('#');
- if (newEnd == -1)
+
+ if (newEnd == -1) {
newEnd = length;
+ }
// Instead of creating a new string.
// if (i != -1) {
// fileName = fileName.substring(0, i);
@@ -190,6 +194,7 @@
public String getContentTypeFor(String fileName) {
String extn = getExtension(fileName);
+
if (extn != null) {
return getContentType(extn);
} else {