KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > keystore > wizards > actions > KeyStoreImportUploadAction


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sslexplorer.keystore.wizards.actions;
21
22 import java.io.File JavaDoc;
23 import java.io.FileOutputStream JavaDoc;
24 import java.io.InputStream JavaDoc;
25 import java.io.OutputStream JavaDoc;
26
27 import javax.servlet.http.HttpServletRequest JavaDoc;
28 import javax.servlet.http.HttpServletResponse JavaDoc;
29
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32 import org.apache.struts.Globals;
33 import org.apache.struts.action.ActionForm;
34 import org.apache.struts.action.ActionForward;
35 import org.apache.struts.action.ActionMapping;
36 import org.apache.struts.action.ActionMessage;
37 import org.apache.struts.action.ActionMessages;
38 import org.apache.struts.upload.FormFile;
39
40 import com.sslexplorer.boot.Util;
41 import com.sslexplorer.core.actions.AuthenticatedAction;
42 import com.sslexplorer.keystore.wizards.AbstractKeyStoreImportType;
43 import com.sslexplorer.keystore.wizards.KeyStoreImportTypeManager;
44 import com.sslexplorer.keystore.wizards.forms.KeyStoreImportFileForm;
45 import com.sslexplorer.keystore.wizards.forms.KeyStoreImportTypeForm;
46 import com.sslexplorer.keystore.wizards.types.ReplyFromCAImportType;
47 import com.sslexplorer.policyframework.Permission;
48 import com.sslexplorer.policyframework.PolicyConstants;
49 import com.sslexplorer.security.Constants;
50 import com.sslexplorer.security.SessionInfo;
51 import com.sslexplorer.vfs.forms.UploadForm;
52 import com.sslexplorer.wizard.AbstractWizardSequence;
53
54 /**
55  * Action to process the file upload initiated during the key store import
56  * wizard.
57  *
58  * @author Brett Smith <a HREF="mailto: brett@3sp.com">&lt;brett@3sp.com&gt;</a>
59  */

60 public class KeyStoreImportUploadAction extends AuthenticatedAction {
61
62     static Log log = LogFactory.getLog(KeyStoreImportUploadAction.class);
63
64     /**
65      * Constructor
66      */

67     public KeyStoreImportUploadAction() {
68         super(PolicyConstants.KEYSTORE_RESOURCE_TYPE, new Permission[] { PolicyConstants.PERM_CHANGE });
69     }
70
71     /*
72      * (non-Javadoc)
73      *
74      * @see com.sslexplorer.core.actions.AuthenticatedAction#onExecute(org.apache.struts.action.ActionMapping,
75      * org.apache.struts.action.ActionForm,
76      * javax.servlet.http.HttpServletRequest,
77      * javax.servlet.http.HttpServletResponse)
78      */

79     public ActionForward onExecute(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
80                     throws Exception JavaDoc {
81         String JavaDoc passphrase = request.getParameter("passphrase");
82         String JavaDoc alias = request.getParameter("alias");
83         UploadForm uploadForm = (UploadForm) form;
84         FormFile uploadFile = uploadForm.getUploadFile();
85         String JavaDoc fileName = uploadFile.getFileName();
86         int fileSize = uploadFile.getFileSize();
87         InputStream JavaDoc in = null;
88         OutputStream JavaDoc out = null;
89         File JavaDoc uploadedFile = File.createTempFile("uploadedFile", "");
90         ActionMessages errs = new ActionMessages();
91
92         try {
93             if (fileName.trim().length() == 0) {
94                 errs.add(Globals.ERROR_KEY, new ActionMessage("keyStoreImportWizard.keyStoreImportFile.noFileProvided"));
95             } else {
96
97                 AbstractWizardSequence seq = (AbstractWizardSequence) request.getSession().getAttribute(Constants.WIZARD_SEQUENCE);
98
99                 AbstractKeyStoreImportType importType = KeyStoreImportTypeManager.getInstance().getType(
100                     (String JavaDoc)seq.getAttribute(KeyStoreImportTypeForm.ATTR_TYPE, ReplyFromCAImportType.REPLY_FROM_CA));
101                 SessionInfo sessionInfo = getSessionInfo(request);
102                 importType.validate(errs, alias, passphrase, seq, sessionInfo);
103
104                 if (errs.size() == 0) {
105
106                     in = uploadFile.getInputStream();
107                     out = new FileOutputStream JavaDoc(uploadedFile);
108                     Util.copy(in, out);
109
110                     if (passphrase != null) {
111                         seq.putAttribute(KeyStoreImportFileForm.ATTR_PASSPHRASE, passphrase);
112                     }
113                     seq.putAttribute(KeyStoreImportFileForm.ATTR_UPLOADED_FILE, uploadedFile);
114                     seq.putAttribute(KeyStoreImportFileForm.ATTR_FILENAME, uploadFile);
115                     if (alias != null) {
116                         seq.putAttribute(KeyStoreImportFileForm.ATTR_ALIAS, alias.toLowerCase());
117                     }
118
119                     ActionMessages msgs = new ActionMessages();
120                     msgs.add(Globals.MESSAGE_KEY, new ActionMessage("keyStoreImportWizard.keyStoreImportFile.uploaded", fileName,
121                                     new Integer JavaDoc(fileSize)));
122                 }
123             }
124         } catch (Exception JavaDoc e) {
125             log.error("Failed to upload to upload key store import file.", e);
126             errs.add(Globals.ERROR_KEY, new ActionMessage("keyStoreImportWizard.keyStoreImportFile.failedToUploadFile", fileName, e
127                             .getMessage() == null ? "No message provided." : e.getMessage()));
128         } finally {
129             Util.closeStream(in);
130             Util.closeStream(out);
131         }
132         saveErrors(request, errs);
133         return errs.size() == 0 ? mapping.findForward("success") : new ActionForward(mapping.getInput(), false);
134     }
135
136     /*
137      * (non-Javadoc)
138      *
139      * @see com.sslexplorer.core.actions.CoreAction#getNavigationContext(org.apache.struts.action.ActionMapping,
140      * org.apache.struts.action.ActionForm,
141      * javax.servlet.http.HttpServletRequest,
142      * javax.servlet.http.HttpServletResponse)
143      */

144     public int getNavigationContext(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) {
145         return SessionInfo.MANAGEMENT_CONSOLE_CONTEXT;
146     }
147
148 }
149
Popular Tags