svn-gvsig-desktop / tags / v1_0_2_Build_914 / applications / appCatalogYNomenclatorClient / src / es / gva / cit / catalogClient / protocols / HTTPGetProtocol.java @ 12200
History | View | Annotate | Download (4.06 KB)
1 | 3566 | jorpiell | |
---|---|---|---|
2 | 2820 | jorpiell | /* 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 | 2047 | luisw | package es.gva.cit.catalogClient.protocols; |
43 | 3613 | jorpiell | import es.gva.cit.catalogClient.metadataXML.XMLTree; |
44 | 3566 | jorpiell | import java.io.ByteArrayInputStream; |
45 | import java.io.InputStream; |
||
46 | import java.net.URL; |
||
47 | import java.util.Collection; |
||
48 | 2047 | luisw | import org.apache.commons.httpclient.DefaultMethodRetryHandler; |
49 | import org.apache.commons.httpclient.HttpClient; |
||
50 | import org.apache.commons.httpclient.HttpStatus; |
||
51 | 2145 | jorpiell | import org.apache.commons.httpclient.NameValuePair; |
52 | 2047 | luisw | import org.apache.commons.httpclient.methods.GetMethod; |
53 | 2724 | jorpiell | |
54 | 2047 | luisw | /**
|
55 | 3566 | jorpiell | *
|
56 | *
|
||
57 | *
|
||
58 | 2820 | jorpiell | * @author Jorge Piera Llodra (piera_jor@gva.es)
|
59 | 2047 | luisw | */
|
60 | public class HTTPGetProtocol implements IProtocols { |
||
61 | 3566 | jorpiell | |
62 | /**
|
||
63 | *
|
||
64 | *
|
||
65 | *
|
||
66 | * @return
|
||
67 | * @param url
|
||
68 | * @param object
|
||
69 | * @param firstRecord
|
||
70 | */
|
||
71 | public Collection doQuery(URL url, Object object, int firstRecord) { |
||
72 | 2724 | jorpiell | NameValuePair[] parameters = (NameValuePair[]) object; |
73 | InputStream is = null; |
||
74 | ByteArrayInputStream output = null; |
||
75 | 3566 | jorpiell | |
76 | 2724 | jorpiell | // Create an instance of HttpClient.
|
77 | HttpClient client = new HttpClient();
|
||
78 | // Create a method instance.
|
||
79 | GetMethod method = new GetMethod("http://" + url.getHost() + ":" + |
||
80 | url.getPort() + url.getPath()); |
||
81 | // Provide custom retry handler is necessary
|
||
82 | DefaultMethodRetryHandler retryhandler = new DefaultMethodRetryHandler();
|
||
83 | retryhandler.setRequestSentRetryEnabled(false);
|
||
84 | retryhandler.setRetryCount(3);
|
||
85 | method.setMethodRetryHandler(retryhandler); |
||
86 | method.setQueryString(parameters); |
||
87 | System.out.println(method.getQueryString());
|
||
88 | try {
|
||
89 | // Execute the method.
|
||
90 | int statusCode = client.executeMethod(method);
|
||
91 | //System.out.println("QueryString>>> "+ method.getQueryString());
|
||
92 | if (statusCode != HttpStatus.SC_OK) {
|
||
93 | System.err.println("Fallo en el m?todo: " + |
||
94 | method.getStatusLine()); |
||
95 | return null; |
||
96 | } |
||
97 | // Process the answer
|
||
98 | is = method.getResponseBodyAsStream(); |
||
99 | byte[] buffer = new byte[256]; |
||
100 | String str = new String(""); |
||
101 | while (true) { |
||
102 | 2047 | luisw | int n = is.read(buffer);
|
103 | 2724 | jorpiell | if (n < 0) { |
104 | break;
|
||
105 | } |
||
106 | 2047 | luisw | str = str + new String(buffer, 0, n); |
107 | 2724 | jorpiell | } |
108 | is.close(); |
||
109 | System.out.println(str);
|
||
110 | if (str.equals("")) { |
||
111 | output = null;
|
||
112 | } else {
|
||
113 | output = new ByteArrayInputStream(str.getBytes()); |
||
114 | } |
||
115 | } catch (Exception e) { |
||
116 | return null; |
||
117 | } finally {
|
||
118 | // Release the connection.
|
||
119 | method.releaseConnection(); |
||
120 | } |
||
121 | 3566 | jorpiell | |
122 | Collection col = new java.util.ArrayList(); |
||
123 | col.add(XMLTree.xmlToTree(output)); |
||
124 | return col;
|
||
125 | } |
||
126 | 2724 | jorpiell | |
127 | 3566 | jorpiell | /**
|
128 | *
|
||
129 | *
|
||
130 | *
|
||
131 | * @return
|
||
132 | * @param url
|
||
133 | */
|
||
134 | public static boolean isProtocolSupported(URL url) { |
||
135 | 2724 | jorpiell | return true; |
136 | 3566 | jorpiell | } |
137 | } |