Source Code Cross Referenced for httpResponse.java in  » Web-Server » Rimfaxe-Web-Server » seda » sandStorm » lib » http » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Web Server » Rimfaxe Web Server » seda.sandStorm.lib.http 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright (c) 2001 by Matt Welsh and The Regents of the University of 
003:         * California. All rights reserved.
004:         *
005:         * Permission to use, copy, modify, and distribute this software and its
006:         * documentation for any purpose, without fee, and without written agreement is
007:         * hereby granted, provided that the above copyright notice and the following
008:         * two paragraphs appear in all copies of this software.
009:         * 
010:         * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
011:         * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
012:         * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
013:         * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
014:         * 
015:         * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
016:         * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
017:         * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
018:         * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
019:         * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
020:         *
021:         * Author: Matt Welsh <mdw@cs.berkeley.edu>
022:         * 
023:         */
024:
025:        package seda.sandStorm.lib.http;
026:
027:        import seda.sandStorm.api.*;
028:        import seda.sandStorm.lib.aSocket.*;
029:        import seda.sandStorm.core.*;
030:
031:        import java.util.*;
032:        import java.io.*;
033:        import java.net.*;
034:
035:        /**
036:         * This is an abstract class corresponding to an HTTP response.
037:         * Use one of the subclasses (such as httpOKResponse or httpNotFoundResponse)
038:         * to push responses back to the client.
039:         * 
040:         * @author Matt Welsh
041:         * @see httpOKResponse
042:         * @see httpNotFoundResponse
043:         */
044:        public abstract class httpResponse implements  httpConst, QueueElementIF {
045:
046:            /** Code corresponding to '100 Continue'. */
047:            public static final int RESPONSE_CONTINUE = 100;
048:            /** Code corresponding to '101 Switching Protocols'. */
049:            public static final int RESPONSE_SWITCHING_PROTOCOLS = 101;
050:
051:            /** Code corresponding to '200 OK'. */
052:            public static final int RESPONSE_OK = 200;
053:            /** Code corresponding to '201 Created'. */
054:            public static final int RESPONSE_CREATED = 201;
055:            /** Code corresponding to '202 ACCEPTED'. */
056:            public static final int RESPONSE_ACCEPTED = 202;
057:            /** Code corresponding to '203 Non-Authoritative Information'. */
058:            public static final int RESPONSE_NON_AUTHORITATIVE_INFORMATION = 203;
059:            /** Code corresponding to '204 No Content'. */
060:            public static final int RESPONSE_NO_CONTENT = 204;
061:            /** Code corresponding to '205 Reset Content'. */
062:            public static final int RESPONSE_RESET_CONTENT = 205;
063:            /** Code corresponding to '206 Partial Content'. */
064:            public static final int RESPONSE_PARTIAL_CONTENT = 206;
065:
066:            /** Code corresponding to '300 Multiple Choices'. */
067:            public static final int RESPONSE_MULTIPLE_CHOICES = 300;
068:            /** Code corresponding to '301 Moved Permanently'. */
069:            public static final int RESPONSE_REDIRECT = 301;
070:            /** Code corresponding to '302 Found'. */
071:            public static final int RESPONSE_FOUND = 302;
072:            /** Code corresponding to '303 See Other'. */
073:            public static final int RESPONSE_SEE_OTHER = 303;
074:            /** Code corresponding to '304 Not Modified'. */
075:            public static final int RESPONSE_NOT_MODIFIED = 304;
076:            /** Code corresponding to '305 Use Proxy'. */
077:            public static final int RESPONSE_USE_PROXY = 305;
078:            /** Code corresponding to '306 (Unused)'. */
079:            public static final int RESPONSE_RESERVED_306 = 306;
080:            /** Code corresponding to '307 Temporary Redirect'. */
081:            public static final int RESPONSE_TEMPORARY_REDIRECT = 307;
082:
083:            /** Code corresponding to '400 Bad Request'. */
084:            public static final int RESPONSE_BAD_REQUEST = 400;
085:            /** Code corresponding to '401 Unauthorized'. */
086:            public static final int RESPONSE_UNAUTHORIZED = 401;
087:            /** Code corresponding to '402 Payment Required'. */
088:            public static final int RESPONSE_PAYMENT_REQUIRED = 402;
089:            /** Code corresponding to '403 Forbidden'. */
090:            public static final int RESPONSE_FORBIDDEN = 403;
091:            /** Code corresponding to '404 Not Found'. */
092:            public static final int RESPONSE_NOT_FOUND = 404;
093:            /** Code corresponding to '405 Method Not Allowed'. */
094:            public static final int RESPONSE_METHOD_NOT_ALLOWED = 405;
095:            /** Code corresponding to '406 Not Acceptable'. */
096:            public static final int RESPONSE_NOT_ACCEPTABLE = 406;
097:            /** Code corresponding to '407 Proxy Authentication Required'. */
098:            public static final int RESPONSE_PROXY_AUTHENTICATION_REQUIRED = 407;
099:            /** Code corresponding to '408 Request Timeout'. */
100:            public static final int RESPONSE_REQUEST_TIMEOUT = 408;
101:            /** Code corresponding to '409 Conflict'. */
102:            public static final int RESPONSE_CONFLICT = 409;
103:            /** Code corresponding to '410 Gone'. */
104:            public static final int RESPONSE_GONE = 410;
105:            /** Code corresponding to '411 Length Required'. */
106:            public static final int RESPONSE_LENGTH_REQUIRED = 411;
107:            /** Code corresponding to '412 Precondition Failed'. */
108:            public static final int RESPONSE_PRECONDITION_FAILED = 412;
109:            /** Code corresponding to '413 Request Entity Too Large'. */
110:            public static final int RESPONSE_REQUEST_ENTITY_TOO_LARGE = 413;
111:            /** Code corresponding to '414 Request-URI Too Long'. */
112:            public static final int RESPONSE_REQUEST_URI_TOO_LONG = 414;
113:            /** Code corresponding to '415 Unsupported Media Type'. */
114:            public static final int RESPONSE_UNSUPPORTED_MEDIA_TYPE = 415;
115:            /** Code corresponding to '416 Requested Range Not Satisfiable'. */
116:            public static final int RESPONSE_REQUESTED_RANGE_NOT_SATISFIABLE = 416;
117:            /** Code corresponding to '417 Expectation Failed'. */
118:            public static final int RESPONSE_EXPECTATION_FAILED = 417;
119:
120:            /** Code corresponding to '500 Internal Server Error'. */
121:            public static final int RESPONSE_INTERNAL_SERVER_ERROR = 500;
122:            /** Code corresponding to '501 Not Implemented'. */
123:            public static final int RESPONSE_NOT_IMPLEMENTED = 501;
124:            /** Code corresponding to '502 Bad Gateway'. */
125:            public static final int RESPONSE_BAD_GATEWAY = 502;
126:            /** Code corresponding to '503 Service Unavailable'. */
127:            public static final int RESPONSE_SERVICE_UNAVAILABLE = 503;
128:
129:            /** Code corresponding to '504 Gateway Timeout'. */
130:            public static final int RESPONSE_GATEWAY_TIMEOUT = 504;
131:            /** Code corresponding to '505 HTTP Version Not Supported'. */
132:            public static final int RESPONSE_HTTP_VERSION_NOT_SUPPORTED = 505;
133:
134:            /** The default MIME type for responses, which is "text/html". */
135:            public static final String DEFAULT_MIME_TYPE = "text/html";
136:
137:            private static final boolean DEBUG = false;
138:
139:            /** The code corresponding to the response. */
140:            protected int code;
141:            /** The default response header. */
142:            protected static String defaultHeader = "Server: Rimfaxe Web Server 1.03"
143:                    + CRLF;
144:
145:            /** The actual data of the response. */
146:            protected BufferElement combinedData;
147:            /** The header for the response. */
148:            protected BufferElement header;
149:            /** The payload for the response. */
150:            protected BufferElement payload;
151:            /** The MIME type of the response. */
152:            protected String contentType;
153:            /** The content-length header. */
154:            protected int contentLength;
155:
156:            protected Hashtable headers = new Hashtable();
157:
158:            private int ServerPort = -1;
159:
160:            /**
161:             * Create an httpResponse with the given response code with the given
162:             * payload. 
163:             *
164:             * @param code The response code; should be one of the constants
165:             *  from httpResponse.RESPONSE_*.
166:             * @param contentType The MIME type of the response content. Should
167:             *  not be CRLF-terminated.
168:             * @param payload The payload of the response.
169:             */
170:            protected httpResponse(int code, String contentType,
171:                    BufferElement payload, Hashtable headertable) {
172:                this .code = code;
173:                this .contentType = contentType;
174:                this .contentLength = payload.size;
175:                this .headers = headertable;
176:
177:                this .combinedData = null;
178:                String hdrString = genHeader();
179:                byte hdr[] = hdrString.getBytes();
180:                this .header = new BufferElement(hdr);
181:                this .payload = payload;
182:            }
183:
184:            /**
185:             * Create an httpResponse with the given response code with no payload.
186:             * A payload can be assigned later using setPayload().
187:             *
188:             * @param code The response code; should be one of the constants
189:             *  from httpResponse.RESPONSE_*.
190:             * @param contentType The MIME type of the response content. Should
191:             *  not be CRLF-terminated.
192:             * @param payload The payload of the response.
193:             */
194:            protected httpResponse(int code, String contentType,
195:                    Hashtable headertable) {
196:                this .code = code;
197:                this .contentType = contentType;
198:                this .contentLength = 0; // Don't know it yet
199:                this .headers = headertable;
200:
201:                //System.out.println("Generate response with code="+code);
202:                this .combinedData = null;
203:                String hdrString = genHeader();
204:                byte hdr[] = hdrString.getBytes();
205:                this .header = new BufferElement(hdr);
206:                this .payload = null;
207:            }
208:
209:            /**
210:             * Create an httpResponse with the given response code with the given
211:             * payload. 
212:             *
213:             * @param code The response code; should be one of the constants
214:             *  from httpResponse.RESPONSE_*.
215:             * @param contentType The MIME type of the response content. Should
216:             *  not be CRLF-terminated.
217:             * @param payload The payload of the response.
218:             */
219:            protected httpResponse(int code, String contentType,
220:                    BufferElement payload) {
221:                this .code = code;
222:                this .contentType = contentType;
223:                this .contentLength = payload.size;
224:
225:                this .combinedData = null;
226:                String hdrString = genHeader();
227:                byte hdr[] = hdrString.getBytes();
228:                this .header = new BufferElement(hdr);
229:                this .payload = payload;
230:            }
231:
232:            /**
233:             * Create an httpResponse with the given response code with the given
234:             * payload. 
235:             *
236:             * @param code The response code; should be one of the constants
237:             *  from httpResponse.RESPONSE_*.
238:             * @param contentType The MIME type of the response content. Should
239:             *  not be CRLF-terminated.
240:             * @param payload The payload of the response.
241:             * @param contentLength The contentLength to place in the header.
242:             */
243:            protected httpResponse(int code, String contentType,
244:                    BufferElement payload, int contentLength) {
245:                this .code = code;
246:                this .contentType = contentType;
247:                this .contentLength = contentLength;
248:
249:                this .combinedData = null;
250:                String hdrString = genHeader();
251:                byte hdr[] = hdrString.getBytes();
252:                this .header = new BufferElement(hdr);
253:                this .payload = payload;
254:            }
255:
256:            /**
257:             * Create an httpResponse with the given response code with no payload.
258:             * A payload can be assigned later using setPayload().
259:             *
260:             * @param code The response code; should be one of the constants
261:             *  from httpResponse.RESPONSE_*.
262:             * @param contentType The MIME type of the response content. Should
263:             *  not be CRLF-terminated.
264:             * @param payload The payload of the response.
265:             */
266:            protected httpResponse(int code, String contentType) {
267:                this .code = code;
268:                this .contentType = contentType;
269:                this .contentLength = 0; // Don't know it yet
270:
271:                this .combinedData = null;
272:                this .header = null;
273:                this .payload = null;
274:            }
275:
276:            /**
277:             * Create an httpResponse with the the given response code, with an
278:             * empty payload of the given size. This can be more efficient than
279:             * providing a payload separately, as the entire contents of the 
280:             * httpResponse can be sent as a single TCP packet. The payload can
281:             * be filled in using the getPayload() method.
282:             *
283:             * @param code The response code; should be one of the constants
284:             *  from httpResponse.RESPONSE_*.
285:             * @param contentType The MIME type of the response content. Should
286:             *  not be CRLF-terminated.
287:             * @param payloadSize The size of the payload to allocate.
288:             * @param compQ The completion queue for the payload.
289:             */
290:            protected httpResponse(int code, String contentType,
291:                    int payloadSize, SinkIF compQ) {
292:                this .code = code;
293:                this .contentType = contentType;
294:                this .contentLength = payloadSize;
295:
296:                String hdrString = genHeader();
297:                byte hdr[] = hdrString.getBytes();
298:                this .combinedData = new BufferElement(hdr.length + payloadSize);
299:                combinedData.compQ = compQ;
300:                this .header = new BufferElement(combinedData.data, 0,
301:                        hdr.length);
302:                System.arraycopy(hdr, 0, header.data, 0, hdr.length);
303:                this .payload = new BufferElement(combinedData.data, hdr.length,
304:                        payloadSize);
305:            }
306:
307:            /**
308:             * Create an httpResponse with the the given response code, with an
309:             * empty payload of the given size. This can be more efficient than
310:             * providing a payload separately, as the entire contents of the 
311:             * httpResponse can be sent as a single TCP packet. The payload can
312:             * be filled in using the getPayload() method.
313:             *
314:             * @param code The response code; should be one of the constants
315:             *  from httpResponse.RESPONSE_*.
316:             * @param contentType The MIME type of the response content. Should
317:             *  not be CRLF-terminated.
318:             * @param payloadSize The size of the payload to allocate.
319:             */
320:            protected httpResponse(int code, String contentType, int payloadSize) {
321:                this (code, contentType, payloadSize, null);
322:            }
323:
324:            /**
325:             * Create an httpResponse with the the given response code, with an
326:             * empty payload of the given size. This can be more efficient than
327:             * providing a payload separately, as the entire contents of the 
328:             * httpResponse can be sent as a single TCP packet. The payload can
329:             * be filled in using the getPayload() method.
330:             *
331:             * @param code The response code; should be one of the constants
332:             *  from httpResponse.RESPONSE_*.
333:             * @param contentType The MIME type of the response content. Should
334:             *  not be CRLF-terminated.
335:             * @param payloadSize The size of the payload to allocate.
336:             */
337:            protected httpResponse(int code, String contentType,
338:                    int payloadSize, Hashtable headertable, boolean withheaders) {
339:                this .code = code;
340:                this .contentType = contentType;
341:                this .contentLength = payloadSize;
342:                this .headers = headertable;
343:
344:                String hdrString = genHeader();
345:                byte hdr[] = hdrString.getBytes();
346:                this .combinedData = new BufferElement(hdr.length + payloadSize);
347:                combinedData.compQ = null;
348:                this .header = new BufferElement(combinedData.data, 0,
349:                        hdr.length);
350:                System.arraycopy(hdr, 0, header.data, 0, hdr.length);
351:                this .payload = new BufferElement(combinedData.data, hdr.length,
352:                        payloadSize);
353:            }
354:
355:            public void setServerPort(int val) {
356:                ServerPort = val;
357:            }
358:
359:            public int getServerPort() {
360:                return ServerPort;
361:            }
362:
363:            /** 
364:             * Return the entity header as a String. Must be implemented by 
365:             * subclasses of httpResponse.
366:             */
367:            protected abstract String getEntityHeader();
368:
369:            /**
370:             * Used to set the payload after creating the response with an 
371:             * empty payload. XXX Should not be used if the payload was allocated 
372:             * by this response (that is, if the payloadSize was specified in the 
373:             * constructor). 
374:             */
375:            public void setPayload(BufferElement payload) {
376:                this .payload = payload;
377:                this .contentLength = payload.size;
378:            }
379:
380:            /**
381:             * Returns the header for this response.
382:             */
383:            public BufferElement getHeader() {
384:                if (this .header == null) {
385:                    String hdrString = genHeader();
386:                    byte hdr[] = hdrString.getBytes();
387:                    this .header = new BufferElement(hdr);
388:                }
389:                return this .header;
390:            }
391:
392:            /**
393:             * Returns the payload for this response.
394:             */
395:            public BufferElement getPayload() {
396:                return payload;
397:            }
398:
399:            /**
400:             * Set the default header string sent in all responses.
401:             */
402:            public static void setDefaultHeader(String defhdr) {
403:                defaultHeader = defhdr;
404:            }
405:
406:            /**
407:             * Return the default header string sent in all responses.
408:             */
409:            public static String getDefaultHeader() {
410:                return defaultHeader;
411:            }
412:
413:            /**
414:             * Generate the header.
415:             */
416:            private String genHeader() 
417:  {
418:    String hdrString;
419:    switch (code) 
420:    {
421:      case RESPONSE_CONTINUE :
422:        hdrString = HTTP_VERSION+" 100 CONTINUE\n"; break; 
423:      case RESPONSE_SWITCHING_PROTOCOLS :
424:        hdrString = HTTP_VERSION+" 101 SWITCHING PROTOCOLS\n"; break; 
425:        
426:      case RESPONSE_OK: 
427:	hdrString = HTTP_VERSION+" 200 OK\n"; break;
428:      case RESPONSE_CREATED:
429:	hdrString = HTTP_VERSION+" 201 CREATED\n"; break;
430:      case RESPONSE_ACCEPTED:
431:	hdrString = HTTP_VERSION+" 202 ACCEPTED\n"; break;  
432:      case RESPONSE_NON_AUTHORITATIVE_INFORMATION: 
433:	hdrString = HTTP_VERSION+" 203 Non-Authoritative Information\n"; break;
434:      case RESPONSE_NO_CONTENT:
435:	hdrString = HTTP_VERSION+" 204 NO CONTENT\n"; break;  
436:      case RESPONSE_RESET_CONTENT: 
437:	hdrString = HTTP_VERSION+" 205 RESET CONTENT\n"; break;
438:      case RESPONSE_PARTIAL_CONTENT:
439:	hdrString = HTTP_VERSION+" 206 PARTIAL CONTENT\n"; break;    
440:        
441:      case RESPONSE_MULTIPLE_CHOICES:
442:	hdrString = HTTP_VERSION+" 300 Multiple Choices\n"; break;  
443:      case RESPONSE_REDIRECT:
444:	hdrString = HTTP_VERSION+" 301 MOVED PERMANENTLY\n"; break;
445:      case RESPONSE_FOUND:
446:	hdrString = HTTP_VERSION+" 302 FOUND\n"; break;  
447:      case RESPONSE_SEE_OTHER:
448:	hdrString = HTTP_VERSION+" 303 SEE OTHER\n"; break;
449:      case RESPONSE_NOT_MODIFIED:
450:	hdrString = HTTP_VERSION+" 304 NOT MODIFIED\n"; break;  
451:        
452:      case RESPONSE_USE_PROXY:
453:	hdrString = HTTP_VERSION+" 305 USE PROXY\n"; break;  
454:      case RESPONSE_RESERVED_306:
455:	hdrString = HTTP_VERSION+" 306 Reserved\n"; break;
456:      case RESPONSE_TEMPORARY_REDIRECT:
457:	hdrString = HTTP_VERSION+" 307 TEMPORARY REDIRECT\n"; break;   
458:        
459:        
460:      case RESPONSE_BAD_REQUEST: 
461:	hdrString = HTTP_VERSION+" 400 BAD REQUEST\n"; break;
462:      case RESPONSE_UNAUTHORIZED: 
463:	hdrString = HTTP_VERSION+" 401 UNAUTHORIZED\n"; break;
464:      case RESPONSE_PAYMENT_REQUIRED: 
465:	hdrString = HTTP_VERSION+" 402 PAYMENT REQUIRED\n"; break;
466:      case RESPONSE_FORBIDDEN: 
467:	hdrString = HTTP_VERSION+" 403 FORBIDDEN\n"; break;
468:      case RESPONSE_NOT_FOUND:
469:	hdrString = HTTP_VERSION+" 404 NOT FOUND\n"; break; 
470:      case RESPONSE_METHOD_NOT_ALLOWED: 
471:	hdrString = HTTP_VERSION+" 405 Method Not Allowed\n"; break;
472:      case RESPONSE_NOT_ACCEPTABLE: 
473:	hdrString = HTTP_VERSION+" 406 Not Acceptable\n"; break;
474:      case RESPONSE_PROXY_AUTHENTICATION_REQUIRED: 
475:	hdrString = HTTP_VERSION+" 407 Proxy Authentication Required\n"; break;
476:      case RESPONSE_REQUEST_TIMEOUT: 
477:	hdrString = HTTP_VERSION+" 408 REQUEST TIMEOUT\n"; break;
478:      case RESPONSE_CONFLICT:
479:	hdrString = HTTP_VERSION+" 409 CONFLICT\n"; break;  
480:        
481:        
482:      case RESPONSE_GONE: 
483:	hdrString = HTTP_VERSION+" 410 GONE\n"; break;
484:      case RESPONSE_LENGTH_REQUIRED: 
485:	hdrString = HTTP_VERSION+" 411 LENGTH REQUIRED\n"; break;
486:      case RESPONSE_PRECONDITION_FAILED: 
487:	hdrString = HTTP_VERSION+" 412 PRECONDITION FAILED\n"; break;
488:      case RESPONSE_REQUEST_ENTITY_TOO_LARGE: 
489:	hdrString = HTTP_VERSION+" 413 Request Entity Too Large\n"; break;
490:      case RESPONSE_REQUEST_URI_TOO_LONG:
491:	hdrString = HTTP_VERSION+" 414 Request-URI Too Long\n"; break; 
492:      case RESPONSE_UNSUPPORTED_MEDIA_TYPE: 
493:	hdrString = HTTP_VERSION+" 415 Unsupported Media Type\n"; break;
494:      case RESPONSE_REQUESTED_RANGE_NOT_SATISFIABLE: 
495:	hdrString = HTTP_VERSION+" 416 Requested Range Not Satisfiable\n"; break;
496:      case RESPONSE_EXPECTATION_FAILED: 
497:	hdrString = HTTP_VERSION+" 417 Expectation Failed\n"; break;
498:        
499:        
500:      case RESPONSE_INTERNAL_SERVER_ERROR:
501:      	hdrString = HTTP_VERSION+" 500 INTERNAL SERVER ERROR\n"; break;
502:      case RESPONSE_NOT_IMPLEMENTED:
503:      	hdrString = HTTP_VERSION+" 501 Not Implemented\n"; break;
504:      case RESPONSE_BAD_GATEWAY:
505:      	hdrString = HTTP_VERSION+" 502 Bad Gateway\n"; break;
506:      case RESPONSE_SERVICE_UNAVAILABLE:
507:      	hdrString = HTTP_VERSION+" 503 SERVICE UNAVAILABLE\n"; break;
508:      case RESPONSE_GATEWAY_TIMEOUT:
509:      	hdrString = HTTP_VERSION+" 504 Gateway Timeout\n"; break;
510:      case RESPONSE_HTTP_VERSION_NOT_SUPPORTED:
511:      	hdrString = HTTP_VERSION+" 505 HTTP Version Not Supported\n"; break;  
512:        
513:        
514:      default: 
515:        System.out.println("Bad code in httpResponse: "+code);
516:        hdrString = HTTP_VERSION+" "+code+" UNKNOWN RESPONSE CODE\n"; break;
517:	//throw new Error("Bad code in httpResponse: "+code);
518:    } 
519:    if (defaultHeader != null) 
520:        hdrString += defaultHeader;
521:    if (contentType != null) 
522:    {
523:      if (!contentType.equalsIgnoreCase("null"))
524:      {
525:        if (!headers.containsKey("Content-Type"))  
526:          hdrString += "Content-Type: "+contentType+CRLF;
527:      }
528:    }
529:    if (contentLength != 0) 
530:    {
531:      if (!headers.containsKey("Content-Length"))      
532:        hdrString += "Content-Length: "+contentLength+CRLF;
533:    }
534:       
535:    
536:    //hdrString += "Connection: close"+CRLF;
537:            
538:    Enumeration enum = headers.keys();
539:    while (enum.hasMoreElements())
540:    {
541:      String hkey = (String) enum.nextElement();
542:      String hval = (String) headers.get(hkey);
543:      hdrString += hkey+": "+hval+CRLF;
544:    }
545:    String ehdr = getEntityHeader();
546:    if (ehdr != null) {
547:      hdrString += ehdr;
548:    }
549:    hdrString += CRLF;
550:    
551:    //System.out.println("headers in response \n\n"+hdrString+"\n");
552:    return hdrString;
553:  }
554:
555:            /**
556:             * Get an array of BufferElements corresponding to this response.
557:             * Used internally when sending the response to a client.
558:             *
559:             * @param sendHeader Indicate whether the header should be included.
560:             */
561:            public BufferElement[] getBuffers(boolean sendHeader) {
562:                if (DEBUG)
563:                    System.err.println("httpResponse: getBuffers() called");
564:
565:                BufferElement bufarr[] = null;
566:                if (combinedData != null) {
567:                    if (sendHeader) {
568:                        if (DEBUG)
569:                            System.err
570:                                    .println("httpResponse: Returning combinedData (len="
571:                                            + combinedData.size + ")");
572:                        bufarr = new BufferElement[1];
573:                        bufarr[0] = combinedData;
574:                    } else {
575:                        if (DEBUG)
576:                            System.err
577:                                    .println("httpResponse: Returning combinedData payload only (len="
578:                                            + payload.size + ")");
579:                        bufarr = new BufferElement[1];
580:                        bufarr[0] = payload;
581:                    }
582:                } else if (sendHeader) {
583:                    if (payload != null) {
584:                        if (DEBUG)
585:                            System.err
586:                                    .println("httpResponse: Returning header and payload (paylen="
587:                                            + payload.size + ")");
588:                        bufarr = new BufferElement[2];
589:                        bufarr[0] = getHeader();
590:                        bufarr[1] = getPayload();
591:                    } else {
592:                        if (DEBUG)
593:                            System.err
594:                                    .println("httpResponse: Returning header only (len="
595:                                            + header.size + ")");
596:                        bufarr = new BufferElement[1];
597:                        bufarr[0] = getHeader();
598:                    }
599:                } else {
600:                    // Don't send header
601:                    if (payload != null) {
602:                        if (DEBUG)
603:                            System.err
604:                                    .println("httpResponse: Returning payload only (paylen="
605:                                            + payload.size + ")");
606:                        bufarr = new BufferElement[1];
607:                        bufarr[0] = payload;
608:                    } else {
609:                        if (DEBUG)
610:                            System.err
611:                                    .println("httpResponse: Nothing to return!");
612:                        bufarr = null;
613:                    }
614:                }
615:
616:                return bufarr;
617:            }
618:
619:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.