From f79cfd48786fb45ad5804d9a51ce5139459a5ca3 Mon Sep 17 00:00:00 2001 From: Kai Moritz Date: Mon, 26 Nov 2012 20:42:50 +0100 Subject: [PATCH 1/1] Initiale Version --- pom.xml | 61 +++++ .../java/de/juplo/fixswf/FixSwfServlet.java | 219 ++++++++++++++++++ src/main/webapp/WEB-INF/web.xml | 26 +++ src/main/webapp/index.jsp | 8 + 4 files changed, 314 insertions(+) create mode 100644 pom.xml create mode 100644 src/main/java/de/juplo/fixswf/FixSwfServlet.java create mode 100644 src/main/webapp/WEB-INF/web.xml create mode 100644 src/main/webapp/index.jsp diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..7546531 --- /dev/null +++ b/pom.xml @@ -0,0 +1,61 @@ + + 4.0.0 + de.juplo + fix-swf + war + 1.0 + fix-swf + http://juplo.de/fix-swf + + 1.2.2 + 2.6 + 2.5 + + + + commons-fileupload + commons-fileupload + ${commons-fileupload.version} + + + commons-lang + commons-lang + ${commons-lang.version} + + + javax.servlet + servlet-api + ${servlet-api.version} + provided + + + + fix-swf + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.6 + 1.6 + utf8 + true + + + + org.mortbay.jetty + jetty-maven-plugin + + + org.codehaus.mojo + tomcat-maven-plugin + 1.1 + + / + UTF-8 + + + + + diff --git a/src/main/java/de/juplo/fixswf/FixSwfServlet.java b/src/main/java/de/juplo/fixswf/FixSwfServlet.java new file mode 100644 index 0000000..42f56a3 --- /dev/null +++ b/src/main/java/de/juplo/fixswf/FixSwfServlet.java @@ -0,0 +1,219 @@ +package de.juplo.fixswf; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.io.PrintWriter; +import java.util.HashMap; +import java.util.Map; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import javax.servlet.ServletConfig; +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import org.apache.commons.fileupload.FileItemIterator; +import org.apache.commons.fileupload.FileItemStream; +import org.apache.commons.fileupload.servlet.ServletFileUpload; +import org.apache.commons.lang.StringEscapeUtils; + +/** + * + * @author kai + */ +public class FixSwfServlet extends HttpServlet +{ + public final String MESSAGE_ATTRIBUTE = getClass().getCanonicalName() + ".MESSAGE"; + + private Map corrections; + private Pattern pattern; + + + @Override + public void init(ServletConfig config) throws ServletException { + corrections = new HashMap(); + corrections.put((byte)12, (byte)10); + corrections.put((byte)14, (byte)11); + pattern = Pattern.compile("([^/\\\\:<>?\"]+?)(?:\\.swf)?$", Pattern.MULTILINE | Pattern.CASE_INSENSITIVE); + } + + + /** + * Processes requests for both HTTP + * GET and + * POST methods. + * + * @param request servlet request + * @param response servlet response + * @throws ServletException if a servlet-specific error occurs + * @throws IOException if an I/O error occurs + */ + protected void processRequest( + HttpServletRequest request, + HttpServletResponse response + ) + throws + ServletException, + IOException + { + response.sendError(HttpServletResponse.SC_NOT_IMPLEMENTED); + } + + /** + * Handles the HTTP + * GET method. + * + * @param request servlet request + * @param response servlet response + * @throws ServletException if a servlet-specific error occurs + * @throws IOException if an I/O error occurs + */ + @Override + protected void doGet( + HttpServletRequest request, + HttpServletResponse response + ) + throws + ServletException, + IOException + { + response.setContentType("text/html;charset=UTF-8"); + PrintWriter out = response.getWriter(); + try + { + out.println(""); + out.println(""); + out.println("Fix-Swf"); + out.println(""); + out.println(""); + if (request.getAttribute(MESSAGE_ATTRIBUTE) != null) + { + out.println("

