|
| 1 | +package evercookie; |
| 2 | + |
| 3 | +import java.awt.Color; |
| 4 | +import java.awt.RenderingHints; |
| 5 | +import java.awt.image.BufferedImage; |
| 6 | +import java.io.IOException; |
| 7 | +import java.io.OutputStream; |
| 8 | + |
| 9 | +import javax.imageio.ImageIO; |
| 10 | +import javax.servlet.ServletException; |
| 11 | +import javax.servlet.annotation.WebServlet; |
| 12 | +import javax.servlet.http.Cookie; |
| 13 | +import javax.servlet.http.HttpServlet; |
| 14 | +import javax.servlet.http.HttpServletRequest; |
| 15 | +import javax.servlet.http.HttpServletResponse; |
| 16 | + |
| 17 | +/** |
| 18 | + * This is a Java Servlet port of evercookie_png.php, the server-side component |
| 19 | + * of Evercookie that stores values in force-cached PNG image data. |
| 20 | + * |
| 21 | + * Install this servlet at /evercookie_png.php in your web.xml (or add a @WebServlet |
| 22 | + * annotation) and you won't even need to modify evercookie.js! This assumes |
| 23 | + * that Evercookie's assets are in your web root. |
| 24 | + * |
| 25 | + * Of course, if you have set $_ec_baseurl to something, you should install this |
| 26 | + * at [$_ec_baseurl]evercookie_png.php. Remember, $ec_baseurl needs a trailing |
| 27 | + * slash in the evercookie.js. |
| 28 | + * |
| 29 | + * @author Gabriel Bauman <[email protected]> |
| 30 | + * |
| 31 | + */ |
| 32 | +public class EvercookiePngServlet extends HttpServlet { |
| 33 | + |
| 34 | + private static final long serialVersionUID = 1L; |
| 35 | + |
| 36 | + public EvercookiePngServlet() { |
| 37 | + super(); |
| 38 | + } |
| 39 | + |
| 40 | + @Override |
| 41 | + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { |
| 42 | + |
| 43 | + boolean cookieExists = false; |
| 44 | + String cookieValue = null; |
| 45 | + Cookie[] cookies = req.getCookies(); |
| 46 | + |
| 47 | + if (null != cookies) { |
| 48 | + // Iterate over cookies until we find one named evercookie_png |
| 49 | + for (Cookie cookie : cookies) |
| 50 | + { |
| 51 | + if (cookie.getName().equals("evercookie_png")) { |
| 52 | + cookieExists = true; |
| 53 | + cookieValue = cookie.getValue(); |
| 54 | + break; |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + // If the cookie doesn't exist, send 304 Not Modified and exit. |
| 60 | + if (!cookieExists) { |
| 61 | + resp.setStatus(304); |
| 62 | + return; |
| 63 | + } |
| 64 | + |
| 65 | + // Generate a PNG image from the cookie value. |
| 66 | + BufferedImage image = new BufferedImage(200, 1, BufferedImage.TYPE_INT_ARGB); |
| 67 | + image.createGraphics().setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF); |
| 68 | + int x = 0; |
| 69 | + |
| 70 | + for (int i = 0; i < cookieValue.length(); i += 3) { |
| 71 | + // Treat every 3 chars of the cookie value as an {R,G,B} triplet. |
| 72 | + Color c = new Color(cookieValue.charAt(i), cookieValue.charAt(i + 1), cookieValue.charAt(i + 2)); |
| 73 | + image.setRGB(x++, 0, c.getRGB()); |
| 74 | + } |
| 75 | + |
| 76 | + // The cookie was present; set up the response headers. |
| 77 | + resp.setContentType("image/png"); |
| 78 | + resp.addHeader("Last-Modified", "Wed, 30 Jun 2010 21:36:48 GMT"); |
| 79 | + resp.addHeader("Expires", "Tue, 31 Dec 2033 23:30:45 GMT"); |
| 80 | + resp.addHeader("Cache-Control", "private, max-age=630720000"); |
| 81 | + |
| 82 | + // Send the generate image data as the response body. |
| 83 | + OutputStream body = resp.getOutputStream(); |
| 84 | + |
| 85 | + try { |
| 86 | + ImageIO.write(image, "png", body); |
| 87 | + } finally { |
| 88 | + body.close(); |
| 89 | + } |
| 90 | + |
| 91 | + // And we're done. |
| 92 | + resp.setStatus(200); |
| 93 | + resp.flushBuffer(); |
| 94 | + } |
| 95 | +} |
0 commit comments