svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.remoteclient / src / main / java / org / gvsig / remoteclient / ogc / request / OGCRequest.java @ 40769
History | View | Annotate | Download (6.56 KB)
1 | 40559 | jjdelcerro | /**
|
---|---|---|---|
2 | * gvSIG. Desktop Geographic Information System.
|
||
3 | 40435 | jjdelcerro | *
|
4 | 40559 | jjdelcerro | * Copyright (C) 2007-2013 gvSIG Association.
|
5 | *
|
||
6 | 40435 | jjdelcerro | * This program is free software; you can redistribute it and/or
|
7 | * modify it under the terms of the GNU General Public License
|
||
8 | 40559 | jjdelcerro | * as published by the Free Software Foundation; either version 3
|
9 | 40435 | jjdelcerro | * of the License, or (at your option) any later version.
|
10 | 40559 | jjdelcerro | *
|
11 | 40435 | jjdelcerro | * 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 | 40559 | jjdelcerro | *
|
16 | 40435 | jjdelcerro | * 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 | 40559 | jjdelcerro | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 | 40435 | jjdelcerro | * MA 02110-1301, USA.
|
20 | 40559 | jjdelcerro | *
|
21 | * For any additional information, do not hesitate to contact us
|
||
22 | * at info AT gvsig.com, or visit our website www.gvsig.com.
|
||
23 | 40435 | jjdelcerro | */
|
24 | |||
25 | package org.gvsig.remoteclient.ogc.request; |
||
26 | |||
27 | import java.io.File; |
||
28 | import java.io.IOException; |
||
29 | import java.net.ConnectException; |
||
30 | import java.net.MalformedURLException; |
||
31 | import java.net.URL; |
||
32 | import java.net.UnknownHostException; |
||
33 | |||
34 | import org.gvsig.compat.CompatLocator; |
||
35 | import org.gvsig.compat.lang.StringUtils; |
||
36 | import org.gvsig.compat.net.ICancellable; |
||
37 | import org.gvsig.remoteclient.RemoteClientStatus; |
||
38 | import org.gvsig.remoteclient.ogc.OGCClientOperation; |
||
39 | import org.gvsig.remoteclient.ogc.OGCProtocolHandler; |
||
40 | import org.gvsig.remoteclient.utils.Utilities; |
||
41 | import org.gvsig.remoteclient.wfs.WFSOperation; |
||
42 | |||
43 | public abstract class OGCRequest { |
||
44 | protected RemoteClientStatus status = null; |
||
45 | protected OGCProtocolHandler protocolHandler = null; |
||
46 | protected boolean isDeleted = false; |
||
47 | protected static final String XMLTAG_STARTCHARACTER = "<"; |
||
48 | protected static final String XMLTAG_FINISHCHARACTER = "</"; |
||
49 | protected static final String XMLTAG_ENDCHARACTER = ">"; |
||
50 | |||
51 | private static final StringUtils stringUtils = CompatLocator.getStringUtils(); |
||
52 | |||
53 | public OGCRequest(RemoteClientStatus status, OGCProtocolHandler protocolHandler) {
|
||
54 | super();
|
||
55 | this.status = status;
|
||
56 | this.protocolHandler = protocolHandler;
|
||
57 | } |
||
58 | |||
59 | /**
|
||
60 | * Send a request to the server.
|
||
61 | * @return
|
||
62 | * The server reply
|
||
63 | * @throws IOException
|
||
64 | * @throws UnknownHostException
|
||
65 | * @throws ConnectException
|
||
66 | */
|
||
67 | public File sendRequest(ICancellable cancel) throws ConnectException, UnknownHostException, IOException{ |
||
68 | //if the status is null is because is a GetCapabilities operation
|
||
69 | if (status != null){ |
||
70 | if (status.getProtocol() != OGCClientOperation.PROTOCOL_UNDEFINED){
|
||
71 | if (status.getProtocol() == OGCClientOperation.PROTOCOL_GET){
|
||
72 | String onlineResource = protocolHandler.getHost();
|
||
73 | String symbol = getSymbol(onlineResource);
|
||
74 | onlineResource = onlineResource + symbol; |
||
75 | return sendHttpGetRequest(onlineResource, cancel);
|
||
76 | }else{
|
||
77 | String onlineResource = protocolHandler.getHost();
|
||
78 | return sendHttpPostRequest(onlineResource);
|
||
79 | } |
||
80 | } |
||
81 | } |
||
82 | |||
83 | //if exists an online resource for the GET operation
|
||
84 | String onlineResource = protocolHandler.getServiceInformation().getOnlineResource(getOperationName(), WFSOperation.PROTOCOL_GET);
|
||
85 | if (onlineResource != null){ |
||
86 | String symbol = getSymbol(onlineResource);
|
||
87 | onlineResource = onlineResource + symbol; |
||
88 | return sendHttpGetRequest(onlineResource, cancel);
|
||
89 | } |
||
90 | //if exists an online resource for the POST operation
|
||
91 | onlineResource = protocolHandler.getServiceInformation().getOnlineResource(getOperationName(), WFSOperation.PROTOCOL_POST); |
||
92 | if (onlineResource != null){ |
||
93 | return sendHttpPostRequest(onlineResource);
|
||
94 | } |
||
95 | //If the online resource doesn't exist, it tries with the server URL and GET
|
||
96 | onlineResource = protocolHandler.getHost(); |
||
97 | String symbol = getSymbol(onlineResource);
|
||
98 | onlineResource = onlineResource + symbol; |
||
99 | return sendHttpGetRequest(onlineResource, cancel);
|
||
100 | } |
||
101 | |||
102 | protected abstract String getHttpGetRequest(String onlineResource); |
||
103 | |||
104 | protected abstract String getHttpPostRequest(String onlineResource); |
||
105 | |||
106 | protected abstract String getTempFilePrefix(); |
||
107 | |||
108 | protected abstract String getOperationName(); |
||
109 | |||
110 | protected abstract String getSchemaLocation(); |
||
111 | |||
112 | /**
|
||
113 | * @return the URL used in the HTTP get operation
|
||
114 | * @throws MalformedURLException
|
||
115 | */
|
||
116 | public URL getURL() throws MalformedURLException{ |
||
117 | String onlineResource = protocolHandler.getServiceInformation().getOnlineResource(getOperationName(), WFSOperation.PROTOCOL_GET);
|
||
118 | if (onlineResource != null){ |
||
119 | String symbol = getSymbol(onlineResource);
|
||
120 | onlineResource = onlineResource + symbol; |
||
121 | return new URL(getHttpGetRequest(onlineResource)); |
||
122 | } |
||
123 | |||
124 | //If the online resource doesn't exist, it tries with the server URL and GET
|
||
125 | onlineResource = protocolHandler.getHost(); |
||
126 | String symbol = getSymbol(onlineResource);
|
||
127 | onlineResource = onlineResource + symbol; |
||
128 | return new URL(getHttpGetRequest(onlineResource)); |
||
129 | } |
||
130 | |||
131 | /**
|
||
132 | * Send a Http request using the get protocol
|
||
133 | * @param onlineResource
|
||
134 | * @return
|
||
135 | * @throws ConnectException
|
||
136 | * @throws UnknownHostException
|
||
137 | * @throws IOException
|
||
138 | */
|
||
139 | private File sendHttpGetRequest(String onlineResource, ICancellable cancel) throws ConnectException, UnknownHostException, IOException{ |
||
140 | URL url = new URL(stringUtils.replaceAll(getHttpGetRequest(onlineResource), " ", "%20")); |
||
141 | if (isDeleted()){
|
||
142 | Utilities.removeURL(url);
|
||
143 | } |
||
144 | return Utilities.downloadFile(url, getTempFilePrefix(), cancel); |
||
145 | } |
||
146 | |||
147 | /**
|
||
148 | * Send a Http request using the post protocol
|
||
149 | * @param onlineResource
|
||
150 | * @return
|
||
151 | * @throws ConnectException
|
||
152 | * @throws UnknownHostException
|
||
153 | * @throws IOException
|
||
154 | */
|
||
155 | private File sendHttpPostRequest(String onlineResource) throws ConnectException, UnknownHostException, IOException{ |
||
156 | URL url = new URL(onlineResource); |
||
157 | String data = getHttpPostRequest(onlineResource);
|
||
158 | if (isDeleted()){
|
||
159 | Utilities.removeURL(url+data);
|
||
160 | } |
||
161 | return Utilities.downloadFile(url, data, getTempFilePrefix(), null); |
||
162 | } |
||
163 | |||
164 | /**
|
||
165 | * Just for not repeat code. Gets the correct separator according
|
||
166 | * to the server URL
|
||
167 | * @param h
|
||
168 | * @return
|
||
169 | */
|
||
170 | protected static String getSymbol(String h) { |
||
171 | String symbol;
|
||
172 | if (h.indexOf("?")==-1) |
||
173 | symbol = "?";
|
||
174 | else if (h.indexOf("?")!=h.length()-1) |
||
175 | symbol = "&";
|
||
176 | else
|
||
177 | symbol = "";
|
||
178 | return symbol;
|
||
179 | } |
||
180 | |||
181 | public boolean isDeleted() { |
||
182 | return isDeleted;
|
||
183 | } |
||
184 | |||
185 | public void setDeleted(boolean isDeleted) { |
||
186 | this.isDeleted = isDeleted;
|
||
187 | } |
||
188 | |||
189 | protected String createXMLStartTag(String tagName){ |
||
190 | return XMLTAG_STARTCHARACTER + tagName + XMLTAG_ENDCHARACTER;
|
||
191 | } |
||
192 | |||
193 | protected String createXMLEndtTag(String tagName){ |
||
194 | return XMLTAG_FINISHCHARACTER + tagName + XMLTAG_ENDCHARACTER;
|
||
195 | } |
||
196 | |||
197 | } |