Statistics
| Revision:

root / branches / v2_0_0_prep / applications / appCatalog / src / org / gvsig / catalog / protocols / SOAPProtocol.java @ 37871

History | View | Annotate | Download (4.74 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 org.gvsig.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.slf4j.Logger;
60
import org.slf4j.LoggerFactory;
61
import org.w3c.dom.Document;
62
import org.xml.sax.InputSource;
63

    
64
import org.gvsig.catalog.metadataxml.XMLTree;
65

    
66

    
67
/**
68
 * 
69
 * 
70
 * 
71
 * @author Jorge Piera Llodra (piera_jor@gva.es)
72
 */
73
public class SOAPProtocol implements IProtocols {
74
    private static final Logger LOG =
75
        LoggerFactory.getLogger(SOAPProtocol.class);
76
    
77
    /**
78
     * @return 
79
     * @param url 
80
     * @param object 
81
     * @param firstRecord 
82
     */
83
    public Collection doQuery(URL url, Object object, int firstRecord) {    
84
        String message = (String) object;
85
        File fileAnswer = null;
86

    
87
        //get the envelope to send
88
        try {
89
            InputStream buffer = new ByteArrayInputStream(message.getBytes());
90
            DocumentBuilder xdb = XMLParserUtils.getXMLDocBuilder();
91
            Document doc = xdb.parse(new InputSource(buffer));
92
            if (doc == null) {
93
                throw new SOAPException(Constants.FAULT_CODE_CLIENT,
94
                "parsing error");
95
            }
96
            Envelope msgEnv = Envelope.unmarshall(doc.getDocumentElement());
97
            // send the message
98
            Message msg = new Message();
99
            msg.send(url, "urn:this-is-the-action-uri", msgEnv);
100
            // receive whatever from the transport and dump it to the screen
101
            SOAPTransport st = msg.getSOAPTransport();
102
            BufferedReader br = st.receive();
103
            String line;
104
            //File to save the answer
105
            fileAnswer = new File("auxiliar");
106
            FileWriter fwAnswer = new FileWriter(fileAnswer);
107
            fwAnswer.write("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>");
108
            //Process the answer                                
109
            while ((line = br.readLine()) != null) {
110
                System.out.println(line);
111
                fwAnswer.write(line);
112
                fwAnswer.flush();
113
            }
114
            fwAnswer.close();
115

    
116

    
117
        } catch (Exception e) {
118
            LOG.error("Error sending the SOAP request", e);
119
            return null;
120
        }
121
        Collection col = new java.util.ArrayList();
122
        col.add(XMLTree.xmlToTree(fileAnswer));
123
        return col;
124
    } 
125

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

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

    
157
    } 
158
}