KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejbca > ui > web > admin > configuration > EjbcaJSFHelper


1 package org.ejbca.ui.web.admin.configuration;
2
3 import java.security.cert.X509Certificate JavaDoc;
4 import java.util.Map JavaDoc;
5
6 import javax.ejb.CreateException JavaDoc;
7 import javax.ejb.EJBException JavaDoc;
8 import javax.faces.application.Application;
9 import javax.faces.context.FacesContext;
10 import javax.faces.el.ValueBinding;
11 import javax.servlet.http.HttpServletRequest JavaDoc;
12 import javax.servlet.http.HttpSession JavaDoc;
13
14 import org.apache.log4j.Logger;
15 import org.ejbca.core.ejb.ServiceLocator;
16 import org.ejbca.core.ejb.approval.IApprovalSessionLocal;
17 import org.ejbca.core.ejb.approval.IApprovalSessionLocalHome;
18 import org.ejbca.core.ejb.ca.caadmin.ICAAdminSessionLocal;
19 import org.ejbca.core.ejb.ca.caadmin.ICAAdminSessionLocalHome;
20 import org.ejbca.core.ejb.ra.raadmin.IRaAdminSessionLocal;
21 import org.ejbca.core.ejb.ra.raadmin.IRaAdminSessionLocalHome;
22 import org.ejbca.core.ejb.services.IServiceSessionLocal;
23 import org.ejbca.core.ejb.services.IServiceSessionLocalHome;
24 import org.ejbca.core.model.authorization.AuthorizationDeniedException;
25 import org.ejbca.core.model.authorization.AvailableAccessRules;
26 import org.ejbca.core.model.log.Admin;
27 import org.ejbca.ui.web.RequestHelper;
28
29
30
31
32 /**
33  * Class used to intergrate the old jsp framework with the new JSF one.
34  * Contains methods for such things as language, themes ext
35  *
36  * @author Philip Vendil
37  * $Id: EjbcaJSFHelper.java,v 1.4 2006/10/26 11:02:45 herrvendil Exp $
38  */