Upload-Results

"); + out.println("

" + StringEscapeUtils.escapeHtml((String)request.getAttribute(MESSAGE_ATTRIBUTE)) + "

"); + } + out.println("

Choose SWF-File to check/fix

"); + out.println("

If the file has to be fixed, the fixed version will be presented to you as a download.

"); + out.println("
"); + out.println(""); + out.println(""); + out.println("
"); + out.println(""); + out.println(""); + } + finally + { + out.close(); + } + } + + /** + * Handles the HTTP + * POST method. + * + * @param request servlet request + * @param response servlet response + * @throws ServletException if a servlet-specific error occurs + * @throws IOException if an I/O error occurs + */ + @Override + protected void doPost( + HttpServletRequest request, + HttpServletResponse response + ) + throws + ServletException, + IOException + { + // Check that we have a file upload request + if (!ServletFileUpload.isMultipartContent(request)) + { + request.setAttribute(MESSAGE_ATTRIBUTE, "No data uploaded!"); + request.getRequestDispatcher("/").forward(request, response); + return; + } + + // Create a new file upload handler + ServletFileUpload upload = new ServletFileUpload(); + + try + { + // Parse the request + FileItemIterator iter = upload.getItemIterator(request); + + while(iter.hasNext()) + { + FileItemStream item = iter.next(); + + if (item.isFormField()) + continue; + + if (!item.getFieldName().equals("swf")) + continue; + + String filename = item.getName(); + InputStream in = item.openStream(); + byte[] buffer = new byte[512]; + int i; + i = in.read(buffer); + + if (i < 4) + { + request.setAttribute(MESSAGE_ATTRIBUTE, "Not enough data uploaded!"); + request.getRequestDispatcher("/").forward(request, response); + return; + } + + if (!corrections.containsKey(buffer[3])) + { + request.setAttribute(MESSAGE_ATTRIBUTE, "Uploaded SWF-file " + filename + " was valid!"); + request.getRequestDispatcher("/").forward(request, response); + return; + } + + /** Correct the Flash-Player-Version in Header-Byte 4 */ + buffer[3] = corrections.get(buffer[3]); + + Matcher matcher = pattern.matcher(filename); + if (matcher.matches()) + { + filename = matcher.group(1) + "_FIXED.swf"; + } + else + { + filename = "FIXED.swf"; + } + + response.setHeader("Content-Type", "application/x-shockwave-flash"); + response.setHeader("Content-Disposition","attachment; filename=\"" + filename + "\""); + OutputStream out = response.getOutputStream(); + do + { + out.write(buffer, 0, i); + } + while ((i = in.read(buffer)) > -1); + + out.close(); + } + } + catch (Exception e) + { + request.setAttribute(MESSAGE_ATTRIBUTE, "Error while parsing upload: " + e); + request.getRequestDispatcher("/").forward(request, response); + return; + } + + /** Fallback, if no data was found (happens when forwarding!) */ + doGet(request, response); + } + + /** + * Returns a short description of the servlet. + * + * @return a String containing servlet description + */ + @Override + public String getServletInfo() { + return "This Servlet fixes the 4th byte of an uploaded SWF-file according to the SWF-filestructre specification."; + } +} diff --git a/src/main/webapp/WEB-INF/web.xml b/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 0000000..56cde92 --- /dev/null +++ b/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,26 @@ + + + + fix-swf + + + + + + fix-swf-servlet + de.juplo.fixswf.FixSwfServlet + + + + + + + fix-swf-servlet + / + + + + diff --git a/src/main/webapp/index.jsp b/src/main/webapp/index.jsp new file mode 100644 index 0000000..cef4aeb --- /dev/null +++ b/src/main/webapp/index.jsp @@ -0,0 +1,8 @@ +<%@page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" session="false" %> + +<%-- + +Unfortionatly, the welcome-mechanism in web.xml does not work with a page +served by a servlet... + +--%> -- 2.20.1