Statistics
| Revision:

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

History | View | Annotate | Download (4.3 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.ByteArrayInputStream;
44
import java.io.File;
45
import java.io.FileOutputStream;
46
import java.io.IOException;
47
import java.io.InputStream;
48
import java.io.OutputStreamWriter;
49
import java.net.HttpURLConnection;
50
import java.net.URL;
51
import java.util.Collection;
52

    
53
import es.gva.cit.catalog.metadataxml.XMLTree;
54
import es.gva.cit.catalog.utils.Strings;
55

    
56
/**
57
 * This class implement the HTTP Post protocol.
58
 * 
59
 * 
60
 * @author Jorge Piera Llodra (piera_jor@gva.es)
61
 */
62
public class HTTPPostProtocol implements IProtocols {
63

    
64
/**
65
 * @return 
66
 * @param url 
67
 * @param message 
68
 * @param firstRecord 
69
 */
70
    public Collection doQuery(URL url, Object message, int firstRecord) {        
71
        String body = (String) message;
72
        ByteArrayInputStream output = null;
73
            
74
        try {
75
            HttpURLConnection c = (HttpURLConnection) url.openConnection();
76
            
77
         c.setRequestProperty("SOAPAction","post");
78
         c.setRequestMethod("POST");
79
         c.setDoOutput(true);
80
         c.setRequestProperty("Content-Type", "text/xml; charset=UTF-8");
81
                 
82
        // Write the request.
83
        OutputStreamWriter w =
84
            new OutputStreamWriter(c.getOutputStream(), "UTF-8");
85
        
86
        w.write(body);
87
        w.flush();
88
              
89
        InputStream is = c.getInputStream();
90
        byte[] buf = new byte[1024];
91
        int len;
92
        String str = "";
93
        
94
        while ((len = is.read(buf)) > 0) {
95
            str = str + new String(buf, 0, len);
96
        }
97
            
98
        str = Strings.replace(str,
99
                                  "<?xml version=\"1.0\" encoding=\"UTF-8\"?>",
100
                  "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>");
101
        System.out.println(str);
102
        output = new ByteArrayInputStream(str.getBytes());
103
            
104
        }  catch (IOException e) {
105
            // TODO Auto-generated catch block
106
            //e.printStackTrace();
107
            return null;
108
        } 
109
        
110
        Collection col = new java.util.ArrayList();
111
        col.add(XMLTree.xmlToTree(output));
112
        return col;            
113
    }    
114

    
115
    public void doQuery(URL url, Object message, int firstRecord, String fileName) {        
116
            String body = (String) message;
117
            FileOutputStream output = null;
118

    
119
            try {
120
                    HttpURLConnection c = (HttpURLConnection) url.openConnection();
121

    
122
                    c.setRequestProperty("SOAPAction","post");
123
                    c.setRequestMethod("POST");
124
                    c.setDoOutput(true);
125
                    c.setRequestProperty("Content-Type", "text/xml; charset=UTF-8");
126

    
127
                    // Write the request.
128
                    OutputStreamWriter w =
129
                            new OutputStreamWriter(c.getOutputStream(), "UTF-8");
130

    
131
                    w.write(body);
132
                    w.flush();
133

    
134
                    InputStream is = c.getInputStream();
135
                    byte[] buf = new byte[1024];
136
                    int len;
137
                    String str = "";
138
                    while ((len = is.read(buf)) > 0) {
139
                            str = str + new String(buf, 0, len);
140
                    }
141
        
142
                    System.out.println(str);
143
                    output = new FileOutputStream(new File(fileName));
144
                    output.write(str.getBytes());
145
                    output.flush();
146
                    output.close();
147
            
148
                    
149
            }  catch (IOException e) {
150
                    e.printStackTrace();           
151
            }           
152
             
153
    }    
154
 }