39
40 public class EjbcaJSFHelper {
41     private static final Logger log = Logger.getLogger(EjbcaJSFHelper.class);
42         
43     private EjbcaJSFLanguageResource text = null;
44     private EjbcaJSFImageResource image = null;
45     private EjbcaWebBean ejbcawebbean;
46     
47
48     private IRaAdminSessionLocal raadminsession;
49     private ICAAdminSessionLocal caadminsession;
50     private IApprovalSessionLocal approvalsession;
51     private IServiceSessionLocal servicesession;
52
53     
54     private FacesContext ctx = FacesContext.getCurrentInstance();
55       
56     private Admin admin = null;
57
58     
59     public EjbcaJSFHelper(){}
60     
61     public void setEjbcaWebBean(EjbcaWebBean ejbcawebbean){
62        this.ejbcawebbean = ejbcawebbean;
63        text = new EjbcaJSFLanguageResource(ejbcawebbean);
64        image = new EjbcaJSFImageResource(ejbcawebbean);
65        admin = getAdmin();
66     }
67     
68     /**
69      * Returns the EJBCA title
70      */

71     public String JavaDoc getEjbcaTitle(){
72         return getEjbcaWebBean().getGlobalConfiguration().getEjbcaTitle();
73     }
74     
75     /**
76      * Returns the EJBCA theme
77      */

78     public String JavaDoc getTheme(){
79         return getEjbcaWebBean().getCssFile();
80     }
81     
82     /**
83      * Returns the EJBCA base url
84      */

85     public String JavaDoc getEjbcaBaseURL(){
86         return getEjbcaWebBean().getBaseUrl();
87     }
88     
89     /**
90      * Returns the EJBCA content string
91      */

92     public String JavaDoc getContent(){
93         return "text/html; charset=" + RequestHelper.getDefaultContentEncoding();
94     }
95     
96    /**
97     * Used for language resources.
98     */

99     public Map JavaDoc getText(){
100         return text;
101     }
102     
103     /**
104      * Used for image resources.
105      */

106      public Map JavaDoc getImage(){
107         return image;
108      }
109     
110      /**
111       * Special function for approval pages since it has two different accessrules
112      * @throws AuthorizationDeniedException
113       *
114       */

115      public void authorizedToApprovalPages() throws AuthorizationDeniedException{
116           // Check Authorization
117
boolean approveendentity = false;
118         boolean approvecaaction = false;
119         try{
120             approveendentity = getEjbcaWebBean().isAuthorizedNoLog(AvailableAccessRules.REGULAR_APPROVEENDENTITY);
121         }catch(AuthorizationDeniedException e){}
122         try{
123             approvecaaction = getEjbcaWebBean().isAuthorizedNoLog(AvailableAccessRules.REGULAR_APPROVECAACTION);
124         }catch(AuthorizationDeniedException e){}
125         if(!approveendentity && !approvecaaction){
126             throw new AuthorizationDeniedException("Not authorized to view approval pages");
127         }
128      }
129      
130      /**
131       * Only superadmins are authorized to services pages
132      * @throws AuthorizationDeniedException
133       *
134       */

135      public void authorizedToServicesPages() throws AuthorizationDeniedException{
136         getEjbcaWebBean().isAuthorizedNoLog(AvailableAccessRules.ROLE_SUPERADMINISTRATOR);
137      }
138      
139     public int getEntriesPerPage(){
140         return getEjbcaWebBean().getEntriesPerPage();
141     }
142     
143     public EjbcaWebBean getEjbcaWebBean(){
144         
145         if(ejbcawebbean == null){
146             FacesContext ctx = FacesContext.getCurrentInstance();
147
148             HttpSession JavaDoc session = (HttpSession JavaDoc) ctx.getExternalContext().getSession(true);
149  
150                         
151             synchronized (session) {
152                 ejbcawebbean = (org.ejbca.ui.web.admin.configuration.EjbcaWebBean) session.getAttribute("ejbcawebbean");
153                 if (ejbcawebbean == null){
154                     ejbcawebbean = new org.ejbca.ui.web.admin.configuration.EjbcaWebBean();
155                     session.setAttribute("ejbcawebbean", ejbcawebbean);
156                 }
157             }
158             
159             try {
160                 ejbcawebbean.initialize((HttpServletRequest JavaDoc) ctx.getExternalContext().getRequest(), "/administrator");
161             } catch (Exception JavaDoc e) {
162                 log.error(e);
163             }
164         }
165         
166         return ejbcawebbean;
167     }
168  
169     public Admin getAdmin() {
170           if(admin == null){
171               X509Certificate JavaDoc[] certificates = (X509Certificate JavaDoc[]) ((HttpServletRequest JavaDoc )ctx.getExternalContext().getRequest()).getAttribute( "javax.servlet.request.X509Certificate" );
172               admin = new Admin(certificates[0]);
173           }
174           return admin;
175       }
176     
177     public static EjbcaJSFHelper getBean(){
178         FacesContext context = FacesContext.getCurrentInstance();
179         Application app = context.getApplication();
180         ValueBinding binding = app.createValueBinding("#{web}");
181         Object JavaDoc value = binding.getValue(context);
182         return (EjbcaJSFHelper) value;
183     }
184     
185     public IRaAdminSessionLocal getRaAdminSession(){
186         if(raadminsession == null){
187             ServiceLocator locator = ServiceLocator.getInstance();
188             IRaAdminSessionLocalHome raadminsessionhome = (IRaAdminSessionLocalHome) locator.getLocalHome(IRaAdminSessionLocalHome.COMP_NAME);
189             try {
190                 raadminsession = raadminsessionhome.create();
191             } catch (CreateException JavaDoc e) {
192                 throw new EJBException JavaDoc(e);
193             }
194         }
195
196         return raadminsession;
197     }
198
199     public ICAAdminSessionLocal getCAAdminSession(){
200         if(caadminsession == null){
201             ServiceLocator locator = ServiceLocator.getInstance();
202             ICAAdminSessionLocalHome caadminsessionhome = (ICAAdminSessionLocalHome) locator.getLocalHome(ICAAdminSessionLocalHome.COMP_NAME);
203             try {
204                 caadminsession = caadminsessionhome.create();
205             } catch (CreateException JavaDoc e) {
206                 throw new EJBException JavaDoc(e);
207             }
208         }
209         return caadminsession;
210     }
211     
212     public IApprovalSessionLocal getApprovalSession(){
213         if(approvalsession == null){
214             ServiceLocator locator = ServiceLocator.getInstance();
215             IApprovalSessionLocalHome approvalsessionhome = (IApprovalSessionLocalHome) locator.getLocalHome(IApprovalSessionLocalHome.COMP_NAME);
216             try {
217                 approvalsession = approvalsessionhome.create();
218             } catch (CreateException JavaDoc e) {
219                 throw new EJBException JavaDoc(e);
220             }
221         }
222         return approvalsession;
223     }
224     
225     
226     public IServiceSessionLocal getServiceSession(){
227         if(servicesession == null){
228             ServiceLocator locator = ServiceLocator.getInstance();
229             IServiceSessionLocalHome servicesessionhome = (IServiceSessionLocalHome) locator.getLocalHome(IServiceSessionLocalHome.COMP_NAME);
230             try {
231                 servicesession = servicesessionhome.create();
232             } catch (CreateException JavaDoc e) {
233                 throw new EJBException JavaDoc(e);
234             }
235         }
236         return servicesession;
237     }
238 }
239
Popular Tags