KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > webservice > MessageLayerClientHandler


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 /*
25  * ClientWSSHandler.java
26  *
27  * Created on June 1, 2004, 11:46 AM
28  */

29
30 package com.sun.enterprise.webservice;
31
32 import javax.xml.rpc.handler.Handler JavaDoc;
33 import javax.xml.rpc.handler.MessageContext JavaDoc;
34 import javax.xml.rpc.handler.soap.SOAPMessageContext JavaDoc;
35 import javax.xml.rpc.JAXRPCException JavaDoc;
36 import javax.xml.namespace.QName JavaDoc;
37 import javax.xml.soap.SOAPMessage JavaDoc;
38 import java.util.logging.*;
39 import java.util.Map JavaDoc;
40 import java.util.HashMap JavaDoc;
41
42 //security apis
43
import com.sun.enterprise.security.jauth.*;
44 import com.sun.enterprise.security.jauth.ClientAuthConfig;
45 import com.sun.enterprise.security.wss.WebServiceSecurity;
46
47 import com.sun.enterprise.Switch;
48
49 import com.sun.logging.*;
50
51 /**
52  * Client Side Handler to be invoked from the appclient
53  * A similiar copy sans appserver specific logging mechanism should be made available
54  * to standalone clients to use our
55  * WSS infrastructure.
56  * @author Harpreet Singh
57  * @version
58  * @since
59  */

60 public class MessageLayerClientHandler implements Handler JavaDoc {
61
62     private static Logger _logger=null;
63     static {
64         _logger = LogDomains.getLogger(LogDomains.SECURITY_LOGGER);
65     }
66
67     // key to ClientAuthConfig in HandlerInfo
68
public static final String JavaDoc CLIENT_AUTH_CONFIG =
69     "com.sun.enterprise.security.wss.ClientAuthConfig";
70
71     // key to ClientAuthContext in SOAPMessageCOntext
72
private static final String JavaDoc CLIENT_AUTH_CONTEXT =
73     "com.sun.enterprise.security.jauth.ClientAuthContext";
74
75     private static String JavaDoc errMsg =
76     "Client WSS Handler: MessageContext not of type SOAPMessageContext";
77
78     private ClientAuthConfig config_ = null;
79
80     private QName JavaDoc qname[] = null;
81
82     private boolean isAppclientContainer = false;
83
84     private QName JavaDoc serviceName = null;
85
86     /** Creates a new instance of MessageLayerClientHandler */
87     public MessageLayerClientHandler() {
88     int containerType = Switch.getSwitch().getContainerType();
89     if(containerType == Switch.APPCLIENT_CONTAINER){
90         isAppclientContainer = true;
91     } else{
92         isAppclientContainer = false;
93     }
94     }
95     
96     public boolean handleFault(MessageContext JavaDoc messageContext) {
97         // no need to do any special processing
98
if(_logger.isLoggable(Level.FINE)){
99             _logger.log(Level.FINE, "wss-auth-client: ClientHandler does not handle" +
100             " SOAP faults");
101         }
102         return true;
103     }
104     
105     public boolean handleRequest(MessageContext JavaDoc messageContext) {
106         boolean retValue;
107         if(!isSoapMessageContext(messageContext)){
108             // cannot process this, as this is not a soap message context
109
throw new JAXRPCException JavaDoc(errMsg);
110         }
111     if (config_ == null) {
112         return true;
113     }
114       
115         // get the ClientAuthContext
116
SOAPMessageContext JavaDoc smc = (SOAPMessageContext JavaDoc) messageContext;
117     SOAPMessage JavaDoc request = smc.getMessage();
118     ClientAuthContext cAC = config_.getAuthContext(null,request);
119     if (cAC == null) {
120         return true;
121     }
122
123     smc.setProperty(CLIENT_AUTH_CONTEXT, cAC);
124     smc.setProperty(javax.xml.ws.handler.MessageContext.WSDL_SERVICE,
125             serviceName);
126
127         try{
128         WebServiceSecurity.secureRequest(smc,cAC,isAppclientContainer);
129         } catch(Exception JavaDoc e){
130             if (_logger.isLoggable(Level.WARNING)){
131                 _logger.log(Level.WARNING, "ws.error_secure_request", e);
132             }
133             throw new JAXRPCException JavaDoc(e);
134         }
135         return true;
136     }
137     
138     public boolean handleResponse(MessageContext JavaDoc messageContext) {
139         boolean retValue;
140         if(!isSoapMessageContext(messageContext)){
141             // cannot process this, as this is not a soap message context
142
throw new JAXRPCException JavaDoc(errMsg);
143         }
144     if (config_ == null) {
145         return true;
146     }
147
148         // get the ClientAuthContext
149
SOAPMessageContext JavaDoc smc = (SOAPMessageContext JavaDoc) messageContext;
150     ClientAuthContext cAC =
151         (ClientAuthContext) smc.getProperty(CLIENT_AUTH_CONTEXT);
152     if (cAC == null) {
153         return true;
154     }
155
156         try{
157         retValue = WebServiceSecurity.validateResponse(smc,cAC);
158         }catch(Exception JavaDoc e){
159             if (_logger.isLoggable(Level.WARNING)){
160                 _logger.log(Level.WARNING, "ws.error_validate_response", e);
161             }
162             throw new JAXRPCException JavaDoc(e);
163         }
164         
165         return retValue;
166     }
167     
168     // if headers contains the list of understood headers, then we need to make
169
// sure that wsse is included; although if so, we need a way to make sure
170
// that the provider honors this commitment.
171
public javax.xml.namespace.QName JavaDoc[] getHeaders() {
172         return qname;
173     }
174     
175     public void destroy() {
176         qname = null;
177     }
178     
179     public void init(javax.xml.rpc.handler.HandlerInfo JavaDoc info) {
180         // 109 mandates saving qnames in init
181
qname = info.getHeaders();
182     config_ = (ClientAuthConfig) info.getHandlerConfig().get(CLIENT_AUTH_CONFIG);
183         serviceName = (QName JavaDoc)info.getHandlerConfig().get(javax.xml.ws.handler.MessageContext.WSDL_SERVICE);
184     }
185
186     /** 109 mandates that each MessageContext be checked to see if it is a
187      * a SOAPMessageContext and whether the handler processes it
188      */

189     private boolean isSoapMessageContext(MessageContext JavaDoc mc){
190         boolean retValue =
191         (mc instanceof SOAPMessageContext JavaDoc)? true: false;
192         if(!retValue && _logger.isLoggable(Level.WARNING)){
193             _logger.log(Level.WARNING, "ws.error_not_soap");
194         }
195         return retValue;
196     }
197
198 }
199
Popular Tags