001: /*
002: * Copyright 2006 Pentaho Corporation. All rights reserved.
003: * This software was developed by Pentaho Corporation and is provided under the terms
004: * of the Mozilla Public License, Version 1.1, or any later version. You may not use
005: * this file except in compliance with the license. If you need a copy of the license,
006: * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
007: * BI Platform. The Initial Developer is Pentaho Corporation.
008: *
009: * Software distributed under the Mozilla Public License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
011: * the license for the specific language governing your rights and limitations.
012: *
013: * @created October 24, 2006
014: * @author Michael D'Amour
015: *
016: */
017:
018: package org.pentaho.ui.servlet;
019:
020: import java.io.IOException;
021: import java.util.Iterator;
022: import java.util.List;
023:
024: import javax.servlet.ServletException;
025: import javax.servlet.http.HttpServletRequest;
026: import javax.servlet.http.HttpServletResponse;
027:
028: import org.apache.commons.fileupload.DiskFileUpload;
029: import org.apache.commons.fileupload.FileItem;
030: import org.apache.commons.logging.Log;
031: import org.apache.commons.logging.LogFactory;
032: import org.pentaho.core.admin.datasources.DataSourceInfo;
033: import org.pentaho.core.admin.datasources.jboss.JBossDatasourceAdmin;
034: import org.pentaho.core.repository.ISolutionRepository;
035: import org.pentaho.core.session.IPentahoSession;
036: import org.pentaho.core.system.PentahoSystem;
037: import org.pentaho.core.util.PublisherUtil;
038: import org.pentaho.core.util.UIUtil;
039: import org.pentaho.messages.util.LocaleHelper;
040:
041: public class RepositoryFilePublisher extends ServletBase {
042:
043: /**
044: *
045: */
046: private static final long serialVersionUID = 9019152264205996418L;
047:
048: private static final Log logger = LogFactory
049: .getLog(GetContent.class);
050:
051: public Log getLogger() {
052: return logger;
053: }
054:
055: public RepositoryFilePublisher() {
056: super ();
057: }
058:
059: protected void doPost(HttpServletRequest request,
060: HttpServletResponse response) throws ServletException,
061: IOException {
062: doGet(request, response);
063: }
064:
065: protected void doGet(HttpServletRequest request,
066: HttpServletResponse response) throws ServletException,
067: IOException {
068: response.setCharacterEncoding(LocaleHelper.getSystemEncoding());
069: IPentahoSession userSession = UIUtil.getPentahoSession(request);
070: ISolutionRepository repository = PentahoSystem
071: .getSolutionRepository(userSession);
072: int status = ISolutionRepository.FILE_ADD_SUCCESSFUL;
073:
074: // Fix the publish path to prevent problems with the RDBMS repository.
075: String publishPath = request.getParameter("publishPath"); //$NON-NLS-1$
076: if ((publishPath != null)
077: && (publishPath.endsWith("/") || publishPath.endsWith("\\"))) { //$NON-NLS-1$ //$NON-NLS-2$
078: publishPath = publishPath.substring(0,
079: publishPath.length() - 1);
080: }
081:
082: String publishKey = request.getParameter("publishKey");//$NON-NLS-1$
083: if (PublisherUtil.checkPublisherKey(publishKey)) {
084: String jndiName = request.getParameter("jndiName");//$NON-NLS-1$
085: String jdbcDriver = request.getParameter("jdbcDriver");//$NON-NLS-1$
086: String jdbcUrl = request.getParameter("jdbcUrl");//$NON-NLS-1$
087: String jdbcUserId = request.getParameter("jdbcUserId");//$NON-NLS-1$
088: String jdbcPassword = request.getParameter("jdbcPassword");//$NON-NLS-1$
089: boolean overwrite = Boolean.valueOf(
090: request.getParameter("overwrite")).booleanValue(); //$NON-NLS-1$
091:
092: if (jndiName != null && !jndiName.equals("")) { //$NON-NLS-1$
093: JBossDatasourceAdmin jbossDSAdmin = new JBossDatasourceAdmin();
094: // jbossDSAdmin.setApplicationRoot(jbossDeployDir);
095: // jbossDSAdmin.setWebApplicationName(webAppName);
096: Iterator keyIterator = jbossDSAdmin
097: .listContainerDataSources().keySet().iterator();
098: boolean exists = false;
099: while (keyIterator.hasNext()) {
100: String key = (String) keyIterator.next();
101: if (jndiName.equals(key)) {
102: exists = true;
103: break;
104: }
105: }
106: if (!exists) {
107: DataSourceInfo simpleJNDIDS = new DataSourceInfo(
108: jndiName, jndiName, "javax.sql.DataSource"); //$NON-NLS-1$
109: simpleJNDIDS.setName(jndiName);
110: simpleJNDIDS.setDriver(jdbcDriver);
111: simpleJNDIDS.setUrl(jdbcUrl);
112: simpleJNDIDS.setUserId(jdbcUserId);
113: simpleJNDIDS.setPassword(jdbcPassword);
114: jbossDSAdmin.saveDataSource(simpleJNDIDS, false);
115: }
116: }
117:
118: String solutionPath = PentahoSystem.getApplicationContext()
119: .getSolutionPath(""); //$NON-NLS-1$
120:
121: DiskFileUpload fu = new DiskFileUpload();
122: // If file size exceeds, a FileUploadException will be thrown
123: fu.setSizeMax(10000000);
124:
125: try {
126: PentahoSystem.systemEntryPoint();
127:
128: List fileItems = fu.parseRequest(request);
129: Iterator itr = fileItems.iterator();
130:
131: while (itr.hasNext()
132: && status == ISolutionRepository.FILE_ADD_SUCCESSFUL) {
133: FileItem fi = (FileItem) itr.next();
134:
135: status = repository.addSolutionFile(solutionPath,
136: publishPath, fi.getName(), fi.get(),
137: overwrite);
138: }
139: } catch (Exception e) {
140: status = ISolutionRepository.FILE_ADD_FAILED;
141: e.printStackTrace();
142: } finally {
143: PentahoSystem.systemExitPoint();
144: }
145: if (status == ISolutionRepository.FILE_ADD_SUCCESSFUL) {
146: response.getWriter().println(status);
147: } else if (status == ISolutionRepository.FILE_EXISTS) {
148: response.getWriter().println(status);
149: } else {
150: response.getWriter().println(status);
151: }
152: } else {
153: status = ISolutionRepository.FILE_ADD_INVALID_PUBLISH_PASSWORD;
154: response.getWriter().println(status);
155: }
156: }
157: }
|