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: package org.pentaho.ui.servlet;
018:
019: import java.io.IOException;
020: import java.io.InputStream;
021: import java.io.OutputStream;
022:
023: import javax.servlet.ServletException;
024: import javax.servlet.http.HttpServletRequest;
025: import javax.servlet.http.HttpServletResponse;
026:
027: import org.apache.commons.logging.Log;
028: import org.apache.commons.logging.LogFactory;
029: import org.pentaho.core.repository.ISolutionRepository;
030: import org.pentaho.core.session.IPentahoSession;
031: import org.pentaho.core.system.PentahoSystem;
032: import org.pentaho.messages.Messages;
033: import org.pentaho.messages.util.LocaleHelper;
034: import org.pentaho.util.StringUtil;
035:
036: public class GetResource extends ServletBase {
037: private static final long serialVersionUID = 1L;
038:
039: protected void doGet(HttpServletRequest arg0,
040: HttpServletResponse arg1) throws ServletException,
041: IOException {
042: doPost(arg0, arg1);
043: }
044:
045: private static final Log logger = LogFactory
046: .getLog(GetResource.class);
047:
048: public Log getLogger() {
049: return logger;
050: }
051:
052: protected void doPost(HttpServletRequest request,
053: HttpServletResponse response) throws ServletException,
054: IOException {
055: // TODO perform any authorization here...
056: // TODO support caching
057: IPentahoSession session = getPentahoSession(request);
058: String resource = request.getParameter("resource"); //$NON-NLS-1$
059: //String expires = request.getParameter("expires"); //$NON-NLS-1$
060: //if ( null == expires )
061: //{
062: // expires = "0";//$NON-NLS-1$
063: //}
064: if (resource == null
065: || StringUtil
066: .doesPathContainParentPathSegment(resource)) {
067: error(Messages
068: .getErrorString("GetResource.ERROR_0001_RESOURCE_PARAMETER_MISSING")); //$NON-NLS-1$
069: response
070: .setStatus(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
071: return;
072: }
073: String resourcePath = null;
074: if (resource.endsWith(".xsl")) { //$NON-NLS-1$
075: resourcePath = "system/custom/xsl/" + resource; //$NON-NLS-1$
076: }
077: if (resource.endsWith(".mondrian.xml")) { //$NON-NLS-1$
078: resourcePath = resource;
079: }
080: if (resource.endsWith(".jpg") || resource.endsWith(".jpeg") || resource.endsWith(".gif") || resource.endsWith(".png") || resource.endsWith(".bmp")) { //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
081: resourcePath = resource;
082: }
083: if (resource.endsWith(".properties")) { //$NON-NLS-1$
084: resourcePath = resource;
085: }
086: if (resource.endsWith(".jar")) { //$NON-NLS-1$
087: resourcePath = resource;
088: }
089: if (resourcePath == null) {
090: error(Messages.getErrorString(
091: "GetResource.ERROR_0002_INVALID_FILE", resource)); //$NON-NLS-1$
092: response
093: .setStatus(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
094: return;
095: }
096: ISolutionRepository repository = PentahoSystem
097: .getSolutionRepository(session);
098: InputStream in = repository.getResourceInputStream(
099: resourcePath, true);
100: if (in == null) {
101: error(Messages
102: .getErrorString(
103: "GetResource.ERROR_0003_RESOURCE_MISSING", resourcePath)); //$NON-NLS-1$
104: response
105: .setStatus(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
106: return;
107: }
108: String mimeType = getServletContext().getMimeType(resourcePath);
109: String resourceName = resourcePath;
110: if (resourcePath.indexOf("/") != -1) { //$NON-NLS-1$
111: resourceName = resourcePath.substring(resourcePath
112: .lastIndexOf("/") + 1); //$NON-NLS-1$
113: }
114: response
115: .setHeader(
116: "content-disposition", "attachment;filename=" + resourceName); //$NON-NLS-1$ //$NON-NLS-2$
117: if ((null == mimeType) || (mimeType.length() <= 0)) {
118: // Hard coded to PNG because BIRT does not give us a mime type at
119: // all...
120: response.setContentType("image/png"); //$NON-NLS-1$
121: } else {
122: response.setContentType(mimeType);
123: }
124: response.setCharacterEncoding(LocaleHelper.getSystemEncoding());
125: response.setHeader("expires", "0"); //$NON-NLS-1$ //$NON-NLS-2$
126: // Open the input and output streams
127: OutputStream out = response.getOutputStream();
128: try {
129: // Copy the contents of the file to the output stream
130: byte[] buf = new byte[1024];
131: int count = 0;
132: int totalBytes = 0;
133: while ((count = in.read(buf)) >= 0) {
134: out.write(buf, 0, count);
135: totalBytes += count;
136: }
137: response.setContentLength(totalBytes);
138: } finally {
139: in.close();
140: out.close();
141: }
142: }
143: }
|