KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > security > authwizard > actions > AthenticationSchemeFinishAction


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sslexplorer.security.authwizard.actions;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.Collections JavaDoc;
24 import java.util.Comparator JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
27
28 import javax.servlet.http.HttpServletRequest JavaDoc;
29 import javax.servlet.http.HttpServletResponse JavaDoc;
30
31 import org.apache.commons.logging.Log;
32 import org.apache.commons.logging.LogFactory;
33 import org.apache.struts.action.ActionForm;
34 import org.apache.struts.action.ActionForward;
35 import org.apache.struts.action.ActionMapping;
36
37 import com.sslexplorer.boot.PropertyList;
38 import com.sslexplorer.core.CoreEvent;
39 import com.sslexplorer.core.CoreEventConstants;
40 import com.sslexplorer.core.CoreServlet;
41 import com.sslexplorer.policyframework.PolicyConstants;
42 import com.sslexplorer.policyframework.PolicyDatabaseFactory;
43 import com.sslexplorer.policyframework.ResourceChangeEvent;
44 import com.sslexplorer.policyframework.ResourceType;
45 import com.sslexplorer.security.AuthenticationScheme;
46 import com.sslexplorer.security.AuthenticationSchemeResourceType;
47 import com.sslexplorer.security.SessionInfo;
48 import com.sslexplorer.security.SystemDatabaseFactory;
49 import com.sslexplorer.security.User;
50 import com.sslexplorer.security.authwizard.forms.AthenticationSchemeDetailsForm;
51 import com.sslexplorer.security.authwizard.forms.AthenticationSchemeSelectionForm;
52 import com.sslexplorer.wizard.AbstractWizardSequence;
53 import com.sslexplorer.wizard.WizardActionStatus;
54 import com.sslexplorer.wizard.actions.AbstractWizardAction;
55 import com.sslexplorer.wizard.forms.AbstractWizardFinishForm;
56
57 /**
58  * Implementation of a {@link AbstractWizardAction}. performs the finish action
59  * for the creation of an authentication scheme.
60  * @author James D Robinson <a HREF="mailto:james@3sp.com">&lt;james@3sp.com&gt;</a>
61  */

