View Javadoc

1   /**
2    * Copyright 2005-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * A servlet which returns a client {@link java.security.KeyStore} object to the user as a file.  It takes in
27   * the KeyStore file as a session attribute byte array.
28   * 
29   * @author Kuali Rice Team (rice.collab@kuali.org)
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  //    application/pkix-cert
37  //    application/pkix-crl
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  }