X-Git-Url: https://git.argeo.org/?a=blobdiff_plain;f=org.argeo.eclipse.ui.rap%2Fsrc%2Forg%2Fargeo%2Feclipse%2Fui%2Fspecific%2FOpenFileService.java;h=4630d63e6393d95ce7d5cdf05b7ae78ed5624b0b;hb=c53fec78daddb69c489686844188036b04e1615a;hp=e48babf0bba7df111a64f2d8ccf9cf6c57e00c5e;hpb=e71b1737815da9f0fdaff1ee1c69773a8c073ef1;p=lgpl%2Fargeo-commons.git diff --git a/org.argeo.eclipse.ui.rap/src/org/argeo/eclipse/ui/specific/OpenFileService.java b/org.argeo.eclipse.ui.rap/src/org/argeo/eclipse/ui/specific/OpenFileService.java index e48babf0b..4630d63e6 100644 --- a/org.argeo.eclipse.ui.rap/src/org/argeo/eclipse/ui/specific/OpenFileService.java +++ b/org.argeo.eclipse.ui.rap/src/org/argeo/eclipse/ui/specific/OpenFileService.java @@ -1,95 +1,96 @@ -/* - * Copyright (C) 2007-2012 Argeo GmbH - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ package org.argeo.eclipse.ui.specific; -import java.io.File; +import static org.argeo.eclipse.ui.util.SingleSourcingConstants.FILE_SCHEME; +import static org.argeo.eclipse.ui.util.SingleSourcingConstants.SCHEME_HOST_SEPARATOR; + import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; -import org.apache.commons.io.FileUtils; -import org.argeo.ArgeoException; +import org.argeo.eclipse.ui.EclipseUiUtils; +import org.argeo.eclipse.ui.util.SingleSourcingConstants; import org.eclipse.rap.rwt.service.ServiceHandler; /** - * Basic Default service handler that retrieves a file on the server file system - * using its absolute path and forwards it to the end user browser. Rap - * specific. + * RWT specific Basic Default service handler that retrieves a file on the + * server file system using its absolute path and forwards it to the end user + * browser. * - * Clients might extend to provide context specific services (to open files from - * a JCR repository for instance) + * Clients might extend to provide context specific services */ public class OpenFileService implements ServiceHandler { - public final static String PARAM_FILE_NAME = "param.fileName"; - public final static String PARAM_FILE_URI = "param.fileURI"; - - public final static String SCHEME_HOST_SEPARATOR = "://"; - public final static String FILE_SCHEME = "file"; - public OpenFileService() { } - public void service(HttpServletRequest request, HttpServletResponse response) - throws IOException, ServletException { - String fileName = request.getParameter(PARAM_FILE_NAME); - String uri = request.getParameter(PARAM_FILE_URI); + public void service(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { + String fileName = request.getParameter(SingleSourcingConstants.PARAM_FILE_NAME); + String uri = request.getParameter(SingleSourcingConstants.PARAM_FILE_URI); + + // Use buffered array to directly write the stream? + if (!uri.startsWith(SingleSourcingConstants.FILE_SCHEME)) + throw new IllegalArgumentException( + "Open file service can only handle files that are on the server file system"); // Set the Metadata - response.setContentType("application/octet-stream"); - response.setContentLength((int) getFileLength(uri)); - if (fileName == null || "".equals(fileName.trim())) + response.setContentLength((int) getFileSize(uri)); + if (EclipseUiUtils.isEmpty(fileName)) fileName = getFileName(uri); + response.setContentType(getMimeType(uri, fileName)); String contentDisposition = "attachment; filename=\"" + fileName + "\""; response.setHeader("Content-Disposition", contentDisposition); - response.getOutputStream().write(getFileAsByteArray(uri)); - // FileUtils.readFileToByteArray(new File(path)) - } + // Useless for current use + // response.setHeader("Content-Transfer-Encoding", "binary"); + // response.setHeader("Pragma", "no-cache"); + // response.setHeader("Cache-Control", "no-cache, must-revalidate"); - protected byte[] getFileAsByteArray(String uri) { - if (uri.startsWith(FILE_SCHEME)) { - try { - return FileUtils.readFileToByteArray(new File( - getFilePathFromUri(uri))); - } catch (IOException ioe) { - throw new ArgeoException("Error while getting the file at " - + uri, ioe); - } - } - return null; + Path path = Paths.get(getAbsPathFromUri(uri)); + Files.copy(path, response.getOutputStream()); + + // FIXME we always use temporary files for the time being. + // the deleteOnClose file only works when the JVM is closed so we + // explicitly delete to avoid overloading the server + if (path.startsWith("/tmp")) + path.toFile().delete(); } - protected long getFileLength(String uri) { - if (uri.startsWith(FILE_SCHEME)) { - return new File(getFilePathFromUri(uri)).length(); + protected long getFileSize(String uri) throws IOException { + if (uri.startsWith(SingleSourcingConstants.FILE_SCHEME)) { + Path path = Paths.get(getAbsPathFromUri(uri)); + return Files.size(path); } return -1l; } protected String getFileName(String uri) { - if (uri.startsWith(FILE_SCHEME)) { - return new File(getFilePathFromUri(uri)).getName(); + if (uri.startsWith(SingleSourcingConstants.FILE_SCHEME)) { + Path path = Paths.get(getAbsPathFromUri(uri)); + return path.getFileName().toString(); } return null; } - private String getFilePathFromUri(String uri) { - return uri.substring((FILE_SCHEME + SCHEME_HOST_SEPARATOR).length()); + private String getAbsPathFromUri(String uri) { + if (uri.startsWith(FILE_SCHEME)) + return uri.substring((FILE_SCHEME + SCHEME_HOST_SEPARATOR).length()); + // else if (uri.startsWith(JCR_SCHEME)) + // return uri.substring((JCR_SCHEME + SCHEME_HOST_SEPARATOR).length()); + else + throw new IllegalArgumentException("Unknown URI prefix for" + uri); } -} \ No newline at end of file + protected String getMimeType(String uri, String fileName) throws IOException { + if (uri.startsWith(FILE_SCHEME)) { + Path path = Paths.get(getAbsPathFromUri(uri)); + String mimeType = Files.probeContentType(path); + if (EclipseUiUtils.notEmpty(mimeType)) + return mimeType; + } + return "application/octet-stream"; + } +}