62 public class AthenticationSchemeFinishAction extends AbstractWizardAction {
63
64     private static final Log log = LogFactory.getLog(AthenticationSchemeFinishAction.class);
65
66     /*
67      * (non-Javadoc)
68      * @see com.sslexplorer.core.actions.CoreAction#getNavigationContext(org.apache.struts.action.ActionMapping,
69      * org.apache.struts.action.ActionForm,
70      * javax.servlet.http.HttpServletRequest,
71      * javax.servlet.http.HttpServletResponse)
72      */

73     public int getNavigationContext(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) {
74         return SessionInfo.USER_CONSOLE_CONTEXT | SessionInfo.MANAGEMENT_CONSOLE_CONTEXT;
75     }
76
77     /*
78      * (non-Javadoc)
79      * @see org.apache.struts.actions.DispatchAction#unspecified(org.apache.struts.action.ActionMapping,
80      * org.apache.struts.action.ActionForm,
81      * javax.servlet.http.HttpServletRequest,
82      * javax.servlet.http.HttpServletResponse)
83      */

84     public ActionForward unspecified(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws Exception JavaDoc {
85         List JavaDoc<WizardActionStatus> actionStatus = new ArrayList JavaDoc<WizardActionStatus>();
86         AbstractWizardSequence seq = getWizardSequence(request);
87         String JavaDoc name = (String JavaDoc) seq.getAttribute(AthenticationSchemeDetailsForm.ATTR_RESOURCE_NAME, null);
88         String JavaDoc description = (String JavaDoc) seq.getAttribute(AthenticationSchemeDetailsForm.ATTR_RESOURCE_DESCRIPTION, null);
89         PropertyList selectedModules = ((PropertyList) seq.getAttribute(AthenticationSchemeSelectionForm.ATTR_SELECTED_MODULES, new PropertyList()));
90         AuthenticationScheme defaultAuthenticationScheme = null;
91         try {
92             try {
93                 int priority = getAuthenticationSchemePriority(request);
94                 defaultAuthenticationScheme = SystemDatabaseFactory.getInstance().createAuthenticationSchemeSequence(
95                                 getSessionInfo(request).getUser().getRealm().getRealmID(), name, description,
96                                 selectedModules.asArray(), true, priority);
97                 CoreEvent evt = new ResourceChangeEvent(this, CoreEventConstants.CREATE_AUTHENTICATION_SCHEME, defaultAuthenticationScheme, getSessionInfo(request), CoreEvent.STATE_SUCCESSFUL);
98                 int authCounter = 1;
99                 for (Iterator JavaDoc i = selectedModules.iterator(); i.hasNext();) {
100                     AuthenticationSchemeResourceType.addAuthenticationModule(evt, (String JavaDoc) i.next(), authCounter);
101                     authCounter++;
102                 }
103                 CoreServlet.getServlet().fireCoreEvent(evt);
104             } catch (Exception JavaDoc e) {
105                 CoreServlet.getServlet().fireCoreEvent(
106                     new ResourceChangeEvent(this, CoreEventConstants.CREATE_AUTHENTICATION_SCHEME, getSessionInfo(request), e));
107                 throw e;
108             }
109             actionStatus.add(new WizardActionStatus(WizardActionStatus.COMPLETED_OK,
110                             "authwizard.athenticationSchemeFinish.status.authenticationSchemeCreated"));
111         } catch (Exception JavaDoc e) {
112             log.error("Failed to create profile.", e);
113             actionStatus.add(new WizardActionStatus(WizardActionStatus.COMPLETED_WITH_ERRORS,
114                             "authwizard.athenticationSchemeFinish.status.failedToCreateAuthenticationScheme", e.getMessage()));
115         }
116         if (defaultAuthenticationScheme != null) {
117             actionStatus.add(attachToPoliciesAndAddToFavorites("authwizard.athenticationSchemeFinish", seq, defaultAuthenticationScheme, false, request));
118         }
119         ((AbstractWizardFinishForm) form).setActionStatus(actionStatus);
120         return super.unspecified(mapping, form, request, response);
121     }
122     
123     private int getAuthenticationSchemePriority(HttpServletRequest JavaDoc request) throws Exception JavaDoc {
124         User user = getSessionInfo(request).getUser();
125         ResourceType resourceType = PolicyConstants.AUTHENTICATION_SCHEMES_RESOURCE_TYPE;
126         List JavaDoc<Integer JavaDoc> granted = PolicyDatabaseFactory.getInstance().getGrantedResourcesOfType(user, resourceType);
127
128         List JavaDoc<AuthenticationScheme> schemes = new ArrayList JavaDoc<AuthenticationScheme>();
129         for (Integer JavaDoc resourceId : granted) {
130             AuthenticationScheme scheme = (AuthenticationScheme) resourceType.getResourceById(resourceId.intValue());
131             schemes.add(scheme);
132         }
133         Collections.sort(schemes, new Comparator JavaDoc<AuthenticationScheme>() {
134             public int compare(AuthenticationScheme o1, AuthenticationScheme o2) {
135                 return Math.abs(o1.getPriorityInt()) - Math.abs(o2.getPriorityInt());
136             }
137         });
138         AuthenticationScheme authenticationScheme = schemes.get(schemes.size() - 1);
139         return authenticationScheme.getPriorityInt() + 1;
140     }
141
142     /**
143      * @param mapping
144      * @param form
145      * @param request
146      * @param response
147      * @return Action forward.
148      * @throws Exception
149      */

150     public ActionForward exit(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
151                     throws Exception JavaDoc {
152         return cancel(mapping, form, request, response);
153     }
154 }
155
Popular Tags