001 /**
002 * Copyright 2005-2014 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package org.kuali.rice.ksb.security.admin;
017
018 import java.io.IOException;
019
020 import javax.servlet.ServletException;
021 import javax.servlet.http.HttpServlet;
022 import javax.servlet.http.HttpServletRequest;
023 import javax.servlet.http.HttpServletResponse;
024
025 /**
026 * A servlet which returns a client {@link java.security.KeyStore} object to the user as a file. It takes in
027 * the KeyStore file as a session attribute byte array.
028 *
029 * @author Kuali Rice Team (rice.collab@kuali.org)
030 */
031 public class ExportServlet extends HttpServlet {
032
033 private static final long serialVersionUID = 3234778044685975458L;
034
035 private static final String MIME_TYPE = "application/octet-stream";
036 // application/pkix-cert
037 // application/pkix-crl
038 public static final String CLIENT_KEYSTORE_DATA = "ClientKeyStoreData";
039
040 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
041 byte[] clientKeyStoreData = (byte[])request.getSession().getAttribute(CLIENT_KEYSTORE_DATA);
042 request.getSession().removeAttribute(CLIENT_KEYSTORE_DATA);
043 if (clientKeyStoreData == null) {
044 throw new ServletException("No keystore file was specified.");
045 }
046 response.setContentType(MIME_TYPE);
047 response.setContentLength(clientKeyStoreData.length);
048 response.setHeader("Content-disposition", "attachment; filename="+extractFileName(request));
049 response.getOutputStream().write(clientKeyStoreData);
050 response.getOutputStream().close();
051 }
052
053 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
054 doPost(request, response);
055 }
056
057 private String extractFileName(HttpServletRequest request) {
058 String path = request.getPathInfo();
059 int index = path.lastIndexOf('/');
060 if (index >= 0) {
061 path = path.substring(index+1);
062 }
063 return path;
064 }
065
066 public static final String generateExportPath(String keystoreFileName, HttpServletRequest request) {
067 String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath();
068 return basePath + "/exportsecurity/"+keystoreFileName;
069 }
070
071 }