KickJava   Java API By Example, From Geeks To Geeks.

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


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.cainterface;
15
16 import java.io.ByteArrayOutputStream JavaDoc;
17 import java.io.IOException JavaDoc;
18 import java.security.cert.Certificate JavaDoc;
19
20 import javax.ejb.EJBException JavaDoc;
21 import javax.servlet.ServletConfig JavaDoc;
22 import javax.servlet.ServletException JavaDoc;
23 import javax.servlet.http.HttpServlet JavaDoc;
24 import javax.servlet.http.HttpServletRequest JavaDoc;
25 import javax.servlet.http.HttpServletResponse JavaDoc;
26
27 import org.apache.log4j.Logger;
28 import org.bouncycastle.asn1.DEROutputStream;
29 import org.bouncycastle.jce.PKCS10CertificationRequest;
30 import org.ejbca.core.ejb.ServiceLocator;
31 import org.ejbca.core.ejb.ca.sign.ISignSessionLocal;
32 import org.ejbca.core.ejb.ca.sign.ISignSessionLocalHome;
33 import org.ejbca.core.model.InternalResources;
34 import org.ejbca.ui.web.RequestHelper;
35 import org.ejbca.ui.web.admin.configuration.EjbcaWebBean;
36 import org.ejbca.ui.web.pub.ServletUtils;
37
38 /**
39  * Servlet used to distribute CRLs.<br>
40  *
41  * The servlet is called with method GET or POST and syntax
42  * <code>command=&lt;command&gt;</code>.
43  * <p>The follwing commands are supported:<br>
44  * <ul>
45  * <li>crl - gets the latest CRL.
46  *
47  * @version $Id: CACertReqServlet.java,v 1.7 2006/12/13 10:35:30 anatom Exp $
48  *
49  * @web.servlet name = "CACertReq"
50  * display-name = "CACertReqServlet"
51  * description="Used to retrive CA certificate request and Processed CA Certificates from AdminWeb GUI"
52  * load-on-startup = "99"
53  *
54  * @web.servlet-mapping url-pattern = "/ca/editcas/cacertreq"
55  *
56  */

