001: package com.sun.portal.app.filesharing.servlet;
002:
003: import com.sun.portal.app.filesharing.repo.RepoException;
004: import com.sun.portal.app.filesharing.repo.RepoFactory;
005: import com.sun.portal.app.filesharing.repo.RepoItem;
006: import com.sun.portal.app.filesharing.repo.Repository;
007: import com.sun.portal.app.filesharing.util.IOUtils;
008: import com.sun.portal.app.filesharing.util.InfoResolver;
009: import com.sun.portal.app.filesharing.util.InfoResolverFactory;
010: import com.sun.portal.app.filesharing.util.LogUtils;
011: import com.sun.portal.app.filesharing.util.ParameterCheck;
012:
013: import javax.servlet.ServletException;
014: import javax.servlet.http.HttpServlet;
015: import javax.servlet.http.HttpServletRequest;
016: import javax.servlet.http.HttpServletResponse;
017: import java.io.IOException;
018: import java.io.InputStream;
019: import java.io.OutputStream;
020: import java.util.logging.Logger;
021: import java.util.logging.Level;
022:
023: public class FileDownloadServlet extends HttpServlet {
024:
025: public static final String FILE_PARAM = "file";
026:
027: private static final String DOWNLOAD_MIME = "application/bin";
028:
029: private static Logger _logger = LogUtils.getInstance().getLogger(
030: FileDownloadServlet.class.getName());
031:
032: protected void doGet(HttpServletRequest req, HttpServletResponse res)
033: throws ServletException, IOException {
034: String filePath = ParameterCheck.decodeCharset(req
035: .getParameter(FILE_PARAM), "UTF-8");
036: InfoResolver info = InfoResolverFactory.getFactory()
037: .getInfoResolver(req, res);
038: String userId = info.getUserId();
039: String repositoryId = info.getRepositoryId();
040:
041: if (filePath == null) {
042: _logger.log(Level.WARNING, "'file' parameter is NULL");
043: res.sendError(HttpServletResponse.SC_BAD_REQUEST);
044: return;
045: }
046:
047: if (userId == null) {
048: _logger.log(Level.WARNING, "userId is NULL");
049: res.sendError(HttpServletResponse.SC_FORBIDDEN);
050: return;
051: }
052:
053: if (repositoryId == null) {
054: _logger.log(Level.WARNING, "repositoryId is NULL");
055: res.sendError(HttpServletResponse.SC_FORBIDDEN);
056: return;
057: }
058:
059: if (!info.isDownloadAllow()) {
060: _logger.log(Level.WARNING, "user is not allowed");
061: res.sendError(HttpServletResponse.SC_FORBIDDEN);
062: return;
063: }
064: try {
065: Repository repo = RepoFactory.getRepository(
066: getServletContext(), repositoryId, userId);
067: RepoItem file = repo.getItemInfo(new RepoItem(filePath,
068: userId));
069: if (file == null) {
070: _logger.log(Level.WARNING, "File not found: "
071: + filePath);
072: res.sendError(HttpServletResponse.SC_NOT_FOUND);
073: } else if (file.isDirectory()) {
074: _logger.log(Level.WARNING, "Is a directory: "
075: + filePath);
076: res.sendError(HttpServletResponse.SC_BAD_REQUEST);
077: } else {
078: if (_logger.isLoggable(Level.FINE)) {
079: _logger.log(Level.FINE, "Downloading, user: "
080: + userId + " repository: " + repositoryId
081: + " file: " + filePath);
082: }
083: if (filePath.endsWith("pdf")) {
084: res.setContentType("application/pdf");
085: } else {
086: res.setContentType(DOWNLOAD_MIME);
087: }
088: res.setContentLength((int) file.getLength());
089:
090: InputStream is = repo.getFile(file);
091: OutputStream os = res.getOutputStream();
092: IOUtils.copyStream(is, os);
093: is.close();
094: }
095: } catch (RepoException ex) {
096: _logger.log(Level.FINE, "Download failed, user: " + userId
097: + " repository: " + repositoryId + " file: "
098: + filePath, ex);
099: res.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
100: }
101:
102: }
103:
104: protected void doPost(HttpServletRequest req,
105: HttpServletResponse res) throws ServletException,
106: IOException {
107: doGet(req, res);
108: }
109:
110: }
|