1 package de.tivsource.page.user.actions.contact;
2
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.io.UnsupportedEncodingException;
6 import java.net.URL;
7 import java.util.Date;
8 import java.util.Properties;
9 import java.util.UUID;
10
11 import javax.mail.MessagingException;
12 import javax.mail.PasswordAuthentication;
13 import javax.mail.Session;
14
15 import org.apache.logging.log4j.LogManager;
16 import org.apache.logging.log4j.Logger;
17 import org.apache.struts2.ServletActionContext;
18 import org.apache.struts2.convention.annotation.Action;
19 import org.apache.struts2.convention.annotation.Actions;
20 import org.apache.struts2.convention.annotation.Result;
21 import org.apache.struts2.tiles.annotation.TilesDefinition;
22 import org.apache.struts2.tiles.annotation.TilesDefinitions;
23 import org.apache.struts2.tiles.annotation.TilesPutAttribute;
24
25 import de.tivsource.ejb3plugin.InjectEJB;
26 import de.tivsource.page.common.captcha.Captcha;
27 import de.tivsource.page.dao.captcha.CaptchaDaoLocal;
28 import de.tivsource.page.dao.message.MessageDaoLocal;
29 import de.tivsource.page.dao.page.PageDaoLocal;
30 import de.tivsource.page.dao.property.PropertyDaoLocal;
31 import de.tivsource.page.entity.message.Message;
32 import de.tivsource.page.entity.page.Page;
33 import de.tivsource.page.helper.EmailSender;
34 import de.tivsource.page.helper.EmailTemplate;
35 import de.tivsource.page.user.actions.EmptyAction;
36
37 @TilesDefinitions({
38 @TilesDefinition(name="contactForm", extend = "userTemplate", putAttributes = {
39 @TilesPutAttribute(name = "content", value = "/WEB-INF/tiles/active/view/contact/contact_form.jsp")
40 }),
41 @TilesDefinition(name="page", extend = "userTemplate", putAttributes = {
42 @TilesPutAttribute(name = "meta", value = "/WEB-INF/tiles/active/meta/content.jsp"),
43 @TilesPutAttribute(name = "twitter", value = "/WEB-INF/tiles/active/twitter/content.jsp"),
44 @TilesPutAttribute(name = "content", value = "/WEB-INF/tiles/active/view/page/page.jsp")
45 })
46 })
47 public class SentAction extends EmptyAction {
48
49
50
51
52 private static final long serialVersionUID = -899397629172153047L;
53
54
55
56
57 private static final Logger LOGGER = LogManager.getLogger(SentAction.class);
58
59 @InjectEJB(name="CaptchaDao")
60 private CaptchaDaoLocal captchaDaoLocal;
61
62 @InjectEJB(name="PageDao")
63 private PageDaoLocal pageDaoLocal;
64
65 @InjectEJB(name="PropertyDao")
66 private PropertyDaoLocal propertyDaoLocal;
67
68 @InjectEJB(name="MessageDao")
69 private MessageDaoLocal messageDaoLocal;
70
71 private Message message;
72
73 private Page page;
74
75 private Captcha captcha;
76
77 private String answer;
78
79 @Actions({
80 @Action(
81 value = "sent",
82 results = {
83 @Result(name = "success", type="tiles", location = "page"),
84 @Result(name = "input", type="tiles", location = "contactForm")
85 }
86 )
87 })
88 public String execute() {
89 LOGGER.info("execute() aufgerufen.");
90
91
92 this.getLanguageFromActionContext();
93
94
95 if(answer != null && captcha != null && !answer.trim().equals("") && captcha.getContent().equals(answer)) {
96 sendMail();
97
98 String remoteAddress = ServletActionContext.getRequest().getRemoteAddr();
99 message.setCreated(new Date());
100 message.setCreatedAddress(remoteAddress);
101 message.setUuid(UUID.randomUUID().toString());
102 messageDaoLocal.merge(message);
103
104 return SUCCESS;
105 } else {
106 captcha = captchaDaoLocal.random();
107 addFieldError("answer", "Bitte geben Sie die Hausnummer ein.");
108 return INPUT;
109 }
110 }
111
112 @Override
113 public Page getPage() {
114 if(page == null) {
115 setUpPage();
116 }
117 return page;
118 }
119
120 public Message getMessage() {
121 return message;
122 }
123
124 public void setMessage(Message message) {
125 this.message = message;
126 }
127
128
129
130
131 public Captcha getCaptcha() {
132 return captcha;
133 }
134
135
136
137
138 public void setCaptcha(Captcha captcha) {
139 this.captcha = captcha;
140 }
141
142
143
144
145 public void setAnswer(String answer) {
146 this.answer = answer;
147 }
148
149 private void sendMail() {
150 LOGGER.info("sendMail() aufgerufen.");
151
152 javax.mail.Authenticator auth = new javax.mail.Authenticator() {
153 @Override
154 public PasswordAuthentication getPasswordAuthentication() {
155 return new PasswordAuthentication(
156 propertyDaoLocal.findByKey("mail.user").getValue(),
157 propertyDaoLocal.findByKey("mail.password").getValue());
158 }
159 };
160
161 InputStream template;
162 try {
163 URL templatePath = new URL(propertyDaoLocal.findByKey("mail.template.path").getValue());
164 LOGGER.info("Pfad zur template Datei: " + templatePath);
165 template = templatePath.openStream();
166 LOGGER.info("Template eingelesen");
167 Object notification = EmailTemplate.getEmailTemplate(template);
168 LOGGER.info("Template eingelesen");
169
170
171 Session session = Session.getInstance(getProperties(), auth);
172 session.setDebug(false);
173
174 EmailSender sendIt = new EmailSender();
175 String[] argu = {
176 message.getGender() ? "Frau" : "Herr",
177 message.getFirstname(),
178 message.getLastname(),
179 message.getMail(),
180 message.getTelephone(),
181 message.getFax(),
182 message.getContent(),
183 message.getContent().replace("\n", "<br/>")
184 };
185
186 new Thread(new Runnable() {
187 public void run(){
188 try {
189 sendIt.send("Kontaktformular", (EmailTemplate)notification, argu, session);
190 } catch (UnsupportedEncodingException | MessagingException e) {
191
192 e.printStackTrace();
193 }
194 return;
195 }
196 }).start();
197
198 } catch (UnsupportedEncodingException e) {
199 e.printStackTrace();
200 } catch (IOException e) {
201
202 e.printStackTrace();
203 }
204
205
206 }
207
208 private Properties getProperties() {
209 LOGGER.info("getProperties() aufgerufen.");
210
211
212 Properties props = System.getProperties();
213
214
215 props.put("mail.transport.protocol",
216 propertyDaoLocal.findByKey("mail.transport.protocol").getValue());
217 props.put("mail.host",
218 propertyDaoLocal.findByKey("mail.host").getValue());
219 props.put("mail.port",
220 propertyDaoLocal.findByKey("mail.port").getValue());
221 props.put("mail.smtp.auth",
222 propertyDaoLocal.findByKey("mail.smtp.auth").getValue());
223 props.put("mail.smtp.tls",
224 propertyDaoLocal.findByKey("mail.smtp.tls").getValue());
225 props.put("mail.smtp.starttls.enable",
226 propertyDaoLocal.findByKey("mail.smtp.starttls.enable").getValue());
227 props.put("mail.smtp.localhost",
228 propertyDaoLocal.findByKey("mail.smtp.localhost").getValue());
229 props.put("mail.user",
230 propertyDaoLocal.findByKey("mail.user").getValue());
231 props.put("mail.password",
232 propertyDaoLocal.findByKey("mail.password").getValue());
233 props.put("mail.mime.charset",
234 propertyDaoLocal.findByKey("mail.mime.charset").getValue());
235 props.put("mail.use8bit",
236 propertyDaoLocal.findByKey("mail.use8bit").getValue());
237
238 return props;
239 }
240
241 private void setUpPage() {
242 LOGGER.info("Action Errors: " + this.getFieldErrors().size());
243 if(this.getFieldErrors().size() > 0) {
244 page = pageDaoLocal.findByTechnical("contact");
245 } else {
246 page = pageDaoLocal.findByTechnical("sent");
247 }
248 }
249
250 }