KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejbca > ui > web > admin > cainterface > CAExportServlet


1 package org.ejbca.ui.web.admin.cainterface;
2
3 import java.io.IOException JavaDoc;
4
5 import javax.servlet.ServletConfig JavaDoc;
6 import javax.servlet.ServletException JavaDoc;
7 import javax.servlet.http.HttpServlet JavaDoc;
8 import javax.servlet.http.HttpServletRequest JavaDoc;
9 import javax.servlet.http.HttpServletResponse JavaDoc;
10
11 import org.apache.log4j.Logger;
12 import org.ejbca.core.model.authorization.AvailableAccessRules;
13 import org.ejbca.ui.web.RequestHelper;
14 import org.ejbca.ui.web.admin.configuration.EjbcaWebBean;
15 import org.ejbca.ui.web.pub.ServletUtils;
16
17 /**
18  * This servlet exports a CA as an octet/stream.
19  *
20  * @web.servlet name = "CAExport"
21  * display-name = "CAExportServlet"
22  * description="Returns the CA as a PKCS#12 file."
23  * load-on-startup = "99"
24  *
25  * @web.servlet-mapping url-pattern = "/ca/exportca"
26  *
27  */

28 public class CAExportServlet extends HttpServlet JavaDoc {
29     private static final Logger log = Logger.getLogger(CAExportServlet.class);
30     private static final long serialVersionUID = 378499368926058906L;
31     public static final String JavaDoc HIDDEN_CANAME = "hiddencaname";
32     public static final String JavaDoc TEXTFIELD_EXPORTCA_PASSWORD = "textfieldexportcapassword";
33
34     /**
35      * Initialize.
36      */

37     public void init(ServletConfig JavaDoc config) throws ServletException JavaDoc {
38         super.init(config);
39     } // init
40

41     /**
42      * Handle HTTP Post. Redirect the request to doGet(..).
43      * This method should not be called explicitly.
44      *
45      * @param req The request.
46      * @param res The response.
47      */

48     public void doPost(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res) throws IOException JavaDoc, ServletException JavaDoc {
49         log.debug(">doPost()");
50         doGet(req, res);
51         log.debug("<doPost()");
52     } //doPost
53

54     /**
55      * Validates the request parameters and outputs the CA as an PKCS#12 output/octet-stream.
56      * This method should not be called explicitly.
57      *
58      * @param req The request.
59      * @param res The response.
60      */

61     public void doGet(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res) throws java.io.IOException JavaDoc, ServletException JavaDoc {
62         log.debug(">doGet()");
63         // Check if authorized
64
EjbcaWebBean ejbcawebbean= (org.ejbca.ui.web.admin.configuration.EjbcaWebBean) req.getSession().getAttribute("ejbcawebbean");
65         if ( ejbcawebbean == null ) {
66           try {
67               ejbcawebbean = (org.ejbca.ui.web.admin.configuration.EjbcaWebBean) java.beans.Beans.instantiate(this.getClass().getClassLoader(), org.ejbca.ui.web.admin.configuration.EjbcaWebBean.class.getName());
68           } catch (ClassNotFoundException JavaDoc e) {
69               throw new ServletException JavaDoc(e.getMessage());
70           } catch (Exception JavaDoc e) {
71               throw new ServletException JavaDoc ("Cannot create bean of class org.ejbca.ui.web.admin.configuration.EjbcaWebBean", e);
72           }
73            req.getSession().setAttribute("ejbcawebbean", ejbcawebbean);
74         }
75         try{
76             ejbcawebbean.initialize(req, AvailableAccessRules.ROLE_SUPERADMINISTRATOR);
77         } catch(Exception JavaDoc e) {
78             throw new java.io.IOException JavaDoc("Authorization Denied");
79         }
80         RequestHelper.setDefaultCharacterEncoding(req);
81         String JavaDoc caname = req.getParameter(HIDDEN_CANAME);
82         String JavaDoc capassword = req.getParameter(TEXTFIELD_EXPORTCA_PASSWORD);
83         javax.naming.Context JavaDoc ictx = null;
84         log.debug("Got request from "+req.getRemoteAddr()+" to export "+caname);
85         try{
86             byte[] keystorebytes = null;
87             if (ictx == null) {
88                 ictx = org.ejbca.core.ejb.InitialContextBuilder.getInstance().getInitialContext();
89             }
90             org.ejbca.core.ejb.ca.caadmin.ICAAdminSessionHome home = (org.ejbca.core.ejb.ca.caadmin.ICAAdminSessionHome) javax.rmi.PortableRemoteObject.narrow(ictx.lookup("CAAdminSession"), org.ejbca.core.ejb.ca.caadmin.ICAAdminSessionHome.class );
91             org.ejbca.core.ejb.ca.caadmin.ICAAdminSessionRemote caadminsession = home.create();
92             if ( !caadminsession.isKeyStorePassword(ejbcawebbean.getAdminObject(), capassword) ) {
93                 throw new IllegalArgumentException JavaDoc("Keystore password does not match user-supplied password.");
94             }
95             keystorebytes = caadminsession.exportCAKeyStore(ejbcawebbean.getAdminObject(), caname, capassword.toCharArray(), capassword.toCharArray(), "SignatureKeyAlias", "EncryptionKeyAlias");
96             ServletUtils.removeCacheHeaders(res); // We must remove cache headers for IE
97
res.setContentType("application/octet-stream");
98             res.setHeader("Cache-Control", "no-cache");
99             res.setContentLength(keystorebytes.length);
100             res.setHeader("Content-Disposition", "attachment;filename=\"" + caname + ".p12\"");
101             res.getOutputStream().write(keystorebytes);
102         } catch(Exception JavaDoc e) {
103             res.setContentType("text/plain");
104             res.sendError( HttpServletResponse.SC_BAD_REQUEST, e.getMessage() );
105         }
106     } // doGet
107
} // CAExportServlet
108
Popular Tags