Source Code Cross Referenced for AuthenticationHeader.java in  » 6.0-JDK-Modules » j2me » sun » net » www » protocol » 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 » 6.0 JDK Modules » j2me » sun.net.www.protocol.http 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * @(#)AuthenticationHeader.java	1.7 06/10/10
003:         *
004:         * Copyright  1990-2006 Sun Microsystems, Inc. All Rights Reserved.
005:         * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006:         * 
007:         * This program is free software; you can redistribute it and/or
008:         * modify it under the terms of the GNU General Public License version
009:         * 2 only, as published by the Free Software Foundation. 
010:         * 
011:         * This program is distributed in the hope that it will be useful, but
012:         * WITHOUT ANY WARRANTY; without even the implied warranty of
013:         * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014:         * General Public License version 2 for more details (a copy is
015:         * included at /legal/license.txt). 
016:         * 
017:         * You should have received a copy of the GNU General Public License
018:         * version 2 along with this work; if not, write to the Free Software
019:         * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020:         * 02110-1301 USA 
021:         * 
022:         * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023:         * Clara, CA 95054 or visit www.sun.com if you need additional
024:         * information or have any questions. 
025:         */
026:
027:        package sun.net.www.protocol.http;
028:
029:        import sun.net.www.*;
030:        import java.util.Iterator;
031:        import java.util.HashMap;
032:
033:        /**
034:         * This class is used to parse the information in WWW-Authenticate: and Proxy-Authenticate:
035:         * headers. It searches among multiple header lines and within each header line
036:         * for the best currently supported scheme. It can also return a HeaderParser
037:         * containing the challenge data for that particular scheme.
038:         *
039:         * Some examples:
040:         *
041:         * WWW-Authenticate: Basic realm="foo" Digest realm="bar" NTLM
042:         *  Note the realm parameter must be associated with the particular scheme.
043:         *
044:         * or
045:         * 
046:         * WWW-Authenticate: Basic realm="foo"
047:         * WWW-Authenticate: Digest realm="foo",qop="auth",nonce="thisisanunlikelynonce"
048:         * WWW-Authenticate: NTLM
049:         * 
050:         * or 
051:         *
052:         * WWW-Authenticate: Basic realm="foo"
053:         * WWW-Authenticate: NTLM ASKAJK9893289889QWQIOIONMNMN
054:         *
055:         * The last example shows how NTLM breaks the rules of rfc2617 for the structure of 
056:         * the authentication header. This is the reason why the raw header field is used for ntlm.
057:         *
058:         * At present, the class chooses schemes in following order :
059:         * 	1. Digest 2. NTLM (if supported) 3. Basic
060:         */
061:
062:        public class AuthenticationHeader {
063:
064:            MessageHeader rsp; // the response to be parsed
065:            HeaderParser preferred;
066:            String preferred_r; // raw Strings
067:
068:            String hdrname; // Name of the header to look for
069:
070:            /**
071:             * parse a set of authentication headers and choose the preferred scheme
072:             * that we support
073:             */
074:            public AuthenticationHeader(String hdrname, MessageHeader response) {
075:                rsp = response;
076:                this .hdrname = hdrname;
077:                schemes = new HashMap();
078:                parse();
079:            }
080:
081:            /* we build up a map of scheme names mapped to SchemeMapValue objects */
082:            static class SchemeMapValue {
083:                SchemeMapValue(HeaderParser h, String r) {
084:                    raw = r;
085:                    parser = h;
086:                }
087:
088:                String raw;
089:                HeaderParser parser;
090:            }
091:
092:            HashMap schemes;
093:
094:            /* Iterate through each header line, and then within each line.
095:             * If multiple entries exist for a particular scheme (unlikely)
096:             * then the last one will be used. The
097:             * preferred scheme that we support will be used.
098:             */
099:            private void parse() {
100:                Iterator iter = rsp.multiValueIterator(hdrname);
101:                while (iter.hasNext()) {
102:                    String raw = (String) iter.next();
103:                    HeaderParser hp = new HeaderParser(raw);
104:                    Iterator keys = hp.keys();
105:                    int i, lastSchemeIndex;
106:                    for (i = 0, lastSchemeIndex = -1; keys.hasNext(); i++) {
107:                        keys.next();
108:                        if (hp.findValue(i) == null) { /* found a scheme name */
109:                            if (lastSchemeIndex != -1) {
110:                                HeaderParser hpn = hp.subsequence(
111:                                        lastSchemeIndex, i);
112:                                String scheme = hpn.findKey(0);
113:                                schemes.put(scheme,
114:                                        new SchemeMapValue(hpn, raw));
115:                            }
116:                            lastSchemeIndex = i;
117:                        }
118:                    }
119:                    if (i > lastSchemeIndex) {
120:                        HeaderParser hpn = hp.subsequence(lastSchemeIndex, i);
121:                        String scheme = hpn.findKey(0);
122:                        schemes.put(scheme, new SchemeMapValue(hpn, raw));
123:                    }
124:                }
125:
126:                /* choose the best of them */
127:                SchemeMapValue v;
128:                if ((v = (SchemeMapValue) schemes.get("digest")) == null) {
129:                    //If !NTLMAuthentication.isSupported()
130:                    if ((v = (SchemeMapValue) schemes.get("ntlm")) == null) {
131:                        v = (SchemeMapValue) schemes.get("basic");
132:                    }
133:                }
134:                if (v != null) {
135:                    preferred = v.parser;
136:                    preferred_r = v.raw;
137:                    ;
138:                }
139:            }
140:
141:            /**
142:             * return a header parser containing the preferred authentication scheme (only).
143:             * The preferred scheme is the strongest of the schemes proposed by the server.
144:             * The returned HeaderParser will contain the relevant parameters for that scheme
145:             */
146:            public HeaderParser headerParser() {
147:                return preferred;
148:            }
149:
150:            /**
151:             * return the name of the preferred scheme
152:             */
153:            public String scheme() {
154:                if (preferred != null) {
155:                    return preferred.findKey(0);
156:                } else {
157:                    return null;
158:                }
159:            }
160:
161:            /* return the raw header field for the preferred/chosen scheme */
162:
163:            public String raw() {
164:                return preferred_r;
165:            }
166:
167:            /**
168:             * returns true is the header exists and contains a recognised scheme
169:             */
170:            public boolean isPresent() {
171:                return preferred != null;
172:            }
173:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.