Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libWMSv0 / src / com / iver / wmsclient / AbstractWMSClient.java @ 189

History | View | Annotate | Download (6 KB)

1
package com.iver.wmsclient;
2

    
3
import org.apache.log4j.Logger;
4

    
5
import org.exolab.castor.xml.ValidationException;
6

    
7
import java.awt.geom.Rectangle2D;
8

    
9
import java.io.ByteArrayOutputStream;
10
import java.io.IOException;
11
import java.io.InputStream;
12

    
13
import java.net.URL;
14

    
15

    
16
/**
17
 * Cliente abstracto que realiza tareas independientes de la versi?n y delega
18
 * en un objeto dependiente de la versi?n las tareas que no puede realizar
19
 *
20
 * @author Fernando Gonz?lez Cort?s
21
 */
22
public class AbstractWMSClient implements WMSClient {
23
    private static Logger logger = Logger.getLogger(AbstractWMSClient.class.getName());
24
    private WMSVersionClient client;
25
    private Capabilities cachedCapabilities;
26

    
27
    /**
28
     * Crea un nuevo AbstractWMSClient.
29
     *
30
     * @param c Cliente dependiente de la versi?n
31
     * @param capabilities Capabilities obtenidas con el cliente que se pasa
32
     *        como par?metro, con la finalidad de no volver a calcularlas
33
     */
34
    public AbstractWMSClient(WMSVersionClient c, Object capabilities) {
35
        client = c;
36
        
37
        cachedCapabilities =  new Capabilities();
38
        cachedCapabilities.setRoot(capabilities);
39
        cachedCapabilities.setVersion(c.getVersion());
40
    }
41

    
42
    /**
43
     * @see com.iver.cit.gvsig.fmap.wms.WMSClient#getCapabilities(java.net.URL)
44
     */
45
    public Capabilities getCapabilities(URL host)
46
        throws UnsupportedVersionException, IllegalStateException, IOException {
47
        if (cachedCapabilities != null) {
48
            return cachedCapabilities;
49
        } else {
50
            return client.getCapabilities(host);
51
        }
52
    }
53

    
54
    /**
55
     * @see com.iver.cit.gvsig.fmap.wms.WMSClient#doMapQuery(com.iver.cit.gvsig.fmap.wms.MapQuery)
56
     */
57
    public byte[] doMapQuery(MapQuery queryInfo)
58
        throws WMSException, IOException, NoSuchFieldException, 
59
            ValidationException {
60
        InputStream in = null;
61
        byte[] bytes = null;
62
        String[] mapURLs = client.getMapURLs();
63

    
64
        if (mapURLs == null) {
65
            throw new RuntimeException("getCapabilities must be invoked before");
66
        }
67

    
68
        for (int url = 0; url < mapURLs.length; url++) {
69
            try {
70
                URL query = new URL(mapURLs[url] + queryInfo.getQuery());
71
                logger.debug("Haciendo la petici?n: " + query);
72
                in = query.openStream();
73

    
74
                logger.debug("Obteniendo la respuesta");
75

    
76
                ByteArrayOutputStream outbytes = new ByteArrayOutputStream();
77
                WMSUtilities.serializar(in, outbytes);
78
                bytes = outbytes.toByteArray();
79

    
80
                //Se comprueba si la respuesta corresponde a una excepcion del servidor
81
                if (new String(bytes, 0, bytes.length).toUpperCase().indexOf("EXCEPTION") != -1) {
82
                    String aux = new String(bytes);
83
                    logger.debug("Obtenida la siguiente respuesta:\n" + aux);
84

    
85
                    String error = client.parseException(bytes);
86

    
87
                    if (error == null) {
88
                        return bytes;
89
                    } else {
90
                        throw new WMSException(error);
91
                    }
92
                } else {
93
                    return bytes;
94
                }
95
            } catch (IOException e) {
96
                if (url == (mapURLs.length - 1)) {
97
                    throw e;
98
                }
99
            } finally {
100
                if (in != null) {
101
                    in.close();
102
                }
103
            }
104
        }
105

    
106
        throw new RuntimeException("No debi? llegar nunca a esta linea");
107
    }
108

    
109
    /**
110
     * @see com.iver.cit.gvsig.fmap.wms.WMSClient#getVersion()
111
     */
112
    public String getVersion() {
113
        return client.getVersion();
114
    }
115

    
116
    /**
117
     * @see com.iver.cit.gvsig.fmap.wms.WMSClient#getBoundingBox(java.lang.String[],
118
     *      java.lang.String)
119
     */
120
    public Rectangle2D getBoundingBox(String[] t, String srs) {
121
        return client.getBoundingBox(t, srs);
122
    }
123

    
124
    /**
125
     * @see com.iver.cit.gvsig.fmap.wms.WMSClient#doFeatureInfo(com.iver.cit.gvsig.fmap.wms.FeatureInfoQuery)
126
     */
127
    public byte[] doFeatureInfo(FeatureInfoQuery query)
128
        throws WMSException, IOException, NoSuchFieldException, 
129
            ValidationException {
130
        InputStream in = null;
131
        byte[] bytes = null;
132
        String[] infoURLs = client.getInfoURLs();
133

    
134
        if (infoURLs == null) {
135
            throw new RuntimeException("getCapabilities must be invoked before");
136
        }
137

    
138
        for (int url = 0; url < infoURLs.length; url++) {
139
            try {
140
                URL queryURL = new URL(infoURLs[url] + query.getQuery());
141
                logger.debug("Realizando la peticion: " + queryURL);
142
                in = queryURL.openStream();
143

    
144
                ByteArrayOutputStream outbytes = new ByteArrayOutputStream();
145
                WMSUtilities.serializar(in, outbytes);
146

    
147
                bytes = outbytes.toByteArray();
148

    
149
                String aux = new String(bytes);
150
                logger.debug("Obtenida la siguiente respuesta: " + aux);
151

    
152
                //Se comprueba si la respuesta se corresponde con una excepcion del servidor
153
                if (new String(bytes, 0, bytes.length).toUpperCase().indexOf("EXCEPTION") != -1) {
154
                    String error = client.parseException(bytes);
155

    
156
                    if (error == null) {
157
                        return bytes;
158
                    } else {
159
                        throw new WMSException(error);
160
                    }
161
                } else {
162
                    return bytes;
163
                }
164
            } catch (IOException e) {
165
                if (url == (infoURLs.length - 1)) {
166
                    throw e;
167
                }
168
            } finally {
169
                in.close();
170
            }
171
        }
172

    
173
        throw new RuntimeException("No debi? llegar nunca a esta linea");
174
    }
175

    
176
    /**
177
     * @see com.iver.wmsclient.WMSClient#getInfoFormats()
178
     */
179
    public String[] getInfoFormats() {
180
        return client.getInfoFormats();
181
    }
182

    
183
        public MapQuery createQuery() {
184
                return new MapQuery(client.getQueryHeader());
185
        }
186
}
187