1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package com.commsen.jwebthumb;
20
21 import java.io.IOException;
22
23 import javax.servlet.ServletException;
24 import javax.servlet.http.HttpServlet;
25 import javax.servlet.http.HttpServletRequest;
26 import javax.servlet.http.HttpServletResponse;
27
28 /***
29 * An abstract sevlet responsible for receiving notifications from webthumb. All extending classes
30 * need to implement {@link #processThumb(String, String)} method in order to react on received
31 * notification.
32 * <p>
33 * This servlet will handle notifications via both GET and POST methods. To prevent fake calls to
34 * your servlet set <code>key</code> parameter to some hard to guess value:
35 *
36 * <pre>
37 * <init-param>
38 * <param-name>key</param-name>
39 * <param-value>my_hard_to_guess_secure_key</param-value>
40 * </init-param>
41 * </pre>
42 *
43 * and make sure to provide the parameter in {@link WebThumbRequest#setNotify(String)} method:
44 *
45 * <pre>
46 * webThumbRequest.setNotify("http://YOUR.WEB.SITE/WebThumbNotifications?key=my_hard_to_guess_secure_key");
47 * </pre>
48 *
49 *
50 * @see http://webthumb.bluga.net/apidoc#notify
51 *
52 * @author <a href="mailto:MilenDyankov@gmail.com">Milen Dyankov</a>
53 *
54 */
55 public abstract class WebThumbNotificationServlet extends HttpServlet {
56
57 /***
58 *
59 */
60 private static final long serialVersionUID = 1L;
61 private static final String PARAM_ID = "id";
62 private static final String PARAM_URL = "url";
63 private static final String PARAM_KEY = "key";
64
65 private String key = null;
66
67
68
69
70
71
72 @Override
73 public void init() throws ServletException {
74 key = getServletConfig().getInitParameter("key");
75 }
76
77
78
79
80
81
82
83 @Override
84 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
85 processRequest(req, resp);
86 }
87
88
89
90
91
92
93
94 @Override
95 protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
96 processRequest(req, resp);
97 }
98
99
100 protected void processRequest(HttpServletRequest req, HttpServletResponse resp) {
101 if (key != null && !key.equals(req.getParameter(PARAM_KEY))) {
102 return;
103 }
104 String id = req.getParameter(PARAM_ID);
105 String url = req.getParameter(PARAM_URL);
106 processThumb(id, url);
107 }
108
109
110 /***
111 * Method called when notification is received. Extending classes should implement this method
112 * and handle notifications as desired.
113 *
114 * @param id the job identifier
115 * @param url the url of the requested site
116 */
117 public abstract void processThumb(String id, String url);
118 }