Statistics
| Revision:

svn-gvsig-desktop / tags / v2_0_0_Build_2021 / libraries / libRemoteServices / src / org / gvsig / remoteclient / ogc / request / OGCRequest.java @ 34101

History | View | Annotate | Download (5.74 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
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., 51 Franklin Street, Fifth Floor, Boston, 
19
 * MA  02110-1301, USA.
20
 * 
21
 */
22

    
23
/*
24
 * AUTHORS (In addition to CIT):
25
 * 2009 Iver T.I.  {{Task}}
26
 */
27

    
28
package org.gvsig.remoteclient.ogc.request;
29

    
30
import java.io.File;
31
import java.io.IOException;
32
import java.net.ConnectException;
33
import java.net.MalformedURLException;
34
import java.net.URL;
35
import java.net.UnknownHostException;
36

    
37
import org.gvsig.compat.net.ICancellable;
38
import org.gvsig.remoteclient.RemoteClientStatus;
39
import org.gvsig.remoteclient.ogc.OGCClientOperation;
40
import org.gvsig.remoteclient.ogc.OGCProtocolHandler;
41
import org.gvsig.remoteclient.utils.Utilities;
42
import org.gvsig.remoteclient.wfs.WFSOperation;
43

    
44
public abstract class OGCRequest {
45
        protected RemoteClientStatus status = null;
46
        protected OGCProtocolHandler protocolHandler = null;
47
        protected boolean isDeleted = false;
48

    
49
        public OGCRequest(RemoteClientStatus status, OGCProtocolHandler protocolHandler) {
50
                super();
51
                this.status = status;
52
                this.protocolHandler = protocolHandler;
53
        }        
54

    
55
        /**
56
         * Send a request to the server.
57
         * @return
58
         * The server reply
59
         * @throws IOException 
60
         * @throws UnknownHostException 
61
         * @throws ConnectException 
62
         */
63
        public File sendRequest(ICancellable cancel) throws ConnectException, UnknownHostException, IOException{
64
                //if the status is null is because is a GetCapabilities operation
65
                if (status != null){
66
                        if (status.getProtocol() != OGCClientOperation.PROTOCOL_UNDEFINED){
67
                                if (status.getProtocol() == OGCClientOperation.PROTOCOL_GET){
68
                                        String onlineResource = protocolHandler.getHost();
69
                                        String symbol = getSymbol(onlineResource);
70
                                        onlineResource = onlineResource + symbol;
71
                                        return sendHttpGetRequest(onlineResource, cancel);
72
                                }else{
73
                                        String onlineResource = protocolHandler.getHost();
74
                                        return sendHttpPostRequest(onlineResource);
75
                                }
76
                        }
77
                }
78

    
79
                //if exists an online resource for the GET operation
80
                String onlineResource = protocolHandler.getServiceInformation().getOnlineResource(getOperationName(), WFSOperation.PROTOCOL_GET);
81
                if (onlineResource != null){
82
                        String symbol = getSymbol(onlineResource);
83
                        onlineResource = onlineResource + symbol;
84
                        return sendHttpGetRequest(onlineResource, cancel);
85
                }
86
                //if exists an online resource for the POST operation
87
                onlineResource =  protocolHandler.getServiceInformation().getOnlineResource(getOperationName(), WFSOperation.PROTOCOL_POST);
88
                if (onlineResource != null){
89
                        return sendHttpPostRequest(onlineResource);
90
                }
91
                //If the online resource doesn't exist, it tries with the server URL and GET
92
                onlineResource = protocolHandler.getHost();
93
                String symbol = getSymbol(onlineResource);
94
                onlineResource = onlineResource + symbol;
95
                return sendHttpGetRequest(onlineResource, cancel);
96
        }
97

    
98
        protected abstract String getHttpGetRequest(String onlineResource);
99

    
100
        protected abstract String getHttpPostRequest(String onlineResource);
101

    
102
        protected abstract String getTempFilePrefix();
103

    
104
        protected abstract String getOperationName();
105

    
106
        protected abstract String getSchemaLocation();        
107

    
108
        /**
109
         * @return the URL used in the HTTP get operation
110
         * @throws MalformedURLException
111
         */
112
        public URL getURL() throws MalformedURLException{
113
                String onlineResource = protocolHandler.getServiceInformation().getOnlineResource(getOperationName(), WFSOperation.PROTOCOL_GET);
114
                if (onlineResource != null){
115
                        String symbol = getSymbol(onlineResource);
116
                        onlineResource = onlineResource + symbol;
117
                        return new URL(getHttpGetRequest(onlineResource));
118
                }
119
                return null;
120
        }
121

    
122
        /**
123
         * Send a Http request using the get protocol
124
         * @param onlineResource
125
         * @return
126
         * @throws ConnectException
127
         * @throws UnknownHostException
128
         * @throws IOException
129
         */
130
        private File sendHttpGetRequest(String onlineResource, ICancellable cancel) throws ConnectException, UnknownHostException, IOException{
131
                URL url = new URL(getHttpGetRequest(onlineResource));
132
                if (isDeleted()){
133
                        Utilities.removeURL(url);
134
                }
135
                return Utilities.downloadFile(url, getTempFilePrefix(), cancel);                
136
        }
137

    
138
        /**
139
         * Send a Http request using the post protocol
140
         * @param onlineResource
141
         * @return
142
         * @throws ConnectException
143
         * @throws UnknownHostException
144
         * @throws IOException
145
         */
146
        private File sendHttpPostRequest(String onlineResource) throws ConnectException, UnknownHostException, IOException{
147
                URL url = new URL(onlineResource);
148
                String data = getHttpPostRequest(onlineResource);
149
                System.out.println(data);
150
                if (isDeleted()){
151
                        Utilities.removeURL(url+data);
152
                }
153
                return Utilities.downloadFile(url, data, getTempFilePrefix(), null);                
154
        }
155

    
156
        /**
157
         * Just for not repeat code. Gets the correct separator according 
158
         * to the server URL
159
         * @param h
160
         * @return
161
         */
162
        protected static String getSymbol(String h) {
163
                String symbol;
164
                if (h.indexOf("?")==-1) 
165
                        symbol = "?";
166
                else if (h.indexOf("?")!=h.length()-1)
167
                        symbol = "&";
168
                else
169
                        symbol = "";
170
                return symbol;
171
        }
172

    
173
        public boolean isDeleted() {
174
                return isDeleted;
175
        }
176

    
177
        public void setDeleted(boolean isDeleted) {
178
                this.isDeleted = isDeleted;
179
        }  
180

    
181
}
182