Leonardo Vannucci
2018-09-19 c4cb974be197acfe06dc06359c25d04611fa47f2
dg1cloud-core/src/main/java/it/digione/dg1cloud/controller/StartURPController.java
....@@ -5,20 +5,25 @@
55
66 import javax.activation.DataHandler;
77 import javax.mail.internet.MimeMessage;
8
+import javax.servlet.http.HttpServletRequest;
89 import javax.validation.Valid;
910 import javax.xml.bind.JAXBElement;
1011
1112 import org.slf4j.Logger;
1213 import org.slf4j.LoggerFactory;
1314 import org.springframework.beans.factory.annotation.Autowired;
15
+import org.springframework.beans.factory.annotation.Value;
1416 import org.springframework.mail.javamail.JavaMailSender;
1517 import org.springframework.mail.javamail.MimeMessageHelper;
1618 import org.springframework.stereotype.Controller;
1719 import org.springframework.validation.BindingResult;
1820 import org.springframework.web.bind.annotation.ControllerAdvice;
21
+import org.springframework.web.bind.annotation.ExceptionHandler;
1922 import org.springframework.web.bind.annotation.ModelAttribute;
2023 import org.springframework.web.bind.annotation.PostMapping;
2124 import org.springframework.web.bind.annotation.RequestMapping;
25
+import org.springframework.web.multipart.MaxUploadSizeExceededException;
26
+import org.springframework.web.multipart.MultipartFile;
2227 import org.springframework.web.servlet.ModelAndView;
2328
2429 import it.digione.dg1cloud.form.StartURPForm;
....@@ -45,6 +50,9 @@
4550 @Autowired IsdConfig isdConfig;
4651 @Autowired JavaMailSender javaMailSender;
4752
53
+ @Value("${tomcat.MaxSwallowSize}")
54
+ private int maxFileSize;
55
+
4856 @ModelAttribute("startURPForm")
4957 public StartURPForm startURPForm() {
5058 return new StartURPForm();
....@@ -55,11 +63,11 @@
5563 return "startURP";
5664 }
5765 @PostMapping("/sendStartRequest")
58
- public Object sendStartRequest(@ModelAttribute("startURPForm") @Valid StartURPForm form,
66
+ public ModelAndView sendStartRequest(@ModelAttribute("startURPForm") @Valid StartURPForm form,
5967 BindingResult result) {
6068
6169 ModelAndView modelAndView;
62
- if (result.hasErrors()){
70
+ if (result.hasErrors()) {
6371 modelAndView = new ModelAndView("startURP");
6472 modelAndView.addObject(result);
6573 modelAndView.addObject("startURPForm", form);
....@@ -94,7 +102,7 @@
94102 helper = new MimeMessageHelper(message, true);
95103 helper.setSubject("Richiesta inviata correttamente al servizio URP");
96104 helper.setFrom(isdProperties.getNotificationFrom());
97
- helper.setTo(form.getMittente());
105
+ helper.setTo(form.getEmail());
98106 helper.setText("Richiesta inviata correttamente: " + numeroProtocollo + " del " + dataProtocollo, true);
99107 javaMailSender.send(message);
100108
....@@ -129,13 +137,14 @@
129137 Contacts contacts = factory.createInstanceMessageCreateRequestContacts();
130138
131139 Contact contact = new Contact();
132
- contact.setDescription(factory.createInstanceMessageCreateRequestContactsContactDescription(startURPForm.getMittente()));
133
- contact.setEmail(factory.createInstanceMessageCreateRequestContactsContactEmail( startURPForm.getMittente()));
140
+ String nominativo = startURPForm.getCognome() + " " + startURPForm.getNome();
141
+ contact.setDescription(factory.createInstanceMessageCreateRequestContactsContactDescription(nominativo));
142
+ contact.setEmail(factory.createInstanceMessageCreateRequestContactsContactEmail( startURPForm.getEmail()));
134143 contact.setType("F");
135144 contact.setReferenceType("AB");
136145 contact.setSearchType("EXTID");
137146 contact.setSearchMode("NOTFOUNDDEF");
138
- contact.setSearchValue(startURPForm.getMittente());
147
+ contact.setSearchValue(startURPForm.getEmail());
139148
140149 contacts.getContact().add(contact);
141150
....@@ -172,20 +181,41 @@
172181
173182 Attachments attachments = new Attachments();
174183
175
- Attachment attachment = new Attachment();
176
- attachment.setFilename( startURPForm.getAllegato().getOriginalFilename());
177
- attachment.setContentType( factory.createInstanceMessageCreateRequestAttachmentsAttachmentContentType( startURPForm.getAllegato().getContentType()));
178
- attachment.setFileset("isharedocMailAttach");
179
- try {
180
- attachment.setData(new DataHandler(startURPForm.getAllegato().getBytes(), startURPForm.getAllegato().getContentType()));
181
- } catch (IOException e) {
182
- throw new RuntimeException("Errore nella lettura dell'allegato da passare al servizio URP", e);
184
+ for ( MultipartFile allegato : startURPForm.getAllegati()) {
185
+ if ( "".equals( allegato.getOriginalFilename() ) )
186
+ continue;
187
+ Attachment attachment = new Attachment();
188
+ attachment.setFilename( allegato.getOriginalFilename());
189
+ attachment.setContentType( factory.createInstanceMessageCreateRequestAttachmentsAttachmentContentType( allegato.getContentType()));
190
+ attachment.setFileset("isharedocMailAttach");
191
+ try {
192
+ DataHandler dh = null;
193
+ if ( "text/plain".equalsIgnoreCase(allegato.getContentType()) == true ) {
194
+ dh = new DataHandler(new String(allegato.getBytes()), allegato.getContentType());
195
+ } else {
196
+ dh = new DataHandler(allegato.getBytes(), allegato.getContentType());
197
+ }
198
+ attachment.setData(dh);
199
+ } catch (IOException e) {
200
+ throw new RuntimeException("Errore nella lettura dell'allegato da passare al servizio URP", e);
201
+ }
202
+ attachments.getAttachment().add(attachment);
183203 }
184
- attachments.getAttachment().add(attachment);
185
-
186
- JAXBElement<Attachments> jaxbAttachments = factory.createInstanceMessageCreateRequestAttachments(attachments);
187
- request.setAttachments(jaxbAttachments);
204
+ if ( attachments.getAttachment().size() > 0 ) {
205
+ JAXBElement<Attachments> jaxbAttachments = factory.createInstanceMessageCreateRequestAttachments(attachments);
206
+ request.setAttachments(jaxbAttachments);
207
+ }
188208
189209 return request;
190210 }
211
+
212
+ @ExceptionHandler(MaxUploadSizeExceededException.class)
213
+ public ModelAndView handleMaxSizeException( MaxUploadSizeExceededException exc, HttpServletRequest request) {
214
+
215
+ ModelAndView modelAndView = new ModelAndView("startURP");
216
+ modelAndView.addObject("startURPForm", new StartURPForm());
217
+ modelAndView.addObject("message", "Gli allegati hanno superato le dimensioni massime consentite di " + maxFileSize + " Mb.");
218
+ modelAndView.addObject("alertClass", "alert-danger");
219
+ return modelAndView;
220
+ }
191221 }