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 March 7, 2005
014: * @author Doug Moran
015: *
016: */
017:
018: package org.pentaho.ui.servlet;
019:
020: import java.io.IOException;
021: import java.io.InputStream;
022:
023: import javax.servlet.ServletException;
024: import javax.servlet.ServletOutputStream;
025: import javax.servlet.http.HttpServletRequest;
026: import javax.servlet.http.HttpServletResponse;
027: import org.jfree.io.IOUtils;
028: import org.apache.commons.logging.Log;
029: import org.apache.commons.logging.LogFactory;
030: import org.pentaho.core.repository.IContentItem;
031: import org.pentaho.core.repository.IContentRepository;
032: import org.pentaho.core.session.IPentahoSession;
033: import org.pentaho.core.system.PentahoSystem;
034: import org.pentaho.messages.Messages;
035: import org.pentaho.messages.util.LocaleHelper;
036:
037: public class GetContent extends ServletBase {
038:
039: /**
040: *
041: */
042: private static final long serialVersionUID = 9019152264205996418L;
043:
044: private static final Log logger = LogFactory
045: .getLog(GetContent.class);
046:
047: public Log getLogger() {
048: return logger;
049: }
050:
051: public GetContent() {
052: super ();
053: }
054:
055: protected void setupSession(IPentahoSession userSession) {
056: PentahoSystem.sessionStartup(userSession);
057: }
058:
059: protected void removeUserSession(IPentahoSession userSession) {
060: userSession.destroy();
061: }
062:
063: protected void doPost(HttpServletRequest request,
064: HttpServletResponse response) throws ServletException,
065: IOException {
066: doGet(request, response);
067: }
068:
069: protected void doGet(HttpServletRequest request,
070: HttpServletResponse response) throws ServletException,
071: IOException {
072:
073: response.setCharacterEncoding(LocaleHelper.getSystemEncoding());
074:
075: PentahoSystem.systemEntryPoint();
076: try {
077: IPentahoSession userSession = getPentahoSession(request);
078:
079: String id = request.getParameter("id"); //$NON-NLS-1$
080: if (id == null) {
081: returnError(
082: response,
083: Messages
084: .getErrorString("GetContent.ERROR_0001_ID_PARAMETER_EMPTY")); //$NON-NLS-1$
085: return;
086: }
087:
088: IContentRepository contentRepos = PentahoSystem
089: .getContentRepository(userSession);
090: if (contentRepos == null) {
091: returnError(
092: response,
093: Messages
094: .getString("GetContent.ERROR_0002_CONTENT_REPOS_UNAVAILABLE")); //$NON-NLS-1$
095: return;
096: }
097:
098: try {
099: IContentItem contentItem = contentRepos
100: .getContentItemById(id);
101: if (contentItem == null) {
102: returnError(
103: response,
104: Messages
105: .getString(
106: "GetContent.ERROR_0005_CONTENT_NOT_FOUND", id)); //$NON-NLS-1$
107: return;
108: }
109:
110: String mimetype = contentItem.getMimeType();
111: if ((mimetype == null) || (mimetype.length() < 1)) {
112: mimetype = request.getParameter("mimetype"); //$NON-NLS-1$
113: }
114:
115: // Set it if we know what it is
116: if ((mimetype != null) && (mimetype.length() > 0)) {
117: response.setContentType(mimetype);
118: }
119: if (!(mimetype.equalsIgnoreCase("text/html"))) { //$NON-NLS-1$
120: response
121: .setHeader(
122: "Content-Disposition", "inline; filename=\"" + contentItem.getTitle() + "\""); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
123: }
124:
125: // Send it back
126: InputStream inStr = contentItem.getInputStream();
127: ServletOutputStream outStr = response.getOutputStream();
128:
129: try {
130: IOUtils.getInstance().copyStreams(inStr, outStr);
131: } finally {
132: inStr.close();
133: outStr.close();
134: }
135: } catch (Throwable t) {
136: error(Messages
137: .getString("GetContent.ERROR_0003_CONTENT_READ_ERROR")); //$NON-NLS-1$
138: }
139:
140: } finally {
141: PentahoSystem.systemExitPoint();
142: }
143: }
144:
145: void returnError(HttpServletResponse response, String message) {
146: error(message);
147:
148: response.setContentType("text/plain"); //$NON-NLS-1$
149: try {
150: response
151: .getWriter()
152: .println(
153: Messages
154: .getString("GetContent.ERROR_0004_RETURN_MESSAGE") + message); //$NON-NLS-1$
155: } catch (Throwable t) {
156: }
157: }
158:
159: }
|