| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 
 |     public JSONObject executeSOAPQuery(String codeBatch, JSONObject params, String WebServiceURL) throws JSONException {
 
 
        Date fullDate = new Date();
 
        SOAPConnection soapConnection;
        try {
            soapConnection = SOAPConnectionFactory.newInstance().createConnection();
 
            URL endpoint = new URL(new URL(WebServiceURL), "WSAppelBatch/", 
                    new URLStreamHandler() {
 
                @Override
                protected URLConnection openConnection(URL u) throws IOException {
                    URL clone = new URL (u.toString ());
                    URLConnection connection = clone.openConnection ();
                    connection.setConnectTimeout (600   1000);      // 10 minutes
                    connection.setReadTimeout (0);                   // Infinite Timeout
                    return connection;
                }
            });
 
            fullDate = new Date();
            System.out.println("Lancement du batch " + codeBatch + " avec les paramètres " + ((params == null) ? "null" : params.toString()));
            System.out.println(params);
            SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(codeBatch, params), endpoint);
            // 
            final StringWriter sw = new StringWriter();
 
            try {
                TransformerFactory.newInstance().newTransformer().transform(
                    new DOMSource(soapResponse.getSOAPPart()),
                    new StreamResult(sw));
                System.out.println(sw);
            } catch (TransformerException e) {
                System.out.println("ec");
                e.printStackTrace();
            }
 
            JSONObject fullResult = XML.toJSONObject(sw.toString());
            String retString = fullResult.getJSONObject("soapenv:Envelope")
                    .getJSONObject("soapenv:Body")
                    .getJSONObject("ns:ExecuteBatchResponse")
                    .getString("ns:return");
            JSONObject retJson = XML.toJSONObject(retString).getJSONObject("RACINE");
            soapConnection.close();
 
            retJson.put("redacteur", "HHH");
            retJson.put("codeBatch", codeBatch);
            retJson.put("date", dayFormat.format(fullDate));
            retJson.put("heure", hourFormat.format(fullDate));
 
            System.out.println(retJson);
            return retJson;
 
        } catch (MalformedURLException | UnsupportedOperationException | SOAPException | TimeoutException e) {
            e.printStackTrace();
            JSONObject retJson = new JSONObject();
 
            retJson.put("redacteur", "HHH");
            retJson.put("codeBatch", codeBatch);
            retJson.put("date", dayFormat.format(fullDate));
            retJson.put("heure", hourFormat.format(fullDate));
 
            System.out.println(retJson);
            return retJson;
        }      
    }
 
 
    private SOAPMessage createSOAPRequest(String codeBatch, JSONObject params) throws SOAPException, JSONException {
        JSONObject batchParams = ParamBatch.getBatchParam(codeBatch, params);
        SOAPMessage soapMessage = MessageFactory.newInstance().createMessage();
        SOAPPart soapPart = soapMessage.getSOAPPart();
 
        String serverURI = "http://WebServices.Communes";
        SOAPEnvelope envelope = soapPart.getEnvelope();
        envelope.addNamespaceDeclaration("bat", serverURI);
 
        SOAPBody soapBody = envelope.getBody();
        SOAPElement soapBodyElem = soapBody.addChildElement("ExecuteBatch", "bat");
        SOAPElement soapBodyElemCode = soapBodyElem.addChildElement("CodeBatch", "bat");
        soapBodyElemCode.addTextNode(batchParams.getString("codeBatch"));
        SOAPElement soapBodyElemParams = soapBodyElem.addChildElement("Params", "bat");
        soapBodyElemParams.addTextNode(batchParams.getString("params"));
 
        soapMessage.saveChanges();
 
 
        return soapMessage;
    }
 
 
} | 
Partager