Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / applications / appCatalog / src / org / gvsig / catalog / protocols / HTTPPostProtocol.java @ 37871

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 org.gvsig.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 org.slf4j.Logger;
54
import org.slf4j.LoggerFactory;
55

    
56
import org.gvsig.catalog.metadataxml.XMLTree;
57
import org.gvsig.catalog.utils.Strings;
58

    
59

    
60
/**
61
 * This class implement the HTTP Post protocol.
62
 * 
63
 * 
64
 * @author Jorge Piera Llodra (piera_jor@gva.es)
65
 */
66
public class HTTPPostProtocol implements IProtocols {
67
    private static final Logger LOG =
68
        LoggerFactory.getLogger(HTTPPostProtocol.class);
69
    
70
    /**
71
     * @return 
72
     * @param url 
73
     * @param message 
74
     * @param firstRecord 
75
     */
76
    public Collection doQuery(URL url, Object message, int firstRecord) {        
77
        String body = (String) message;
78
        ByteArrayInputStream output = null;
79
            
80
        try {
81
            HttpURLConnection c = (HttpURLConnection) url.openConnection();
82
            
83
         c.setRequestProperty("SOAPAction","post");
84
         c.setRequestMethod("POST");
85
         c.setDoOutput(true);
86
         c.setRequestProperty("Content-Type", "text/xml; charset=UTF-8");
87
                 
88
        // Write the request.
89
        OutputStreamWriter w =
90
            new OutputStreamWriter(c.getOutputStream(), "UTF-8");
91
        
92
        w.write(body);
93
        w.flush();
94
              
95
        InputStream is = c.getInputStream();
96
        byte[] buf = new byte[1024];
97
        int len;
98
        String str = "";
99
        
100
        while ((len = is.read(buf)) > 0) {
101
            str = str + new String(buf, 0, len);
102
        }
103
            
104
        str = Strings.replace(str,
105
                                  "<?xml version=\"1.0\" encoding=\"UTF-8\"?>",
106
                  "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>");
107
        System.out.println(str);
108
        output = new ByteArrayInputStream(str.getBytes());
109
            
110
        }  catch (IOException e) {
111
            LOG.error("Error sending the POST request", e);
112
            return null;
113
        } 
114
        
115
        Collection col = new java.util.ArrayList();
116
        col.add(XMLTree.xmlToTree(output));
117
        return col;            
118
    }    
119

    
120
    public void doQuery(URL url, Object message, int firstRecord, String fileName) {        
121
            String body = (String) message;
122
            FileOutputStream output = null;
123

    
124
            try {
125
                    HttpURLConnection c = (HttpURLConnection) url.openConnection();
126

    
127
                    c.setRequestProperty("SOAPAction","post");
128
                    c.setRequestMethod("POST");
129
                    c.setDoOutput(true);
130
                    c.setRequestProperty("Content-Type", "text/xml; charset=UTF-8");
131

    
132
                    // Write the request.
133
                    OutputStreamWriter w =
134
                            new OutputStreamWriter(c.getOutputStream(), "UTF-8");
135

    
136
                    w.write(body);
137
                    w.flush();
138

    
139
                    InputStream is = c.getInputStream();
140
                    byte[] buf = new byte[1024];
141
                    int len;
142
                    String str = "";
143
                    while ((len = is.read(buf)) > 0) {
144
                            str = str + new String(buf, 0, len);
145
                    }
146
        
147
                    System.out.println(str);
148
                    output = new FileOutputStream(new File(fileName));
149
                    output.write(str.getBytes());
150
                    output.flush();
151
                    output.close();
152
            
153
                    
154
            }  catch (IOException e) {
155
                LOG.error("Error sending the POST request", e);
156
            }           
157
             
158
    }    
159
 }