0
|
1 package nabble.view.web.util;
|
|
2
|
|
3 import fschmidt.util.java.IoUtils;
|
|
4 import fschmidt.util.servlet.JtpContext;
|
|
5 import nabble.view.lib.UrlMappable;
|
|
6
|
|
7 import javax.imageio.ImageIO;
|
|
8 import javax.servlet.ServletException;
|
|
9 import javax.servlet.http.HttpServlet;
|
|
10 import javax.servlet.http.HttpServletRequest;
|
|
11 import javax.servlet.http.HttpServletResponse;
|
|
12 import java.awt.AlphaComposite;
|
|
13 import java.awt.Color;
|
|
14 import java.awt.Graphics2D;
|
|
15 import java.awt.RenderingHints;
|
|
16 import java.awt.Shape;
|
|
17 import java.awt.geom.Ellipse2D;
|
|
18 import java.awt.image.BufferedImage;
|
|
19 import java.awt.image.RenderedImage;
|
|
20 import java.io.ByteArrayInputStream;
|
|
21 import java.io.ByteArrayOutputStream;
|
|
22 import java.io.IOException;
|
|
23 import java.io.InputStream;
|
|
24 import java.io.OutputStream;
|
|
25 import java.util.HashMap;
|
|
26 import java.util.Map;
|
|
27 import java.util.regex.Matcher;
|
|
28 import java.util.regex.Pattern;
|
|
29
|
|
30 /**
|
|
31 * Quadrants
|
|
32 * 1 | 2
|
|
33 * 3 | 4
|
|
34 *
|
|
35 * @author Hugo Teixeira
|
|
36 */
|
|
37 public class RoundedCorner extends HttpServlet implements UrlMappable {
|
|
38
|
|
39 private static final Pattern URL_PATTERN = Pattern.compile("/rounded/q(\\d)_([A-Fa-f0-9]{6})([A-Fa-f0-9]{2})?$");
|
|
40
|
|
41 public Map<String,String[]> getParameterMapFromUrl(HttpServletRequest request,String mappedUrl) {
|
|
42 Matcher m = URL_PATTERN.matcher(mappedUrl);
|
|
43 if( !m.find() )
|
|
44 throw new RuntimeException();
|
|
45 Map<String,String[]> params = new HashMap<String,String[]>();
|
|
46 String quadrant = m.group(1);
|
|
47 params.put("quadrant",new String[]{quadrant});
|
|
48 String bgColor = m.group(2) + (m.group(3) == null? "" : m.group(3));
|
|
49 params.put( "bgColor", new String[]{bgColor} );
|
|
50 return params;
|
|
51 }
|
|
52
|
|
53 public Pattern getUrlPattern() {
|
|
54 return URL_PATTERN;
|
|
55 }
|
|
56
|
|
57 protected void service(HttpServletRequest request, HttpServletResponse response)
|
|
58 throws ServletException, IOException {
|
|
59 OutputStream out = response.getOutputStream();
|
|
60 response.setContentType("image/png");
|
|
61
|
|
62 JtpContext jtpContext = (JtpContext)getServletContext().getAttribute(JtpContext.attrName);
|
|
63 jtpContext.setEtag(request,response);
|
|
64
|
|
65 int quadrant = Integer.valueOf(request.getParameter("quadrant"));
|
|
66 String bgColor = request.getParameter("bgColor");
|
|
67
|
|
68 ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
|
69 ImageIO.write(createImage(quadrant, bgColor), "png", baos);
|
|
70 final byte[] bytes = baos.toByteArray();
|
|
71
|
|
72 InputStream in = new ByteArrayInputStream(bytes);
|
|
73 try {
|
|
74 IoUtils.copyAll(in,out);
|
|
75 } finally {
|
|
76 in.close();
|
|
77 }
|
|
78 }
|
|
79
|
|
80 private RenderedImage createImage(int quadrant, String bgColor) {
|
|
81 int radius = 12;
|
|
82 int[] xy = { 0, 0 };
|
|
83 if (quadrant == 2) xy = new int[]{ -radius, 0 };
|
|
84 else if (quadrant == 3) xy = new int[]{ 0, -radius };
|
|
85 else if (quadrant == 4) xy = new int[]{ -radius, -radius };
|
|
86
|
|
87 Color bg = parseColor(bgColor);
|
|
88 BufferedImage img = new BufferedImage(radius, radius, BufferedImage.TYPE_INT_ARGB);
|
|
89 Graphics2D g2 = img.createGraphics();
|
|
90 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
|
|
91 g2.setColor(bg);
|
|
92 g2.fillRect(0, 0, radius, radius);
|
|
93 g2.setComposite(AlphaComposite.getInstance(AlphaComposite.CLEAR, 0.5f));
|
|
94 Shape s = new Ellipse2D.Float(xy[0], xy[1], 2 * radius, 2* radius);
|
|
95 g2.fill(s);
|
|
96 g2.dispose();
|
|
97 return img;
|
|
98 }
|
|
99
|
|
100 private Color parseColor(String c) {
|
|
101 if (c.length() == 6)
|
|
102 return Color.decode("0x"+c);
|
|
103 int r = Integer.parseInt(c.substring(0,2), 16);
|
|
104 int g = Integer.parseInt(c.substring(2,4), 16);
|
|
105 int b = Integer.parseInt(c.substring(4,6), 16);
|
|
106 int a = Integer.parseInt(c.substring(6,8), 16);
|
|
107 return new Color(r, g, b, a);
|
|
108 }
|
|
109 } |