You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2006/08/04 17:50:06 UTC

svn commit: r428761 - /jakarta/httpcomponents/httpcore/trunk/src/examples/org/apache/http/examples/ElementalHttpServer.java

Author: olegk
Date: Fri Aug  4 08:50:05 2006
New Revision: 428761

URL: http://svn.apache.org/viewvc?rev=428761&view=rev
Log:
Changed the elemental HTTP server demo to make use of the EntityTemplate class

Modified:
    jakarta/httpcomponents/httpcore/trunk/src/examples/org/apache/http/examples/ElementalHttpServer.java

Modified: jakarta/httpcomponents/httpcore/trunk/src/examples/org/apache/http/examples/ElementalHttpServer.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/src/examples/org/apache/http/examples/ElementalHttpServer.java?rev=428761&r1=428760&r2=428761&view=diff
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/src/examples/org/apache/http/examples/ElementalHttpServer.java (original)
+++ jakarta/httpcomponents/httpcore/trunk/src/examples/org/apache/http/examples/ElementalHttpServer.java Fri Aug  4 08:50:05 2006
@@ -32,6 +32,8 @@
 import java.io.File;
 import java.io.IOException;
 import java.io.InterruptedIOException;
+import java.io.OutputStream;
+import java.io.OutputStreamWriter;
 import java.net.ServerSocket;
 import java.net.Socket;
 import java.net.URLDecoder;
@@ -42,8 +44,9 @@
 import org.apache.http.HttpServerConnection;
 import org.apache.http.HttpStatus;
 import org.apache.http.MethodNotSupportedException;
+import org.apache.http.entity.ContentProducer;
+import org.apache.http.entity.EntityTemplate;
 import org.apache.http.entity.FileEntity;
-import org.apache.http.entity.StringEntity;
 import org.apache.http.impl.DefaultHttpParams;
 import org.apache.http.impl.DefaultHttpServerConnection;
 import org.apache.http.params.HttpConnectionParams;
@@ -96,22 +99,53 @@
             String docroot = (String) getContext().getAttribute("server.docroot");
             
             String target = request.getRequestLine().getUri();
-            File file = new File(docroot, URLDecoder.decode(target));
+            
+            final File file = new File(docroot, URLDecoder.decode(target));
             if (!file.exists()) {
+
                 response.setStatusCode(HttpStatus.SC_NOT_FOUND);
-                StringEntity body = new StringEntity("File not found", "UTF-8");
+                EntityTemplate body = new EntityTemplate(new ContentProducer() {
+                    
+                    public void writeTo(final OutputStream outstream) throws IOException {
+                        OutputStreamWriter writer = new OutputStreamWriter(outstream, "UTF-8"); 
+                        writer.write("<html><body><h1>");
+                        writer.write("File ");
+                        writer.write(file.getPath());
+                        writer.write(" not found");
+                        writer.write("</h1></body></html>");
+                        writer.flush();
+                    }
+                    
+                });
+                body.setContentType("text/html; charset=UTF-8");
                 response.setEntity(body);
                 System.out.println("File " + file.getPath() + " not found");
+                
             } else if (!file.canRead() || file.isDirectory()) {
+                
                 response.setStatusCode(HttpStatus.SC_FORBIDDEN);
-                StringEntity body = new StringEntity("Access Denied", "UTF-8");
+                EntityTemplate body = new EntityTemplate(new ContentProducer() {
+                    
+                    public void writeTo(final OutputStream outstream) throws IOException {
+                        OutputStreamWriter writer = new OutputStreamWriter(outstream, "UTF-8"); 
+                        writer.write("<html><body><h1>");
+                        writer.write("Access denied");
+                        writer.write("</h1></body></html>");
+                        writer.flush();
+                    }
+                    
+                });
+                body.setContentType("text/html; charset=UTF-8");
                 response.setEntity(body);
                 System.out.println("Cannot read file " + file.getPath());
+                
             } else {
+                
                 response.setStatusCode(HttpStatus.SC_OK);
                 FileEntity body = new FileEntity(file, "text/html");
                 response.setEntity(body);
                 System.out.println("Serving file " + file.getPath());
+                
             }
         }