1 | /* |
---|
2 | * Created on 3 mai 2005 |
---|
3 | */ |
---|
4 | package com.medias.megapoli.struts.actions; |
---|
5 | |
---|
6 | import com.medias.annuaire.Personne; |
---|
7 | import com.medias.megapoli.struts.forms.DatasForm; |
---|
8 | import com.medias.megapoli.utils.Requete; |
---|
9 | import org.apache.struts.action.Action; |
---|
10 | import org.apache.struts.action.ActionErrors; |
---|
11 | import org.apache.struts.action.ActionForm; |
---|
12 | import org.apache.struts.action.ActionForward; |
---|
13 | import org.apache.struts.action.ActionMapping; |
---|
14 | import org.jdom.Document; |
---|
15 | import org.jdom.output.XMLOutputter; |
---|
16 | |
---|
17 | import javax.servlet.ServletException; |
---|
18 | import javax.servlet.http.HttpServletRequest; |
---|
19 | import javax.servlet.http.HttpServletResponse; |
---|
20 | import java.io.BufferedReader; |
---|
21 | import java.io.DataOutputStream; |
---|
22 | import java.io.InputStreamReader; |
---|
23 | import java.net.URL; |
---|
24 | import java.net.URLConnection; |
---|
25 | import java.net.URLEncoder; |
---|
26 | |
---|
27 | /** |
---|
28 | * @author pignot |
---|
29 | * |
---|
30 | * Action déclenchée au moment de lancer l'extraction |
---|
31 | */ |
---|
32 | public class DataExtractAction extends Action { |
---|
33 | public ActionForward execute(ActionMapping mapping, ActionForm form, |
---|
34 | HttpServletRequest request, HttpServletResponse response) |
---|
35 | throws ServletException { |
---|
36 | String target = "success"; |
---|
37 | if (this.isCancelled(request)) { |
---|
38 | target = "cancel"; |
---|
39 | return mapping.findForward(target); |
---|
40 | } |
---|
41 | |
---|
42 | ActionErrors errors = new ActionErrors(); |
---|
43 | if (form != null) { |
---|
44 | DatasForm dform = (DatasForm) form; |
---|
45 | XMLOutputter outXml = new XMLOutputter(); |
---|
46 | Personne pers = (Personne)request.getSession().getAttribute("SES_USER"); |
---|
47 | if (pers == null) { |
---|
48 | target = "cancel"; |
---|
49 | return mapping.findForward(target); |
---|
50 | } |
---|
51 | Document docXml = Requete.toXml( dform, pers ); |
---|
52 | String requete = outXml.outputString(docXml); |
---|
53 | try { |
---|
54 | String encoded = "requete="+URLEncoder.encode(requete,"UTF-8"); |
---|
55 | // Configuration de l'URL |
---|
56 | String urlCGIStr = (String) request.getSession().getServletContext().getAttribute("APP_CGI"); |
---|
57 | URL urlCGI = new URL(urlCGIStr); |
---|
58 | URLConnection conn = urlCGI.openConnection(); |
---|
59 | conn.setDoOutput(true); |
---|
60 | conn.setUseCaches(false); |
---|
61 | conn.setRequestProperty("content-type", "application/x-www-form-urlencoded"); |
---|
62 | // Envoi de la requête |
---|
63 | DataOutputStream out = new DataOutputStream(conn.getOutputStream()); |
---|
64 | out.writeBytes(encoded); |
---|
65 | out.flush(); |
---|
66 | out.close(); |
---|
67 | |
---|
68 | // Réponse du CGI |
---|
69 | BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream())); |
---|
70 | String aLine; |
---|
71 | while ((aLine = in.readLine()) != null) { |
---|
72 | if (aLine.length() == 0) continue; |
---|
73 | if (aLine.startsWith("1")) { |
---|
74 | System.err.println("ERROR :"+aLine); |
---|
75 | } else System.out.println("REPONSE :"+aLine); |
---|
76 | |
---|
77 | } |
---|
78 | } catch (Exception e) { |
---|
79 | e.printStackTrace(); |
---|
80 | } |
---|
81 | |
---|
82 | } |
---|
83 | if (!errors.isEmpty()) { |
---|
84 | saveErrors(request, errors); |
---|
85 | } |
---|
86 | return mapping.findForward(target); |
---|
87 | } |
---|
88 | } |
---|