1 package de.tivsource.page.image.actions.captcha;
2
3 import java.text.SimpleDateFormat;
4 import java.util.Calendar;
5 import java.util.GregorianCalendar;
6
7 import javax.servlet.http.HttpServletRequest;
8 import javax.servlet.http.HttpServletResponse;
9
10 import org.apache.logging.log4j.LogManager;
11 import org.apache.logging.log4j.Logger;
12 import org.apache.struts2.ServletActionContext;
13 import org.apache.struts2.convention.annotation.Action;
14 import org.apache.struts2.convention.annotation.Actions;
15 import org.apache.struts2.convention.annotation.Result;
16 import org.apache.struts2.interceptor.ServletRequestAware;
17
18 import com.opensymphony.xwork2.ActionSupport;
19
20 import de.tivsource.ejb3plugin.InjectEJB;
21 import de.tivsource.page.common.captcha.image.CaptchaImage;
22 import de.tivsource.page.dao.captcha.CaptchaDaoLocal;
23 import de.tivsource.page.dao.property.PropertyDaoLocal;
24
25
26
27
28
29 public class ImageAction extends ActionSupport implements ServletRequestAware {
30
31
32
33
34 private static final long serialVersionUID = 7367696315767291095L;
35
36 private static final Logger logger = LogManager.getLogger(ImageAction.class);
37
38
39
40
41 private HttpServletRequest servletRequest;
42
43 @InjectEJB(name="PropertyDao")
44 private PropertyDaoLocal propertyDaoLocal;
45
46 @InjectEJB(name = "CaptchaDao")
47 private CaptchaDaoLocal captchaImageDaoLocal;
48
49
50
51
52 private CaptchaImage captchaImage;
53
54
55
56
57 private boolean cache = true;
58
59 public void setServletRequest(HttpServletRequest servletRequest) {
60 this.servletRequest = servletRequest;
61 }
62
63
64
65
66 public CaptchaImage getCaptchaImage() {
67 return captchaImage;
68 }
69
70
71
72
73 public void setCache(boolean cache) {
74 this.cache = cache;
75 }
76
77
78
79
80
81
82
83
84 @Actions({
85 @Action(
86 value = "*/micro",
87 results = {
88 @Result(
89 name = "success",
90 type="stream",
91 params= { "contentType","image/png","inputName","captchaImage.microFileInputStream"}
92 ),
93 @Result(name = "input", type="tiles", location="imageTemplate")
94 }
95 ),
96 @Action(
97 value = "*/thumbnail",
98 results = {
99 @Result(
100 name = "success",
101 type="stream",
102 params= { "contentType","image/png","inputName","captchaImage.thumbnailFileInputStream"}
103 ),
104 @Result(name = "input", type="tiles", location="imageTemplate")
105 }
106 ),
107 @Action(
108 value = "*/small",
109 results = {
110 @Result(
111 name = "success",
112 type="stream",
113 params= { "contentType","image/png","inputName","captchaImage.smallFileInputStream"}
114 ),
115 @Result(name = "input", type="tiles", location="imageTemplate")
116 }
117 ),
118 @Action(
119 value = "*/normal",
120 results = {
121 @Result(
122 name = "success",
123 type="stream",
124 params= { "contentType","image/png","inputName","captchaImage.normalFileInputStream"}
125 ),
126 @Result(name = "input", type="tiles", location="imageTemplate")
127 }
128 ),
129 @Action(
130 value = "*/large",
131 results = {
132 @Result(
133 name = "success",
134 type="stream",
135 params= { "contentType","image/png","inputName","captchaImage.largeFileInputStream"}
136 ),
137 @Result(name = "input", type="tiles", location="imageTemplate")
138 }
139 ),
140 @Action(
141 value = "*/original",
142 results = {
143 @Result(
144 name = "success",
145 type="stream",
146 params= { "contentType","image/png","inputName","captchaImage.originalFileInputStream"}
147 ),
148 @Result(name = "input", type="tiles", location="imageTemplate")
149 }
150 )
151 })
152 public String execute() {
153 logger.trace("execute() aufgerufen.");
154
155
156 String lastModified = propertyDaoLocal.findByKey("captcha.image.lastModified").getValue();
157
158 String since = servletRequest.getHeader("If-Modified-Since");
159 logger.info("If-Modified-Since: " + since);
160
161
162
163 SimpleDateFormat simpleDateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz");
164 Calendar gregorianCalendar = new GregorianCalendar();
165 gregorianCalendar.add(Calendar.HOUR, 12);
166 String expires = simpleDateFormat.format(gregorianCalendar.getTime());
167
168 HttpServletResponse response = ServletActionContext.getResponse();
169
170
171
172 if(cache) {
173 response.setHeader("Expires", expires);
174 response.setHeader("Last-Modified", lastModified);
175 response.setHeader("Retry-After", expires);
176 } else {
177 response.setHeader("Cache-control", "no-cache, no-store, must-revalidate");
178 response.setHeader("Pragma", "no-cache");
179 response.setHeader("Expires", "-1");
180 }
181
182 if(cache && since!=null && since.equals(lastModified)) {
183 response.setStatus(304);
184 return INPUT;
185 }
186
187 String captchaUuid = ServletActionContext.getRequest().getServletPath();
188
189 logger.info("Slider Image UUID: " + captchaUuid);
190 captchaUuid = captchaUuid.replaceAll("/captcha/", "");
191 captchaUuid = captchaUuid.replaceAll("/micro.png", "");
192 captchaUuid = captchaUuid.replaceAll("/thumbnail.png", "");
193 captchaUuid = captchaUuid.replaceAll("/small.png", "");
194 captchaUuid = captchaUuid.replaceAll("/normal.png", "");
195 captchaUuid = captchaUuid.replaceAll("/large.png", "");
196 captchaUuid = captchaUuid.replaceAll("/original.png", "");
197 logger.info("Slider Image UUID: " + captchaUuid);
198
199 try {
200 this.captchaImage = this.captchaImageDaoLocal.findByUuid(captchaUuid).getImage();
201 return SUCCESS;
202 } catch (NumberFormatException exception) {
203 addActionError(getText("slider.actionError.get.numberFormatException"));
204 exception.printStackTrace();
205 return ERROR;
206 } catch (NullPointerException exception) {
207 addActionError(getText("slider.actionError.get.nullPointerException"));
208 exception.printStackTrace();
209 return ERROR;
210 }
211 }
212
213 }