1 | package com.medias.megapoli.struts.actions; |
---|
2 | |
---|
3 | import org.apache.struts.action.Action; |
---|
4 | import org.apache.struts.action.ActionForward; |
---|
5 | import org.apache.struts.action.ActionForm; |
---|
6 | import org.apache.struts.action.ActionMapping; |
---|
7 | import org.apache.struts.action.ActionMessages; |
---|
8 | import org.apache.struts.action.ActionMessage; |
---|
9 | import org.apache.struts.action.ActionErrors; |
---|
10 | import org.apache.struts.action.ActionError; |
---|
11 | import org.apache.struts.upload.FormFile; |
---|
12 | |
---|
13 | import javax.servlet.http.HttpServletRequest; |
---|
14 | import javax.servlet.http.HttpServletResponse; |
---|
15 | import javax.servlet.ServletException; |
---|
16 | |
---|
17 | import com.medias.xml.hierarchie.Hierarchie; |
---|
18 | import com.medias.megapoli.struts.forms.UploadForm; |
---|
19 | import com.medias.Context; |
---|
20 | import com.medias.mail.Mail; |
---|
21 | import com.medias.mail.MailFactory; |
---|
22 | |
---|
23 | |
---|
24 | /** |
---|
25 | * @author combaz |
---|
26 | * Created on 3 févr. 2005 |
---|
27 | * |
---|
28 | */ |
---|
29 | public class UploadAction extends Action { |
---|
30 | |
---|
31 | /** |
---|
32 | * Copie le fichier récupéré par l'UploadForm à l'endroit de l'arborescence qui lui a été attribué, |
---|
33 | * et répercute ces modifications dans la hiérarchie... |
---|
34 | * @param mapping Le mapping associé à cette action |
---|
35 | * @param form Le formulaire (UploadForm) associé à cette action |
---|
36 | * @param request La requete qui a donné lieu à l'exécution de l'action |
---|
37 | * @param response La reponse |
---|
38 | * @return target L'identifiant de la page a afficher a l'issue de l'execution. |
---|
39 | * @throws ServletException |
---|
40 | */ |
---|
41 | public ActionForward execute(ActionMapping mapping,ActionForm form, |
---|
42 | HttpServletRequest request, HttpServletResponse response) |
---|
43 | throws ServletException { |
---|
44 | String target = "failure"; |
---|
45 | ActionErrors errors = new ActionErrors(); |
---|
46 | ActionMessages messages = new ActionMessages(); |
---|
47 | |
---|
48 | if(form != null) { |
---|
49 | UploadForm upload = (UploadForm) form; |
---|
50 | String path = upload.getPath(); |
---|
51 | FormFile file = upload.getNewFile(); |
---|
52 | if((file.getFileSize()!=0) && (path.length()!=0)) { |
---|
53 | Hierarchie dataTree = (Hierarchie)this.getServlet().getServletContext().getAttribute("APP_UPLOADTREE"); |
---|
54 | try { |
---|
55 | dataTree.saveFile(path,file); |
---|
56 | //Envoi d'un mail automatique au webmaster |
---|
57 | MailFactory mf = (MailFactory) this.servlet.getServletContext().getAttribute("APP_MAILFACTORY"); |
---|
58 | String from = "megapoli_info@obs-mip.fr"; |
---|
59 | String to = Context.getWebmaster(request); |
---|
60 | String subject = "[MEGAPOLI] Fichier '"+file.getFileName()+"' depose dans '"+upload.getPath()+"'"; |
---|
61 | Mail mail = new Mail(from, to, null, "", subject); |
---|
62 | mf.sendMail(mail); |
---|
63 | target="success"; |
---|
64 | messages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("data.upload.succeded")); |
---|
65 | } |
---|
66 | catch(Exception e){ |
---|
67 | System.out.println ("exception_uploadAction"); |
---|
68 | errors.add(ActionErrors.GLOBAL_MESSAGE, new ActionError("errors.upload.failed")); |
---|
69 | e.printStackTrace(); |
---|
70 | } |
---|
71 | } |
---|
72 | else { |
---|
73 | errors.add(ActionErrors.GLOBAL_MESSAGE, new ActionError("errors.upload.file.required")); |
---|
74 | } |
---|
75 | } |
---|
76 | if(!errors.isEmpty()){ |
---|
77 | saveErrors(request, errors); |
---|
78 | } |
---|
79 | if(!messages.isEmpty()){ |
---|
80 | saveMessages(request, messages); |
---|
81 | } |
---|
82 | |
---|
83 | return (mapping.findForward(target)); |
---|
84 | } |
---|
85 | |
---|
86 | } |
---|