From 980a506dd212242779fc5bd480d8687844339aa5 Mon Sep 17 00:00:00 2001 From: Walter Oggioni Date: Thu, 27 Aug 2015 22:07:43 +0200 Subject: [PATCH] aggiunto servlet dedicato per il download dei file --- .../frontend/component/PackageTable.java | 2 +- .../frontend/servlet/FileServlet.java | 68 ++++++++++++++++ .../frontend/servlet/JPacRepoServlet.java | 2 +- src/main/resources/WEB-INF/web.xml | 7 +- src/main/webapp/404.xhtml | 79 +++++++++++++++++++ 5 files changed, 155 insertions(+), 3 deletions(-) create mode 100644 src/main/java/org/jpacrepo/frontend/servlet/FileServlet.java create mode 100644 src/main/webapp/404.xhtml diff --git a/src/main/java/org/jpacrepo/frontend/component/PackageTable.java b/src/main/java/org/jpacrepo/frontend/component/PackageTable.java index 59a3251..1a504fb 100644 --- a/src/main/java/org/jpacrepo/frontend/component/PackageTable.java +++ b/src/main/java/org/jpacrepo/frontend/component/PackageTable.java @@ -55,7 +55,7 @@ public class PackageTable extends WTable img.setStyleClass("download-icon"); img.setWidth(new WLength("16px")); button.setImage(img); - button.setLink(new WLink(WLink.Type.Url, "rest/pkg/download/" + pkg.fileName)); + button.setLink(new WLink(WLink.Type.Url, "archive/" + pkg.fileName)); button.addStyleClass("text-center"); new WText("Download",button); getElementAt(row, 5).addWidget(button); diff --git a/src/main/java/org/jpacrepo/frontend/servlet/FileServlet.java b/src/main/java/org/jpacrepo/frontend/servlet/FileServlet.java new file mode 100644 index 0000000..124f544 --- /dev/null +++ b/src/main/java/org/jpacrepo/frontend/servlet/FileServlet.java @@ -0,0 +1,68 @@ +package org.jpacrepo.frontend.servlet; + +import org.jpacrepo.context.ApplicationContext; +import org.jpacrepo.context.DefaultConfiguration; + +import javax.inject.Inject; +import javax.servlet.ServletContext; +import javax.servlet.ServletException; +import javax.servlet.ServletOutputStream; +import javax.servlet.annotation.WebServlet; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.logging.Logger; + +/** + * Created by walter on 29/07/15. + */ + + +@WebServlet("/archive/*") +public class FileServlet extends HttpServlet +{ + @Inject + @DefaultConfiguration + private ApplicationContext ctx; + + private Logger log = Logger.getLogger(FileServlet.class.getName()); + + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException + { + String fileName = request.getPathInfo(); + if (fileName == null || fileName.equals("")) + { + throw new ServletException("File Name can't be null or empty"); + } + File file = new File(ctx.getSystemProperties().getProperty("RepoFolder"), fileName); + if (!file.exists()) + { + response.sendError(HttpServletResponse.SC_NOT_FOUND); + //response.sendRedirect("../404.xhtml"); + return; + } + log.fine("File location on server::" + file.getAbsolutePath()); + ServletContext ctx = getServletContext(); + InputStream fis = new FileInputStream(file); + String mimeType = ctx.getMimeType(file.getAbsolutePath()); + response.setContentType(mimeType != null ? mimeType : "application/octet-stream"); + response.setContentLength((int) file.length()); + response.setHeader("Content-Disposition", String.format("attachment; filename=\"%s\"", fileName)); + + ServletOutputStream os = response.getOutputStream(); + byte[] bufferData = new byte[1024]; + int read; + while ((read = fis.read(bufferData)) != -1) + { + os.write(bufferData, 0, read); + } + os.close(); + fis.close(); + log.fine("File successfully downloaded to client"); + } + +} \ No newline at end of file diff --git a/src/main/java/org/jpacrepo/frontend/servlet/JPacRepoServlet.java b/src/main/java/org/jpacrepo/frontend/servlet/JPacRepoServlet.java index f6edfc3..907a6d4 100644 --- a/src/main/java/org/jpacrepo/frontend/servlet/JPacRepoServlet.java +++ b/src/main/java/org/jpacrepo/frontend/servlet/JPacRepoServlet.java @@ -15,7 +15,7 @@ import javax.servlet.annotation.WebServlet; */ -@WebServlet(name="app.CherebaServlet", urlPatterns={"/view/*"}) +@WebServlet(urlPatterns = {"/view/*"}) public class JPacRepoServlet extends WtServlet { @Inject diff --git a/src/main/resources/WEB-INF/web.xml b/src/main/resources/WEB-INF/web.xml index e39c8f0..e72318c 100644 --- a/src/main/resources/WEB-INF/web.xml +++ b/src/main/resources/WEB-INF/web.xml @@ -11,7 +11,12 @@ jpacrepo - + + + 404 + 404.xhtml + + diff --git a/src/main/webapp/404.xhtml b/src/main/webapp/404.xhtml new file mode 100644 index 0000000..d43927f --- /dev/null +++ b/src/main/webapp/404.xhtml @@ -0,0 +1,79 @@ + + + + + + + + + + + + + + + + + + + + + + + + + +

+ The page cannot be found

+
Possible + causes:
+
+
    +
  • Baptist explanation: There + must be sin in your life. Everyone else opened it fine.
    +
  • +
  • Presbyterian explanation: It's + not God's will for you to open this link.
    +
  • +
  • Word of Faith explanation: + You lack the faith to open this link. Your negative words have prevented + you from realizing this link's fulfillment.
    +
  • +
  • Charismatic explanation: Thou + art loosed! Be commanded to OPEN!
    +
  • +
  • Unitarian explanation: All + links are equal, so if this link doesn't work for you, feel free to + experiment with other links that might bring you joy and fulfillment.
    +
  • +
  • Buddhist explanation: .........................
    +
  • +
  • Episcopalian explanation: + Are you saying you have something against homosexuals?
    +
  • +
  • Christian Science explanation: There + really is no link.
    +
  • +
  • Atheist explanation: The only + reason you think this link exists is because you needed to invent it.
    +
  • +
  • Church counselor's explanation: + And what did you feel when the link would not open?
  • +
+


+

+ +

+ HTTP 404 - File not found - Internet Explorer
+

+
+ + + + +