Statistics
| Revision:

root / trunk / libraries / libRemoteServices / src / org / gvsig / remoteClient / wms / WMSProtocolHandler.java @ 3345

History | View | Annotate | Download (6.74 KB)

1

    
2
package org.gvsig.remoteClient.wms;
3

    
4
import java.io.BufferedOutputStream;
5
import java.io.DataOutputStream;
6
import java.io.File;
7
import java.io.FileOutputStream;
8
import java.io.InputStream;
9
import java.net.URL;
10
import java.util.TreeMap;
11
import java.util.Vector;
12

    
13
/**
14
 * <p> Abstract class that represents handlers to comunicate via WMS protocol.
15
 * </p>
16
 * 
17
 */
18
public abstract class WMSProtocolHandler {
19

    
20
        // procotol handler name
21
    protected String name;
22
    // protocol handler version
23
    protected String version;
24
    //host of the WMS to conenct
25
    protected String host;
26
    // port number of the comunication channel of the WMS to connect
27
    protected String port;    
28
    //WMS metadata
29
    protected ServiceInformation serviceInfo;
30
    public TreeMap layers;
31
    public Vector srs;
32
    
33

    
34
    // abstract methods to implement by the handlers that implement
35
    // the connection to a WMS with certain version.
36
    //    public abstract String getName();   
37
    //    public abstract String getVersion();
38
    
39
    /**
40
     * parses the data retrieved by the WMS in XML format. 
41
     * It will be mostly the WMS Capabilities, but the implementation
42
     * will be placed in the handler implementing certain version of the protocol.
43
     * 
44
     */
45
    public abstract void parse(File f);
46

    
47
    public String getName() {        
48
            return name;
49
    } 
50

    
51
    public String getVersion() {        
52
            return version;
53
    }    
54
    
55
    public ServiceInformation getServiceInformation() {        
56
        return serviceInfo;
57
    }  
58
    public String getHost ()
59
    {
60
            return host;
61
    }
62
    public void setHost(String _host)
63
    {
64
            host = _host;
65
    }
66
    public String getPort()
67
    {
68
            return port;
69
    }
70
    public void setPort(String _port)
71
    {
72
            port = _port;
73
    }
74

    
75
    /**
76
     * <p>Builds a GetMap request that is sent to the WMS
77
     * the response (image) will be redirect to the
78
     * WMS client</p>
79
     */
80
    
81
    public byte[] getMap(WMSStatus status) {        
82
        return null;
83
    } 
84

    
85
        /**
86
         * <p>Builds a GetCapabilities request that is sent to the WMS
87
         * the response will be parse to extract the data needed by the
88
         * WMS client</p>
89
         */
90
    public void getCapabilities()
91
    {                
92
            URL request = null;
93
                try
94
                {
95
                        request = new URL(buildCapabilitiesRequest());
96
                }
97
                catch(Exception e)
98
                {
99
                        // TODO something about the exceptionsssss....!!!
100
                }
101
                
102
                   File f = null;
103
                   f = new File("tochangethis.txt");
104
            
105
            if (!f.exists())
106
            {
107
                    try
108
                    {
109
                            DataOutputStream dos = new DataOutputStream( new BufferedOutputStream(new FileOutputStream(f)));
110
                            byte[] buffer = new byte[1024*256];
111
                            InputStream is = request.openStream();
112
                            long readed = 0;
113
                            for (int i = is.read(buffer); i>0; i = is.read(buffer)){
114
                                    dos.write(buffer, 0, i);
115
                                    readed += i;
116
                            }
117
                            dos.close();
118
                    }
119
                    catch(Exception e)
120
                    {}
121
            }
122
            parse(f);
123
            //return parse(f);
124
    }
125

    
126
/**
127
 * <p>It will send a GetFeatureInfo request to the WMS
128
 * Parsing the response and redirecting the info to the WMS client</p>
129
 */
130
    public String getFeatureInfo()
131
    {
132
            return null;
133
    }
134

    
135
    /**
136
     * Builds the GetCapabilitiesRequest according to the OGC WMS Specifications
137
     */
138
    private String buildCapabilitiesRequest()
139
    {
140
                String req = new String();
141
                
142
                req = req + getHost() + "REQUEST=GetCapabilities&SERVICE=WMS&";
143
                req = req + "VERSION=" + getVersion();
144
                req += ("&EXCEPTIONS=XML");
145
                return req;
146
    }
147

    
148
    /**
149
     * Builds the GetMapRequest according to the OGC WMS Specifications
150
     */
151
    private String buildMapRequest(WMSStatus status)
152
    {
153
                String req = new String();
154
                
155
                req = req + getHost() + "REQUEST=GetMap&SERVICE=WMS&";
156
                req = req + "VERSION=" + getVersion() + "&";
157
                req = req + getPartialQuery(status);
158
       if (status.getExceptionFormat() != null) {
159
            req += ("&EXCEPTIONS=" + status.getExceptionFormat());
160
        } else {
161
            req += ("&EXCEPTIONS=XML");
162
        }
163
                return req;
164
    }
165

    
166
    /**
167
     * Gets the part of the OGC request that share GetMap and GetFeatureInfo
168
     * @return String request
169
     */
170
    public String getPartialQuery(WMSStatus status)
171
    {
172
            
173
            
174
        String req = "LAYERS=" + status.getLayerNames() + "&STYLES=" +
175
                                status.getStyles() + "&SRS=" + status.getSrs() +
176
            "&BBOX=" + status.getExtent() + "&WIDTH=" + status.getWidth() +
177
            "&HEIGHT=" + status.getHeight() + "&FORMAT=" + status.getFormat();
178

    
179
//        if (status.getTransparency()) {
180
//            req += "&TRANSPARENT=TRUE";
181
//        }
182
//
183
//        if (status.getBGColor() != null) {
184
//            req += ("&COLOR=" + status.getBGColor());
185
//        }
186
//
187
//        if (time != null) {
188
//            req += ("&TIME=" + time);
189
//        }
190
//
191
//        if (elevation != -1) {
192
//            req += ("&ELEVATION=" + elevation);
193
//        }
194

    
195
        return req.replaceAll(" ", "%20");
196
    }
197

    
198
    
199
    
200
    public void close() {        
201
        // your code here
202
    } 
203
    
204
    /**
205
     * Inner class that represents the description of the WMS metadata.
206
     * The first part of the capabilities will return the service information
207
     * from the WMS, this class will hold this information. 
208
     * 
209
     */
210
    public class ServiceInformation {
211

    
212
        public String online_resource;
213
        public String version;
214
        public String name;
215
        public String scope;
216
        public String title;
217
        public String abstr;
218
        public String keywords;
219
        public String fees;
220
        public String operationsInfo;
221
        public String personname;
222
        public String organization;
223
        public String function;
224
        public String addresstype;
225
        public String address;
226
        public String place;
227
        public String province;
228
        public String postcode;
229
        public String country;
230
        public String phone;
231
        public String fax;
232
        public String email;
233
        public Vector formats;
234
        
235
        public ServiceInformation()
236
        {
237
                online_resource = new String();
238
            version = new String();
239
            name = new String();
240
            scope = new String();
241
            title = new String();
242
            abstr = new String();
243
            keywords = new String();
244
            fees = new String();
245
            operationsInfo = new String();
246
            personname = new String();
247
            organization = new String();
248
            function = new String();
249
            addresstype = new String();
250
            address = new String();
251
            place = new String();
252
            province = new String();
253
            postcode = new String();
254
            country = new String();
255
            phone = new String();
256
            fax = new String();
257
            email = new String();
258
            formats = new Vector();               
259
        }
260
     }    
261
 }