| .. | .. |
|---|
| 5 | 5 | |
|---|
| 6 | 6 | import javax.activation.DataHandler; |
|---|
| 7 | 7 | import javax.mail.internet.MimeMessage; |
|---|
| 8 | +import javax.servlet.http.HttpServletRequest; |
|---|
| 8 | 9 | import javax.validation.Valid; |
|---|
| 9 | 10 | import javax.xml.bind.JAXBElement; |
|---|
| 10 | 11 | |
|---|
| 11 | 12 | import org.slf4j.Logger; |
|---|
| 12 | 13 | import org.slf4j.LoggerFactory; |
|---|
| 13 | 14 | import org.springframework.beans.factory.annotation.Autowired; |
|---|
| 15 | +import org.springframework.beans.factory.annotation.Value; |
|---|
| 14 | 16 | import org.springframework.mail.javamail.JavaMailSender; |
|---|
| 15 | 17 | import org.springframework.mail.javamail.MimeMessageHelper; |
|---|
| 16 | 18 | import org.springframework.stereotype.Controller; |
|---|
| 17 | 19 | import org.springframework.validation.BindingResult; |
|---|
| 18 | 20 | import org.springframework.web.bind.annotation.ControllerAdvice; |
|---|
| 21 | +import org.springframework.web.bind.annotation.ExceptionHandler; |
|---|
| 19 | 22 | import org.springframework.web.bind.annotation.ModelAttribute; |
|---|
| 20 | 23 | import org.springframework.web.bind.annotation.PostMapping; |
|---|
| 21 | 24 | import org.springframework.web.bind.annotation.RequestMapping; |
|---|
| 25 | +import org.springframework.web.multipart.MaxUploadSizeExceededException; |
|---|
| 26 | +import org.springframework.web.multipart.MultipartFile; |
|---|
| 22 | 27 | import org.springframework.web.servlet.ModelAndView; |
|---|
| 23 | 28 | |
|---|
| 24 | 29 | import it.digione.dg1cloud.form.StartURPForm; |
|---|
| .. | .. |
|---|
| 45 | 50 | @Autowired IsdConfig isdConfig; |
|---|
| 46 | 51 | @Autowired JavaMailSender javaMailSender; |
|---|
| 47 | 52 | |
|---|
| 53 | + @Value("${tomcat.MaxSwallowSize}") |
|---|
| 54 | + private int maxFileSize; |
|---|
| 55 | + |
|---|
| 48 | 56 | @ModelAttribute("startURPForm") |
|---|
| 49 | 57 | public StartURPForm startURPForm() { |
|---|
| 50 | 58 | return new StartURPForm(); |
|---|
| .. | .. |
|---|
| 55 | 63 | return "startURP"; |
|---|
| 56 | 64 | } |
|---|
| 57 | 65 | @PostMapping("/sendStartRequest") |
|---|
| 58 | | - public Object sendStartRequest(@ModelAttribute("startURPForm") @Valid StartURPForm form, |
|---|
| 66 | + public ModelAndView sendStartRequest(@ModelAttribute("startURPForm") @Valid StartURPForm form, |
|---|
| 59 | 67 | BindingResult result) { |
|---|
| 60 | 68 | |
|---|
| 61 | 69 | ModelAndView modelAndView; |
|---|
| 62 | | - if (result.hasErrors()){ |
|---|
| 70 | + if (result.hasErrors()) { |
|---|
| 63 | 71 | modelAndView = new ModelAndView("startURP"); |
|---|
| 64 | 72 | modelAndView.addObject(result); |
|---|
| 65 | 73 | modelAndView.addObject("startURPForm", form); |
|---|
| .. | .. |
|---|
| 94 | 102 | helper = new MimeMessageHelper(message, true); |
|---|
| 95 | 103 | helper.setSubject("Richiesta inviata correttamente al servizio URP"); |
|---|
| 96 | 104 | helper.setFrom(isdProperties.getNotificationFrom()); |
|---|
| 97 | | - helper.setTo(form.getMittente()); |
|---|
| 105 | + helper.setTo(form.getEmail()); |
|---|
| 98 | 106 | helper.setText("Richiesta inviata correttamente: " + numeroProtocollo + " del " + dataProtocollo, true); |
|---|
| 99 | 107 | javaMailSender.send(message); |
|---|
| 100 | 108 | |
|---|
| .. | .. |
|---|
| 129 | 137 | Contacts contacts = factory.createInstanceMessageCreateRequestContacts(); |
|---|
| 130 | 138 | |
|---|
| 131 | 139 | 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())); |
|---|
| 134 | 143 | contact.setType("F"); |
|---|
| 135 | 144 | contact.setReferenceType("AB"); |
|---|
| 136 | 145 | contact.setSearchType("EXTID"); |
|---|
| 137 | 146 | contact.setSearchMode("NOTFOUNDDEF"); |
|---|
| 138 | | - contact.setSearchValue(startURPForm.getMittente()); |
|---|
| 147 | + contact.setSearchValue(startURPForm.getEmail()); |
|---|
| 139 | 148 | |
|---|
| 140 | 149 | contacts.getContact().add(contact); |
|---|
| 141 | 150 | |
|---|
| .. | .. |
|---|
| 172 | 181 | |
|---|
| 173 | 182 | Attachments attachments = new Attachments(); |
|---|
| 174 | 183 | |
|---|
| 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); |
|---|
| 183 | 203 | } |
|---|
| 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 | + } |
|---|
| 188 | 208 | |
|---|
| 189 | 209 | return request; |
|---|
| 190 | 210 | } |
|---|
| 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 | + } |
|---|
| 191 | 221 | } |
|---|