Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: raining/protocol/http/Http503Response.java


1   /*
2    * $Author: rahul_kumar $
3    * $Id: Http503Response.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 503 200 resp code differs.
15   * XXX : Are we even required to send content? i think we can SHOULD  avoid.
16   * @author rahul kumar
17   * @see Response
18   */
19  
20  public class Http503Response implements Response {
21  
22      ///// static variables ////////////////////////////////
23  
24      static byte[] content = null;
25      static String sheader = null;
26  
27      public final static String HTTP_VERSION = "HTTP/1.1";
28      public final static String CRLF = "\r\n";
29      public final static String CRLF2 = "\r\n\r\n";
30      public final static String contentType = "text/html";
31      protected final static String scontent = "<html><head><title>503 Service Unavailable</title></head><body bgcolor=white><font face=\"helvetica\"><big><big><b>503 Service Unavailable</b></big></big><p>The requested service is unavailable. </body></html>";
32  
33      static {
34          sheader = HTTP_VERSION + " 503 SERVICE UNAVAILABLE"+ CRLF +
35              "Retry-After: 600" + CRLF +
36              "Content-Type: "+ contentType + CRLF +
37              "Content-Length: "+ NioUtil.int2byte(scontent.length())+ CRLF2 ;
38          content = (sheader+scontent+CRLF2).getBytes();
39      }
40      ///// instance variables ////////////////////////////////
41      protected byte[] thiscontent = null;
42      /** ctor that takes a request object. 
43       * Usually the URL would be used in the reply, and logged.
44       * Currently a static response is returned.
45       */
46      public Http503Response (Request request){
47  
48          // currently i keep a hardcoded 503 message, so dont do
49          // anything. Later we would log this to some file.
50          this.thiscontent = content;
51  
52  
53      }
54      public Http503Response (Request request, String cause){
55  
56          // currently i keep a hardcoded 503 message, so dont do
57          // anything. Later we would log this to some file.
58          String tmp = "<html><head><title>503 Service Unavailable</title></head><body bgcolor=white><font face=\"helvetica\"><big><big><b>503 Service Unavailable</b></big></big><p>The requested service is unavailable. The reason given was:<p><blockquote><tt>"+cause+"</tt></blockquote></body></html>\n";
59          this.thiscontent = (sheader + tmp).getBytes();
60  
61      }
62  
63      /** Return as a stream of bytes.
64       * Preferred since we can use bytes
65       * throughout and avoid costly string to byte conversions.
66       */
67      public byte[] getBytes(){
68          return this.thiscontent;
69      }
70      /** return content as a String: to start with we use this, this does a conversion 3 times, so
71       * avoid. */
72      public String getString(){
73          return new String(this.thiscontent);
74      }
75      /** Returns a static 503 string to be used, when a server is
76       * reaching capacity, and wants to reject connections with a reply.
77       */
78      public static byte[] getStaticContent(){
79          return content;
80      }
81      public static final String P="Http503Response";
82  
83      public static void main (String args[]){ 
84  
85          System.out.println(  "--string cons--------");
86          Http503Response ok = new Http503Response(null);
87          System.out.println(  ok.getString());
88          String s1 = ok.getString();
89          System.out.println(  "-As Bytes:");
90          System.out.println(  ok.getBytes());
91          byte[] b1 = ok.getBytes();
92          System.out.println(  "----------");
93          System.out.println(  "-- byte cons--------");
94          ok = new Http503Response(null);
95          System.out.println(  ok.getString());
96          String s2 = ok.getString();
97          System.out.println(  "-As Bytes:");
98          System.out.println(  ok.getBytes());
99          byte[] b2 = ok.getBytes();
100         System.out.println(  "s2 eq s1:"+s2.equals(s1));
101             System.out.println(  "Length:"+s1.length()+","+s2.length());
102             System.out.println(  "BLength:"+b1.length+","+b2.length);
103         if (!s2.equals(s1)){
104             System.out.println(  ">>>>:"+s2.compareTo(s1));
105             for( int i = 0; i < s1.length(); i++ ){ 
106                 if (s1.charAt(i) != s2.charAt(i)){ System.out.print(  i+" not equal!");
107                     System.out.println(  "["+s1.charAt(i) + "]:["+ s2.charAt(i)+ "]" );
108                 }
109             }
110             System.out.println(  "---");
111 
112         }
113         for( int i = 0; i < s1.length(); i++ ){ 
114             if (b1[i] != b2[i]) System.out.println(  i+" not equal!");
115         }
116         long start = System.currentTimeMillis();
117         for( int j = 0; j < 100000; j++ ){ 
118             ok = new Http503Response(null);
119         }
120         long finish = System.currentTimeMillis();
121         System.out.println(  "time:"+ (finish-start));
122 
123         start = System.currentTimeMillis();
124         byte b3[] = "Hello There Boys".getBytes();
125         for( int j = 0; j < 100000; j++ ){ 
126             ok = new Http503Response(null);
127         }
128         finish = System.currentTimeMillis();
129         System.out.println(  "time:"+ (finish-start));
130 
131     }
132 
133 
134 } // end of class
135 
136         //String str = "<html><head><title>503 Not Found</title></head><body bgcolor=white><font face=\"helvetica\"><big><big><b>503 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";
137 
138