001: /***************************************************************
002: * This file is part of the [fleXive](R) project.
003: *
004: * Copyright (c) 1999-2007
005: * UCS - unique computing solutions gmbh (http://www.ucs.at)
006: * All rights reserved
007: *
008: * The [fleXive](R) project is free software; you can redistribute
009: * it and/or modify it under the terms of the GNU General Public
010: * License as published by the Free Software Foundation;
011: * either version 2 of the License, or (at your option) any
012: * later version.
013: *
014: * The GNU General Public License can be found at
015: * http://www.gnu.org/copyleft/gpl.html.
016: * A copy is found in the textfile GPL.txt and important notices to the
017: * license from the author are found in LICENSE.txt distributed with
018: * these libraries.
019: *
020: * This library is distributed in the hope that it will be useful,
021: * but WITHOUT ANY WARRANTY; without even the implied warranty of
022: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
023: * GNU General Public License for more details.
024: *
025: * For further information about UCS - unique computing solutions gmbh,
026: * please see the company website: http://www.ucs.at
027: *
028: * For further information about [fleXive](R), please see the
029: * project website: http://www.flexive.org
030: *
031: *
032: * This copyright notice MUST APPEAR in all copies of the file!
033: ***************************************************************/package com.flexive.war.servlet;
034:
035: import com.flexive.shared.value.BinaryDescriptor;
036: import com.flexive.war.beans.admin.content.ContentEditorBean;
037:
038: import javax.servlet.*;
039: import javax.servlet.http.HttpServletRequest;
040: import javax.servlet.http.HttpServletResponse;
041: import java.io.IOException;
042: import java.net.URLDecoder;
043:
044: /**
045: * Donwload servlet used by the ContentEditorBean.
046: *
047: * @author Gregor Schober (gregor.schober@flexive.com), UCS - unique computing solutions gmbh (http://www.ucs.at)
048: */
049: public class CeFileDownload implements Servlet {
050:
051: private ServletConfig servletConfig = null;
052:
053: public void init(ServletConfig servletConfig)
054: throws ServletException {
055: this .servletConfig = servletConfig;
056: }
057:
058: public ServletConfig getServletConfig() {
059: return servletConfig;
060: }
061:
062: public String getServletInfo() {
063: return this .getClass().getName();
064: }
065:
066: public void destroy() {
067: // nothing to do
068: }
069:
070: /**
071: * Downloads a file specified by the currently loaded content beans in the content editor and a xpath
072: * that is provided in the url.
073: * The "If-Modified-Since" tag is not handled since we do not want any caching when downloading the files.
074: *
075: * @param servletRequest the servlet request
076: * @param servletResponse the servlet response
077: * @throws ServletException if a servlet error occurs
078: * @throws IOException if a io error occurs
079: */
080: public void service(ServletRequest servletRequest,
081: ServletResponse servletResponse) throws ServletException,
082: IOException {
083: final HttpServletRequest request = (HttpServletRequest) servletRequest;
084: final HttpServletResponse response = (HttpServletResponse) servletResponse;
085: final ContentEditorBean ceb = ContentEditorBean.getSingleton()
086: .getInstance(request);
087: if (ceb == null) {
088: response
089: .sendError(
090: HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
091: "No active contentEditor bean found in the session");
092: return;
093: }
094:
095: String downloadFileName = null;
096: String xpath = null;
097: try {
098: // Example URI: '/flexive/cefiledownload/xpath:|SUBGROUP[1]|FILE[1]/demo.jpg'
099: String uri[] = URLDecoder.decode(request.getRequestURI(),
100: "UTF8").split("/");
101: downloadFileName = uri[uri.length - 1].contains(":") ? null
102: : uri[uri.length - 1];
103: for (String param : uri) {
104: try {
105: String paramSplit[] = param.split(":");
106: if (paramSplit[0].equalsIgnoreCase("xpath")) {
107: xpath = paramSplit[1].replace('|', '/');
108: }
109: } catch (Throwable t) {
110: System.err.println(this .getClass()
111: + ": ignoring url parameter " + param);
112: }
113: }
114: } catch (Throwable t) {
115: System.err.println(this .getClass() + " ERROR: "
116: + t.getMessage());
117: }
118:
119: // Send the data
120: ServletOutputStream sos = null;
121: try {
122: BinaryDescriptor bd = ((BinaryDescriptor) ceb.getContent()
123: .getValue(xpath).getBestTranslation());
124: response.setContentType(bd.getMimeType());
125: response.setContentLength((int) bd.getSize());
126: response.setHeader("Content-Disposition",
127: "attachment; filename=\"" + bd.getName() + "\";");
128: sos = response.getOutputStream();
129: bd.download(sos);
130: } catch (Throwable t) {
131: response.sendError(
132: HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
133: "Failed to send data for file '" + downloadFileName
134: + "': " + t.getMessage());
135: } finally {
136: if (sos != null)
137: sos.close();
138: }
139:
140: }
141: }
|