Statistics
| Revision:

root / branches / v2_0_0_prep / libraries / libRemoteServices / src / org / gvsig / remoteclient / ogc / request / OGCRequest.java @ 30324

History | View | Annotate | Download (5.63 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.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

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

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

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

    
97
        protected abstract String getHttpGetRequest(String onlineResource);
98

    
99
        protected abstract String getHttpPostRequest(String onlineResource);
100

    
101
        protected abstract String getTempFilePrefix();
102

    
103
        protected abstract String getOperationName();
104

    
105
        protected abstract String getSchemaLocation();        
106

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

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

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

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

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

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

    
180
}
181