KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > ca > helper > DownloadCertificateServlet


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.geronimo.ca.helper;
19
20 import java.io.IOException JavaDoc;
21 import java.io.OutputStream JavaDoc;
22 import java.math.BigInteger JavaDoc;
23 import java.security.cert.Certificate JavaDoc;
24
25 import javax.servlet.ServletException JavaDoc;
26 import javax.servlet.http.HttpServletRequest JavaDoc;
27 import javax.servlet.http.HttpServletResponse JavaDoc;
28
29 import org.apache.geronimo.ca.helper.util.CAHelperUtils;
30 import org.apache.geronimo.management.geronimo.CertificateRequestStore;
31 import org.apache.geronimo.management.geronimo.CertificateStore;
32
33 /**
34  * Servlet implementation class for Servlet: DownloadCertificateServlet
35  *
36  * @version $Rev: 476291 $ $Date: 2006-11-17 15:05:24 -0500 (Fri, 17 Nov 2006) $
37  */

38  public class DownloadCertificateServlet extends javax.servlet.http.HttpServlet JavaDoc implements javax.servlet.Servlet JavaDoc {
39     /* (non-Java-doc)
40      * @see javax.servlet.http.HttpServlet#HttpServlet()
41      */

42     public DownloadCertificateServlet() {
43         super();
44     }
45
46     /* (non-Java-doc)
47      * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
48      */

49     protected void doGet(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws ServletException JavaDoc, IOException JavaDoc {
50         doPost(request, response);
51     }
52
53     /* (non-Java-doc)
54      * @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
55      */

56     protected void doPost(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws ServletException JavaDoc, IOException JavaDoc {
57         String JavaDoc type = request.getParameter("type");
58         String JavaDoc csrId = request.getParameter("csrId");
59         try {
60             if(type != null && type.equals("ca")){
61                 // Request is to download CA's certificate
62
// Retrieve CA's certificate from the CertificateStore
63
CertificateStore certStore = CAHelperUtils.getCertificateStore();
64                 Certificate JavaDoc cert = certStore.getCACertificate();
65                 byte[] data = cert.getEncoded();
66                 // Upload the certificate with mime-header for CA certificates
67
response.setContentType("application/x-x509-ca-cert");
68                 response.setContentLength(data.length);
69                 response.getOutputStream().write(data);
70             } else if(csrId != null){
71                 // Request is to download user's own certificate
72
// Get the serial number of the certificate based on the csrId
73
CertificateRequestStore certReqStore = CAHelperUtils.getCertificateRequestStore();
74                 BigInteger JavaDoc sNo = certReqStore.getSerialNumberForRequest(csrId);
75                 if(sNo == null) {
76                     // Either the CSR is yet to be fulfilled or the csrId is invalid.
77
throw new Exception JavaDoc("Either the CSR is yet to be fulfilled or the csrId is invalid. csrId = "+csrId);
78                 }
79                 CertificateStore certStore = CAHelperUtils.getCertificateStore();
80                 Certificate JavaDoc cert = certStore.getCertificate(sNo);
81                 byte[] data = cert.getEncoded();
82                 
83                 // Create a link for "verify certificate" page.
84
String JavaDoc host = request.getServerName();
85                 int port = CAHelperUtils.getHttpsClientAuthPort();
86                 String JavaDoc contextPath = request.getContextPath();
87                 String JavaDoc link = "https://"+host+":"+port+""+contextPath+"/verifyCertificate.jsp?csrId="+request.getParameter("csrId");
88
89                 // Create a multi-part mime message with user's certificate and an information page.
90
response.setContentType("multipart/mixed; boundary=\"BOUNDARY\"");
91                 OutputStream JavaDoc out = response.getOutputStream();
92                 out.write("This is a multi-part message in MIME format.\n".getBytes());
93
94                 // Upload the certificate with mime-header for user certificates.
95
out.write("--BOUNDARY\n".getBytes());
96                 out.write(("Content-type: application/x-x509-user-cert\n\n").getBytes());
97                 out.write(data);
98
99                 // A web page showing "verify certificate" link if an HTTPS client-authentication connector is configured.
100
out.write("--BOUNDARY\n".getBytes());
101                 out.write("Content-type: text/html\n\n".getBytes());
102                 out.write("<html><body>".getBytes());
103                 out.write("<p>Certificate is downloaded successfully. ".getBytes());
104                 if(port != -1)
105                     out.write(("Access <a HREF="+link+">this link</a> to verify.</p>\n").getBytes());
106                 else
107                     out.write("No HTTPS client-authentication port is configured to verify.</p>\n".getBytes());
108
109                 out.write(("<a HREF=\""+contextPath+"\"> Back to CA Helper home</a>").getBytes());
110                 out.write("</body></html>".getBytes());
111
112                 out.write("--BOUNDARY--\n".getBytes());
113                 out.flush();
114             } else {
115                 // Request is for downloading neither CA's certificate nor user's certificate.
116
throw new Exception JavaDoc("Invalid certificate download request.");
117             }
118         } catch (Exception JavaDoc e) {
119             throw new ServletException JavaDoc("Exception while uploading certificate.", e);
120         }
121     }
122 }
123
Popular Tags