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