KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > web > ProxyHandlerImpl


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.enterprise.web;
25
26 import java.io.ByteArrayInputStream JavaDoc;
27 import java.security.cert.X509Certificate JavaDoc;
28 import java.security.cert.CertificateException JavaDoc;
29 import java.security.cert.CertificateFactory JavaDoc;
30 import javax.servlet.http.HttpServletRequest JavaDoc;
31 import com.sun.appserv.ProxyHandler;
32
33 /**
34  * Default ProxyHandler implementation.
35  */

36 public class ProxyHandlerImpl extends ProxyHandler {
37
38     /**
39      * Gets the SSL client certificate chain with which the client
40      * had authenticated itself to the SSL offloader, and which the
41      * SSL offloader has added as a custom request header on the
42      * given request.
43      *
44      * @param request The request from which to retrieve the SSL client
45      * certificate chain
46      *
47      * @return Array of java.security.cert.X509Certificate instances
48      * representing the SSL client certificate chain, or null if this
49      * information is not available from the given request
50      *
51      * @throws CertificateException if the certificate chain retrieved
52      * from the request header cannot be parsed
53      */

54     public X509Certificate JavaDoc[] getSSLClientCertificateChain(
55                         HttpServletRequest JavaDoc request)
56             throws CertificateException JavaDoc {
57
58         X509Certificate JavaDoc[] certs = null;
59
60         String JavaDoc clientCert = request.getHeader("Proxy-auth-cert");
61         if (clientCert != null) {
62             clientCert = clientCert.replaceAll("% d% a", "\n");
63             clientCert = "-----BEGIN CERTIFICATE-----\n" + clientCert
64                          + "\n-----END CERTIFICATE-----";
65             byte[] certBytes = new byte[clientCert.length()];
66             clientCert.getBytes(0, clientCert.length(), certBytes, 0);
67             ByteArrayInputStream JavaDoc bais = new ByteArrayInputStream JavaDoc(certBytes);
68             CertificateFactory JavaDoc cf = CertificateFactory.getInstance("X.509");
69             certs = new X509Certificate JavaDoc[1];
70             certs[0] = (X509Certificate JavaDoc) cf.generateCertificate(bais);
71         }
72
73         return certs;
74     }
75
76     /**
77      * Returns the SSL keysize with which the original client request that
78      * was intercepted by the SSL offloader has been protected, and which
79      * the SSL offloader has added as a custom request header on the
80      * given request.
81      *
82      * @param request The request from which to retrieve the SSL key
83      * size
84      *
85      * @return SSL keysize, or -1 if this information is not available from
86      * the given request
87      */

88     public int getSSLKeysize(HttpServletRequest JavaDoc request) {
89
90         int keySize = -1;
91
92         String JavaDoc header = request.getHeader("Proxy-keysize");
93         if (header != null) {
94             keySize = Integer.parseInt(header);
95         }
96
97         return keySize;
98     }
99
100     /**
101      * Gets the Internet Protocol (IP) source port of the client request that
102      * was intercepted by the proxy server.
103      *
104      * @param request The request from which to retrieve the IP source port
105      * of the original client request
106      *
107      * @return IP source port of the original client request, or null if this
108      * information is not available from the given request
109      */

110     public String JavaDoc getRemoteAddress(HttpServletRequest JavaDoc request) {
111         return request.getHeader("Proxy-ip");
112     }
113 }
114
Popular Tags