Statistics
| Revision:

root / trunk / extensions / extWCS / src / es / uji / lsi / wcs / client / WCSClient.java @ 1877

History | View | Annotate | Download (8.53 KB)

1 1877 luisw
package es.uji.lsi.wcs.client;
2
/*
3
 * WCSclient.java
4
 *
5
 * Created on 1 de diciembre de 2004, 21:56
6
 *
7
 * This file is supposed to implement the interface beetween server and client.
8
 * It will implement the GetCapabilities, DescribeCoverage and GetCoverage
9
 * operations.
10
 */
11
import java.io.BufferedOutputStream;
12
import java.io.DataOutputStream;
13
import java.io.File;
14
import java.io.FileOutputStream;
15
import java.io.IOException;
16
import java.io.InputStream;
17
import java.net.MalformedURLException;
18
import java.net.URL;
19
import java.util.Enumeration;
20
import java.util.Hashtable;
21
22
import es.uji.lsi.wcs.XmlWcsParsing.Capabilities;
23
import es.uji.lsi.wcs.XmlWcsParsing.CoverageOffering;
24
import es.uji.lsi.wcs.XmlWcsParsing.DescribeCoverageResponse;
25
import es.uji.lsi.wcs.XmlWcsParsing.WCSExceptionResponse;
26
import es.uji.lsi.wcs.XmlWcsParsing.XMLNode;
27
28
29
/**
30
 *
31
 * @author  jaume
32
 */
