View Javadoc

1   /**********************************************************************
2    * HttpResponse.java
3    * created on 04.03.2005 by netseeker
4    * $Source: /cvsroot/ejoe/EJOE/src/de/netseeker/ejoe/http/HttpResponse.java,v $
5    * $Date: 2006/12/02 17:57:36 $
6    * $Revision: 1.3 $
7    *
8    * ====================================================================
9    *
10   *  Copyright 2005-2006 netseeker aka Michael Manske
11   *
12   *  Licensed under the Apache License, Version 2.0 (the "License");
13   *  you may not use this file except in compliance with the License.
14   *  You may obtain a copy of the License at
15   *
16   *      http://www.apache.org/licenses/LICENSE-2.0
17   *
18   *  Unless required by applicable law or agreed to in writing, software
19   *  distributed under the License is distributed on an "AS IS" BASIS,
20   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21   *  See the License for the specific language governing permissions and
22   *  limitations under the License.
23   * ====================================================================
24   *
25   * This file is part of the ejoe framework.
26   * For more information on the author, please see
27   * <http://www.manskes.de/>.
28   *
29   *********************************************************************/
30  package de.netseeker.ejoe.http;
31  
32  import java.text.SimpleDateFormat;
33  import java.util.Date;
34  
35  import de.netseeker.ejoe.ConnectionHeader;
36  import de.netseeker.ejoe.EJConstants;
37  
38  /***
39   * A simple container encapsulating a HTTP Response.
40   * 
41   * @author netseeker
42   * @since 0.3.9.1
43   */
44  public class HttpResponse extends HttpRequest
45  {
46      /*
47       * Date format used in HTTP responses according to the RFC
48       */
49      public static final String  HTTP_DATE_FORMAT           = "EEE, d MMM yyyy HH:mm:ss z";
50  
51      public static final String  HTTP_OK                    = "200 OK";
52  
53      public static final String  HTTP_BAD_REQUEST           = "400 Bad Request";
54  
55      public static final String  HTTP_NOT_FOUND             = "404 Not Found";
56  
57      public static final String  HTTP_METHOD_NOT_ALLOWED    = "405 Method Not Allowed";
58  
59      public static final String  HTTP_LENGTH_REQUIRED       = "411 Length Required";
60  
61      public static final String  HTTP_INTERNAL_SERVER_ERROR = "500 Internal Server Error";
62  
63      public static final String  HTTP_VERSION_NOT_SUPPORTED = "505 HTTP Version Not Supported";
64  
65      private static final String HDR_CHUNK1                 = "HTTP/1.0 ";
66  
67      private static final String HDR_CHUNK2                 = LINE_SEP + "Server: EJServer/" + EJConstants.EJOE_VERSION
68                                                                     + LINE_SEP + "Date: ";
69  
70      private static final String HDR_CHUNK3                 = LINE_SEP + "Content-Type: ";
71  
72      private static final String HDR_CHUNK4                 = LINE_SEP + "Content-Encoding: x-gzip";
73  
74      private static final String HDR_CHUNK5                 = LINE_SEP + "Connection: ";
75  
76      private static final String HDR_CHUNK6                 = LINE_SEP + "Content-Length: ";
77  
78      private static final String HDR_CHUNK7                 = LINE_SEP + LINE_SEP;
79  
80      private String              _status;
81  
82      /***
83       * Creates a new HTTP response using the given connection header and the given HTTP status code string
84       * 
85       * @param header a valid connection header
86       * @param status a valid HTTP status code
87       */
88      public HttpResponse(ConnectionHeader header, String status)
89      {
90          super( header, null );
91          this._status = status;
92      }
93  
94      /***
95       * Creates a new HTTP response using the given connection header, the given content (mime) type and the given HTTP
96       * status code string
97       * 
98       * @param header a valid connection header
99       * @param contentType a valid mime type
100      * @param status a valid HTTP status code
101      */
102     public HttpResponse(ConnectionHeader header, String contentType, String status)
103     {
104         super( header, contentType, null );
105         this._status = status;
106     }
107 
108     /*
109      * (non-Javadoc)
110      * 
111      * @see de.netseeker.ejoe.http.HttpRequest#toHeaderString()
112      */
113     protected String toHeaderString()
114     {
115         StringBuffer sb = new StringBuffer( HDR_CHUNK1 );
116         sb.append( this._status );
117         sb.append( HDR_CHUNK2 );
118         SimpleDateFormat sdf = new SimpleDateFormat( HTTP_DATE_FORMAT );
119         sb.append( sdf.format( new Date() ) );
120         sb.append( HDR_CHUNK3 );
121         sb.append( getContentType() );
122         if ( this._header.hasCompression() )
123         {
124             sb.append( HDR_CHUNK4 );
125         }
126         sb.append( HDR_CHUNK5 );
127         sb.append( this._header.isPersistent() ? "keep-alive" : "close" );
128         sb.append( HDR_CHUNK6 );
129         sb.append( this._out.getBackingBuffer().position() );
130         sb.append( HDR_CHUNK7 );
131 
132         return sb.toString();
133     }
134 
135     /***
136      * @return the HTTP status
137      */
138     public String getStatus()
139     {
140         return _status;
141     }
142 
143 }