Source Code Cross Referenced for TestNTLMAuth.java in  » Net » Apache-common-HttpClient » org » apache » commons » httpclient » auth » 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.auth 
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/auth/TestNTLMAuth.java,v 1.2 2004/11/07 12:31:42 olegk Exp $
003:         * $Revision: 480424 $
004:         * $Date: 2006-11-29 06:56:49 +0100 (Wed, 29 Nov 2006) $
005:         * ====================================================================
006:         *
007:         *  Licensed to the Apache Software Foundation (ASF) under one or more
008:         *  contributor license agreements.  See the NOTICE file distributed with
009:         *  this work for additional information regarding copyright ownership.
010:         *  The ASF licenses this file to You under the Apache License, Version 2.0
011:         *  (the "License"); you may not use this file except in compliance with
012:         *  the License.  You may obtain a copy of the License at
013:         *
014:         *      http://www.apache.org/licenses/LICENSE-2.0
015:         *
016:         *  Unless required by applicable law or agreed to in writing, software
017:         *  distributed under the License is distributed on an "AS IS" BASIS,
018:         *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
019:         *  See the License for the specific language governing permissions and
020:         *  limitations under the License.
021:         * ====================================================================
022:         *
023:         * This software consists of voluntary contributions made by many
024:         * individuals on behalf of the Apache Software Foundation.  For more
025:         * information on the Apache Software Foundation, please see
026:         * <http://www.apache.org/>.
027:         *
028:         */
029:
030:        package org.apache.commons.httpclient.auth;
031:
032:        import java.io.IOException;
033:
034:        import junit.framework.Test;
035:        import junit.framework.TestSuite;
036:
037:        import org.apache.commons.httpclient.FakeHttpMethod;
038:        import org.apache.commons.httpclient.Header;
039:        import org.apache.commons.httpclient.HttpClientTestBase;
040:        import org.apache.commons.httpclient.HttpState;
041:        import org.apache.commons.httpclient.HttpStatus;
042:        import org.apache.commons.httpclient.HttpVersion;
043:        import org.apache.commons.httpclient.NTCredentials;
044:        import org.apache.commons.httpclient.methods.GetMethod;
045:        import org.apache.commons.httpclient.protocol.Protocol;
046:        import org.apache.commons.httpclient.server.HttpService;
047:        import org.apache.commons.httpclient.server.RequestLine;
048:        import org.apache.commons.httpclient.server.SimpleRequest;
049:        import org.apache.commons.httpclient.server.SimpleResponse;
050:
051:        /**
052:         * Test Methods for NTLM Authentication.
053:         *
054:         * @author Rodney Waldhoff
055:         * @author <a href="mailto:jsdever@apache.org">Jeff Dever</a>
056:         * @version $Id: TestNTLMAuth.java 480424 2006-11-29 05:56:49Z bayard $
057:         */
058:        public class TestNTLMAuth extends HttpClientTestBase {
059:
060:            // ------------------------------------------------------------ Constructor
061:            public TestNTLMAuth(String testName) throws IOException {
062:                super (testName);
063:            }
064:
065:            // ------------------------------------------------------------------- Main
066:            public static void main(String args[]) {
067:                String[] testCaseName = { TestNTLMAuth.class.getName() };
068:                junit.textui.TestRunner.main(testCaseName);
069:            }
070:
071:            // ------------------------------------------------------- TestCase Methods
072:
073:            public static Test suite() {
074:                return new TestSuite(TestNTLMAuth.class);
075:            }
076:
077:            // --------------------------------- 
078:
079:            public void testNTLMAuthenticationResponse1() throws Exception {
080:                String challenge = "NTLM";
081:                String expected = "NTLM TlRMTVNTUAABAAAABlIAAAYABgAkAAAABAAEACAAAABIT"
082:                        + "1NURE9NQUlO";
083:                NTCredentials cred = new NTCredentials("username", "password",
084:                        "host", "domain");
085:                FakeHttpMethod method = new FakeHttpMethod();
086:                AuthScheme authscheme = new NTLMScheme(challenge);
087:                authscheme.processChallenge(challenge);
088:                String response = authscheme.authenticate(cred, method);
089:                assertEquals(expected, response);
090:                assertFalse(authscheme.isComplete());
091:            }
092:
093:            public void testNTLMAuthenticationResponse2() throws Exception {
094:                String challenge = "NTLM TlRMTVNTUAACAAAACgAKADAAAAAGgoEAPc4kP4LtCV8AAAAAAAAAAJ4AngA"
095:                        + "6AAAASU5UUkFFUEhPWAIAFABJAE4AVABSAEEARQBQAEgATwBYAAEAEgBCAE8AQQB"
096:                        + "SAEQAUgBPAE8ATQAEACgAaQBuAHQAcgBhAGUAcABoAG8AeAAuAGUAcABoAG8AeAA"
097:                        + "uAGMAbwBtAAMAPABCAG8AYQByAGQAcgBvAG8AbQAuAGkAbgB0AHIAYQBlAHAAaAB"
098:                        + "vAHgALgBlAHAAaABvAHgALgBjAG8AbQAAAAAA";
099:
100:                String expected = "NTLM TlRMTVNTUAADAAAAGAAYAFIAAAAAAAAAagAAAAYABgB"
101:                        + "AAAAACAAIAEYAAAAEAAQATgAAAAAAAABqAAAABlIAAERPTUFJTlVTRVJOQU1FSE"
102:                        + "9TVAaC+vLxUEHnUtpItj9Dp4kzwQfd61Lztg==";
103:                NTCredentials cred = new NTCredentials("username", "password",
104:                        "host", "domain");
105:                FakeHttpMethod method = new FakeHttpMethod();
106:                AuthScheme authscheme = new NTLMScheme(challenge);
107:                authscheme.processChallenge(challenge);
108:                String response = authscheme.authenticate(cred, method);
109:                assertEquals(expected, response);
110:                assertTrue(authscheme.isComplete());
111:            }
112:
113:            private class NTLMAuthService implements  HttpService {
114:
115:                public NTLMAuthService() {
116:                    super ();
117:                }
118:
119:                public boolean process(final SimpleRequest request,
120:                        final SimpleResponse response) throws IOException {
121:                    RequestLine requestLine = request.getRequestLine();
122:                    HttpVersion ver = requestLine.getHttpVersion();
123:                    Header auth = request.getFirstHeader("Authorization");
124:                    if (auth == null) {
125:                        response.setStatusLine(ver, HttpStatus.SC_UNAUTHORIZED);
126:                        response.addHeader(new Header("WWW-Authenticate",
127:                                "NTLM"));
128:                        response.setBodyString("Authorization required");
129:                        return true;
130:                    } else {
131:                        String authstr = auth.getValue();
132:
133:                        if (authstr
134:                                .equals("NTLM TlRMTVNTUAABAAAABlIAAAYABgAkAAAABAAEACAAAABIT1NURE9NQUlO")) {
135:                            response.setStatusLine(ver,
136:                                    HttpStatus.SC_UNAUTHORIZED);
137:                            response
138:                                    .addHeader(new Header("WWW-Authenticate",
139:                                            "NTLM TlRMTVNTUAACAAAAAAAAACgAAAABggAAU3J2Tm9uY2UAAAAAAAAAAA=="));
140:                            response.setBodyString("Authorization required");
141:                            return true;
142:                        }
143:                        if (authstr
144:                                .equals("NTLM TlRMTVNTUAADAAAAGAAYAFIAAAAAAAAAagAAAAYABgBAAAAACAAIAEYAAAAEAAQATgAAAAAAAABqAAAABlIAAERPTUFJTlVTRVJOQU1FSE9TVJxndWIt46bHm11TPrt5Z6wrz7ziq04yRA==")) {
145:                            response.setStatusLine(ver, HttpStatus.SC_OK);
146:                            response.setBodyString("Authorization successful");
147:                            return true;
148:                        } else {
149:                            response.setStatusLine(ver,
150:                                    HttpStatus.SC_UNAUTHORIZED);
151:                            response.addHeader(new Header("WWW-Authenticate",
152:                                    "NTLM"));
153:                            response.setBodyString("Authorization required");
154:                            return true;
155:                        }
156:                    }
157:                }
158:            }
159:
160:            public void testNTLMAuthenticationRetry() throws Exception {
161:
162:                this .server.setHttpService(new NTLMAuthService());
163:
164:                // configure the client
165:                this .client.getHostConfiguration().setHost(
166:                        server.getLocalAddress(), server.getLocalPort(),
167:                        Protocol.getProtocol("http"));
168:
169:                this .client.getState().setCredentials(
170:                        AuthScope.ANY,
171:                        new NTCredentials("username", "password", "host",
172:                                "domain"));
173:
174:                FakeHttpMethod httpget = new FakeHttpMethod("/");
175:                try {
176:                    client.executeMethod(httpget);
177:                } finally {
178:                    httpget.releaseConnection();
179:                }
180:                assertNull(httpget.getResponseHeader("WWW-Authenticate"));
181:                assertEquals(200, httpget.getStatusCode());
182:            }
183:
184:            private class PreemptiveNTLMAuthService implements  HttpService {
185:
186:                public PreemptiveNTLMAuthService() {
187:                    super ();
188:                }
189:
190:                public boolean process(final SimpleRequest request,
191:                        final SimpleResponse response) throws IOException {
192:                    RequestLine requestLine = request.getRequestLine();
193:                    HttpVersion ver = requestLine.getHttpVersion();
194:                    Header auth = request.getFirstHeader("Authorization");
195:                    if (auth == null) {
196:                        response.setStatusLine(ver, HttpStatus.SC_BAD_REQUEST);
197:                        response.setBodyString("Authorization header missing");
198:                        return true;
199:                    } else {
200:                        String authstr = auth.getValue();
201:
202:                        if (authstr.indexOf("NTLM") != -1) {
203:                            response.setStatusLine(ver, HttpStatus.SC_OK);
204:                            return true;
205:                        } else if (authstr.indexOf("Basic") != -1) {
206:                            response.setStatusLine(ver,
207:                                    HttpStatus.SC_UNAUTHORIZED);
208:                            response.addHeader(new Header("WWW-Authenticate",
209:                                    "Negotiate"));
210:                            response.addHeader(new Header("WWW-Authenticate",
211:                                    "NTLM"));
212:                            response.setBodyString("Authorization required");
213:                            return true;
214:                        } else {
215:                            response.setStatusLine(ver,
216:                                    HttpStatus.SC_BAD_REQUEST);
217:                            response.setBodyString("Unknown auth type: "
218:                                    + authstr);
219:                            return true;
220:                        }
221:                    }
222:                }
223:            }
224:
225:            /**
226:             * Make sure preemptive authorization works when the server requires NLM.
227:             * @throws Exception
228:             */
229:            public void testPreemptiveAuthorization() throws Exception {
230:
231:                NTCredentials creds = new NTCredentials("testuser", "testpass",
232:                        "host", "domain");
233:
234:                HttpState state = new HttpState();
235:                state.setCredentials(AuthScope.ANY, creds);
236:                this .client.setState(state);
237:                this .client.getParams().setAuthenticationPreemptive(true);
238:
239:                this .server.setHttpService(new PreemptiveNTLMAuthService());
240:
241:                GetMethod httpget = new GetMethod("/test/");
242:                try {
243:                    this.client.executeMethod(httpget);
244:                } finally {
245:                    httpget.releaseConnection();
246:                }
247:                assertNotNull(httpget.getStatusLine());
248:                assertEquals(HttpStatus.SC_OK, httpget.getStatusLine()
249:                        .getStatusCode());
250:            }
251:
252:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.