33
public class WCSClient {
34
    public Capabilities capabilities;
35
    public DescribeCoverageResponse describeCoverageResponse;
36
37
    private OperationGetCapabilities operationGetCapabilities;
38
    private OperationDescribeCoverage operationDescribeCoverage;
39
    private OperationGetCoverage operationGetCoverage;
40
    private WCSExceptionResponse wcs_exception;
41
    // the working address
42
    private String working_address;
43
44
    // When working off-line (for development uses only)
45
    private boolean working_offline = false;
46
    private File file = null;
47
    private String path, datapath, coveragefilename, coveragefileextension;
48
49
    /** Creates a new instance of WCSclient */
50
    public WCSClient(String text) {
51
        this.working_address = text;
52
        operationGetCapabilities = new OperationGetCapabilities();
53
        operationDescribeCoverage = new OperationDescribeCoverage();
54
        operationGetCoverage = new OperationGetCoverage();
55
    }
56
57
    public WCSClient(){
58
        working_address = "http://localhost:8080";
59
        operationGetCapabilities = new OperationGetCapabilities();
60
        operationDescribeCoverage = new OperationDescribeCoverage();
61
        operationGetCoverage = new OperationGetCoverage();
62
    }
63
64
    public WCSClient(File file){
65
        working_offline = true;
66
        this.file = file;
67
        operationGetCapabilities = new OperationGetCapabilities();
68
        operationDescribeCoverage = new OperationDescribeCoverage();
69
        operationGetCoverage = new OperationGetCoverage();
70
71
    }
72
73
    public Capabilities getTheCapabilities(){
74
        return capabilities;
75
    }
76
77
    public void getCapabilities(){
78
        // Opens the source of the GetCapabilities XML description
79
            System.out.println("WCSClient>>> GetCapabilities");
80
        try {
81
82
            XMLNode node;
83
            if (working_offline) node = new XMLNode(file);
84
            else{
85
                System.out.println(working_address + operationGetCapabilities.getParamsString());
86
                URL url = new URL(working_address + operationGetCapabilities.getParamsString());
87
                node = new XMLNode(url.openStream());
88
                System.out.println("WCSClient>>> GetCapabilities done...");
89
            }
90
            capabilities = new Capabilities(node);
91
92
        } catch (MalformedURLException e){
93
            new Error("Bad URL");
94
        } catch (IOException e){
95
            new Error("Unable to retrieve server capabilities");
96
        } catch (Exception e){
97
            new Error();
98
        }
99
100
    }
101
    /**
102
     * Por documentar, para usar el mismo sistema que gvSIG
103
     *
104
     * @param url
105
     */
106
    public void getCapabilities(URL url){
107
            XMLNode node;
108
            try {
109
110
                    node = new XMLNode(url.openStream());
111
                    capabilities = new Capabilities(node);
112
            } catch (Exception e){
113
                    new Error(e.getMessage());
114
            }
115
    }
116
117
    /**
118
     * Devuelve la descripci?n de la cobertura ofrecida en el DescribeCoverage
119
     *
120
     * @param nomCobertura
121
     * @return es.uji.lsi.XmlWcsParsing.CoverageOffering
122
     */
123
    public CoverageOffering getCoverageDescription(String nomCobertura){
124
            return describeCoverageResponse.getCoverageOffering(nomCobertura);
125
        }
126
127
128
    /**
129
     * Por documentar, para usar el mismo sistema que gvSIG
130
     *
131
     * @param url
132
     */
133
134
    public void describeCoverage(URL url){
135
            XMLNode node;
136
            try {
137
                    node = new XMLNode(url.openStream());
138
            } catch (Exception e){
139
                    new Error(e.getMessage());
140
            }
141
    }
142
143
144
145
    public void describeCoverage(){
146
        try {
147
            XMLNode node;
148
            if (working_offline) node = new XMLNode(file);
149
            else{
150
                System.out.println(working_address+operationDescribeCoverage.getParamsString());
151
                URL url = new URL(working_address+operationDescribeCoverage.getParamsString());
152
                node = new XMLNode(url.openStream());
153
            }
154
            describeCoverageResponse = new DescribeCoverageResponse(node, capabilities);
155
        } catch (MalformedURLException e){
156
            new Error("Bad URL");
157
        } catch (IOException e){
158
            new Error("Unable to retrieve server capabilities, connection error.");
159
        } catch (Exception e){
160
            new Error();
161
        }
162
    }
163
164
    public void describeCoverage(String coverage_name){
165
        try {
166
            System.out.print("paso por aqu? tambien ya ves");
167
            operationDescribeCoverage.setParam("coverage", coverage_name);
168
            System.out.print("paso por aqu? tambien ya ves tu que tio");
169
            describeCoverage();
170
        } catch (Exception e){
171
            new Error("Fallo asignando valor al par?metro <coverage>");
172
        }
173
    }
174
175
    public boolean getCoverage(){
176
        System.out.println(operationGetCoverage.getParamsString());
177
        try {
178
            String s = this.working_address + operationGetCoverage.getParamsString();
179
            System.out.println(s);
180
            URL miurl = new URL(s);
181
            downloadCoverageFile(miurl, this.datapath);
182
            return (this.coveragefilename!=null);
183
        }
184
        catch (MalformedURLException e){
185
            new Error("WCS: GetCoverage> URL error.");
186
        }
187
        return false;
188
    }
189
190
    public void downloadCoverageFile(URL url, String targetdir){
191
        try{
192
            String path = this.getDataPath();
193
            String filename = this.getCoverageFileName()+this.getCoverageFileExtension();
194
            String where = path+filename;
195
            DataOutputStream dos = new DataOutputStream( new BufferedOutputStream( new FileOutputStream(where)));
196
            byte[] buffer = new byte[1024*256]; // 256KB
197
            InputStream is = url.openStream();
198
            long readed = 0;
199
            for (int i = is.read(buffer); i>0; i = is.read(buffer)){
200
                dos.write(buffer, 0, i);
201
                readed += i;
202
            }
203
            dos.close();
204
205
206
        } catch (IOException ie){
207
            new Error("La petici?n de la cobertura no tuvo ?xito.");
208
        }
209
210
    }
211
212
    public boolean getCoverage(Hashtable hashtable){
213
        Enumeration en = hashtable.keys();
214
        try {
215
            while (en.hasMoreElements()){
216
                String key = (String) en.nextElement();
217
                operationGetCoverage.setParam(key, (String) hashtable.get(key));
218
            }
219
            return getCoverage();
220
        } catch (Exception e){
221
            new Error("GetCoverage error.");
222
        }
223
        return false;
224
    }
225
226
    public String getWorkingAddress(){
227
        return working_address;
228
    }
229
230
231
    public String getDataPath(){
232
        return this.datapath;
233
    }
234
235
    public String getCoverageFileName(){
236
        return this.coveragefilename;
237
    }
238
239
240
    public String getCoverageFileExtension(){
241
        return this.coveragefileextension;
242
    }
243
244
    public String toString(){
245
        return capabilities.toString();
246
    }
247
248
    public void setBaseDirectory(String path){
249
        this.path = path;
250
        this.datapath = this.path+"/data/";
251
    }
252
253
    public void setCoverageFileName(String name){
254
        this.coveragefilename = name;
255
    }
256
257
    public void setCoverageFileExtension(String extension){
258
        this.coveragefileextension = "."+extension;
259
    }
260
261
    public boolean isNotAnException(File file){
262
        this.wcs_exception = new WCSExceptionResponse(file);
263
        return (this.wcs_exception.getText() == null);
264
    }
265
266
    public String getWCSException(){
267
        return this.wcs_exception.getText();
268
    }
269
270
    private boolean checkExceptionResponse(File file){
271
        wcs_exception = new WCSExceptionResponse(file);
272
        return wcs_exception.getText()!=null;
273
    }
274
}
275