/** * OLAT - Online Learning and Training<br> * http://www.olat.org * <p> * Licensed under the Apache License, Version 2.0 (the "License"); <br> * you may not use this file except in compliance with the License.<br> * You may obtain a copy of the License at * <p> * http://www.apache.org/licenses/LICENSE-2.0 * <p> * Unless required by applicable law or agreed to in writing,<br> * software distributed under the License is distributed on an "AS IS" BASIS, <br> * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. <br> * See the License for the specific language governing permissions and <br> * limitations under the License. * <p> * Copyright (c) since 2004 at Multimedia- & E-Learning Services (MELS),<br> * University of Zurich, Switzerland. * <hr> * <a href="http://www.openolat.org"> * OpenOLAT - Online Learning and Training</a><br> * This file has been modified by the OpenOLAT community. Changes are licensed * under the Apache 2.0 license as the original file. * <p> */ package org.olat.core.util.vfs; import java.io.InputStream; import javax.servlet.http.HttpServletResponse; import org.olat.core.CoreSpringFactory; import org.olat.core.commons.modules.bc.FilesInfoMBean; import org.olat.core.gui.media.MediaResource; import org.olat.core.util.StringHelper; import org.olat.core.util.WebappHelper; public class VFSMediaResource implements MediaResource { private static final String MIME_TYPE_OCTET_STREAM = "application/octet-stream"; //use this pseudo mime-type to force download on ie 6 public static final String MIME_TYPE_FORCE_DOWNLOAD = "application/force-download"; private VFSLeaf vfsLeaf; private String encoding; boolean unknownMimeType = false; private boolean downloadable = false; private FilesInfoMBean filesInfoMBean; public VFSMediaResource(VFSLeaf vfsLeaf) { this.vfsLeaf = vfsLeaf; this.filesInfoMBean = (FilesInfoMBean) CoreSpringFactory.getBean(FilesInfoMBean.class.getCanonicalName()); } @Override public boolean acceptRanges() { return true; } @Override public String getContentType() { String mimeType; if(downloadable) { mimeType = WebappHelper.getMimeType(vfsLeaf.getName()); //html, xhtml and javascript are set to force download if (mimeType == null || "text/html".equals(mimeType) || "application/xhtml+xml".equals(mimeType) || "application/javascript".equals(mimeType) || "image/svg+xml".equals(mimeType)) { mimeType = MIME_TYPE_FORCE_DOWNLOAD; unknownMimeType = true; } else if (encoding != null) { mimeType = mimeType + ";charset=" + encoding; } } else { mimeType = WebappHelper.getMimeType(vfsLeaf.getName()); if (mimeType == null) { mimeType = MIME_TYPE_OCTET_STREAM; unknownMimeType = true; } else { // if any encoding is set, append it for the browser if (encoding != null) { mimeType = mimeType + ";charset=" + encoding; unknownMimeType = false; } } } return mimeType; } @Override public Long getSize() { long size = vfsLeaf.getSize(); return (size == VFSConstants.UNDEFINED) ? null : new Long(size); } @Override public InputStream getInputStream() { filesInfoMBean.logDownload(getSize()); return vfsLeaf.getInputStream(); } @Override public Long getLastModified() { long lastModified = vfsLeaf.getLastModified(); return (lastModified == VFSConstants.UNDEFINED) ? null : new Long(lastModified); } /** * @see org.olat.core.gui.media.MediaResource#prepare(javax.servlet.http.HttpServletResponse) */ @Override public void prepare(HttpServletResponse hres) { String filename = StringHelper.urlEncodeUTF8(vfsLeaf.getName()); if (unknownMimeType) { hres.setHeader("Content-Disposition", "attachment; filename*=UTF-8''" + filename); } else { hres.setHeader("Content-Disposition", "filename*=UTF-8''" + filename); } } @Override public void release() { // nothing to do here } /** * if set, then content type will be modified such that the encoding is * appended, e.g. "text/html;charset=utf-8". Makes sense only for non-binary * data * * @param encoding e.g. "iso-8859-1", or "utf-8" */ public void setEncoding(String encoding) { this.encoding = encoding; } public void setDownloadable(boolean downloadable) { this.downloadable = downloadable; } }