source: ether_statistics/service/implementation/com/medias/megapoli/struts/actions/DataExtractAction.java @ 569

Last change on this file since 569 was 569, checked in by vmipsl, 12 years ago

Nouveau projet

File size: 2.9 KB
Line 
1/*
2 * Created on 3 mai 2005
3 */
4package com.medias.megapoli.struts.actions;
5
6import com.medias.annuaire.Personne;
7import com.medias.megapoli.struts.forms.DatasForm;
8import com.medias.megapoli.utils.Requete;
9import org.apache.struts.action.Action;
10import org.apache.struts.action.ActionErrors;
11import org.apache.struts.action.ActionForm;
12import org.apache.struts.action.ActionForward;
13import org.apache.struts.action.ActionMapping;
14import org.jdom.Document;
15import org.jdom.output.XMLOutputter;
16
17import javax.servlet.ServletException;
18import javax.servlet.http.HttpServletRequest;
19import javax.servlet.http.HttpServletResponse;
20import java.io.BufferedReader;
21import java.io.DataOutputStream;
22import java.io.InputStreamReader;
23import java.net.URL;
24import java.net.URLConnection;
25import java.net.URLEncoder;
26
27/**
28 * @author pignot
29 *
30 * Action déclenchée au moment de lancer l'extraction
31 */
32public 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}
Note: See TracBrowser for help on using the repository browser.