KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejbca > ui > web > admin > rainterface > EditUserDataSourceJSPHelper


1 /*************************************************************************
2  * *
3  * EJBCA: The OpenSource Certificate Authority *
4  * *
5  * This software is free software; you can redistribute it and/or *
6  * modify it under the terms of the GNU Lesser General Public *
7  * License as published by the Free Software Foundation; either *
8  * version 2.1 of the License, or any later version. *
9  * *
10  * See terms of license at gnu.org. *
11  * *
12  *************************************************************************/

13
14 package org.ejbca.ui.web.admin.rainterface;
15
16 import java.io.UnsupportedEncodingException JavaDoc;
17 import java.util.ArrayList JavaDoc;
18 import java.util.Collection JavaDoc;
19 import java.util.HashSet JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.Set JavaDoc;
22 import java.util.TreeMap JavaDoc;
23
24 import javax.servlet.http.HttpServletRequest JavaDoc;
25
26 import org.ejbca.core.ejb.ra.userdatasource.IUserDataSourceSessionLocal;
27 import org.ejbca.core.model.authorization.AuthorizationDeniedException;
28 import org.ejbca.core.model.authorization.AvailableAccessRules;
29 import org.ejbca.core.model.log.Admin;
30 import org.ejbca.core.model.ra.userdatasource.BaseUserDataSource;
31 import org.ejbca.core.model.ra.userdatasource.CustomUserDataSourceContainer;
32 import org.ejbca.core.model.ra.userdatasource.UserDataSourceConnectionException;
33 import org.ejbca.core.model.ra.userdatasource.UserDataSourceExistsException;
34 import org.ejbca.core.model.ra.userdatasource.UserDataSourceVO;
35 import org.ejbca.ui.web.RequestHelper;
36 import org.ejbca.ui.web.admin.configuration.EjbcaWebBean;
37 import org.ejbca.util.dn.DNFieldExtractor;
38
39
40 /**
41  * Contains help methods used to parse a edit user data source jsp page requests.
42  *
43  * @author Philip Vendil
44  * @version $Id: EditUserDataSourceJSPHelper.java,v 1.2 2006/12/02 11:17:57 anatom Exp $
45  */

