1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.ksb.security.admin;
17
18 import java.io.IOException;
19
20 import javax.servlet.ServletException;
21 import javax.servlet.http.HttpServlet;
22 import javax.servlet.http.HttpServletRequest;
23 import javax.servlet.http.HttpServletResponse;
24
25
26
27
28
29
30
31 public class ExportServlet extends HttpServlet {
32
33 private static final long serialVersionUID = 3234778044685975458L;
34
35 private static final String MIME_TYPE = "application/octet-stream";
36
37
38 public static final String CLIENT_KEYSTORE_DATA = "ClientKeyStoreData";
39
40 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
41 byte[] clientKeyStoreData = (byte[])request.getSession().getAttribute(CLIENT_KEYSTORE_DATA);
42 request.getSession().removeAttribute(CLIENT_KEYSTORE_DATA);
43 if (clientKeyStoreData == null) {
44 throw new ServletException("No keystore file was specified.");
45 }
46 response.setContentType(MIME_TYPE);
47 response.setContentLength(clientKeyStoreData.length);
48 response.setHeader("Content-disposition", "attachment; filename="+extractFileName(request));
49 response.getOutputStream().write(clientKeyStoreData);
50 response.getOutputStream().close();
51 }
52
53 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
54 doPost(request, response);
55 }
56
57 private String extractFileName(HttpServletRequest request) {
58 String path = request.getPathInfo();
59 int index = path.lastIndexOf('/');
60 if (index >= 0) {
61 path = path.substring(index+1);
62 }
63 return path;
64 }
65
66 public static final String generateExportPath(String keystoreFileName, HttpServletRequest request) {
67 String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath();
68 return basePath + "/exportsecurity/"+keystoreFileName;
69 }
70
71 }