Statistics
| Revision:

root / branches / v2_0_0_prep / libraries / libRemoteServices / src / org / gvsig / remoteclient / wmts / WMTSProtocolHandlerFactory.java @ 34645

History | View | Annotate | Download (5.94 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
package org.gvsig.remoteclient.wmts;
23

    
24
import java.io.DataInputStream;
25
import java.io.IOException;
26
import java.io.StringReader;
27
import java.net.ConnectException;
28
import java.net.URL;
29
import java.util.ArrayList;
30
import java.util.Iterator;
31

    
32
import org.kxml2.io.KXmlParser;
33
import org.xmlpull.v1.XmlPullParserException;
34

    
35
import org.gvsig.remoteclient.utils.CapabilitiesTags;
36

    
37
/**
38
 * This factory builds a new handler depending on the supported protocol.
39
 * This protocol could be negotiated with the server. Now only 1.0.0 version
40
 * is supported.
41
 * 
42
 * @author Nacho Brodin (nachobrodin@gmail.com)
43
 */
44
public class WMTSProtocolHandlerFactory {
45
        public org.gvsig.remoteclient.wms.WMSProtocolHandler wMSProtocolHandler;
46

    
47
        private static ArrayList supportedVersions = new ArrayList();
48

    
49
        static {
50
                supportedVersions.add("1.0.0");
51
        }
52

    
53
        /**
54
     * M?todo que dada una respuesta de getCapabilities y un iterador sobre una
55
     * coleccion de WMSClient's ordenada descendentemente devuelve el cliente
56
     * cuya version es igual o inmediatamente inferior
57
     *
58
     * @param caps Capabilities con la respuesta del servidor
59
     * @param clients Iterador de conjunto ordenado descendientemente
60
     *
61
     * @return cliente cuya version es igual o inmediatamente inferior
62
     * @throws IllegalAccessException
63
     * @throws InstantiationException
64
     *
65
     */
66
        private static String getDriverVersion(String version, Iterator clients) throws InstantiationException, IllegalAccessException {
67
        while (clients.hasNext()) {
68
            String clientVersion = (String)clients.next();
69
            int ret = version.compareTo(clientVersion);
70

    
71
            if (ret >= 0) {
72
                return clientVersion;
73
            }
74
        }
75
        return null;
76
    }
77

    
78
    /**
79
     * Establece la versi?n con la que se comunicar? con el servidor y devuelve
80
     * el objeto Capabilities obtenido con dicha versi?n
81
     *
82
     * @param host maquina con la que se negocia
83
     *
84
     * @return instancia de un cliente capaz de negociar con el host que se
85
     *         pasa como par?metro
86
     */
87
    public static WMTSProtocolHandler negotiate(String host) throws ConnectException, IOException {
88
            String highestVersionSupportedByServer  = getSuitableWMTSVersion(host, "");
89
            if (supportedVersions.contains(highestVersionSupportedByServer)) {
90
                    //we support the highest version supported by the server
91
                    // this is the best case
92
                    return createVersionDriver(highestVersionSupportedByServer);
93
            }
94
            return null;
95
    }
96

    
97
     /**
98
      * Sends a GetCapabilities to the WMTS server to get the version
99
      * if the version parameter is null, the WMTS will return the highest version supported
100
      * if not it will return the lower highest version than the one requested.
101
      * @param host
102
      * @param version
103
      * @return suitable version supported by the server
104
      */
105
     private static String getSuitableWMTSVersion(String host, String _version) throws ConnectException, IOException {
106
             String request = WMTSProtocolHandler.buildCapabilitiesSuitableVersionRequest(host, _version);
107
             String version = "";
108
             StringReader reader = null;
109
             DataInputStream dis = null;
110

    
111
             try {
112
                     URL url = new URL(request);
113
                     byte[] buffer = new byte[1024];
114
                     dis = new DataInputStream(url.openStream());
115
                     dis.readFully(buffer);
116
                     String string = new String(buffer);
117

    
118
                     int a = string.toLowerCase().indexOf("<?xml");
119
                     if (a !=-1)
120
                             string = string.substring(a, string.length());
121

    
122
                     reader = new StringReader(string);
123
                     KXmlParser kxmlParser = null;
124
                     kxmlParser = new KXmlParser();
125
                     kxmlParser.setInput(reader);
126
                     kxmlParser.nextTag();
127
                     if ( kxmlParser.getEventType() != KXmlParser.END_DOCUMENT ) {
128
                             if ((kxmlParser.getName().compareTo(CapabilitiesTags.WMTS_CAPABILITIES) == 0)) {
129
                                     version = kxmlParser.getAttributeValue("", CapabilitiesTags.VERSION);
130
                             }
131
                     }
132
                     // do not forget to close the Stream.
133
                     reader.close();
134
                     dis.close();
135
                     return version;
136
             } catch(XmlPullParserException xmlEx) {
137
                     xmlEx.printStackTrace();
138
                     return "";
139
             } finally {
140
                     if (reader != null) {
141
                             try {
142
                                     reader.close();
143
                             } catch(Exception ex) {
144
                                     ex.printStackTrace();
145
                             }
146
                     }
147
                     if (dis != null) {
148
                             try {
149
                                     dis.close();
150
                             } catch(Exception ex) {
151
                                     ex.printStackTrace();
152
                             }
153
                     }
154
             }
155
     }
156

    
157
     /**
158
      * It creates an instance of a WMSDriver class.
159
      *
160
      * @param String, with the version of the driver to be created
161
      * @return WMSDriver.
162
      */
163
     private static WMTSProtocolHandler createVersionDriver(String version) {
164
             try {
165
                     Class driver;
166
                     version = version.replace('.', '_');
167
                     driver = Class.forName("org.gvsig.remoteclient.wmts.wmts_" + version + ".WMTSProtocolHandler" + version);
168
                     return (WMTSProtocolHandler)driver.newInstance();
169
             } catch (Exception e) {
170
                     e.printStackTrace();
171
                     //throw new Exception("WMSDriverFactory. Unknown driver version " + e);
172
                     return null;
173
             }
174
     }
175

    
176
}