Statistics
| Revision:

root / trunk / applications / appCatalogAndGazetteerClient / src / es / gva / cit / catalog / protocols / SOAPProtocol.java @ 15558

History | View | Annotate | Download (4.47 KB)

1

    
2
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
3
*
4
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
5
*
6
* This program is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU General Public License
8
* as published by the Free Software Foundation; either version 2
9
* of the License, or (at your option) any later version.
10
*
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
* GNU General Public License for more details.
15
*
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
19
*
20
* For more information, contact:
21
*
22
*  Generalitat Valenciana
23
*   Conselleria d'Infraestructures i Transport
24
*   Av. Blasco Ib??ez, 50
25
*   46010 VALENCIA
26
*   SPAIN
27
*
28
*      +34 963862235
29
*   gvsig@gva.es
30
*      www.gvsig.gva.es
31
*
32
*    or
33
*
34
*   IVER T.I. S.A
35
*   Salamanca 50
36
*   46005 Valencia
37
*   Spain
38
*
39
*   +34 963163400
40
*   dac@iver.es
41
*/
42
package es.gva.cit.catalog.protocols;
43
import java.io.BufferedReader;
44
import java.io.ByteArrayInputStream;
45
import java.io.File;
46
import java.io.FileWriter;
47
import java.io.InputStream;
48
import java.net.URL;
49
import java.util.Collection;
50

    
51
import javax.xml.parsers.DocumentBuilder;
52

    
53
import org.apache.soap.Constants;
54
import org.apache.soap.Envelope;
55
import org.apache.soap.SOAPException;
56
import org.apache.soap.messaging.Message;
57
import org.apache.soap.transport.SOAPTransport;
58
import org.apache.soap.util.xml.XMLParserUtils;
59
import org.w3c.dom.Document;
60
import org.xml.sax.InputSource;
61

    
62
import es.gva.cit.catalog.metadataxml.XMLTree;
63

    
64
/**
65
 * 
66
 * 
67
 * 
68
 * @author Jorge Piera Llodra (piera_jor@gva.es)
69
 */
70
public class SOAPProtocol implements IProtocols {
71

    
72
/**
73
 * 
74
 * 
75
 * 
76
 * @return 
77
 * @param url 
78
 * @param object 
79
 * @param firstRecord 
80
 */
81
    public Collection doQuery(URL url, Object object, int firstRecord) {        
82
        String message = (String) object;
83
        File fileAnswer = null;
84
       
85
        //get the envelope to send
86
        try {
87
            InputStream buffer = new ByteArrayInputStream(message.getBytes());
88
            DocumentBuilder xdb = XMLParserUtils.getXMLDocBuilder();
89
            Document doc = xdb.parse(new InputSource(buffer));
90
            if (doc == null) {
91
                throw new SOAPException(Constants.FAULT_CODE_CLIENT,
92
                    "parsing error");
93
            }
94
            Envelope msgEnv = Envelope.unmarshall(doc.getDocumentElement());
95
            // send the message
96
            Message msg = new Message();
97
            msg.send(url, "urn:this-is-the-action-uri", msgEnv);
98
            // receive whatever from the transport and dump it to the screen
99
            SOAPTransport st = msg.getSOAPTransport();
100
            BufferedReader br = st.receive();
101
            String line;
102
            //File to save the answer
103
            fileAnswer = new File("auxiliar");
104
            FileWriter fwAnswer = new FileWriter(fileAnswer);
105
            fwAnswer.write("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>");
106
            //Process the answer                                
107
            while ((line = br.readLine()) != null) {
108
                System.out.println(line);
109
                fwAnswer.write(line);
110
                fwAnswer.flush();
111
            }
112
            fwAnswer.close();
113
               
114
         
115
        } catch (Exception e) {
116
            return null;
117
        }
118
        Collection col = new java.util.ArrayList();
119
        col.add(XMLTree.xmlToTree(fileAnswer));
120
        return col;
121
    } 
122

    
123
/**
124
 * 
125
 * 
126
 * 
127
 * @return 
128
 * @param url 
129
 */
130
    public static boolean isProtocolSupported(URL url) {        
131
        return true;
132
    } 
133

    
134
/**
135
 * This function returns a SOAP message
136
 * 
137
 * 
138
 * @return 
139
 * @param message the XML message
140
 * @param schemas 
141
 */
142
    public static String setSOAPMessage(String message, String[] schemas) {        
143
        String soap =  "<SOAP:Envelope xmlns:SOAP=\"http://schemas.xmlsoap.org/soap/envelope/\" " +
144
                "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"" ;
145
        if (schemas != null){
146
            for (int i=0 ; i<schemas.length ; i++){
147
                soap = soap + " " + schemas[i];
148
            }
149
        }
150
        soap = soap + ">";
151
        soap = soap + "<SOAP:Body>" + message + "</SOAP:Body>" + "</SOAP:Envelope>";
152
        return soap;
153
        
154
    } 
155
 }