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" ),
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" ),
61 NO_CACHE( "no-cache" ),
62 NO_STORE( "no-store" ),
63 NO_TRANSFORM( "no-transform" ),
64 MUST_REVALIDATE( "must-revalidate" ),
65 PROXY_REVALIDATE( "proxy-revalidate" ),
66 MAX_AGE( "max-age" ),
67 S_MAX_AGE( "s-maxage" ),
68 CACHE_EXT( "cache-extension" );
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 ),
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
132
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
144
145
146
147
148
149
150
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
168
169
170
171
172 void setCacheControlMaxAgeHeader( Long deltaSeconds );
173
174 void setCacheControlPrivateMaxAgeHeader( Long deltaSeconds );
175
176
177
178
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
198
199
200 void setVaryHeader( String string );
201
202 void setDateHeader( Date date );
203
204 void close();
205
206
207
208
209
210
211
212 void sendRedirect( String url );
213
214
215
216
217
218
219
220
221
222
223
224 Cookie setCookie( Cookie cookie );
225
226
227
228
229
230
231
232
233
234
235
236
237
238 Cookie setCookie( String name, String value );
239 }