svn-gvsig-desktop / trunk / applications / appCatalogAndGazetteerClient / src / es / gva / cit / catalog / protocols / HTTPPostProtocol.java @ 39586
History | View | Annotate | Download (4.72 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.FileOutputStream; |
47 |
import java.io.IOException; |
48 |
import java.io.InputStream; |
49 |
import java.io.InputStreamReader; |
50 |
import java.io.OutputStreamWriter; |
51 |
import java.net.HttpURLConnection; |
52 |
import java.net.URL; |
53 |
import java.util.Collection; |
54 |
|
55 |
import org.apache.log4j.Logger; |
56 |
|
57 |
import com.iver.utiles.xml.XMLEncodingUtils; |
58 |
|
59 |
import es.gva.cit.catalog.metadataxml.XMLNode; |
60 |
import es.gva.cit.catalog.metadataxml.XMLTree; |
61 |
|
62 |
/**
|
63 |
* This class implement the HTTP Post protocol.
|
64 |
*
|
65 |
*
|
66 |
* @author Jorge Piera Llodra (piera_jor@gva.es)
|
67 |
*/
|
68 |
public class HTTPPostProtocol implements IProtocols { |
69 |
|
70 |
private static Logger logger = Logger.getLogger(HTTPPostProtocol.class); |
71 |
/**
|
72 |
* @return
|
73 |
* @param url
|
74 |
* @param message
|
75 |
* @param firstRecord
|
76 |
*/
|
77 |
public Collection doQuery(URL url, Object message, int firstRecord) { |
78 |
String body = (String) message; |
79 |
Collection col = new java.util.ArrayList();; |
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 |
if (logger.isDebugEnabled()) {
|
96 |
// read the stream as a String to allow logging it before parsing it to XMLTree object
|
97 |
InputStream is = c.getInputStream();
|
98 |
char[] buf = new char[1024]; |
99 |
int len;
|
100 |
StringBuilder strBuilder = new StringBuilder(); |
101 |
|
102 |
XMLEncodingUtils xmlEnc = new XMLEncodingUtils(is);
|
103 |
InputStreamReader isReader = xmlEnc.getReader();
|
104 |
String encoding = isReader.getEncoding();
|
105 |
BufferedReader bufReader = new BufferedReader(isReader); |
106 |
strBuilder.append(xmlEnc.getBytesRead()); |
107 |
|
108 |
while ((len = bufReader.read(buf))>0) { |
109 |
strBuilder.append(buf, 0, len);
|
110 |
} |
111 |
|
112 |
String str = strBuilder.toString();
|
113 |
System.out.println(str);
|
114 |
ByteArrayInputStream output = new ByteArrayInputStream(str.getBytes(encoding)); |
115 |
|
116 |
col.add(XMLTree.xmlToTree(output)); |
117 |
} |
118 |
else {
|
119 |
// when no logging is necessary, we can directly pass the connection IS to the parser, which will deal with encodings
|
120 |
XMLNode node = XMLTree.xmlToTree(c.getInputStream()); |
121 |
col.add(node); |
122 |
} |
123 |
} catch (IOException e) { |
124 |
logger.error(e.getMessage(), e); |
125 |
} |
126 |
return col;
|
127 |
} |
128 |
|
129 |
public void doQuery(URL url, Object message, int firstRecord, String fileName) { |
130 |
String body = (String) message; |
131 |
FileOutputStream output = null; |
132 |
|
133 |
try {
|
134 |
HttpURLConnection c = (HttpURLConnection) url.openConnection(); |
135 |
|
136 |
c.setRequestProperty("SOAPAction","post"); |
137 |
c.setRequestMethod("POST");
|
138 |
c.setDoOutput(true);
|
139 |
c.setRequestProperty("Content-Type", "text/xml; charset=UTF-8"); |
140 |
|
141 |
// Write the request.
|
142 |
OutputStreamWriter w =
|
143 |
new OutputStreamWriter(c.getOutputStream(), "UTF-8"); |
144 |
|
145 |
w.write(body); |
146 |
w.flush(); |
147 |
|
148 |
InputStream is = c.getInputStream();
|
149 |
byte[] buf = new byte[1024]; |
150 |
int len;
|
151 |
String str = ""; |
152 |
while ((len = is.read(buf)) > 0) { |
153 |
str = str + new String(buf, 0, len); |
154 |
} |
155 |
|
156 |
System.out.println(str);
|
157 |
output = new FileOutputStream(new File(fileName)); |
158 |
output.write(str.getBytes()); |
159 |
output.flush(); |
160 |
output.close(); |
161 |
|
162 |
|
163 |
} catch (IOException e) { |
164 |
logger.error(e.getMessage(), e); |
165 |
} |
166 |
|
167 |
} |
168 |
} |