Source Code Cross Referenced for HttpRequest.java in  » JMX » jfoxmx » org » huihoo » jfox » jmx » adaptor » 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 » JMX » jfoxmx » org.huihoo.jfox.jmx.adaptor.http 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /* JFox, the OpenSource J2EE Application Server
002:         *
003:         * Copyright (C) 2002 huihoo.com
004:         * Distributable under GNU LGPL license
005:         * See the GNU Lesser General Public License for more details.
006:         */
007:
008:        package org.huihoo.jfox.jmx.adaptor.http;
009:
010:        import java.util.Map;
011:        import java.util.HashMap;
012:        import java.util.StringTokenizer;
013:        import java.io.UnsupportedEncodingException;
014:
015:        /**
016:         *
017:         * @author <a href="mailto:young_yy@hotmail.com">Young Yang</a>
018:         */
019:
020:        public class HttpRequest {
021:            private HttpThread thread = null;
022:            private String method = "GET";
023:            private String uri = "/";
024:            private Map headersMap = new HashMap();
025:            private Map parameterMap = new HashMap();
026:
027:            private final static String[] RESERVED_HEADERS = {
028:                    HttpConstants.CONTENT_LENGTH_HEADER,
029:                    HttpConstants.CONTENT_TYPE_HEADER,
030:                    HttpConstants.CONNECTION, HttpConstants.DATE_HEADER,
031:                    HttpConstants.WWW_AUTHENTICATE_HEADER,
032:                    HttpConstants.AUTHORIZATION_HEADER };
033:
034:            public HttpRequest(HttpThread thread) {
035:                this .thread = thread;
036:            }
037:
038:            public void setURI(String uri) {
039:                this .uri = uri;
040:                parameterMap.clear();
041:                parseQueryString(getQueryString());
042:            }
043:
044:            private void setHeader(String name, String value) {
045:                headersMap.put(name.toUpperCase(), value.trim());
046:            }
047:
048:            public String getHeader(String name) {
049:                return (String) headersMap.get(name.toUpperCase());
050:            }
051:
052:            void setMethod(String method) {
053:                this .method = method;
054:            }
055:
056:            public String getMethod() {
057:                return method;
058:            }
059:
060:            // parse a head line String
061:            void parseHeadLine(String line, int line_no)
062:                    throws MalformedHttpHeaderException {
063:                if (line == null || line.trim().length() == 0)
064:                    return;
065:                line = line.trim();
066:                if (line_no == 1) { // Method & uri
067:                    parseMethodAndURI(line);
068:                } else {
069:                    parseHeader(line);
070:                }
071:
072:            }
073:
074:            private void parseMethodAndURI(String line)
075:                    throws MalformedHttpHeaderException {
076:                int i = line.indexOf(' ');
077:                int j = line.indexOf(' ', i + 1);
078:                if (i == -1)
079:                    throw new MalformedHttpHeaderException(
080:                            "Malformed request line");
081:                String _method = line.substring(0, i);
082:
083:                if (_method.equalsIgnoreCase("GET"))
084:                    method = "GET";
085:                else if (_method.equalsIgnoreCase("POST"))
086:                    method = "POST";
087:                else
088:                    throw new MalformedHttpHeaderException(
089:                            "Unsupported HTTP method");
090:                if (j > 0)
091:                    uri = line.substring(i + 1, j);
092:                else
093:                    uri = line.substring(i + 1);
094:
095:                //    System.out.println("URI: " + uri);
096:                if (uri.trim().equals("/"))
097:                    uri = "/Index"; // the default command
098:                if (uri.trim().startsWith("/?"))
099:                    uri = "/Index?" + uri.substring(2); // /?x=y => /Index?x=y
100:
101:                uri = decodeUrl(uri);
102:
103:                parseQueryString(getQueryString());
104:
105:            }
106:
107:            private void parseQueryString(String qstring) {
108:                if (qstring == null || qstring.trim().length() == 0)
109:                    return;
110:                StringTokenizer st = new StringTokenizer(qstring, "&");
111:                while (st.hasMoreTokens()) {
112:                    String nvPair = st.nextToken();
113:                    int index = nvPair.indexOf("=");
114:                    if (index < 0) { // only key , not have value
115:                        setParameter(nvPair, "");
116:                    } else {
117:                        String name = nvPair.substring(0, index);
118:                        String value = nvPair.substring(index + 1);
119:                        setParameter(name, value);
120:                    }
121:                }
122:            }
123:
124:            private void parseHeader(String line)
125:                    throws MalformedHttpHeaderException {
126:                line = line.trim();
127:                int index = line.indexOf(":");
128:                if (index < 0) {
129:                    throw new MalformedHttpHeaderException(
130:                            "Malformed HTTP header");
131:                } else {
132:                    String name = line.substring(0, index).trim();
133:                    String value = line.substring(index + 1).trim();
134:                    if (isKnownHeader(name))
135:                        setHeader(name, value);
136:                }
137:            }
138:
139:            private boolean isKnownHeader(String header) {
140:                boolean isKnown = false;
141:                for (int i = 0; i < RESERVED_HEADERS.length; i++) {
142:                    if (RESERVED_HEADERS[i].equalsIgnoreCase(header.trim())) {
143:                        isKnown = true;
144:                        break;
145:                    }
146:                }
147:                return isKnown;
148:            }
149:
150:            public String getRequestURI() {
151:                return "http://" + thread.getServer().getHost() + ":"
152:                        + thread.getServer().getPort() + uri;
153:            }
154:
155:            public String getQueryString() {
156:                int index = uri.indexOf("?");
157:                if (index < 0) {
158:                    return "";
159:                } else {
160:
161:                    return uri.substring(index + 1);
162:                }
163:            }
164:
165:            public String getCommand() {
166:                //    int index = uri.indexOf("?");
167:                //    if(index < 0){
168:                //      return uri.substring(1);
169:                //    }
170:                //    else{
171:                //      return uri.substring(1,index);
172:                //    }
173:                String command = this .getParameter("action");
174:                if (command == null || command.trim().length() == 0) {
175:                    command = "Agent";
176:                }
177:                return command;
178:
179:            }
180:
181:            public String getParameter(String name) {
182:                return (String) parameterMap.get(name.toUpperCase());
183:            }
184:
185:            public HttpThread getThread() {
186:                return thread;
187:            }
188:
189:            private void setParameter(String name, String value) {
190:                parameterMap.put(name.toUpperCase(), value.trim());
191:            }
192:
193:            // decode url
194:            private String decodeUrl(String encodeURL) {
195:                int length = encodeURL.length();
196:                char[] charArray = encodeURL.toCharArray();
197:                byte[] byteArray = new byte[length];
198:                int j = 0;
199:                boolean flag = true;
200:                for (int k = 0; k < length; k++) {
201:                    char c = charArray[k];
202:                    if (c < 0 || c > '\177')
203:                        throw new IllegalArgumentException("Not encoded");
204:                    byte byte0 = (byte) (0x7f & c);
205:                    if (c == '+') {
206:                        flag = false;
207:                        byte0 = 32;
208:                    } else if (c == '%' && k + 2 < length) {
209:                        flag = false;
210:                        byte0 = (byte) (0xff & Integer.parseInt(encodeURL
211:                                .substring(k + 1, k + 3), 16));
212:                        k += 2;
213:                    }
214:                    byteArray[j++] = byte0;
215:                }
216:
217:                if (flag)
218:                    return encodeURL;
219:                try {
220:                    return new String(byteArray, 0, j, "ISO-8859-1");
221:                } catch (UnsupportedEncodingException unsupportedencodingexception) {
222:                    return new String(byteArray, 0, j);
223:                }
224:            }
225:
226:        }
227:
228:        /*
229:         GET / HTTP/1.1
230:         Accept: * /*
231:         Accept-Language: zh-cn
232:         Accept-Encoding: gzip, deflate
233:         User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)
234:         Host: localhost:8082
235:         Connection: Keep-Alive
236:         */
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.