57 public class CACertReqServlet extends HttpServlet JavaDoc {
58
59     private static final Logger log = Logger.getLogger(CACertReqServlet.class);
60     /** Internal localization of logs and errors */
61     private static final InternalResources intres = InternalResources.getInstance();
62
63     private static final String JavaDoc COMMAND_PROPERTY_NAME = "cmd";
64     private static final String JavaDoc COMMAND_CERTREQ = "certreq";
65     private static final String JavaDoc COMMAND_CERT = "cert";
66     private static final String JavaDoc COMMAND_CERTPKCS7 = "certpkcs7";
67     
68     private ISignSessionLocal signsession = null;
69    
70    private synchronized ISignSessionLocal getSignSession(){
71       if(signsession == null){
72         try {
73             ISignSessionLocalHome signhome = (ISignSessionLocalHome)ServiceLocator.getInstance().getLocalHome(ISignSessionLocalHome.COMP_NAME);
74             signsession = signhome.create();
75         }catch(Exception JavaDoc e){
76             throw new EJBException JavaDoc(e);
77         }
78       }
79       return signsession;
80    }
81    
82    
83     public void init(ServletConfig JavaDoc config) throws ServletException JavaDoc {
84         super.init(config);
85     }
86
87     public void doPost(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res)
88         throws IOException JavaDoc, ServletException JavaDoc {
89         log.debug(">doPost()");
90         doGet(req, res);
91         log.debug("<doPost()");
92     } //doPost
93

94     public void doGet(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res) throws java.io.IOException JavaDoc, ServletException JavaDoc {
95         log.debug(">doGet()");
96
97         // Check if authorized
98
EjbcaWebBean ejbcawebbean= (org.ejbca.ui.web.admin.configuration.EjbcaWebBean)
99                                    req.getSession().getAttribute("ejbcawebbean");
100         if ( ejbcawebbean == null ){
101           try {
102             ejbcawebbean = (org.ejbca.ui.web.admin.configuration.EjbcaWebBean) java.beans.Beans.instantiate(this.getClass().getClassLoader(), "org.ejbca.ui.web.admin.configuration.EjbcaWebBean");
103            } catch (ClassNotFoundException JavaDoc exc) {
104                throw new ServletException JavaDoc(exc.getMessage());
105            }catch (Exception JavaDoc exc) {
106                throw new ServletException JavaDoc (" Cannot create bean of class "+"org.ejbca.ui.web.admin.configuration.EjbcaWebBean", exc);
107            }
108            req.getSession().setAttribute("ejbcawebbean", ejbcawebbean);
109         }
110
111         // Check if authorized
112
CAInterfaceBean cabean= (org.ejbca.ui.web.admin.cainterface.CAInterfaceBean)
113                                    req.getSession().getAttribute("cabean");
114         if ( cabean == null ){
115           try {
116             cabean = (org.ejbca.ui.web.admin.cainterface.CAInterfaceBean) java.beans.Beans.instantiate(this.getClass().getClassLoader(), "org.ejbca.ui.web.admin.cainterface.CAInterfaceBean");
117            } catch (ClassNotFoundException JavaDoc exc) {
118                throw new ServletException JavaDoc(exc.getMessage());
119            }catch (Exception JavaDoc exc) {
120                throw new ServletException JavaDoc (" Cannot create bean of class "+"org.ejbca.ui.web.admin.cainterface.CAInterfaceBean", exc);
121            }
122            req.getSession().setAttribute("cabean", cabean);
123         }
124
125
126         try{
127           ejbcawebbean.initialize(req, "/super_administrator");
128         } catch(Exception JavaDoc e){
129            throw new java.io.IOException JavaDoc("Authorization Denied");
130         }
131
132         try{
133           cabean.initialize(req, ejbcawebbean);
134         } catch(Exception JavaDoc e){
135            throw new java.io.IOException JavaDoc("Error initializing CACertReqServlet");
136         }
137                 
138         
139         String JavaDoc command;
140         // Keep this for logging.
141
String JavaDoc remoteAddr = req.getRemoteAddr();
142         RequestHelper.setDefaultCharacterEncoding(req);
143         command = req.getParameter(COMMAND_PROPERTY_NAME);
144         if (command == null)
145             command = "";
146         if (command.equalsIgnoreCase(COMMAND_CERTREQ)) {
147             try {
148                 
149                 PKCS10CertificationRequest pkcs10request = cabean.getPKCS10RequestData();
150                 ByteArrayOutputStream JavaDoc bOut = new ByteArrayOutputStream JavaDoc();
151                 DEROutputStream dOut = new DEROutputStream(bOut);
152                 dOut.writeObject(pkcs10request);
153                 dOut.close();
154                 byte[] b64certreq = org.ejbca.util.Base64.encode(bOut.toByteArray());
155                 String JavaDoc out = "-----BEGIN CERTIFICATE REQUEST-----\n";
156                 out += new String JavaDoc(b64certreq);
157                 out += "\n-----END CERTIFICATE REQUEST-----\n";
158                 // We must remove cache headers for IE
159
ServletUtils.removeCacheHeaders(res);
160                 String JavaDoc filename = "pkcs10certificaterequest.pem";
161                 res.setHeader("Content-disposition", "attachment; filename=" + filename);
162                 res.setContentType("application/octet-stream");
163                 res.setContentLength(out.length());
164                 res.getOutputStream().write(out.getBytes());
165                 String JavaDoc iMsg = intres.getLocalizedMessage("certreq.sentlatestcertreq", remoteAddr);
166                 log.info(iMsg);
167             } catch (Exception JavaDoc e) {
168                 String JavaDoc errMsg = intres.getLocalizedMessage("certreq.errorsendlatestcertreq", remoteAddr);
169                 log.error(errMsg, e);
170                 res.sendError(HttpServletResponse.SC_NOT_FOUND, errMsg);
171                 return;
172             }
173         }
174         if (command.equalsIgnoreCase(COMMAND_CERT)) {
175              try {
176                 Certificate JavaDoc cert = cabean.getProcessedCertificate();
177                 byte[] b64cert = org.ejbca.util.Base64.encode(cert.getEncoded());
178                 RequestHelper.sendNewB64Cert(b64cert, res, RequestHelper.BEGIN_CERTIFICATE_WITH_NL, RequestHelper.END_CERTIFICATE_WITH_NL);
179              } catch (Exception JavaDoc e) {
180                  String JavaDoc errMsg = intres.getLocalizedMessage("certreq.errorsendcert", remoteAddr, e.getMessage());
181                  log.error(errMsg, e);
182                  res.sendError(HttpServletResponse.SC_NOT_FOUND, errMsg);
183                  return;
184              }
185          }
186         if (command.equalsIgnoreCase(COMMAND_CERTPKCS7)) {
187              try {
188                 Certificate JavaDoc cert = cabean.getProcessedCertificate();
189                 byte[] pkcs7 = getSignSession().createPKCS7(ejbcawebbean.getAdminObject(), cert, true);
190                 byte[] b64cert = org.ejbca.util.Base64.encode(pkcs7);
191                 RequestHelper.sendNewB64Cert(b64cert, res, RequestHelper.BEGIN_PKCS7_WITH_NL, RequestHelper.END_PKCS7_WITH_NL);
192              } catch (Exception JavaDoc e) {
193                  String JavaDoc errMsg = intres.getLocalizedMessage("certreq.errorsendcert", remoteAddr, e.getMessage());
194                  log.error(errMsg, e);
195                  res.sendError(HttpServletResponse.SC_NOT_FOUND, errMsg);
196                  return;
197              }
198          }
199
200
201
202
203     } // doGet
204

205 }
206
Popular Tags