Revision 39586 trunk/applications/appCatalogAndGazetteerClient/src/es/gva/cit/catalog/protocols/HTTPPostProtocol.java
HTTPPostProtocol.java | ||
---|---|---|
40 | 40 |
* dac@iver.es |
41 | 41 |
*/ |
42 | 42 |
package es.gva.cit.catalog.protocols; |
43 |
import java.io.BufferedReader; |
|
43 | 44 |
import java.io.ByteArrayInputStream; |
44 | 45 |
import java.io.File; |
45 | 46 |
import java.io.FileOutputStream; |
46 | 47 |
import java.io.IOException; |
47 | 48 |
import java.io.InputStream; |
49 |
import java.io.InputStreamReader; |
|
48 | 50 |
import java.io.OutputStreamWriter; |
49 | 51 |
import java.net.HttpURLConnection; |
50 | 52 |
import java.net.URL; |
51 | 53 |
import java.util.Collection; |
52 | 54 |
|
55 |
import org.apache.log4j.Logger; |
|
56 |
|
|
57 |
import com.iver.utiles.xml.XMLEncodingUtils; |
|
58 |
|
|
59 |
import es.gva.cit.catalog.metadataxml.XMLNode; |
|
53 | 60 |
import es.gva.cit.catalog.metadataxml.XMLTree; |
54 |
import es.gva.cit.catalog.utils.Strings; |
|
55 | 61 |
|
56 | 62 |
/** |
57 | 63 |
* This class implement the HTTP Post protocol. |
... | ... | |
61 | 67 |
*/ |
62 | 68 |
public class HTTPPostProtocol implements IProtocols { |
63 | 69 |
|
70 |
private static Logger logger = Logger.getLogger(HTTPPostProtocol.class); |
|
64 | 71 |
/** |
65 | 72 |
* @return |
66 | 73 |
* @param url |
67 | 74 |
* @param message |
68 | 75 |
* @param firstRecord |
69 | 76 |
*/ |
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 |
} |
|
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(); |
|
114 | 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 |
|
|
115 | 129 |
public void doQuery(URL url, Object message, int firstRecord, String fileName) { |
116 | 130 |
String body = (String) message; |
117 | 131 |
FileOutputStream output = null; |
... | ... | |
147 | 161 |
|
148 | 162 |
|
149 | 163 |
} catch (IOException e) { |
150 |
e.printStackTrace();
|
|
164 |
logger.error(e.getMessage(), e);
|
|
151 | 165 |
} |
152 | 166 |
|
153 | 167 |
} |
Also available in: Unified diff