Statistics
| Revision:

svn-gvsig-desktop / tags / v2_0_0_Build_2021 / libraries / libRemoteServices / src / org / gvsig / remoteclient / wms / WMSProtocolHandlerFactory.java @ 34101

History | View | Annotate | Download (8.26 KB)

1 3323 ldiaz
2 29658 jpiera
package org.gvsig.remoteclient.wms;
3 3323 ldiaz
4 3773 ldiaz
import java.io.DataInputStream;
5 3785 ldiaz
import java.io.IOException;
6 3755 ldiaz
import java.io.StringReader;
7 3785 ldiaz
import java.net.ConnectException;
8 3323 ldiaz
import java.net.URL;
9
import java.util.ArrayList;
10 3687 ldiaz
import java.util.Iterator;
11 3323 ldiaz
12 3755 ldiaz
import org.kxml2.io.KXmlParser;
13 3785 ldiaz
import org.xmlpull.v1.XmlPullParserException;
14 3755 ldiaz
15 33738 jpiera
import org.gvsig.remoteclient.utils.CapabilitiesTags;
16
17 3323 ldiaz
/**
18
 * <p></p>
19 11381 jaume
 *
20 3323 ldiaz
 */
21
public class WMSProtocolHandlerFactory {
22
/**
23
 * <p></p>
24 11381 jaume
 *
25 3323 ldiaz
 */
26 29658 jpiera
    public org.gvsig.remoteclient.wms.WMSProtocolHandler wMSProtocolHandler;
27 3323 ldiaz
28 3687 ldiaz
    private static ArrayList supportedVersions = new ArrayList();
29 3323 ldiaz
30
    static {
31
        /*
32
         * Se meten en el array versions las distintas versiones
33
         * del protocolo en orden descendente
34
         */
35 3687 ldiaz
            //versions.add(WMSProtocolHandler1_3_0.class);
36
        //versions.add(WMSProtocolHandler1_1_1.class);
37
            supportedVersions.add("1.3.0");
38
            supportedVersions.add("1.1.1");
39
            supportedVersions.add("1.1.0");
40 3323 ldiaz
     }
41
42
    /**
43
     * M?todo que dada una respuesta de getCapabilities y un iterador sobre una
44
     * coleccion de WMSClient's ordenada descendentemente devuelve el cliente
45
     * cuya version es igual o inmediatamente inferior
46
     *
47
     * @param caps Capabilities con la respuesta del servidor
48
     * @param clients Iterador de conjunto ordenado descendientemente
49
     *
50
     * @return cliente cuya version es igual o inmediatamente inferior
51
     * @throws IllegalAccessException
52
     * @throws InstantiationException
53 11381 jaume
     *
54 3323 ldiaz
     */
55 3687 ldiaz
    private static String getDriverVersion(String version, Iterator clients) throws InstantiationException, IllegalAccessException {
56
        while (clients.hasNext()) {
57
            String clientVersion = (String)clients.next();
58
            int ret = version.compareTo(clientVersion);
59 3323 ldiaz
60 3687 ldiaz
            if (ret >= 0) {
61
                return clientVersion;
62
            }
63
        }
64
        return null;
65
    }
66
67 3323 ldiaz
    /**
68
     * Establece la versi?n con la que se comunicar? con el servidor y devuelve
69
     * el objeto Capabilities obtenido con dicha versi?n
70
     *
71
     * @param host maquina con la que se negocia
72
     *
73
     * @return instancia de un cliente capaz de negociar con el host que se
74
     *         pasa como par?metro
75
     */
76 5239 ldiaz
     public static WMSProtocolHandler negotiate(String host) throws ConnectException, IOException {
77 3687 ldiaz
78 11381 jaume
        if (supportedVersions.size() == 0)
79 3687 ldiaz
        {
80
                return null;
81
        }
82
83
        try
84 11381 jaume
        {
85 3687 ldiaz
                String highestVersionSupportedByServer  = getSuitableWMSVersion(host,"");
86
                if (supportedVersions.contains(highestVersionSupportedByServer))
87
                {
88
                        //we support the highest version supported by the server
89 11381 jaume
                        // this is the best case
90 5239 ldiaz
                        return createVersionDriver(highestVersionSupportedByServer);
91 3687 ldiaz
                }
92 11381 jaume
93
94
        else
95 3687 ldiaz
                {
96
                        // in case we dont support the highest version from the server
97
                        // we start the negotiation process in which we have to get the higest version
98
                        // the WMS supports and we are able to read.
99
                        Iterator iVersion = supportedVersions.iterator();
100
                        String wmsVersion;
101
                        String gvSIGVersion;
102 11381 jaume
103
                        while (iVersion.hasNext()) {
104 3687 ldiaz
                                gvSIGVersion = (String)iVersion.next();
105
                                wmsVersion = getSuitableWMSVersion(host,gvSIGVersion);
106
                                //TODO:
107
                                //compare with the version returned by the WMS!!!!!
108
                                // send GetCapabilities and read the version to compare.
109
                                int res = wmsVersion.compareTo(gvSIGVersion);
110 11381 jaume
111
                                if (res == 0) { //Si es la misma que nuestra version
112 5239 ldiaz
                                    return createVersionDriver(gvSIGVersion);
113 3687 ldiaz
                                } else if (res > 0) { //Si es mayor que nuestra version
114
                                    throw new Exception("Server Version too high: " + wmsVersion);
115
                                } else { //Si es menor que nuestra version
116
                                         //Obtenemos la primera version menor o igual que tengamos
117
                                    String lowerVersion = WMSProtocolHandlerFactory.getDriverVersion(wmsVersion, iVersion);
118 11381 jaume
119 3687 ldiaz
                                    if (lowerVersion == null) { //Si no hay ninguna
120
                                        throw new Exception("Lowest server version is " + wmsVersion);
121
                                    } else {
122 11381 jaume
                                        if (lowerVersion.equals(wmsVersion)) {
123 5239 ldiaz
                                            return createVersionDriver(lowerVersion);
124 3687 ldiaz
                                        } else { //Si hay una version menor que la que retorno el servidor
125
                                            //iV = lower;
126
                                        }
127
                                    }
128
                                }
129
                        }
130
                }//case we had to start the negotiation process.
131 11381 jaume
                return null; // if it did not find any suitable version.
132 3785 ldiaz
        }
133
        catch(ConnectException conEx)
134 3687 ldiaz
        {
135 3785 ldiaz
                throw conEx;
136
        }
137 3803 ldiaz
        catch(IOException ioEx)
138
        {
139
                throw ioEx;
140 11381 jaume
        }
141 3785 ldiaz
        catch(Exception e)
142
        {
143 3687 ldiaz
                e.printStackTrace();
144
                return null;
145
        }
146 3323 ldiaz
    }
147 11381 jaume
148 3323 ldiaz
     /**
149 3687 ldiaz
      * Sends a GetCapabilities to the WMS server to get the version
150
      * if the version parameter is null, the WMS will return the highest version supported
151
      * if not it will return the lower highest version than the one requested.
152
      * @param host
153
      * @param version
154 11381 jaume
      * @return suitable version supported by the server
155 3687 ldiaz
      */
156 11381 jaume
     private static String getSuitableWMSVersion(String host, String _version) throws ConnectException, IOException
157
     {
158 3687 ldiaz
            String request = WMSProtocolHandler.buildCapabilitiesSuitableVersionRequest(host, _version);
159 11381 jaume
            String version = new String();
160 3755 ldiaz
            StringReader reader = null;
161 3773 ldiaz
            //InputStreamReader reader;
162
            //InputStream is = null;
163
            DataInputStream dis = null;
164 3687 ldiaz
                try
165 11381 jaume
                {
166 3687 ldiaz
                        URL url = new URL(request);
167 4222 jaume
            byte[] buffer = new byte[1024];//new byte[1024*256];
168 11381 jaume
//            is = url.openStream();
169
//            reader = new InputStreamReader(is);
170
            //int numberOfBytes = is.read(buffer);
171 3773 ldiaz
            //String readed = new String(buffer);
172 11381 jaume
            dis = new DataInputStream(url.openStream());
173 3773 ldiaz
            dis.readFully(buffer);
174 11381 jaume
            String string = new String(buffer);
175
176
            // patch for ArcIMS + WMS connector > 9.0 bug
177
            int a = string.toLowerCase().indexOf("<?xml");
178
            if (a !=-1)
179
                    string = string.substring(a, string.length());
180
            // end patch
181
182
183
            reader = new StringReader(string);
184 3687 ldiaz
                    KXmlParser kxmlParser = null;
185
                    kxmlParser = new KXmlParser();
186 11381 jaume
                    kxmlParser.setInput(reader);
187 3687 ldiaz
                        kxmlParser.nextTag();
188 11381 jaume
                    if ( kxmlParser.getEventType() != KXmlParser.END_DOCUMENT )
189
                    {
190 3743 ldiaz
                            if ((kxmlParser.getName().compareTo(CapabilitiesTags.CAPABILITIES_ROOT1_1_0)==0)
191
                                    ||(kxmlParser.getName().compareTo(CapabilitiesTags.CAPABILITIES_ROOT1_1_1)==0)
192 3687 ldiaz
                                    ||(kxmlParser.getName().compareTo(CapabilitiesTags.CAPABILITIES_ROOT1_3_0)==0))
193
                            {
194
                                    version = kxmlParser.getAttributeValue("", CapabilitiesTags.VERSION);
195
                            }
196
                    }
197 11381 jaume
                    // do not forget to close the Stream.
198
                    reader.close();
199 3773 ldiaz
                    dis.close();
200 11381 jaume
                        return version;
201 3785 ldiaz
                }
202
                catch(ConnectException conEx)
203
                {
204 11381 jaume
                        throw new ConnectException(conEx.getMessage());
205 3785 ldiaz
                }
206
                catch(IOException ioEx)
207
                {
208 11381 jaume
                        throw new IOException(ioEx.getMessage());
209 3785 ldiaz
                }
210
                catch(XmlPullParserException xmlEx)
211
                {
212
                        xmlEx.printStackTrace();
213
                        return "";
214
                }
215 11381 jaume
                finally{
216 3743 ldiaz
                        if (reader != null)
217
                        {
218
                                try{
219
                                        reader.close();
220
                                }catch(Exception ex){
221 11381 jaume
                                        ex.printStackTrace();
222 3743 ldiaz
                                }
223
                        }
224 3773 ldiaz
                        if (dis != null)
225 3743 ldiaz
                        {
226 4500 jaume
                                try {
227 3773 ldiaz
                                        dis.close();
228 4500 jaume
                                } catch(Exception ex) {
229 11381 jaume
                                        ex.printStackTrace();
230 3743 ldiaz
                                }
231
                        }
232 11381 jaume
                }
233 3687 ldiaz
     }
234 11381 jaume
235 3687 ldiaz
     /**
236 3323 ldiaz
      * It creates an instance of a WMSDriver class.
237
      *
238
      * @param String, with the version of the driver to be created
239
      * @return WMSDriver.
240
      */
241 11381 jaume
       private static WMSProtocolHandler createVersionDriver(String version)
242
       {
243 3687 ldiaz
         try
244
         {
245
           Class driver;
246
           version = version.replace('.', '_');
247 29658 jpiera
           driver = Class.forName("org.gvsig.remoteclient.wms.wms_"+version+".WMSProtocolHandler" + version);
248 5239 ldiaz
           return (WMSProtocolHandler)driver.newInstance();
249 3687 ldiaz
         }
250
         catch (Exception e)
251
         {
252
                 e.printStackTrace();
253
           //throw new Exception("WMSDriverFactory. Unknown driver version " + e);
254
                 return null;
255
         }
256 11381 jaume
       }
257
258 3323 ldiaz
 }