Source Code Cross Referenced for HttpUtils.java in  » EJB-Server-JBoss-4.2.1 » testsuite » org » jboss » test » util » web » 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 » EJB Server JBoss 4.2.1 » testsuite » org.jboss.test.util.web 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * JBoss, Home of Professional Open Source.
003:         * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004:         * as indicated by the @author tags. See the copyright.txt file in the
005:         * distribution for a full listing of individual contributors.
006:         *
007:         * This is free software; you can redistribute it and/or modify it
008:         * under the terms of the GNU Lesser General Public License as
009:         * published by the Free Software Foundation; either version 2.1 of
010:         * the License, or (at your option) any later version.
011:         *
012:         * This software is distributed in the hope that it will be useful,
013:         * but WITHOUT ANY WARRANTY; without even the implied warranty of
014:         * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015:         * Lesser General Public License for more details.
016:         *
017:         * You should have received a copy of the GNU Lesser General Public
018:         * License along with this software; if not, write to the Free
019:         * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020:         * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021:         */
022:        package org.jboss.test.util.web;
023:
024:        import java.net.URL;
025:        import java.net.HttpURLConnection;
026:        import java.io.IOException;
027:        import org.apache.commons.httpclient.HttpClient;
028:        import org.apache.commons.httpclient.UsernamePasswordCredentials;
029:        import org.apache.commons.httpclient.HttpMethodBase;
030:        import org.apache.commons.httpclient.Header;
031:        import org.apache.commons.httpclient.methods.GetMethod;
032:        import org.apache.commons.httpclient.methods.PostMethod;
033:        import org.apache.commons.httpclient.methods.HeadMethod;
034:        import org.apache.commons.httpclient.methods.OptionsMethod;
035:        import org.apache.commons.httpclient.methods.PutMethod;
036:        import org.apache.commons.httpclient.methods.DeleteMethod;
037:        import org.apache.commons.httpclient.methods.TraceMethod;
038:        import org.jboss.logging.Logger;
039:
040:        /** Utilities for client http requests
041:         * 
042:         * @author Scott.Stark@jboss.org
043:         * @version $Revision: 57211 $
044:         */
045:        public class HttpUtils {
046:            private static Logger log = Logger.getLogger(HttpUtils.class);
047:            private static String baseURL = "http://jduke:theduke@"
048:                    + System.getProperty("jbosstest.server.host", "localhost")
049:                    + ":" + Integer.getInteger("web.port", 8080) + "/";
050:            private static String baseURLNoAuth = "http://"
051:                    + System.getProperty("jbosstest.server.host", "localhost")
052:                    + ":" + Integer.getInteger("web.port", 8080) + "/";
053:
054:            public static final int GET = 1;
055:            public static final int POST = 2;
056:            public static final int HEAD = 3;
057:            public static final int OPTIONS = 4;
058:            public static final int PUT = 5;
059:            public static final int DELETE = 6;
060:            public static final int TRACE = 7;
061:
062:            public static String getBaseURL() {
063:                return baseURL;
064:            }
065:
066:            public static String getBaseURL(String username, String password) {
067:                String url = "http://"
068:                        + username
069:                        + ":"
070:                        + password
071:                        + "@"
072:                        + System.getProperty("jbosstest.server.host",
073:                                "localhost") + ":"
074:                        + Integer.getInteger("web.port", 8080) + "/";
075:                return url;
076:            }
077:
078:            public static String getBaseURLNoAuth() {
079:                return baseURLNoAuth;
080:            }
081:
082:            /** Perform a get on the indicated URL and assert an HTTP_OK response code
083:             * 
084:             * @param url
085:             * @return The commons HttpClient used to perform the get
086:             * @throws Exception on any failure
087:             */
088:            public static HttpMethodBase accessURL(URL url) throws Exception {
089:                return accessURL(url, "JBossTest Servlets",
090:                        HttpURLConnection.HTTP_OK);
091:            }
092:
093:            /** Perform a get on the indicated URL and assert that the response code
094:             * matches the expectedHttpCode argument.
095:             * 
096:             * @param url
097:             * @param expectedHttpCode the http response code expected
098:             * @return The commons HttpClient used to perform the get
099:             * @throws Exception on any failure
100:             */
101:            public static HttpMethodBase accessURL(URL url, String realm,
102:                    int expectedHttpCode) throws Exception {
103:                return accessURL(url, realm, expectedHttpCode, null);
104:            }
105:
106:            public static HttpMethodBase accessURL(URL url, String realm,
107:                    int expectedHttpCode, int type) throws Exception {
108:                return accessURL(url, realm, expectedHttpCode, null, type);
109:            }
110:
111:            public static HttpMethodBase accessURL(URL url, String realm,
112:                    int expectedHttpCode, Header[] hdrs) throws Exception {
113:                return accessURL(url, realm, expectedHttpCode, hdrs, GET);
114:            }
115:
116:            public static HttpMethodBase accessURL(URL url, String realm,
117:                    int expectedHttpCode, Header[] hdrs, int type)
118:                    throws Exception {
119:                HttpClient httpConn = new HttpClient();
120:                HttpMethodBase request = createMethod(url, type);
121:                int hdrCount = hdrs != null ? hdrs.length : 0;
122:                for (int n = 0; n < hdrCount; n++)
123:                    request.addRequestHeader(hdrs[n]);
124:                try {
125:                    log.debug("Connecting to: " + url);
126:                    String userInfo = url.getUserInfo();
127:                    if (userInfo != null) {
128:                        UsernamePasswordCredentials auth = new UsernamePasswordCredentials(
129:                                userInfo);
130:                        httpConn.getState().setCredentials(realm,
131:                                url.getHost(), auth);
132:                    }
133:                    log.debug("RequestURI: " + request.getURI());
134:                    int responseCode = httpConn.executeMethod(request);
135:                    String response = request.getStatusText();
136:                    log.debug("responseCode=" + responseCode + ", response="
137:                            + response);
138:                    String content = request.getResponseBodyAsString();
139:                    log.debug(content);
140:                    // Validate that we are seeing the requested response code
141:                    if (responseCode != expectedHttpCode) {
142:                        throw new IOException("Expected reply code:"
143:                                + expectedHttpCode + ", actual=" + responseCode);
144:                    }
145:                } catch (IOException e) {
146:                    throw e;
147:                }
148:                return request;
149:            }
150:
151:            public static HttpMethodBase createMethod(URL url, int type) {
152:                HttpMethodBase request = null;
153:                switch (type) {
154:                case GET:
155:                    request = new GetMethod(url.toString());
156:                    break;
157:                case POST:
158:                    request = new PostMethod(url.toString());
159:                    break;
160:                case HEAD:
161:                    request = new HeadMethod(url.toString());
162:                    break;
163:                case OPTIONS:
164:                    request = new OptionsMethod(url.toString());
165:                    break;
166:                case PUT:
167:                    request = new PutMethod(url.toString());
168:                    break;
169:                case DELETE:
170:                    request = new DeleteMethod(url.toString());
171:                    break;
172:                case TRACE:
173:                    request = new TraceMethod(url.toString());
174:                    break;
175:                }
176:                return request;
177:            }
178:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.