46 public class EditUserDataSourceJSPHelper implements java.io.Serializable JavaDoc {
47
48     /**
49      * Determines if a de-serialized file is compatible with this class.
50      *
51      * Maintainers must change this value if and only if the new version
52      * of this class is not compatible with old versions. See Sun docs
53      * for <a HREF=http://java.sun.com/products/jdk/1.1/docs/guide
54      * /serialization/spec/version.doc.html> details. </a>
55      *
56      */

57     private static final long serialVersionUID = 436830207093078432L;
58     
59     public static final String JavaDoc ACTION = "action";
60     public static final String JavaDoc ACTION_EDIT_USERDATASOURCES = "edituserdatasources";
61     public static final String JavaDoc ACTION_EDIT_USERDATASOURCE = "edituserdatasource";
62
63     public static final String JavaDoc ACTION_CHANGE_USERDATASOURCETYPE = "changeuserdatasourcetype";
64
65
66     public static final String JavaDoc CHECKBOX_VALUE = BaseUserDataSource.TRUE;
67
68 // Used in userdatasources.jsp
69
public static final String JavaDoc BUTTON_EDIT_USERDATASOURCE = "buttonedituserdatasource";
70     public static final String JavaDoc BUTTON_DELETE_USERDATASOURCE = "buttondeleteuserdatasource";
71     public static final String JavaDoc BUTTON_ADD_USERDATASOURCE = "buttonadduserdatasource";
72     public static final String JavaDoc BUTTON_RENAME_USERDATASOURCE = "buttonrenameuserdatasource";
73     public static final String JavaDoc BUTTON_CLONE_USERDATASOURCE = "buttoncloneuserdatasource";
74
75     public static final String JavaDoc SELECT_USERDATASOURCE = "selectuserdatasource";
76     public static final String JavaDoc TEXTFIELD_USERDATASOURCENAME = "textfielduserdatasourcename";
77     public static final String JavaDoc HIDDEN_USERDATASOURCENAME = "hiddenuserdatasourcename";
78
79 // Buttons used in userdatasource.jsp
80
public static final String JavaDoc BUTTON_TESTCONNECTION = "buttontestconnection";
81     public static final String JavaDoc BUTTON_SAVE = "buttonsave";
82     public static final String JavaDoc BUTTON_CANCEL = "buttoncancel";
83
84     public static final String JavaDoc TYPE_CUSTOM = "typecustom";
85
86     public static final String JavaDoc HIDDEN_USERDATASOURCETYPE = "hiddenuserdatasourcetype";
87     public static final String JavaDoc SELECT_USERDATASOURCETYPE = "selectuserdatasourcetype";
88
89     public static final String JavaDoc SELECT_APPLICABLECAS = "selectapplicablecas";
90     public static final String JavaDoc SELECT_MODIFYABLEFIELDS = "selectmodifyablefields";
91     public static final String JavaDoc TEXTAREA_DESCRIPTION = "textareadescription";
92
93     public static final String JavaDoc TEXTFIELD_CUSTOMCLASSPATH = "textfieldcustomclasspath";
94     public static final String JavaDoc TEXTAREA_CUSTOMPROPERTIES = "textareacustomproperties";
95
96     public static final String JavaDoc PAGE_USERDATASOURCE = "userdatasourcepage.jspf";
97     public static final String JavaDoc PAGE_USERDATASOURCES = "userdatasourcespage.jspf";
98
99     
100
101
102
103     /** Creates new LogInterfaceBean */
104     public EditUserDataSourceJSPHelper(){
105     }
106     // Public methods.
107
/**
108      * Method that initialized the bean.
109      *
110      * @param request is a reference to the http request.
111      */

112     public void initialize(HttpServletRequest JavaDoc request, EjbcaWebBean ejbcawebbean,
113             RAInterfaceBean rabean) throws Exception JavaDoc{
114
115         if(!initialized){
116             initialized = true;
117             userdatasourcesession = rabean.getUserDataSourceSession();
118             issuperadministrator = false;
119             admin = ejbcawebbean.getAdminObject();
120             this.ejbcawebbean = ejbcawebbean;
121             try{
122                 issuperadministrator = ejbcawebbean.isAuthorizedNoLog(AvailableAccessRules.ROLE_SUPERADMINISTRATOR);
123             }catch(AuthorizationDeniedException ade){}
124         }
125     }
126
127     public String JavaDoc parseRequest(HttpServletRequest JavaDoc request) throws AuthorizationDeniedException{
128         String JavaDoc includefile = PAGE_USERDATASOURCES;
129         String JavaDoc userdatasource = null;
130         String JavaDoc action = null;
131
132         try {
133             RequestHelper.setDefaultCharacterEncoding(request);
134         } catch (UnsupportedEncodingException JavaDoc e1) {
135             // itgnore
136
}
137         action = request.getParameter(ACTION);
138         if( action != null){
139             if( action.equals(ACTION_EDIT_USERDATASOURCES)){
140                 if( request.getParameter(BUTTON_EDIT_USERDATASOURCE) != null){
141                     userdatasource = request.getParameter(SELECT_USERDATASOURCE);
142                     if(userdatasource != null){
143                         if(!userdatasource.trim().equals("")){
144                             includefile=PAGE_USERDATASOURCE;
145                             this.userdatasourcename = userdatasource;
146                             this.userdatasourcedata = userdatasourcesession.getUserDataSource(admin,userdatasourcename);
147                         }
148                         else{
149                             userdatasource= null;
150                         }
151                     }
152                     if(userdatasource == null){
153                         includefile=PAGE_USERDATASOURCES;
154                     }
155                 }
156                 if( request.getParameter(BUTTON_DELETE_USERDATASOURCE) != null) {
157                     userdatasource = request.getParameter(SELECT_USERDATASOURCE);
158                     if(userdatasource != null){
159                         if(!userdatasource.trim().equals("")){
160                             userdatasourcedeletefailed = !userdatasourcesession.removeUserDataSource(admin,userdatasource);
161                         }
162                     }
163                     includefile=PAGE_USERDATASOURCES;
164                 }
165                 if( request.getParameter(BUTTON_RENAME_USERDATASOURCE) != null){
166                     // Rename selected userdatasource and display profilespage.
167
String JavaDoc newuserdatasourcename = request.getParameter(TEXTFIELD_USERDATASOURCENAME);
168                     String JavaDoc olduserdatasourcename = request.getParameter(SELECT_USERDATASOURCE);
169                     if(olduserdatasourcename != null && newuserdatasourcename != null){
170                         if(!newuserdatasourcename.trim().equals("") && !olduserdatasourcename.trim().equals("")){
171                             try{
172                                 userdatasourcesession.renameUserDataSource(admin,olduserdatasourcename.trim(),newuserdatasourcename.trim());
173                             }catch( UserDataSourceExistsException e){
174                                 userdatasourceexists=true;
175                             }
176                         }
177                     }
178                     includefile=PAGE_USERDATASOURCES;
179                 }
180                 if( request.getParameter(BUTTON_ADD_USERDATASOURCE) != null){
181                     userdatasource = request.getParameter(TEXTFIELD_USERDATASOURCENAME);
182                     if(userdatasource != null){
183                         if(!userdatasource.trim().equals("")){
184                             try{
185                                 userdatasourcesession.addUserDataSource(admin,userdatasource.trim(), new CustomUserDataSourceContainer());
186                             }catch( UserDataSourceExistsException e){
187                                 userdatasourceexists=true;
188                             }
189                         }
190                     }
191                     includefile=PAGE_USERDATASOURCES;
192                 }
193                 if( request.getParameter(BUTTON_CLONE_USERDATASOURCE) != null){
194                     String JavaDoc newuserdatasourcename = request.getParameter(TEXTFIELD_USERDATASOURCENAME);
195                     String JavaDoc olduserdatasourcename = request.getParameter(SELECT_USERDATASOURCE);
196                     if(olduserdatasourcename != null && newuserdatasourcename != null){
197                         if(!newuserdatasourcename.trim().equals("") && !olduserdatasourcename.trim().equals("")){
198                             try{
199                                 userdatasourcesession.cloneUserDataSource(admin,olduserdatasourcename.trim(),newuserdatasourcename.trim());
200                             }catch( UserDataSourceExistsException e){
201                                 userdatasourceexists=true;
202                             }
203                         }
204                     }
205                     includefile=PAGE_USERDATASOURCES;
206                 }
207             }
208             if( action.equals(ACTION_EDIT_USERDATASOURCE)){
209                 // Display edit access rules page.
210
userdatasource = request.getParameter(HIDDEN_USERDATASOURCENAME);
211                 this.userdatasourcename = userdatasource;
212                 if(userdatasource != null){
213                     if(!userdatasource.trim().equals("")){
214                         if(request.getParameter(BUTTON_SAVE) != null ||
215                                 request.getParameter(BUTTON_TESTCONNECTION) != null){
216
217                             if(userdatasourcedata == null){
218                                 int tokentype = new Integer JavaDoc(request.getParameter(HIDDEN_USERDATASOURCETYPE)).intValue();
219                                 if(tokentype == CustomUserDataSourceContainer.TYPE_CUSTOMUSERDATASOURCECONTAINER)
220                                     userdatasourcedata = new CustomUserDataSourceContainer();
221
222                             }
223                             // Save changes.
224

225                             // General settings
226
String JavaDoc value = request.getParameter(TEXTAREA_DESCRIPTION);
227                             if(value != null){
228                                 value = value.trim();
229                                 userdatasourcedata.setDescription(value);
230                             }
231                             
232                             String JavaDoc[] values = request.getParameterValues(SELECT_MODIFYABLEFIELDS);
233                             if(values != null){
234                                 Set JavaDoc modifyablefields = new HashSet JavaDoc();
235                                 for(int i=0;i< values.length;i++){
236                                     modifyablefields.add(new Integer JavaDoc(values[i]));
237                                 }
238
239                                 userdatasourcedata.setModifiableFields(modifyablefields);
240                             }else{
241                                 userdatasourcedata.setModifiableFields(new HashSet JavaDoc());
242                             }
243
244                             values = request.getParameterValues(SELECT_APPLICABLECAS);
245                             if(values != null){
246                                 ArrayList JavaDoc useCAs = new ArrayList JavaDoc();
247                                 for(int i=0;i< values.length;i++){
248                                     Integer JavaDoc caid = new Integer JavaDoc(values[i]);
249                                     if(caid.intValue() == BaseUserDataSource.ANYCA){
250                                         useCAs = new ArrayList JavaDoc();
251                                         useCAs.add(caid);
252                                         break;
253                                     }
254                                     useCAs.add(new Integer JavaDoc(values[i]));
255                                 }
256
257                                 userdatasourcedata.setApplicableCAs(useCAs);
258                             }else{
259                                 userdatasourcedata.setApplicableCAs(new ArrayList JavaDoc());
260                             }
261                             
262                            
263
264
265                             if(userdatasourcedata instanceof CustomUserDataSourceContainer){
266                                 value = request.getParameter(TEXTFIELD_CUSTOMCLASSPATH);
267                                 if(value != null){
268                                     value = value.trim();
269                                     ((CustomUserDataSourceContainer) userdatasourcedata).setClassPath(value);
270                                 }
271                                 value = request.getParameter(TEXTAREA_CUSTOMPROPERTIES);
272                                 if(value != null){
273                                     value = value.trim();
274                                     ((CustomUserDataSourceContainer) userdatasourcedata).setPropertyData(value);
275                                 }
276                             }
277
278                             if(request.getParameter(BUTTON_SAVE) != null){
279                                 userdatasourcesession.changeUserDataSource(admin,userdatasource,userdatasourcedata);
280                                 includefile=PAGE_USERDATASOURCES;
281                             }
282                             if(request.getParameter(BUTTON_TESTCONNECTION)!= null){
283                                 connectionmessage = true;
284                                 userdatasourcesession.changeUserDataSource(admin, userdatasource,userdatasourcedata);
285                                 try{
286                                     int userdatasourceid = userdatasourcesession.getUserDataSourceId(admin,userdatasource);
287                                     userdatasourcesession.testConnection(admin,userdatasourceid);
288                                     connectionsuccessful = true;
289                                 }catch(UserDataSourceConnectionException pce){
290                                     connectionerrormessage = pce.getMessage();
291                                 }
292                                 includefile=PAGE_USERDATASOURCE;
293                             }
294
295                         }
296                         if(request.getParameter(BUTTON_CANCEL) != null){
297                             // Don't save changes.
298
includefile=PAGE_USERDATASOURCES;
299                         }
300
301                     }
302                 }
303             }
304
305             if( action.equals(ACTION_CHANGE_USERDATASOURCETYPE)){
306                 this.userdatasourcename = request.getParameter(HIDDEN_USERDATASOURCENAME);
307                 String JavaDoc value = request.getParameter(SELECT_USERDATASOURCETYPE);
308                 if(value!=null){
309                     int profiletype = Integer.parseInt(value);
310                     switch(profiletype){
311                     case CustomUserDataSourceContainer.TYPE_CUSTOMUSERDATASOURCECONTAINER :
312                         userdatasourcedata = new CustomUserDataSourceContainer();
313                         break;
314                     }
315                 }
316
317                 includefile=PAGE_USERDATASOURCE;
318             }
319         }
320
321         return includefile;
322     }
323
324     public int getUserDataSourceType(){
325         int retval = CustomUserDataSourceContainer.TYPE_CUSTOMUSERDATASOURCECONTAINER;
326
327         if(userdatasourcedata instanceof CustomUserDataSourceContainer)
328             retval = CustomUserDataSourceContainer.TYPE_CUSTOMUSERDATASOURCECONTAINER;
329
330
331         return retval;
332     }
333
334     
335     public TreeMap JavaDoc getAuthorizedUserDataSourceNames(){
336         TreeMap JavaDoc retval = new TreeMap JavaDoc();
337         
338         Collection JavaDoc authorizedsources = userdatasourcesession.getAuthorizedUserDataSourceIds(admin,false);
339         Iterator JavaDoc iter = authorizedsources.iterator();
340         while(iter.hasNext()){
341             Integer JavaDoc id = (Integer JavaDoc) iter.next();
342             retval.put(userdatasourcesession.getUserDataSourceName(admin,id.intValue()),id);
343         }
344         
345         
346         return retval;
347     }
348     
349     public TreeMap JavaDoc getModifyableFieldTexts(){
350         if(modifyableFieldTexts ==null){
351             modifyableFieldTexts = new TreeMap JavaDoc();
352             
353             String JavaDoc subjectdntext = ejbcawebbean.getText("SUBJECTDN");
354             String JavaDoc subjectaltnametext = ejbcawebbean.getText("SUBALTNAME");
355             String JavaDoc subjectdirattrtext = ejbcawebbean.getText("SUBDIRATTR");
356             
357             modifyableFieldTexts.put(subjectdntext + " : " + ejbcawebbean.getText("UID"),new Integer JavaDoc(DNFieldExtractor.UID));
358             modifyableFieldTexts.put(subjectdntext + " : " + ejbcawebbean.getText("COMMONNAME"),new Integer JavaDoc(DNFieldExtractor.CN));
359             modifyableFieldTexts.put(subjectdntext + " : " + ejbcawebbean.getText("DNSERIALNUMBER"), new Integer JavaDoc(DNFieldExtractor.SN));
360             modifyableFieldTexts.put(subjectdntext + " : " + ejbcawebbean.getText("GIVENNAME1"),new Integer JavaDoc(DNFieldExtractor.GIVENNAME));
361             modifyableFieldTexts.put(subjectdntext + " : " + ejbcawebbean.getText("INITIALS"), new Integer JavaDoc(DNFieldExtractor.INITIALS));
362             modifyableFieldTexts.put(subjectdntext + " : " + ejbcawebbean.getText("SURNAME"), new Integer JavaDoc(DNFieldExtractor.SURNAME));
363             modifyableFieldTexts.put(subjectdntext + " : " + ejbcawebbean.getText("TITLE"), new Integer JavaDoc(DNFieldExtractor.T));
364             modifyableFieldTexts.put(subjectdntext + " : " + ejbcawebbean.getText("ORGANIZATIONUNIT"), new Integer JavaDoc(DNFieldExtractor.OU));
365             modifyableFieldTexts.put(subjectdntext + " : " + ejbcawebbean.getText("ORGANIZATION"), new Integer JavaDoc(DNFieldExtractor.O));
366             modifyableFieldTexts.put(subjectdntext + " : " + ejbcawebbean.getText("LOCALE"), new Integer JavaDoc(DNFieldExtractor.L));
367             modifyableFieldTexts.put(subjectdntext + " : " + ejbcawebbean.getText("STATE"), new Integer JavaDoc(DNFieldExtractor.ST));
368             modifyableFieldTexts.put(subjectdntext + " : " + ejbcawebbean.getText("DOMAINCOMPONENT"), new Integer JavaDoc(DNFieldExtractor.DC));
369             modifyableFieldTexts.put(subjectdntext + " : " + ejbcawebbean.getText("COUNTRY"), new Integer JavaDoc(DNFieldExtractor.C));
370             modifyableFieldTexts.put(subjectdntext + " : " + ejbcawebbean.getText("UNSTRUCTUREDADDRESS"), new Integer JavaDoc(DNFieldExtractor.UNSTRUCTUREDADDRESS));
371             modifyableFieldTexts.put(subjectdntext + " : " + ejbcawebbean.getText("UNSTRUCTUREDNAME"), new Integer JavaDoc(DNFieldExtractor.UNSTRUCTUREDNAME));
372                         
373             modifyableFieldTexts.put(subjectaltnametext + " : " + ejbcawebbean.getText("DNSNAME"), new Integer JavaDoc(DNFieldExtractor.DNSNAME));
374             modifyableFieldTexts.put(subjectaltnametext + " : " + ejbcawebbean.getText("IPADDRESS"), new Integer JavaDoc(DNFieldExtractor.IPADDRESS));
375             modifyableFieldTexts.put(subjectaltnametext + " : " + ejbcawebbean.getText("DIRECTORYNAME"), new Integer JavaDoc(DNFieldExtractor.DIRECTORYNAME));
376             modifyableFieldTexts.put(subjectaltnametext + " : " + ejbcawebbean.getText("UNIFORMRESOURCEID"), new Integer JavaDoc(DNFieldExtractor.URI));
377             modifyableFieldTexts.put(subjectaltnametext + " : " + ejbcawebbean.getText("UPN"), new Integer JavaDoc(DNFieldExtractor.UPN));
378             modifyableFieldTexts.put(subjectaltnametext + " : " + ejbcawebbean.getText("GUID"), new Integer JavaDoc(DNFieldExtractor.GUID));
379             
380             modifyableFieldTexts.put(subjectdirattrtext + " : " + ejbcawebbean.getText("DATEOFBIRTH"), new Integer JavaDoc(DNFieldExtractor.DATEOFBIRTH));
381             modifyableFieldTexts.put(subjectdirattrtext + " : " + ejbcawebbean.getText("PLACEOFBIRTH"),new Integer JavaDoc( DNFieldExtractor.PLACEOFBIRTH));
382             modifyableFieldTexts.put(subjectdirattrtext + " : " + ejbcawebbean.getText("GENDER"),new Integer JavaDoc( DNFieldExtractor.GENDER));
383             modifyableFieldTexts.put(subjectdirattrtext + " : " + ejbcawebbean.getText("COUNTRYOFCITIZENSHIP"),new Integer JavaDoc( DNFieldExtractor.COUNTRYOFCITIZENSHIP));
384             modifyableFieldTexts.put(subjectdirattrtext + " : " + ejbcawebbean.getText("COUNTRYOFRESIDENCE"),new Integer JavaDoc( DNFieldExtractor.COUNTRYOFRESIDENCE));
385
386
387             modifyableFieldTexts.put(ejbcawebbean.getText("USERNAME"), new Integer JavaDoc(UserDataSourceVO.ISMODIFYABLE_USERNAME));
388             modifyableFieldTexts.put(ejbcawebbean.getText("PASSWORD"), new Integer JavaDoc(UserDataSourceVO.ISMODIFYABLE_PASSWORD));
389             modifyableFieldTexts.put(ejbcawebbean.getText("CA"), new Integer JavaDoc(UserDataSourceVO.ISMODIFYABLE_CAID));
390             modifyableFieldTexts.put(ejbcawebbean.getText("EMAIL"), new Integer JavaDoc(UserDataSourceVO.ISMODIFYABLE_EMAILDATA));
391             modifyableFieldTexts.put(ejbcawebbean.getText("PASSWORD"), new Integer JavaDoc(UserDataSourceVO.ISMODIFYABLE_TYPE));
392             modifyableFieldTexts.put(ejbcawebbean.getText("ENDENTITYPROFILE"), new Integer JavaDoc(UserDataSourceVO.ISMODIFYABLE_ENDENTITYPROFILE));
393             modifyableFieldTexts.put(ejbcawebbean.getText("CERTIFICATEPROFILE"), new Integer JavaDoc(UserDataSourceVO.ISMODIFYABLE_CERTIFICATEPROFILE));
394             modifyableFieldTexts.put(ejbcawebbean.getText("TOKEN"), new Integer JavaDoc(UserDataSourceVO.ISMODIFYABLE_TOKENTYPE));
395             modifyableFieldTexts.put(ejbcawebbean.getText("HARDTOKENISSUER"), new Integer JavaDoc(UserDataSourceVO.ISMODIFYABLE_HARDTOKENISSUER));
396                     
397             
398         }
399         return modifyableFieldTexts;
400     }
401
402     private boolean initialized=false;
403     public boolean userdatasourceexists = false;
404     public boolean userdatasourcedeletefailed = false;
405     public boolean connectionmessage = false;
406     public boolean connectionsuccessful = false;
407     public String JavaDoc connectionerrormessage = "";
408     public boolean issuperadministrator = false;
409     public BaseUserDataSource userdatasourcedata = null;
410     public String JavaDoc userdatasourcename = null;
411     private TreeMap JavaDoc modifyableFieldTexts = null;
412     private IUserDataSourceSessionLocal userdatasourcesession = null;
413     private Admin admin = null;
414     private EjbcaWebBean ejbcawebbean = null;
415
416
417 }
418
Popular Tags