View Javadoc

1   package com.bradmcevoy.http;
2   
3   import java.io.OutputStream;
4   import java.util.Date;
5   import java.util.List;
6   import java.util.Map;
7   
8   public interface Response {
9   
10      public final static String HTTP = "text/html";
11      public final static String IMAGE_JPG = "image/jpg";
12      public final static String MULTIPART = "multipart/form-data";
13      public final static String XML = "text/xml; charset=UTF-8";
14  
15      public enum ContentType {
16  
17          HTTP,
18          MULTIPART,
19          IMAGE_JPG,
20          XML;
21      }
22  
23      public enum ContentEncoding {
24  
25          GZIP( "gzip" );
26          public String code;
27  
28          ContentEncoding( String code ) {
29              this.code = code;
30          }
31      }
32  
33      enum Header {
34  
35          CACHE_CONTROL( "Cache-Control" ),
36          WWW_AUTHENTICATE( "WWW-Authenticate" ),
37          CONTENT_LENGTH( "Content-Length" ),
38          CONTENT_TYPE( "Content-Type" ),
39          CONTENT_ENCODING( "Content-Encoding" ),
40          LOCATION( "Location" ),
41          ALLOW( "Allow" ),
42          DAV( "DAV" ),
43          DATE( "Date" ),  // was all-caps , eg DATE, which is wrong
44          LAST_MODIFIED( "Last-Modified" ),
45          LOCK_TOKEN( "Lock-Token" ),
46          EXPIRES( "Expires" ),
47          ETAG( "Etag" ),
48          VARY( "Vary" ),
49          CONTENT_RANGE( "Content-Range" );
50          public String code;
51  
52          Header( String code ) {
53              this.code = code;
54          }
55      }
56  
57      enum CacheControlResponse {
58  
59          PUBLIC( "public" ),
60          PRIVATE( "private" ), // [ "=" <"> 1#field-name <"> ] ; Section 14.9.1
61          NO_CACHE( "no-cache" ), // [ "=" <"> 1#field-name <"> ]; Section 14.9.1
62          NO_STORE( "no-store" ), //                             ; Section 14.9.2
63          NO_TRANSFORM( "no-transform" ), //                         ; Section 14.9.5
64          MUST_REVALIDATE( "must-revalidate" ), //                     ; Section 14.9.4
65          PROXY_REVALIDATE( "proxy-revalidate" ), //                   ; Section 14.9.4
66          MAX_AGE( "max-age" ), // "=" delta-seconds            ; Section 14.9.3
67          S_MAX_AGE( "s-maxage" ), // "=" delta-seconds           ; Section 14.9.3
68          CACHE_EXT( "cache-extension" );  //                       ; Section 14.9.6
69          public String code;
70  
71          CacheControlResponse( String code ) {
72              this.code = code;
73          }
74      }
75  
76      enum Status {
77  
78          SC_OK( ResponseStatus.SC_OK ),
79          SC_CREATED( ResponseStatus.SC_CREATED ),
80          SC_ACCEPTED( ResponseStatus.SC_ACCEPTED ),
81          SC_NO_CONTENT( ResponseStatus.SC_NO_CONTENT ),
82          SC_MULTI_STATUS( 207 ),
83          SC_MOVED_PERMANENTLY( ResponseStatus.SC_MOVED_PERMANENTLY ),
84          SC_MOVED_TEMPORARILY( ResponseStatus.SC_MOVED_TEMPORARILY ),
85          SC_NOT_MODIFIED( ResponseStatus.SC_NOT_MODIFIED ),
86          SC_BAD_REQUEST( ResponseStatus.SC_BAD_REQUEST ),
87          SC_UNAUTHORIZED( ResponseStatus.SC_UNAUTHORIZED ),
88          SC_FORBIDDEN( ResponseStatus.SC_FORBIDDEN ),
89          SC_NOT_FOUND( ResponseStatus.SC_NOT_FOUND ),
90          SC_INTERNAL_SERVER_ERROR( ResponseStatus.SC_INTERNAL_SERVER_ERROR ),
91          SC_NOT_IMPLEMENTED( ResponseStatus.SC_NOT_IMPLEMENTED ),
92          SC_BAD_GATEWAY( ResponseStatus.SC_BAD_GATEWAY ),
93          SC_SERVICE_UNAVAILABLE( ResponseStatus.SC_SERVICE_UNAVAILABLE ),
94          SC_PARTIAL_CONTENT( ResponseStatus.SC_PARTIAL_CONTENT ),
95          SC_CONTINUE( 100 ),
96          SC_METHOD_NOT_ALLOWED( 405 ),
97          SC_CONFLICT( 409 ),
98          SC_PRECONDITION_FAILED( 412 ),
99          SC_REQUEST_TOO_LONG( 413 ),
100         SC_UNSUPPORTED_MEDIA_TYPE( 415 ),
101         SC_EXPECTATION_FAILED( ResponseStatus.SC_EXPECTATION_FAILED ), // 417
102         SC_UNPROCESSABLE_ENTITY( 418 ),
103         SC_INSUFFICIENT_STORAGE( 507 ),
104         SC_METHOD_FAILURE( 420 ),
105         SC_LOCKED( 423 );
106         public int code;
107 
108         Status( int code ) {
109             this.code = code;
110         }
111 
112         @Override
113         public String toString() {
114             return "HTTP/1.1 " + code;
115         }
116 
117         public Status fromCode( int i ) {
118             for( Status s : this.values() ) {
119                 if( s.code == i ) return s;
120             }
121             return null;
122         }
123     }
124 
125     public Response.Status getStatus();
126 
127     public Map<String, String> getHeaders();
128 
129     /**
130      * 
131      * @return - the content length which might have been set by a handler, or null
132      * if none has been set
133      */
134     public Long getContentLength();
135 
136     public void setContentEncodingHeader( ContentEncoding encoding );
137 
138     public void setExpiresHeader( Date expiresAt );
139 
140     public void setLockTokenHeader( String tokenId );
141 
142     /**
143      * Must set multiple Authenticate headers, one for each challenge
144      *
145      * This will usually be one each for basic, digest, ntlm, etc
146      *
147      * Note that order might be significant to some clients, so should be
148      * preserved. ie list item zero should be first.
149      *
150      * @param challenges - a list of http authentication challenges
151      */
152     void setAuthenticateHeader( List<String> challenges );
153 
154     void setStatus( Status status );
155 
156     void setEtag( String uniqueId );
157 
158     void setContentRangeHeader( long start, long finish, Long totalLength );
159 
160     void setContentLengthHeader( Long totalLength );
161 
162     void setContentTypeHeader( String string );
163 
164     String getContentTypeHeader();
165 
166     /**
167      * Set the cache control header to allow the resource to be cached
168      * for the given number of seconds
169      *
170      * @param deltaSeconds - must not be null
171      */
172     void setCacheControlMaxAgeHeader( Long deltaSeconds );
173 
174     void setCacheControlPrivateMaxAgeHeader( Long deltaSeconds );
175 
176     /**
177      * Set the cache control header to indicate that the resource should not
178      * be cached
179      */
180     void setCacheControlNoCacheHeader();
181 
182     void setLastModifiedHeader( Date date );
183 
184     void setDavHeader( String string );
185 
186     void setNonStandardHeader( String code, String value );
187 
188     String getNonStandardHeader( String code );
189 
190     void setAllowHeader( List<String> methodsAllowed );
191 
192     OutputStream getOutputStream();
193 
194     void setLocationHeader( String redirectUrl );
195 
196     /**
197      * Sets the Vary response header, necessary for response compression
198      * @param string
199      */
200     void setVaryHeader( String string );
201 
202     void setDateHeader( Date date );
203 
204     void close();
205 
206     /**
207      * Will set the status to moved_temporaruly and set the location header
208      * to the given url
209      * 
210      * @param unencodedUrl
211      */
212     void sendRedirect( String url );
213 
214     /**
215      * The <code>setCookie</code> method is used to set a cookie value
216      * with the cookie name. This will add a cookie to the response
217      * stored under the name of the cookie, when this is committed it
218      * will be added as a Set-Cookie header to the resulting response.
219      *
220      * @param cookie this is the cookie to be added to the response
221      *
222      * @return returns the cookie that has been set in the response
223      */
224     Cookie setCookie( Cookie cookie );
225 
226     /**
227      * The <code>setCookie</code> method is used to set a cookie value
228      * with the cookie name. This will add a cookie to the response
229      * stored under the name of the cookie, when this is committed it
230      * will be added as a Set-Cookie header to the resulting response.
231      * This is a convenience method that avoids cookie creation.
232      *
233      * @param name this is the cookie to be added to the response
234      * @param value this is the cookie value that is to be used
235      *
236      * @return returns the cookie that has been set in the response
237      */
238     Cookie setCookie( String name, String value );
239 }