Statistics
| Revision:

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 @ 40559

History | View | Annotate | Download (6.63 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
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 3
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
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
/*
25
 * AUTHORS (In addition to CIT):
26
 * 2009 Iver T.I.  {{Task}}
27
 */
28

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

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

    
38
import org.gvsig.compat.CompatLocator;
39
import org.gvsig.compat.lang.StringUtils;
40
import org.gvsig.compat.net.ICancellable;
41
import org.gvsig.remoteclient.RemoteClientStatus;
42
import org.gvsig.remoteclient.ogc.OGCClientOperation;
43
import org.gvsig.remoteclient.ogc.OGCProtocolHandler;
44
import org.gvsig.remoteclient.utils.Utilities;
45
import org.gvsig.remoteclient.wfs.WFSOperation;
46

    
47
public abstract class OGCRequest {
48
        protected RemoteClientStatus status = null;
49
        protected OGCProtocolHandler protocolHandler = null;
50
        protected boolean isDeleted = false;
51
        protected static final String XMLTAG_STARTCHARACTER = "<";
52
        protected static final String XMLTAG_FINISHCHARACTER = "</";
53
        protected static final String XMLTAG_ENDCHARACTER = ">";
54
        
55
        private static final StringUtils stringUtils = CompatLocator.getStringUtils();
56

    
57
        public OGCRequest(RemoteClientStatus status, OGCProtocolHandler protocolHandler) {
58
                super();
59
                this.status = status;
60
                this.protocolHandler = protocolHandler;
61
        }        
62

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

    
87
                //if exists an online resource for the GET operation
88
                String onlineResource = protocolHandler.getServiceInformation().getOnlineResource(getOperationName(), WFSOperation.PROTOCOL_GET);
89
                if (onlineResource != null){
90
                        String symbol = getSymbol(onlineResource);
91
                        onlineResource = onlineResource + symbol;
92
                        return sendHttpGetRequest(onlineResource, cancel);
93
                }
94
                //if exists an online resource for the POST operation
95
                onlineResource =  protocolHandler.getServiceInformation().getOnlineResource(getOperationName(), WFSOperation.PROTOCOL_POST);
96
                if (onlineResource != null){
97
                        return sendHttpPostRequest(onlineResource);
98
                }
99
                //If the online resource doesn't exist, it tries with the server URL and GET
100
                onlineResource = protocolHandler.getHost();
101
                String symbol = getSymbol(onlineResource);
102
                onlineResource = onlineResource + symbol;
103
                return sendHttpGetRequest(onlineResource, cancel);
104
        }
105

    
106
        protected abstract String getHttpGetRequest(String onlineResource);
107

    
108
        protected abstract String getHttpPostRequest(String onlineResource);
109

    
110
        protected abstract String getTempFilePrefix();
111

    
112
        protected abstract String getOperationName();
113

    
114
        protected abstract String getSchemaLocation();        
115

    
116
        /**
117
         * @return the URL used in the HTTP get operation
118
         * @throws MalformedURLException
119
         */
120
        public URL getURL() throws MalformedURLException{
121
                String onlineResource = protocolHandler.getServiceInformation().getOnlineResource(getOperationName(), WFSOperation.PROTOCOL_GET);
122
                if (onlineResource != null){
123
                        String symbol = getSymbol(onlineResource);
124
                        onlineResource = onlineResource + symbol;
125
                        return new URL(getHttpGetRequest(onlineResource));
126
                }
127
                
128
                //If the online resource doesn't exist, it tries with the server URL and GET
129
                onlineResource = protocolHandler.getHost();
130
                String symbol = getSymbol(onlineResource);
131
                onlineResource = onlineResource + symbol;
132
                return new URL(getHttpGetRequest(onlineResource));
133
        }
134

    
135
        /**
136
         * Send a Http request using the get protocol
137
         * @param onlineResource
138
         * @return
139
         * @throws ConnectException
140
         * @throws UnknownHostException
141
         * @throws IOException
142
         */
143
        private File sendHttpGetRequest(String onlineResource, ICancellable cancel) throws ConnectException, UnknownHostException, IOException{
144
                URL url = new URL(stringUtils.replaceAll(getHttpGetRequest(onlineResource), " ", "%20"));
145
                if (isDeleted()){
146
                        Utilities.removeURL(url);
147
                }
148
                return Utilities.downloadFile(url, getTempFilePrefix(), cancel);                
149
        }
150

    
151
        /**
152
         * Send a Http request using the post protocol
153
         * @param onlineResource
154
         * @return
155
         * @throws ConnectException
156
         * @throws UnknownHostException
157
         * @throws IOException
158
         */
159
        private File sendHttpPostRequest(String onlineResource) throws ConnectException, UnknownHostException, IOException{
160
                URL url = new URL(onlineResource);
161
                String data = getHttpPostRequest(onlineResource);
162
                if (isDeleted()){
163
                        Utilities.removeURL(url+data);
164
                }
165
                return Utilities.downloadFile(url, data, getTempFilePrefix(), null);                
166
        }
167

    
168
        /**
169
         * Just for not repeat code. Gets the correct separator according 
170
         * to the server URL
171
         * @param h
172
         * @return
173
         */
174
        protected static String getSymbol(String h) {
175
                String symbol;
176
                if (h.indexOf("?")==-1) 
177
                        symbol = "?";
178
                else if (h.indexOf("?")!=h.length()-1)
179
                        symbol = "&";
180
                else
181
                        symbol = "";
182
                return symbol;
183
        }
184

    
185
        public boolean isDeleted() {
186
                return isDeleted;
187
        }
188

    
189
        public void setDeleted(boolean isDeleted) {
190
                this.isDeleted = isDeleted;
191
        }
192
        
193
        protected String createXMLStartTag(String tagName){
194
            return XMLTAG_STARTCHARACTER + tagName + XMLTAG_ENDCHARACTER;
195
        }
196
        
197
   protected String createXMLEndtTag(String tagName){
198
        return XMLTAG_FINISHCHARACTER + tagName + XMLTAG_ENDCHARACTER;
199
    }
200

    
201
}
202