View Javadoc

1   /*
2    * Copyright (c) 2010 Commsen International. All rights reserved.
3    * 
4    * This file is part of JWebThumb library.
5    *	
6    * JWebThumb library is free software: you can redistribute it and/or modify 
7    * it under the terms of the GNU Lesser General Public License as published by
8    * the Free Software Foundation, either version 2 of the License, or
9    * (at your option) any later version.
10   * 
11   * JWebThumb library is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU Lesser General Public License for more details.
15   * 
16   * You should have received a copy of the GNU Lesser General Public License
17   * along with JWebThumb library.  If not, see <http://www.gnu.org/licenses/lgpl.html>.
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   * &lt;init-param&gt;
38   *   &lt;param-name&gt;key&lt;/param-name&gt;
39   *   &lt;param-value&gt;my_hard_to_guess_secure_key&lt;/param-value&gt;
40   * &lt;/init-param&gt;
41   * </pre>
42   * 
43   * and make sure to provide the parameter in {@link WebThumbRequest#setNotify(String)} method:
44   * 
45   * <pre>
46   * webThumbRequest.setNotify(&quot;http://YOUR.WEB.SITE/WebThumbNotifications?key=my_hard_to_guess_secure_key&quot;);
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  	 * (non-Javadoc)
70  	 * @see javax.servlet.GenericServlet#init()
71  	 */
72  	@Override
73  	public void init() throws ServletException {
74  		key = getServletConfig().getInitParameter("key");
75  	}
76  
77  
78  	/*
79  	 * (non-Javadoc)
80  	 * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest,
81  	 * javax.servlet.http.HttpServletResponse)
82  	 */
83  	@Override
84  	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
85  		processRequest(req, resp);
86  	}
87  
88  
89  	/*
90  	 * (non-Javadoc)
91  	 * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest,
92  	 * javax.servlet.http.HttpServletResponse)
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 }