Source Code Cross Referenced for SimpleResponse.java in  » Net » Apache-common-HttpClient » org » apache » commons » httpclient » server » 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 » Net » Apache common HttpClient » org.apache.commons.httpclient.server 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/test/org/apache/commons/httpclient/server/SimpleResponse.java,v 1.8 2004/11/13 12:21:28 olegk Exp $
003:         * $Revision: 480424 $
004:         * $Date: 2006-11-29 06:56:49 +0100 (Wed, 29 Nov 2006) $
005:         *
006:         * ====================================================================
007:         *
008:         *  Licensed to the Apache Software Foundation (ASF) under one or more
009:         *  contributor license agreements.  See the NOTICE file distributed with
010:         *  this work for additional information regarding copyright ownership.
011:         *  The ASF licenses this file to You under the Apache License, Version 2.0
012:         *  (the "License"); you may not use this file except in compliance with
013:         *  the License.  You may obtain a copy of the License at
014:         *
015:         *      http://www.apache.org/licenses/LICENSE-2.0
016:         *
017:         *  Unless required by applicable law or agreed to in writing, software
018:         *  distributed under the License is distributed on an "AS IS" BASIS,
019:         *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
020:         *  See the License for the specific language governing permissions and
021:         *  limitations under the License.
022:         * ====================================================================
023:         *
024:         * This software consists of voluntary contributions made by many
025:         * individuals on behalf of the Apache Software Foundation.  For more
026:         * information on the Apache Software Foundation, please see
027:         * <http://www.apache.org/>.
028:         *
029:         */
030:
031:        package org.apache.commons.httpclient.server;
032:
033:        import java.io.ByteArrayInputStream;
034:        import java.io.ByteArrayOutputStream;
035:        import java.io.IOException;
036:        import java.io.InputStream;
037:        import java.io.UnsupportedEncodingException;
038:        import java.util.Iterator;
039:
040:        import org.apache.commons.httpclient.ChunkedInputStream;
041:        import org.apache.commons.httpclient.ContentLengthInputStream;
042:        import org.apache.commons.httpclient.Header;
043:        import org.apache.commons.httpclient.HeaderElement;
044:        import org.apache.commons.httpclient.HeaderGroup;
045:        import org.apache.commons.httpclient.HttpStatus;
046:        import org.apache.commons.httpclient.HttpVersion;
047:        import org.apache.commons.httpclient.NameValuePair;
048:        import org.apache.commons.httpclient.StatusLine;
049:
050:        /**
051:         * A generic HTTP response.
052:         * 
053:         * @author Christian Kohlschuetter
054:         * @author Oleg Kalnichevski
055:         */
056:        public class SimpleResponse {
057:
058:            public static final String DEFAULT_CONTENT_CHARSET = "ISO-8859-1";
059:
060:            private HttpVersion ver = HttpVersion.HTTP_1_1;
061:            private int statuscode = HttpStatus.SC_OK;
062:            private String phrase = HttpStatus.getStatusText(HttpStatus.SC_OK);
063:            private HeaderGroup headers = new HeaderGroup();
064:            private InputStream entity = null;
065:
066:            public SimpleResponse() {
067:                super ();
068:            }
069:
070:            public SimpleResponse(final StatusLine statusline,
071:                    final Header[] headers, final InputStream content)
072:                    throws IOException {
073:                super ();
074:                if (statusline == null) {
075:                    throw new IllegalArgumentException(
076:                            "Status line may not be null");
077:                }
078:                setStatusLine(HttpVersion.parse(statusline.getHttpVersion()),
079:                        statusline.getStatusCode(), statusline
080:                                .getReasonPhrase());
081:                setHeaders(headers);
082:                if (content != null) {
083:                    InputStream in = content;
084:                    Header contentLength = this .headers
085:                            .getFirstHeader("Content-Length");
086:                    Header transferEncoding = this .headers
087:                            .getFirstHeader("Transfer-Encoding");
088:
089:                    if (transferEncoding != null) {
090:                        if (transferEncoding.getValue().indexOf("chunked") != -1) {
091:                            in = new ChunkedInputStream(in);
092:                        }
093:                    } else if (contentLength != null) {
094:                        long len = getContentLength();
095:                        if (len >= 0) {
096:                            in = new ContentLengthInputStream(in, len);
097:                        }
098:                    }
099:                    this .entity = in;
100:                }
101:            }
102:
103:            public void setStatusLine(final HttpVersion ver, int statuscode,
104:                    final String phrase) {
105:                if (ver == null) {
106:                    throw new IllegalArgumentException(
107:                            "HTTP version may not be null");
108:                }
109:                if (statuscode <= 0) {
110:                    throw new IllegalArgumentException(
111:                            "Status code may not be negative or zero");
112:                }
113:                this .ver = ver;
114:                this .statuscode = statuscode;
115:                if (phrase != null) {
116:                    this .phrase = phrase;
117:                } else {
118:                    this .phrase = HttpStatus.getStatusText(statuscode);
119:                }
120:            }
121:
122:            public void setStatusLine(final HttpVersion ver, int statuscode) {
123:                setStatusLine(ver, statuscode, null);
124:            }
125:
126:            public String getPhrase() {
127:                return this .phrase;
128:            }
129:
130:            public int getStatuscode() {
131:                return this .statuscode;
132:            }
133:
134:            public HttpVersion getHttpVersion() {
135:                return this .ver;
136:            }
137:
138:            public String getStatusLine() {
139:                StringBuffer buffer = new StringBuffer();
140:                buffer.append(this .ver);
141:                buffer.append(' ');
142:                buffer.append(this .statuscode);
143:                if (this .phrase != null) {
144:                    buffer.append(' ');
145:                    buffer.append(this .phrase);
146:                }
147:                return buffer.toString();
148:            }
149:
150:            public boolean containsHeader(final String name) {
151:                return this .headers.containsHeader(name);
152:            }
153:
154:            public Header[] getHeaders() {
155:                return this .headers.getAllHeaders();
156:            }
157:
158:            public Header getFirstHeader(final String name) {
159:                return this .headers.getFirstHeader(name);
160:            }
161:
162:            public void removeHeaders(final String s) {
163:                if (s == null) {
164:                    return;
165:                }
166:                Header[] headers = this .headers.getHeaders(s);
167:                for (int i = 0; i < headers.length; i++) {
168:                    this .headers.removeHeader(headers[i]);
169:                }
170:            }
171:
172:            public void addHeader(final Header header) {
173:                if (header == null) {
174:                    return;
175:                }
176:                this .headers.addHeader(header);
177:            }
178:
179:            public void setHeader(final Header header) {
180:                if (header == null) {
181:                    return;
182:                }
183:                removeHeaders(header.getName());
184:                addHeader(header);
185:            }
186:
187:            public void setHeaders(final Header[] headers) {
188:                if (headers == null) {
189:                    return;
190:                }
191:                this .headers.setHeaders(headers);
192:            }
193:
194:            public Iterator getHeaderIterator() {
195:                return this .headers.getIterator();
196:            }
197:
198:            public String getCharset() {
199:                String charset = DEFAULT_CONTENT_CHARSET;
200:                Header contenttype = this .headers
201:                        .getFirstHeader("Content-Type");
202:                if (contenttype != null) {
203:                    HeaderElement values[] = contenttype.getElements();
204:                    if (values.length == 1) {
205:                        NameValuePair param = values[0]
206:                                .getParameterByName("charset");
207:                        if (param != null) {
208:                            charset = param.getValue();
209:                        }
210:                    }
211:                }
212:                return charset;
213:            }
214:
215:            public long getContentLength() {
216:                Header contentLength = this .headers
217:                        .getFirstHeader("Content-Length");
218:                if (contentLength != null) {
219:                    try {
220:                        return Long.parseLong(contentLength.getValue());
221:                    } catch (NumberFormatException e) {
222:                        return -1;
223:                    }
224:                } else {
225:                    return -1;
226:                }
227:            }
228:
229:            public void setBodyString(final String string) {
230:                if (string != null) {
231:                    byte[] raw = null;
232:                    try {
233:                        raw = string.getBytes(DEFAULT_CONTENT_CHARSET);
234:                    } catch (UnsupportedEncodingException e) {
235:                        raw = string.getBytes();
236:                    }
237:                    this .entity = new ByteArrayInputStream(raw);
238:                    if (!containsHeader("Content-Type")) {
239:                        setHeader(new Header("Content-Type", "text/plain"));
240:                    }
241:                    setHeader(new Header("Content-Length", Long
242:                            .toString(raw.length)));
243:                } else {
244:                    this .entity = null;
245:                }
246:            }
247:
248:            public void setBody(final InputStream instream) {
249:                this .entity = instream;
250:            }
251:
252:            public InputStream getBody() {
253:                return this .entity;
254:            }
255:
256:            public byte[] getBodyBytes() throws IOException {
257:                InputStream in = getBody();
258:                if (in != null) {
259:                    byte[] tmp = new byte[4096];
260:                    int bytesRead = 0;
261:                    ByteArrayOutputStream buffer = new ByteArrayOutputStream(
262:                            1024);
263:                    while ((bytesRead = in.read(tmp)) != -1) {
264:                        buffer.write(tmp, 0, bytesRead);
265:                    }
266:                    return buffer.toByteArray();
267:                } else {
268:                    return null;
269:                }
270:            }
271:
272:            public String getBodyString() throws IOException {
273:                byte[] raw = getBodyBytes();
274:                if (raw != null) {
275:                    return new String(raw, getCharset());
276:                } else {
277:                    return null;
278:                }
279:            }
280:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.