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 Jul 26, 2005
014: * @author Gretchen Moran
015: *
016: */
017:
018: package org.pentaho.ui.servlet;
019:
020: import java.io.IOException;
021: import java.io.InputStream;
022: import java.io.OutputStream;
023:
024: import javax.servlet.ServletException;
025: import javax.servlet.http.HttpServletRequest;
026: import javax.servlet.http.HttpServletResponse;
027:
028: import org.apache.commons.logging.Log;
029: import org.apache.commons.logging.LogFactory;
030: import org.pentaho.core.repository.ISolutionRepository;
031: import org.pentaho.core.system.PentahoSystem;
032: import org.pentaho.core.util.UIUtil;
033: import org.pentaho.messages.Messages;
034: import org.pentaho.messages.util.LocaleHelper;
035:
036: public class GetMondrianModel extends ServletBase {
037:
038: private static final long serialVersionUID = 1L;
039:
040: protected void doGet(HttpServletRequest arg0,
041: HttpServletResponse arg1) throws ServletException,
042: IOException {
043: doPost(arg0, arg1);
044: }
045:
046: private static final Log logger = LogFactory
047: .getLog(GetMondrianModel.class);
048:
049: public Log getLogger() {
050: return logger;
051: }
052:
053: protected void doPost(HttpServletRequest request,
054: HttpServletResponse response) throws ServletException,
055: IOException {
056:
057: // TODO perform any authorization here...
058: getPentahoSession(request);
059:
060: String model = null;
061: if (request.getParameter("model") != null) { //$NON-NLS-1$
062: model = request.getParameter("model"); //$NON-NLS-1$
063: }
064:
065: if (model == null) {
066: error(Messages
067: .getErrorString("MondrianModel.ERROR_0001_MODEL_PARAMETER_MISSING")); //$NON-NLS-1$
068: return;
069: }
070:
071: if (!model.endsWith(".mondrian.xml")) { //$NON-NLS-1$
072: error(Messages.getErrorString(
073: "MondrianModel.ERROR_0002_INVALID_FILE", model)); //$NON-NLS-1$
074: return;
075: }
076:
077: // Open the input and output streams
078: ISolutionRepository repository = PentahoSystem
079: .getSolutionRepository(UIUtil
080: .getPentahoSession(request));
081: if (repository != null) {
082: String mimeType = "text/xml"; //$NON-NLS-1$
083: response.setContentType(mimeType);
084: response.setCharacterEncoding(LocaleHelper
085: .getSystemEncoding());
086:
087: InputStream in = repository.getResourceInputStream(model,
088: true);
089: OutputStream out = response.getOutputStream();
090:
091: try {
092: // Copy the contents of the file to the output stream
093: byte[] buf = new byte[2048];
094: int count = 0;
095: int length = 0;
096: while ((count = in.read(buf)) >= 0) {
097: out.write(buf, 0, count);
098: length += count;
099: }
100: response.setContentLength(length);
101: } finally {
102: in.close();
103: out.close();
104: }
105: } else {
106: error(Messages
107: .getErrorString("MondrianModel.ERROR_0004_INVALID_REPOSITORY")); //$NON-NLS-1$
108: }
109: }
110:
111: }
|