Source code: raining/protocol/http/Http404Response.java
1 /*
2 * $Author: rahul_kumar $
3 * $Id: Http404Response.java,v 1.1 2003/10/20 18:09:56 rahul_kumar Exp rahul $
4 */
5
6 package raining.protocol.http;
7 import raining.core.*;
8 import raining.util.*;
9 import java.util.*;
10
11 /**
12 * Interface for Response, including response header creation.
13 * Remember that headers need to b cached as does data.
14 * TODO. I need to refactor this. only the 404 200 resp code differs.
15 * @author rahul kumar
16 * @see Response
17 */
18
19 public class Http404Response implements Response {
20
21 ///// static variables ////////////////////////////////
22 //static byte[] header = null;
23 static byte[] content = null;
24 static String sheader = null;
25
26 public final static String HTTP_VERSION = "HTTP/1.1";
27 public final static String CRLF = "\r\n";
28 public final static String CRLF2 = "\r\n\r\n";
29 public final static String contentType = "text/html";
30 protected final static String scontent = "<html><head><title>404 Not Found</title></head><body bgcolor=white><font face=\"helvetica\"><big><big><b>404 Not Found</b></big></big><p>The URL you requested <p><blockquote><tt></tt></blockquote><p>could not be found. </tt></blockquote></body></html>\n";
31
32 static {
33 sheader = HTTP_VERSION + " 404 NOT FOUND\n"+
34 "Content-Type: "+ contentType + CRLF +
35 "Content-Length: "+ NioUtil.int2byte(scontent.length())+ CRLF2 +scontent ;
36 content = sheader.getBytes();
37 }
38 ///// instance variables ////////////////////////////////
39 //byte[] content = null;
40 /** ctor that takes a request object.
41 * Usually the URL would be used in the reply, and logged.
42 * Currently a static response is returned.
43 */
44 public Http404Response (Request request){
45
46 // currently i keep a hardcoded 404 message, so dont do
47 // anything. Later we would log this to some file.
48
49
50 }
51
52 /** Return as a stream of bytes.
53 * Preferred since we can use bytes
54 * throughout and avoid costly string to byte conversions.
55 */
56 public byte[] getBytes(){
57 return content;
58 }
59 /** return content as a String: to start with we use this, this does a conversion 3 times, so
60 * avoid. */
61 public String getString(){
62 return new String(content);
63 }
64 public static final String P="Http404Response";
65
66 public static void main (String args[]){
67
68 System.out.println( "--string cons--------");
69 Http404Response ok = new Http404Response(null);
70 System.out.println( ok.getString());
71 String s1 = ok.getString();
72 System.out.println( "-As Bytes:");
73 System.out.println( ok.getBytes());
74 byte[] b1 = ok.getBytes();
75 System.out.println( "----------");
76 System.out.println( "-- byte cons--------");
77 ok = new Http404Response(null);
78 System.out.println( ok.getString());
79 String s2 = ok.getString();
80 System.out.println( "-As Bytes:");
81 System.out.println( ok.getBytes());
82 byte[] b2 = ok.getBytes();
83 System.out.println( "s2 eq s1:"+s2.equals(s1));
84 System.out.println( "Length:"+s1.length()+","+s2.length());
85 System.out.println( "BLength:"+b1.length+","+b2.length);
86 if (!s2.equals(s1)){
87 System.out.println( ">>>>:"+s2.compareTo(s1));
88 for( int i = 0; i < s1.length(); i++ ){
89 if (s1.charAt(i) != s2.charAt(i)){ System.out.print( i+" not equal!");
90 System.out.println( "["+s1.charAt(i) + "]:["+ s2.charAt(i)+ "]" );
91 }
92 }
93 System.out.println( "---");
94
95 }
96 for( int i = 0; i < s1.length(); i++ ){
97 if (b1[i] != b2[i]) System.out.println( i+" not equal!");
98 }
99 long start = System.currentTimeMillis();
100 for( int j = 0; j < 100000; j++ ){
101 ok = new Http404Response(null);
102 }
103 long finish = System.currentTimeMillis();
104 System.out.println( "time:"+ (finish-start));
105
106 start = System.currentTimeMillis();
107 byte b3[] = "Hello There Boys".getBytes();
108 for( int j = 0; j < 100000; j++ ){
109 ok = new Http404Response(null);
110 }
111 finish = System.currentTimeMillis();
112 System.out.println( "time:"+ (finish-start));
113
114 }
115
116
117 } // end of class
118
119 //String str = "<html><head><title>404 Not Found</title></head><body bgcolor=white><font face=\"helvetica\"><big><big><b>404 Not Found</b></big></big><p>The URL you requested:<p><blockquote><tt>"+request.getURL()+"</tt></blockquote><p>could not be found. </tt></blockquote></body></html>\n";
120
121