KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > admingui > handlers > MsgSecurityConfigHandler


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  * MsgSecurityConfigHandler.java
26  *
27  * Created on June 6, 2004, 8:08 AM
28  */

29
30 package com.sun.enterprise.tools.admingui.handlers;
31
32
33 import java.util.Map JavaDoc;
34
35 import com.sun.enterprise.tools.guiframework.exception.FrameworkException;
36 import com.sun.enterprise.tools.guiframework.view.HandlerContext;
37 import com.sun.enterprise.tools.guiframework.view.DescriptorContainerView;
38 import com.sun.enterprise.tools.guiframework.view.descriptors.ViewDescriptor;
39 import com.iplanet.jato.view.html.SelectableGroup;
40 import com.iplanet.jato.view.html.OptionList;
41 import com.iplanet.jato.RequestContext;
42
43 import javax.management.Attribute JavaDoc;
44 import javax.management.AttributeList JavaDoc;
45 import com.sun.enterprise.tools.admingui.util.MBeanUtil;
46 import com.sun.enterprise.tools.admingui.util.AMXUtil;
47 import com.sun.enterprise.tools.admingui.util.Util;
48
49 import com.sun.appserv.management.config.ConfigConfig;
50 import com.sun.appserv.management.config.SecurityServiceConfig;
51
52 /**
53  *
54  * @author anilam
55  */

