KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pentaho > ui > servlet > RepositoryFilePublisher


1 /*
2  * Copyright 2006 Pentaho Corporation. All rights reserved.
3  * This software was developed by Pentaho Corporation and is provided under the terms
4  * of the Mozilla Public License, Version 1.1, or any later version. You may not use
5  * this file except in compliance with the license. If you need a copy of the license,
6  * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
7  * BI Platform. The Initial Developer is Pentaho Corporation.
8  *
9  * Software distributed under the Mozilla Public License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
11  * the license for the specific language governing your rights and limitations.
12 */

13 /*
14  * Copyright 2006 Pentaho Corporation. All rights reserved.
15  * This software was developed by Pentaho Corporation and is provided under the terms
16  * of the Mozilla Public License, Version 1.1, or any later version. You may not use
17  * this file except in compliance with the license. If you need a copy of the license,
18  * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
19  * BI Platform. The Initial Developer is Pentaho Corporation.
20  *
21  * Software distributed under the Mozilla Public License is distributed on an "AS IS"
22  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
23  * the license for the specific language governing your rights and limitations.
24  *
25  * @created October 24, 2006
26  * @author Michael D'Amour
27  *
28  */

29
30 package org.pentaho.ui.servlet;
31
32 import java.io.File JavaDoc;
33 import java.io.IOException JavaDoc;
34 import java.util.Iterator JavaDoc;
35 import java.util.List JavaDoc;
36
37 import javax.servlet.ServletException JavaDoc;
38 import javax.servlet.http.HttpServletRequest JavaDoc;
39 import javax.servlet.http.HttpServletResponse JavaDoc;
40
41 import org.apache.commons.fileupload.DiskFileUpload;
42 import org.apache.commons.fileupload.FileItem;
43 import org.apache.commons.logging.Log;
44 import org.apache.commons.logging.LogFactory;
45 import org.pentaho.core.admin.datasources.DataSourceInfo;
46 import org.pentaho.core.admin.datasources.jboss.JBossDatasourceAdmin;
47 import org.pentaho.core.repository.ISolutionRepository;
48 import org.pentaho.core.session.IPentahoSession;
49 import org.pentaho.core.system.PentahoSystem;
50 import org.pentaho.core.util.PublisherUtil;
51 import org.pentaho.core.util.UIUtil;
52 import org.pentaho.messages.util.LocaleHelper;
53
54 public class RepositoryFilePublisher extends ServletBase {
55
56     /**
57      *
58      */

59     private static final long serialVersionUID = 9019152264205996418L;
60
61     private static final Log logger = LogFactory.getLog(GetContent.class);
62
63     public Log getLogger() {
64         return logger;
65     }
66
67     public RepositoryFilePublisher() {
68         super();
69     }
70
71     protected void doPost(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws ServletException JavaDoc, IOException JavaDoc {
72         doGet(request, response);
73     }
74
75     protected void doGet(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws ServletException JavaDoc, IOException JavaDoc {
76         response.setCharacterEncoding(LocaleHelper.getSystemEncoding());
77         IPentahoSession userSession = UIUtil.getPentahoSession(request);
78         ISolutionRepository repository = PentahoSystem.getSolutionRepository(userSession);
79         int status = ISolutionRepository.FILE_ADD_SUCCESSFUL;
80
81         String JavaDoc publishPath = request.getParameter("publishPath"); //$NON-NLS-1$
82

83         String JavaDoc publishKey = request.getParameter("publishKey");//$NON-NLS-1$
84
if (PublisherUtil.checkPublisherKey(publishKey)) {
85             String JavaDoc jndiName = request.getParameter("jndiName");//$NON-NLS-1$
86
String JavaDoc jdbcDriver = request.getParameter("jdbcDriver");//$NON-NLS-1$
87
String JavaDoc jdbcUrl = request.getParameter("jdbcUrl");//$NON-NLS-1$
88
String JavaDoc jdbcUserId = request.getParameter("jdbcUserId");//$NON-NLS-1$
89
String JavaDoc jdbcPassword = request.getParameter("jdbcPassword");//$NON-NLS-1$
90
boolean overwrite = Boolean.valueOf(request.getParameter("overwrite")).booleanValue(); //$NON-NLS-1$
91

92             if (jndiName != null && !jndiName.equals("")) { //$NON-NLS-1$
93
JBossDatasourceAdmin jbossDSAdmin = new JBossDatasourceAdmin();
94                 // jbossDSAdmin.setApplicationRoot(jbossDeployDir);
95
// jbossDSAdmin.setWebApplicationName(webAppName);
96
Iterator JavaDoc keyIterator = jbossDSAdmin.listContainerDataSources().keySet().iterator();
97                 boolean exists = false;
98                 while (keyIterator.hasNext()) {
99                     String JavaDoc key = (String JavaDoc) keyIterator.next();
100                     if (jndiName.equals(key)) {
101                         exists = true;
102                         break;
103                     }
104                 }
105                 if (!exists) {
106                     DataSourceInfo simpleJNDIDS = new DataSourceInfo(jndiName, jndiName, "javax.sql.DataSource"); //$NON-NLS-1$
107
simpleJNDIDS.setName(jndiName);
108                     simpleJNDIDS.setDriver(jdbcDriver);
109                     simpleJNDIDS.setUrl(jdbcUrl);
110                     simpleJNDIDS.setUserId(jdbcUserId);
111                     simpleJNDIDS.setPassword(jdbcPassword);
112                     jbossDSAdmin.saveDataSource(simpleJNDIDS, false);
113                 }
114             }
115
116             String JavaDoc solutionPath = PentahoSystem.getApplicationContext().getSolutionPath(""); //$NON-NLS-1$
117

118             DiskFileUpload fu = new DiskFileUpload();
119             // If file size exceeds, a FileUploadException will be thrown
120
fu.setSizeMax(10000000);
121
122             try {
123                 PentahoSystem.systemEntryPoint();
124                 
125                 List JavaDoc fileItems = fu.parseRequest(request);
126                 Iterator JavaDoc itr = fileItems.iterator();
127
128                 while (itr.hasNext() && status == ISolutionRepository.FILE_ADD_SUCCESSFUL) {
129                     FileItem fi = (FileItem) itr.next();
130
131                     File JavaDoc fNew = File.createTempFile(fi.getName(), ".tmp"); //$NON-NLS-1$
132
fNew.deleteOnExit();
133                     fi.write(fNew);
134
135                     status = repository.addSolutionFile(solutionPath, publishPath, fi.getName(), fNew, overwrite);
136                 }
137             } catch (Exception JavaDoc e) {
138                 status = ISolutionRepository.FILE_ADD_FAILED;
139                 e.printStackTrace();
140             } finally {
141                 PentahoSystem.systemExitPoint();
142             }
143             if (status == ISolutionRepository.FILE_ADD_SUCCESSFUL) {
144                 response.getWriter().println(status);
145             } else if (status == ISolutionRepository.FILE_EXISTS) {
146                 response.getWriter().println(status);
147             } else {
148                 response.getWriter().println(status);
149             }
150         } else {
151             status = ISolutionRepository.FILE_ADD_INVALID_PUBLISH_PASSWORD;
152             response.getWriter().println(status);
153         }
154     }
155 }
156
Popular Tags