root / trunk / applications / appCatalogYNomenclatorClient / src / es / gva / cit / catalogClient / protocols / HTTPPostProtocol.java @ 2047
History | View | Annotate | Download (2.8 KB)
1 |
/*
|
---|---|
2 |
* Created on 22-abr-2005
|
3 |
*
|
4 |
* TODO To change the template for this generated file go to
|
5 |
* Window - Preferences - Java - Code Style - Code Templates
|
6 |
*/
|
7 |
package es.gva.cit.catalogClient.protocols; |
8 |
|
9 |
import java.io.ByteArrayInputStream; |
10 |
import java.io.InputStream; |
11 |
import java.net.URL; |
12 |
|
13 |
import org.apache.commons.httpclient.DefaultMethodRetryHandler; |
14 |
import org.apache.commons.httpclient.HttpClient; |
15 |
import org.apache.commons.httpclient.HttpStatus; |
16 |
import org.apache.commons.httpclient.NameValuePair; |
17 |
import org.apache.commons.httpclient.methods.PostMethod; |
18 |
import org.w3c.dom.Node; |
19 |
|
20 |
|
21 |
import es.gva.cit.catalogClient.metadataXML.XMLTree; |
22 |
|
23 |
/**
|
24 |
* @author jpiera
|
25 |
*
|
26 |
* TODO Implementa la operacion POST de HTTP. Sin Probar!!!
|
27 |
*/
|
28 |
public class HTTPPostProtocol implements IProtocols { |
29 |
|
30 |
public Node[] doQuery(URL url,Object object,int firstRecord){ |
31 |
NameValuePair[] parameters = (NameValuePair[])object; |
32 |
//String parameters = (String) object;
|
33 |
|
34 |
InputStream is = null; |
35 |
ByteArrayInputStream output = null; |
36 |
|
37 |
// Solo devuleve un nodo
|
38 |
Node[] nodes = new Node[1]; |
39 |
|
40 |
// Create an instance of HttpClient.
|
41 |
HttpClient client = new HttpClient();
|
42 |
|
43 |
// Create a method instance.
|
44 |
PostMethod method = new PostMethod("http://" + url.getHost() + ":" + url.getPort()+ url.getPath()); |
45 |
|
46 |
// Provide custom retry handler is necessary
|
47 |
// Provide custom retry handler is necessary
|
48 |
DefaultMethodRetryHandler retryhandler = new DefaultMethodRetryHandler();
|
49 |
retryhandler.setRequestSentRetryEnabled(false);
|
50 |
retryhandler.setRetryCount(3);
|
51 |
method.setMethodRetryHandler(retryhandler); |
52 |
|
53 |
method.setQueryString(parameters); |
54 |
|
55 |
System.out.print(method.getQueryString());
|
56 |
|
57 |
try{
|
58 |
// Execute the method
|
59 |
int statusCode = client.executeMethod(method);
|
60 |
//System.out.println("QueryString>>> "+ method.getQueryString());
|
61 |
if (statusCode != HttpStatus.SC_OK) {
|
62 |
System.err.println("Fallo en el m?todo: " + method.getStatusLine()); |
63 |
return null; |
64 |
} |
65 |
|
66 |
// Process the answer
|
67 |
is = method.getResponseBodyAsStream(); |
68 |
|
69 |
byte[] buffer= new byte[256]; |
70 |
String str = new String(""); |
71 |
|
72 |
while (true) { |
73 |
int n = is.read(buffer);
|
74 |
if (n < 0) |
75 |
break;
|
76 |
str = str + new String(buffer, 0, n); |
77 |
} |
78 |
|
79 |
System.out.println(str);
|
80 |
|
81 |
is.close(); |
82 |
output = new ByteArrayInputStream(str.getBytes()); |
83 |
|
84 |
|
85 |
|
86 |
}catch (Exception e) { |
87 |
System.err.println("No se encuentra el servidor."); |
88 |
return null; |
89 |
}finally{
|
90 |
|
91 |
// Release the connection.
|
92 |
method.releaseConnection(); |
93 |
} |
94 |
|
95 |
nodes[0] = XMLTree.XMLToTree(output);
|
96 |
|
97 |
return nodes;
|
98 |
} |
99 |
|
100 |
public static boolean isProtocolSupported(URL url){ |
101 |
|
102 |
return true; |
103 |
|
104 |
} |
105 |
} |