56 public class MsgSecurityConfigHandler {
57     
58     /**
59      * This method creates the message-security-config element together with the provider-config
60      * which is required.
61      *
62      * @param reqCtx The RequestContext
63      * @param handlerCtx The HandlerContext
64      */

65     public void createMessageSecurityProvider(RequestContext reqCtx, HandlerContext handlerCtx) {
66         String JavaDoc objectName = (String JavaDoc)handlerCtx.getInputValue("objectName");
67         String JavaDoc configName = (String JavaDoc)handlerCtx.getInputValue("configName");
68         String JavaDoc msgSecurityName = (String JavaDoc)handlerCtx.getInputValue("msgSecurityName");
69         String JavaDoc isDefaultProvider = (String JavaDoc)handlerCtx.getInputValue("isDefaultProvider");
70         Object JavaDoc providerProperties = handlerCtx.getInputValue("providerProperties");
71         AttributeList JavaDoc providerAttr = (AttributeList JavaDoc)handlerCtx.getInputValue("providerAttr");
72     AttributeList JavaDoc requestAttr = (AttributeList JavaDoc)handlerCtx.getInputValue("requestAttr");
73     AttributeList JavaDoc responseAttr = (AttributeList JavaDoc)handlerCtx.getInputValue("responseAttr");
74     
75         if (objectName == null) {
76         throw new IllegalArgumentException JavaDoc(
77         "The parameter map did not contain objectName");
78     }
79     try {
80             String JavaDoc providerId = getAttrValue( providerAttr, "provider-id");
81             String JavaDoc providerType = getAttrValue( providerAttr, "provider-type");
82             String JavaDoc className = getAttrValue( providerAttr, "class-name");
83             
84             String JavaDoc requestAuthSource = getAttrValue( requestAttr, "auth-source");
85             String JavaDoc requestRecipient = getAttrValue( requestAttr, "auth-recipient");
86             
87             String JavaDoc responseAuthSource = getAttrValue( responseAttr, "auth-source");
88             String JavaDoc responseRecipient = getAttrValue( responseAttr, "auth-recipient");
89             
90             if(Util.isEmpty(requestAuthSource)) requestAuthSource = null;
91             if(Util.isEmpty(requestRecipient)) requestRecipient = null;
92             if(Util.isEmpty(responseAuthSource)) responseAuthSource = null;
93             if(Util.isEmpty(responseRecipient)) responseRecipient = null;
94             
95             Object JavaDoc[] params = new Object JavaDoc[] {
96                 msgSecurityName, // auth-layer attribute of message-security-config
97
providerId, // provider id
98
providerType, // "client"/"server"/"client-server"
99
className, // The class-name of java class of the provider.
100
requestAuthSource, //requestAuthSource
101
requestRecipient, //requestAuthRecipient
102
responseAuthSource, //responseAuthSource
103
responseRecipient, //responseAuthRecipient
104
new Boolean JavaDoc(isDefaultProvider), // set pro
105
providerProperties, //provider's properties
106
configName //requestAuthSource
107
};
108             
109             String JavaDoc[] types = new String JavaDoc[] {
110             "java.lang.String",
111             "java.lang.String",
112             "java.lang.String",
113             "java.lang.String",
114             "java.lang.String",
115             "java.lang.String",
116             "java.lang.String",
117             "java.lang.String",
118             "boolean",
119             "java.util.Properties",
120             "java.lang.String"
121             };
122         
123             /*
124             System.out.println("========= MsgSecurityConfigHandler ======");
125             System.out.println("objectName=" + objectName);
126             for(int i=0; i<params.length; i++){
127                 System.out.println( i + " : " + params[i]);
128             }
129              */

130             MBeanUtil.invoke(
131         objectName,
132         "createMessageSecurityProvider",
133         params,
134         types);
135     } catch (Exception JavaDoc ex) {
136         throw new FrameworkException(
137         "Error while calling createMessageSecurityProvider() on MBean '"+objectName+
138         "'!", ex, handlerCtx.getViewDescriptor(),
139         handlerCtx.getView());
140     }
141     }
142     
143     
144     private String JavaDoc getAttrValue(AttributeList JavaDoc attrList, String JavaDoc attrName){
145         if (attrList == null)
146             return null;
147         for (int i=0; i < attrList.size(); i++) {
148             Attribute JavaDoc attr = (Attribute JavaDoc) attrList.get(i);
149                 String JavaDoc name = attr.getName();
150                 if(attrName.equals(name))
151                     return attr.getValue().toString();
152         }
153         return null;
154     }
155
156     
157     /**
158      * This method modifies the request-policy and response-policy.
159      *
160      * @param reqCtx The RequestContext
161      * @param handlerCtx The HandlerContext
162      */

163     public void saveRequestAndResponsePolicy(RequestContext reqCtx, HandlerContext handlerCtx) {
164         String JavaDoc objectName = (String JavaDoc)handlerCtx.getInputValue("objectName");
165         AttributeList JavaDoc requestAttr = (AttributeList JavaDoc)handlerCtx.getInputValue("requestAttr");
166         AttributeList JavaDoc responseAttr = (AttributeList JavaDoc)handlerCtx.getInputValue("responseAttr");
167         boolean requestExist = ((Boolean JavaDoc)handlerCtx.getInputValue("requestExist")).booleanValue();
168         boolean responseExist = ((Boolean JavaDoc)handlerCtx.getInputValue("responseExist")).booleanValue();
169         
170     if (objectName == null) {
171         throw new IllegalArgumentException JavaDoc(
172         "The parameter map did not contain objectName");
173     }
174         try {
175             savePolicy(objectName, responseAttr, responseExist, "removeResponsePolicy", "createResponsePolicy");
176             savePolicy(objectName, requestAttr, requestExist, "removeRequestPolicy", "createRequestPolicy");
177         }catch (Exception JavaDoc ex) {
178             throw new FrameworkException(
179                 "Error while calling saveRequestAndResponsePolicy() on MBean '"+objectName+
180                 "'!", ex, handlerCtx.getViewDescriptor(),
181                 handlerCtx.getView());
182         }
183     }
184     
185      public Boolean JavaDoc testNewButtonAllowed(RequestContext reqCtx, HandlerContext handlerCtx) {
186         Object JavaDoc value = handlerCtx.getInputValue("value");
187         if (value == null)
188             return TRUE;
189         if (value instanceof Object JavaDoc[]){
190             if (((Object JavaDoc[])value).length < 2)
191                 return TRUE;
192         }
193         // WE are just testing for the # of msg security config, assume that no more than 1 will have the same layer (SOAP or HttpServlet)
194
//Can add more test here if we want.
195
return FALSE;
196      }
197         
198      
199      public void popularMsgLayerAllowed(RequestContext reqCtx, HandlerContext handlerCtx) {
200          // the child should be something like: com.sun.web.ui.view.html.CCDropDownMenu
201
SelectableGroup dropDownChild = (SelectableGroup) handlerCtx.getView();
202         // get the parent container of the dropdown
203
DescriptorContainerView parent = (DescriptorContainerView) dropDownChild.getParent();
204         
205         // need the child descriptor to get a parameter, retrieve from the parent descriptor.
206
ViewDescriptor vd = parent.getViewDescriptor();
207         ViewDescriptor cvd = vd.getChildDescriptor(dropDownChild.getName());
208         
209         String JavaDoc configName = (String JavaDoc) handlerCtx.getInputValue("configName");
210         Map JavaDoc cmap = (Map JavaDoc) AMXUtil.getDomainConfig().getConfigConfigMap();
211         SecurityServiceConfig ssc = ((ConfigConfig) cmap.get(configName)).getSecurityServiceConfig();
212         Map JavaDoc msgSCMap = ssc.getMessageSecurityConfigMap();
213         boolean hasSOAP = false;
214         boolean hasHttpServlet = false;
215         int count = 2;
216         
217         if (msgSCMap != null ){
218             if (msgSCMap.get("SOAP") != null){
219                 hasSOAP=true;
220                 count--;
221             }
222             if (msgSCMap.get("HttpServlet") != null){
223                 hasHttpServlet=true;
224                 count--;
225             }
226         }
227         String JavaDoc[] options = null;
228         
229         if (count == 0) //should never happen
230
return;
231         if (count == 2){
232             options = new String JavaDoc[] {"SOAP", "HttpServlet"};
233         }else
234         if (count == 1){
235             if (hasSOAP)
236                 options = new String JavaDoc[] { "HttpServlet"};
237             else
238                 options = new String JavaDoc[] {"SOAP"};
239
240         }
241         OptionList optionList = new OptionList(options, options);
242         dropDownChild.setOptions(optionList);
243      }
244      
245     
246     private void savePolicy(String JavaDoc objectName, AttributeList JavaDoc attrs, boolean exist, String JavaDoc removeMethod, String JavaDoc createMethod)
247     {
248         boolean hasValue = attrHasValue(attrs);
249         if (hasValue){
250             if (exist){
251                 MBeanUtil.invoke(objectName, removeMethod, null, null);
252             }
253             Object JavaDoc[] params = new Object JavaDoc[] {attrs};
254             String JavaDoc[] types = new String JavaDoc[] {"javax.management.AttributeList" };
255             MBeanUtil.invoke( objectName, createMethod, params, types);
256             return;
257         }else{
258             if(exist){
259                MBeanUtil.invoke(objectName, removeMethod, null, null);
260             }
261         }
262     }
263     
264     private boolean attrHasValue(AttributeList JavaDoc attrList){
265         for (int i=0; i < attrList.size(); i++) {
266             Attribute JavaDoc attr = (Attribute JavaDoc) attrList.get(i);
267             if (hasValue(attr.getValue().toString()))
268                 return true;
269         }
270         return false;
271     }
272     
273     private boolean hasValue(String JavaDoc val){
274         return (val != null) && !"".equals(val);
275     }
276     
277      /**
278      * FALSE
279      */

280     public static final Boolean JavaDoc FALSE = new Boolean JavaDoc(false);
281
282     /**
283      * TRUE
284      */

285     public static final Boolean JavaDoc TRUE = new Boolean JavaDoc(true);
286 }
287
Popular Tags