View Javadoc

1   /**
2    *
3    * Copyright 2004, 2005 The Apache Software Foundation or its licensors, as applicable.
4    *
5    *  Licensed under the Apache License, Version 2.0 (the "License");
6    *  you may not use this file except in compliance with the License.
7    *  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   *  Unless required by applicable law or agreed to in writing, software
12   *  distributed under the License is distributed on an "AS IS" BASIS,
13   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   *  See the License for the specific language governing permissions and
15   *  limitations under the License.
16   */
17  
18  package org.apache.geronimo.console.certmanager.actions;
19  
20  import java.io.File;
21  import java.io.FileInputStream;
22  import java.io.IOException;
23  import java.io.InputStream;
24  import java.net.URLDecoder;
25  import java.net.URLEncoder;
26  import java.security.cert.CertificateFactory;
27  import java.util.Collection;
28  import java.util.Iterator;
29  import java.util.List;
30  
31  import javax.portlet.ActionRequest;
32  import javax.portlet.ActionResponse;
33  import javax.portlet.PortletException;
34  import javax.portlet.PortletRequestDispatcher;
35  import javax.portlet.RenderRequest;
36  import javax.portlet.RenderResponse;
37  
38  import org.apache.commons.fileupload.FileItem;
39  import org.apache.commons.fileupload.FileUploadException;
40  import org.apache.commons.fileupload.disk.DiskFileItemFactory;
41  import org.apache.commons.fileupload.portlet.PortletFileUpload;
42  import org.apache.geronimo.console.certmanager.CertManagerPortlet;
43  
44  public class UploadCertificateFile {
45  
46      public static void action(CertManagerPortlet portlet,
47              ActionRequest request, ActionResponse response)
48              throws PortletException, IOException {
49          if (!PortletFileUpload.isMultipartContent(request)) {
50              throw new PortletException("Expected file upload");
51          }
52  
53          File rootDir = new File(System.getProperty("java.io.tmpdir"));
54          PortletFileUpload uploader = new PortletFileUpload(
55                  new DiskFileItemFactory(10240, rootDir));
56          File certFile = null;
57  
58          try {
59              List items = uploader.parseRequest(request);
60              for (Iterator i = items.iterator(); i.hasNext();) {
61                  FileItem item = (FileItem) i.next();
62                  if (!item.isFormField()) {
63                      String name = item.getName().trim();
64  
65                      if (name.length() == 0) {
66                          certFile = null;
67                      } else {
68                          // Firefox sends basename, IE sends full path
69                          int index = name.lastIndexOf('\\');
70                          if (index != -1) {
71                              name = name.substring(index + 1);
72                          }
73                          certFile = new File(rootDir, name);
74                      }
75  
76                      if (certFile != null) {
77                          try {
78                              item.write(certFile);
79                          } catch (Exception e) {
80                              throw new PortletException(e);
81                          }
82                      }
83                  }
84              }
85          } catch (FileUploadException e) {
86              throw new PortletException(e);
87          }
88  
89          // pass certificate file name along
90          String certFileName = certFile.getCanonicalPath();
91          String enc = URLEncoder.encode(certFileName, "UTF-8");
92  
93          portlet.getPortletContext().log("cert-file-name: " + certFileName);
94          portlet.getPortletContext().log("enc: " + enc);
95  
96          response.setRenderParameter("org.apache.geronimo.console.cert.file.enc", enc);
97          response.setRenderParameter("action", request.getParameter("action"));
98      }
99  
100     public static void render(CertManagerPortlet portlet,
101             RenderRequest request, RenderResponse response)
102             throws PortletException, IOException {
103 
104         String encodedCertFileName = request
105                 .getParameter("org.apache.geronimo.console.cert.file.enc");
106         String certFileName = URLDecoder.decode(encodedCertFileName, "UTF-8");
107         portlet.getPortletContext().log("cert file: " + certFileName);
108 
109         Collection certs = null;
110         InputStream is = null;
111 
112         if (certFileName != null) {
113             File certFile = new File(certFileName);
114             try {
115                 is = new FileInputStream(certFile);
116 
117                 CertificateFactory cf = CertificateFactory.getInstance("X.509");
118                 certs = cf.generateCertificates(is);
119             } catch (Exception e) {
120                 throw new PortletException(e);
121             } finally {
122                 try {
123                     if (is != null) {
124                         is.close();
125                     }
126                 } catch (Exception e) {
127                 }
128             }
129 
130             request.setAttribute("org.apache.geronimo.console.certs", certs);
131             request.setAttribute("org.apache.geronimo.console.cert.file.enc",
132                     encodedCertFileName);
133         }
134 
135         PortletRequestDispatcher prd = null;
136 
137         prd = portlet.getPortletContext().getRequestDispatcher(
138                 "/WEB-INF/view/certmanager/importTrustedCertNormal.jsp");
139 
140         prd.include(request, response);
141     }
142 }