KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > wsif > wsdl > AuthenticatingProxyWSDLLocatorImpl


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2002 The Apache Software Foundation. All rights
6  * reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "WSIF" and "Apache Software Foundation" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation and was
52  * originally based on software copyright (c) 2001, 2002, International
53  * Business Machines, Inc., http://www.apache.org. For more
54  * information on the Apache Software Foundation, please see
55  * <http://www.apache.org/>.
56  */

57  
58 package org.apache.wsif.wsdl;
59  
60 import java.io.IOException JavaDoc;
61 import java.io.InputStream JavaDoc;
62 import java.io.InputStreamReader JavaDoc;
63 import java.io.Reader JavaDoc;
64 import java.io.UnsupportedEncodingException JavaDoc;
65 import java.net.URL JavaDoc;
66 import java.net.URLConnection JavaDoc;
67
68 import javax.wsdl.WSDLException;
69
70 /**
71  * Implementation of javax.wsdl.xml.WSDLLocator. This class can be used to
72  * locate a wsdl document behind an authenticating proxy. Only http and ftp
73  * urls for the wsdl location are supported.
74  *
75  * @author Owen Burroughs <owenb@apache.org>
76  */

77 public class AuthenticatingProxyWSDLLocatorImpl implements javax.wsdl.xml.WSDLLocator {
78  
79     private static final String JavaDoc PROXY_AUTH="Proxy-Authorization";
80     private static final char[] BASE64_CHARS = {
81         'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
82         'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
83         'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd',
84         'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
85         'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
86         'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7',
87         '8', '9', '+', '/'
88     };
89     private static final char BASE64_PAD_CHAR = '=';
90      
91     Reader JavaDoc baseReader = null;
92     Reader JavaDoc importReader = null;
93     String JavaDoc documentBase = "";
94     String JavaDoc importBase = "";
95     String JavaDoc wsdlLocation = "";
96     String JavaDoc username = null;
97     String JavaDoc password = null;
98     String JavaDoc authString = null;
99
100     /**
101      * Create an instance of AuthenticatingProxyWSDLLocatorImpl.
102      * @param wsdlLoc The uri for the base wsdl document
103      * @param un The username for proxy authentication
104      * @param passed The password for proxy authentication
105      */

106     public AuthenticatingProxyWSDLLocatorImpl(String JavaDoc wsdlLoc, String JavaDoc un, String JavaDoc passwd)
107         throws WSDLException {
108
109         if (wsdlLoc == null
110             || (wsdlLoc.indexOf("http://") == -1
111                 && wsdlLoc.indexOf("ftp://") == -1)) {
112             throw new WSDLException(
113                 WSDLException.OTHER_ERROR,
114                 "Base wsdl location type not supported. The AuthenticatingProxyWSDLLocatorImpl class "
115                     + "only supports http and ftp urls for base wsdl locations");
116         }
117         this.wsdlLocation = wsdlLoc;
118         this.username = un;
119         this.password = passwd;
120     }
121
122     /**
123      * Get a reader for the base wsdl document. Returns null if the document
124      * cannot be located.
125      * @return The reader or null if the import cannot be resolved
126      */

127     public Reader JavaDoc getBaseReader() {
128         if (baseReader == null) {
129             try {
130                 URL JavaDoc url = new URL JavaDoc(wsdlLocation);
131                 URLConnection JavaDoc con = url.openConnection();
132                 createAuthString();
133                 if (authString != null) {
134                     con.setRequestProperty(PROXY_AUTH, authString);
135                 }
136                 InputStream JavaDoc in = con.getInputStream();
137                 if (in != null) {
138                     baseReader = new InputStreamReader JavaDoc(in);
139                 }
140                 if (url != null)
141                     documentBase = url.toString();
142             } catch (Exception JavaDoc e) {
143                 documentBase = wsdlLocation;
144             }
145         }
146         return baseReader;
147     }
148
149     /**
150      * Get a reader for an imported wsdl document. Returns null if the import document
151      * cannot be located.
152      * @param base The document base uri for the parent wsdl document
153      * @param relativeLocation The relative uri of the import wsdl document
154      * @return The reader or null if the import cannot be resolved
155      */

156     public Reader JavaDoc getImportReader(String JavaDoc base, String JavaDoc relativeLocation) {
157         try {
158             URL JavaDoc contextURL = (base != null) ? new URL JavaDoc(base) : null;
159             URL JavaDoc url = new URL JavaDoc(contextURL, relativeLocation);
160             URLConnection JavaDoc con = url.openConnection();
161             createAuthString();
162             if (authString != null) {
163                 con.setRequestProperty(PROXY_AUTH, authString);
164             }
165             InputStream JavaDoc in = con.getInputStream();
166             if (in != null) {
167                 importReader = new InputStreamReader JavaDoc(in);
168             }
169             importBase = (url == null) ? relativeLocation : url.toString();
170         } catch (Exception JavaDoc e2) {
171             // we can't find the import so set a temporary value for the import URI. This is
172
// necessary to avoid a NullPointerException in WSDLReaderImpl
173
importBase = "unknownImportURI";
174         }
175         return importReader;
176     }
177
178     /**
179      * Get the document base uri for the base wsdl document
180      * @return The document base uri
181      */

182     public String JavaDoc getBaseURI() {
183         return documentBase;
184     }
185
186     /**
187      * Get the document base uri for the last import document to be resolved
188      * by this locator. This is useful if resolving imports within imports.
189      * @return The document base uri
190      */

191     public String JavaDoc getLatestImportURI() {
192         return importBase;
193     }
194
195     /**
196      * Generate the proxy authentication header value. Base64 encoding
197      * code is based on that from Apache Axis and Apache SOAP, written
198      * by TAMURA Kent &lt;kent@trl.ibm.co.jp&gt;
199      */

200     private void createAuthString() {
201         // Don't recreate the String if it has already been created
202
if (authString != null)
203             return;
204         // Unless all information is provided we can't create the String
205
if (username == null || password == null)
206             return;
207
208         byte[] data = null;
209         try {
210             data = (username + ":" + password).getBytes("8859_1");
211         } catch (UnsupportedEncodingException JavaDoc uee) {
212             return;
213         }
214         int len = data.length;
215         char[] out = new char[len / 3 * 4 + 4];
216         int readIndex = 0;
217         int writeIndex = 0;
218         int remainingBytes = len;
219         while (remainingBytes >= 3) {
220             int i =
221                 ((data[readIndex] & 0xff) << 16)
222                     + ((data[readIndex + 1] & 0xff) << 8)
223                     + (data[readIndex + 2] & 0xff);
224             out[writeIndex++] = BASE64_CHARS[i >> 18];
225             out[writeIndex++] = BASE64_CHARS[(i >> 12) & 0x3f];
226             out[writeIndex++] = BASE64_CHARS[(i >> 6) & 0x3f];
227             out[writeIndex++] = BASE64_CHARS[i & 0x3f];
228             readIndex += 3;
229             remainingBytes -= 3;
230         }
231         // Deal with adding padding characters if the number of bytes in the data
232
// array was not exactly divisible by 3.
233
if (remainingBytes == 1) {
234             int i = data[readIndex] & 0xff;
235             out[writeIndex++] = BASE64_CHARS[i >> 2];
236             out[writeIndex++] = BASE64_CHARS[(i << 4) & 0x3f];
237             out[writeIndex++] = BASE64_PAD_CHAR;
238             out[writeIndex++] = BASE64_PAD_CHAR;
239         } else if (remainingBytes == 2) {
240             int i =
241                 ((data[readIndex] & 0xff) << 8) + (data[readIndex + 1] & 0xff);
242             out[writeIndex++] = BASE64_CHARS[i >> 10];
243             out[writeIndex++] = BASE64_CHARS[(i >> 4) & 0x3f];
244             out[writeIndex++] = BASE64_CHARS[(i << 2) & 0x3f];
245             out[writeIndex++] = BASE64_PAD_CHAR;
246         }
247
248         String JavaDoc encoded = new String JavaDoc(out, 0, writeIndex);
249         authString = "Basic " + encoded;
250     }
251
252     /**
253      * Close any Reader objects that have been created
254      * @throws IOException If a call to close() on one of the Reader objects fails
255      */

256     public void close() throws IOException JavaDoc {
257         if (baseReader != null) baseReader.close();
258         if (importReader != null) importReader.close();
259     }
260 }
Popular Tags