Revision 45301

View differences:

tags/org.gvsig.desktop-2.0.293/org.gvsig.desktop.compat.cdc/org.gvsig.remoteclient/src/main/java/org/gvsig/remoteclient/exceptions/ServerErrorException.java
1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
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 3
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
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.remoteclient.exceptions;
25

  
26
/**
27
 * <p>
28
 * Exception thrown when an unexpected response to a request was obtained
29
 * from a server. It denotes that the server could not process the request
30
 * by a critical error and it could not answer a service-specific defined
31
 * exception.
32
 * </p>
33
 * <p>
34
 * Use it to distinguish a server exception that is not part of a service
35
 * exception definition.
36
 * </p>
37
 * @author jaume
38
 *
39
 */
40
public class ServerErrorException extends Exception {
41
	private static final long serialVersionUID = 1L;
42

  
43
	public ServerErrorException(){
44
        super();
45
    }
46
    
47
    public ServerErrorException(String message){
48
        super(message);
49
    }
50
    
51
    public ServerErrorException(String message, Throwable e){
52
        super(message, e);
53
    }
54
}
0 55

  
tags/org.gvsig.desktop-2.0.293/org.gvsig.desktop.compat.cdc/org.gvsig.remoteclient/src/main/java/org/gvsig/remoteclient/ILayer.java
1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
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 3
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
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24

  
25
package org.gvsig.remoteclient;
26
/**
27
 * <p></p>
28
 * 
29
 */
30
public interface ILayer {
31
/**
32
 * <p>Does ...</p>
33
 * 
34
 * 
35
 * @return 
36
 */
37
    public String getName();
38
/**
39
 * <p>Does ...</p>
40
 * 
41
 * 
42
 * @param _name 
43
 */
44
    public void setName(String _name);
45
/**
46
 * <p>Does ...</p>
47
 * 
48
 * 
49
 * @param _title 
50
 */
51
    public void setTitle(String _title);
52
/**
53
 * <p>Does ...</p>
54
 * 
55
 * 
56
 * @return 
57
 */
58
    public String getTitle();
59
/**
60
 * <p>Does ...</p>
61
 * 
62
 * 
63
 * @return 
64
 */
65
    public String getAbstract();
66
/**
67
 * <p>Does ...</p>
68
 * 
69
 * 
70
 * @param _abstract 
71
 */
72
    public void setAbstract(String _abstract);
73
}
74

  
75

  
0 76

  
tags/org.gvsig.desktop-2.0.293/org.gvsig.desktop.compat.cdc/org.gvsig.remoteclient/src/main/java/org/gvsig/remoteclient/IRasterClient.java
1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
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 3
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
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24

  
25
package org.gvsig.remoteclient;
26
/**
27
 * <p></p>
28
 * 
29
 */
30
public interface IRasterClient {
31
}
32

  
33

  
0 34

  
tags/org.gvsig.desktop-2.0.293/org.gvsig.desktop.compat.cdc/org.gvsig.remoteclient/src/main/java/org/gvsig/remoteclient/ogc/OGCClientOperation.java
1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
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 3
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
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
 
25
package org.gvsig.remoteclient.ogc;
26

  
27
import java.util.Hashtable;
28

  
29
public abstract class OGCClientOperation {
30
	public static final int PROTOCOL_UNDEFINED = -1;
31
	public static final int PROTOCOL_GET = 0;
32
	public static final int PROTOCOL_POST = 1;
33
	protected String operationName;
34
	protected String onlineResource;
35
		
36
	public OGCClientOperation(String operationName) {
37
		super();
38
		this.operationName = operationName;		
39
	}		
40

  
41
	public OGCClientOperation(String operationName, String onlineResource) {
42
		this.onlineResource = onlineResource;
43
		this.operationName = operationName;
44
	}	
45
	
46
	public abstract Hashtable getOperations(); 
47
	
48
	/**
49
	 * @return Returns the onlineResource.
50
	 */
51
	public String getOnlineResource() {
52
		return onlineResource;
53
	}
54
	/**
55
	 * @param onlineResource The onlineResource to set.
56
	 */
57
	public void setOnlineResource(String onlineResource) {
58
		this.onlineResource = onlineResource;
59
	}	
60

  
61
	/**
62
	 * @return Returns the operationName.
63
	 */
64
	public String getOperationName() {
65
		return operationName;
66
	}
67
	
68
	/**
69
	 * @param operationName The operationName to set.
70
	 */
71
	public void setOperationName(String operationName) {
72
		this.operationName = operationName;
73
	}		
74
}
75

  
76

  
77

  
0 78

  
tags/org.gvsig.desktop-2.0.293/org.gvsig.desktop.compat.cdc/org.gvsig.remoteclient/src/main/java/org/gvsig/remoteclient/ogc/OGCProtocolHandler.java
1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
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 3
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
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.remoteclient.ogc;
25

  
26
import java.io.File;
27
import java.io.FileInputStream;
28
import java.io.IOException;
29

  
30
import org.kxml2.io.KXmlParser;
31
import org.xmlpull.v1.XmlPullParserException;
32

  
33
import org.gvsig.compat.CompatLocator;
34
import org.gvsig.compat.net.Downloader;
35
import org.gvsig.remoteclient.utils.CapabilitiesTags;
36

  
37
public abstract class OGCProtocolHandler {
38
    protected static final Downloader downloader = CompatLocator.getDownloader();
39
    
40
    /**
41
     * procotol handler name
42
     */
43
    protected String name;
44
    /**
45
     * protocol handler version
46
     */
47
    protected String version;
48
    /**
49
     * host of the WMS to connect
50
     */
51
    protected String host;
52
    /**
53
     *  port number of the comunication channel of the WMS to connect
54
     */
55
    protected String port;    
56

  
57
    /**
58
     * @return Returns the host.
59
     */
60
    public String getHost() {
61
        return host;
62
    }
63
    /**
64
     * @param host The host to set.
65
     */
66
    public void setHost(String host) {
67
        this.host = host;
68
    }
69
    /**
70
     * @return Returns the name.
71
     */
72
    public String getName() {
73
        return name;
74
    }
75
    /**
76
     * @param name The name to set.
77
     */
78
    public void setName(String name) {
79
        this.name = name;
80
    }
81
    /**
82
     * @return Returns the port.
83
     */
84
    public String getPort() {
85
        return port;
86
    }
87
    /**
88
     * @param port The port to set.
89
     */
90
    public void setPort(String port) {
91
        this.port = port;
92
    }
93
    /**
94
     * @return Returns the version.
95
     */
96
    public String getVersion() {
97
        return version;
98
    }
99
    /**
100
     * @param version The version to set.
101
     */
102
    public void setVersion(String version) {
103
        this.version = version;
104
    }
105

  
106
    /**
107
     * parses the data retrieved by the Capabilities XML document
108
     */
109
    public abstract boolean parseCapabilities(File f);
110

  
111
    public abstract OGCServiceInformation getServiceInformation();
112

  
113
    /**
114
     * Just for not repeat code. Gets the correct separator according 
115
     * to the server URL
116
     * @param h
117
     * @return
118
     */
119
    protected static String getSymbol(String h) {
120
        String symbol;
121
        if (h.indexOf("?")==-1) 
122
            symbol = "?";
123
        else if (h.indexOf("?")!=h.length()-1)
124
            symbol = "&";
125
        else
126
            symbol = "";
127
        return symbol;
128
    }  
129

  
130
    /**
131
     * Parse an operation into a DcpType tag
132
     * @param parser
133
     * The KXMLParser
134
     * @param operation
135
     * The WFS operation to parse
136
     * @throws IOException 
137
     * @throws XmlPullParserException 
138
     */
139
    protected void parserDcpType(KXmlParser parser, String operation) throws XmlPullParserException, IOException {        
140
        int currentTag;
141
        boolean end = false;
142

  
143
        currentTag = parser.next();
144

  
145
        while (!end) 
146
        {
147
            switch(currentTag)
148
            {
149
            case KXmlParser.START_TAG:
150
                if(parser.getName().compareTo(CapabilitiesTags.HTTP)==0){
151
                    parseHTTPTag(parser, operation);
152
                }	         
153
                break;
154
            case KXmlParser.END_TAG:
155
                if (parser.getName().compareTo(CapabilitiesTags.DCPTYPE) == 0)
156
                    end = true;
157
                break;
158
            case KXmlParser.TEXT:                   
159
                break;
160
            }
161
            if (!end){
162
                currentTag = parser.next();
163
            }
164
        }     
165
    }
166

  
167
    /**
168
     * Parse an operation into a HTTP tag
169
     * @param parser
170
     * The KXMLParser
171
     * @param operation
172
     * The WFS operation to parse
173
     * @throws IOException 
174
     * @throws XmlPullParserException 
175
     */
176
    protected void parseHTTPTag(KXmlParser parser, String operation) throws XmlPullParserException, IOException {        
177
        int currentTag;
178
        boolean end = false;
179

  
180
        currentTag = parser.next();
181
        int protocol = -1;
182

  
183
        while (!end) 
184
        {
185
            switch(currentTag)
186
            {
187
            case KXmlParser.START_TAG:
188
                String value = null;
189
                if(parser.getName().compareTo(CapabilitiesTags.GET)==0){
190
                    protocol = OGCClientOperation.PROTOCOL_GET;
191
                    addOperationByAttribute(parser, operation, protocol);
192
                }else if(parser.getName().compareTo(CapabilitiesTags.POST)==0){
193
                    protocol = OGCClientOperation.PROTOCOL_POST;
194
                    addOperationByAttribute(parser, operation, protocol);
195
                }else if(parser.getName().compareTo(CapabilitiesTags.ONLINERESOURCE)==0){
196
                    addOperationByAttribute(parser, operation, protocol);
197
                }				
198
                break;
199
            case KXmlParser.END_TAG:
200
                if (parser.getName().compareTo(CapabilitiesTags.HTTP) == 0)
201
                    end = true;
202
                break;
203
            case KXmlParser.TEXT:                   
204
                break;
205
            }
206
            if (!end){
207
                currentTag = parser.next();
208
            }
209
        }     
210
    }
211

  
212
    /**
213
     * Add an operation and the online resource 
214
     * @param parser
215
     * The parser
216
     * @param operation
217
     * The operation to add
218
     * @param protocol
219
     * The parser to add
220
     */
221
    protected void addOperationByAttribute(KXmlParser parser, String operation, int protocol){
222
        String value = null;
223
        if (protocol > -1){
224
            for (int i=0 ; i<parser.getAttributeCount() ; i++){
225
                if ((parser.getAttributeName(i).toUpperCase().compareTo(CapabilitiesTags.ONLINERESOURCE.toUpperCase()) == 0) ||
226
                    (parser.getAttributeName(i).equals(CapabilitiesTags.XLINK_HREF))){					
227
                    value = parser.getAttributeValue(i);
228
                }
229
            }								
230
            if (value == null){
231
                getServiceInformation().addOperation(operation, protocol);
232
            }else{
233
                getServiceInformation().addOperation(operation, protocol, value);
234
            }
235
        }  
236
    }
237

  
238
    /**
239
     * Copy the file in a byte array
240
     * @param file
241
     * The file to copy
242
     * @return
243
     * An array of bytes
244
     * @throws IOException
245
     */
246
    protected byte[] fileToBytes(File file) throws IOException{
247
        FileInputStream fis = null;
248
        byte[] bytes = null;
249
        try{
250
            fis = new FileInputStream(file);
251

  
252
            long length = file.length(); 
253
            bytes = new byte[(int)file.length()];
254

  
255
            int offset = 0;
256
            int numRead = 0; 
257
            while (offset < bytes.length && (numRead=fis.read(bytes, offset, bytes.length-offset)) >= 0) 
258
            { 
259
                offset += numRead; 
260
            }
261
        }catch (IOException e) {
262
            throw e;
263
        }finally{
264
            if (fis != null){
265
                fis.close();
266
            }
267
        }         
268
        return bytes;
269
    }
270
}
0 271

  
tags/org.gvsig.desktop-2.0.293/org.gvsig.desktop.compat.cdc/org.gvsig.remoteclient/src/main/java/org/gvsig/remoteclient/ogc/request/OGCRequest.java
1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
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 3
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
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24

  
25
package org.gvsig.remoteclient.ogc.request;
26

  
27
import java.io.File;
28
import java.io.IOException;
29
import java.net.ConnectException;
30
import java.net.MalformedURLException;
31
import java.net.URL;
32
import java.net.UnknownHostException;
33

  
34
import org.gvsig.compat.CompatLocator;
35
import org.gvsig.compat.lang.StringUtils;
36
import org.gvsig.compat.net.ICancellable;
37
import org.gvsig.remoteclient.RemoteClientStatus;
38
import org.gvsig.remoteclient.ogc.OGCClientOperation;
39
import org.gvsig.remoteclient.ogc.OGCProtocolHandler;
40
import org.gvsig.remoteclient.utils.Utilities;
41

  
42
public abstract class OGCRequest {
43
	protected RemoteClientStatus status = null;
44
	protected OGCProtocolHandler protocolHandler = null;
45
	protected boolean isDeleted = false;
46
	protected static final String XMLTAG_STARTCHARACTER = "<";
47
	protected static final String XMLTAG_FINISHCHARACTER = "</";
48
	protected static final String XMLTAG_ENDCHARACTER = ">";
49
	
50
	private static final StringUtils stringUtils = CompatLocator.getStringUtils();
51

  
52
	public OGCRequest(RemoteClientStatus status, OGCProtocolHandler protocolHandler) {
53
		super();
54
		this.status = status;
55
		this.protocolHandler = protocolHandler;
56
	}	
57

  
58
	/**
59
	 * Send a request to the server.
60
	 * @return
61
	 * The server reply
62
	 * @throws IOException 
63
	 * @throws UnknownHostException 
64
	 * @throws ConnectException 
65
	 */
66
	public File sendRequest(ICancellable cancel) throws ConnectException, UnknownHostException, IOException{
67
		//if the status is null is because is a GetCapabilities operation
68
		if (status != null){
69
			if (status.getProtocol() != OGCClientOperation.PROTOCOL_UNDEFINED){
70
				if (status.getProtocol() == OGCClientOperation.PROTOCOL_GET){
71
					String onlineResource = protocolHandler.getHost();
72
					String symbol = getSymbol(onlineResource);
73
					onlineResource = onlineResource + symbol;
74
					return sendHttpGetRequest(onlineResource, cancel);
75
				}else{
76
					String onlineResource = protocolHandler.getHost();
77
					return sendHttpPostRequest(onlineResource);
78
				}
79
			}
80
		}
81

  
82
		//if exists an online resource for the GET operation
83
		String onlineResource = protocolHandler.getServiceInformation().getOnlineResource(getOperationName(), OGCClientOperation.PROTOCOL_GET);
84
		if (onlineResource != null){
85
			String symbol = getSymbol(onlineResource);
86
			onlineResource = onlineResource + symbol;
87
			return sendHttpGetRequest(onlineResource, cancel);
88
		}
89
		//if exists an online resource for the POST operation
90
		onlineResource =  protocolHandler.getServiceInformation().getOnlineResource(getOperationName(), OGCClientOperation.PROTOCOL_POST);
91
		if (onlineResource != null){
92
			return sendHttpPostRequest(onlineResource);
93
		}
94
		//If the online resource doesn't exist, it tries with the server URL and GET
95
		onlineResource = protocolHandler.getHost();
96
		String symbol = getSymbol(onlineResource);
97
		onlineResource = onlineResource + symbol;
98
		return sendHttpGetRequest(onlineResource, cancel);
99
	}
100

  
101
	protected abstract String getHttpGetRequest(String onlineResource);
102

  
103
	protected abstract String getHttpPostRequest(String onlineResource);
104

  
105
	protected abstract String getTempFilePrefix();
106

  
107
	protected abstract String getOperationName();
108

  
109
	protected abstract String getSchemaLocation();	
110

  
111
	/**
112
	 * @return the URL used in the HTTP get operation
113
	 * @throws MalformedURLException
114
	 */
115
	public URL getURL() throws MalformedURLException{
116
		String onlineResource = protocolHandler.getServiceInformation().getOnlineResource(getOperationName(), OGCClientOperation.PROTOCOL_GET);
117
		if (onlineResource != null){
118
			String symbol = getSymbol(onlineResource);
119
			onlineResource = onlineResource + symbol;
120
			return new URL(getHttpGetRequest(onlineResource));
121
		}
122
		
123
		//If the online resource doesn't exist, it tries with the server URL and GET
124
		onlineResource = protocolHandler.getHost();
125
		String symbol = getSymbol(onlineResource);
126
		onlineResource = onlineResource + symbol;
127
		return new URL(getHttpGetRequest(onlineResource));
128
	}
129

  
130
	/**
131
	 * Send a Http request using the get protocol
132
	 * @param onlineResource
133
	 * @return
134
	 * @throws ConnectException
135
	 * @throws UnknownHostException
136
	 * @throws IOException
137
	 */
138
	private File sendHttpGetRequest(String onlineResource, ICancellable cancel) throws ConnectException, UnknownHostException, IOException{
139
		URL url = new URL(stringUtils.replaceAll(getHttpGetRequest(onlineResource), " ", "%20"));
140
		if (isDeleted()){
141
			Utilities.removeURL(url);
142
		}
143
		return Utilities.downloadFile(url, getTempFilePrefix(), cancel);		
144
	}
145

  
146
	/**
147
	 * Send a Http request using the post protocol
148
	 * @param onlineResource
149
	 * @return
150
	 * @throws ConnectException
151
	 * @throws UnknownHostException
152
	 * @throws IOException
153
	 */
154
	private File sendHttpPostRequest(String onlineResource) throws ConnectException, UnknownHostException, IOException{
155
		URL url = new URL(onlineResource);
156
		String data = getHttpPostRequest(onlineResource);
157
		if (isDeleted()){
158
			Utilities.removeURL(url+data);
159
		}
160
		return Utilities.downloadFile(url, data, getTempFilePrefix(), null);		
161
	}
162

  
163
	/**
164
	 * Just for not repeat code. Gets the correct separator according 
165
	 * to the server URL
166
	 * @param h
167
	 * @return
168
	 */
169
	protected static String getSymbol(String h) {
170
		String symbol;
171
		if (h.indexOf("?")==-1) 
172
			symbol = "?";
173
		else if (h.indexOf("?")!=h.length()-1)
174
			symbol = "&";
175
		else
176
			symbol = "";
177
		return symbol;
178
	}
179

  
180
	public boolean isDeleted() {
181
		return isDeleted;
182
	}
183

  
184
	public void setDeleted(boolean isDeleted) {
185
		this.isDeleted = isDeleted;
186
	}
187
	
188
	protected String createXMLStartTag(String tagName){
189
	    return XMLTAG_STARTCHARACTER + tagName + XMLTAG_ENDCHARACTER;
190
	}
191
	
192
   protected String createXMLEndtTag(String tagName){
193
        return XMLTAG_FINISHCHARACTER + tagName + XMLTAG_ENDCHARACTER;
194
    }
195

  
196
}
197

  
0 198

  
tags/org.gvsig.desktop-2.0.293/org.gvsig.desktop.compat.cdc/org.gvsig.remoteclient/src/main/java/org/gvsig/remoteclient/ogc/OGCServiceInformation.java
1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
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 3
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
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
 
25
package org.gvsig.remoteclient.ogc;
26

  
27
import java.util.HashMap;
28
import java.util.Hashtable;
29
import java.util.Iterator;
30

  
31

  
32

  
33
public abstract class OGCServiceInformation {
34
	public String online_resource = null;
35
	protected HashMap operationsGet = new HashMap();
36
	protected HashMap operationsPost = new HashMap();
37
	
38

  
39
	/**
40
	 * @return Returns the online_resource.
41
	 */
42
	 public String getOnline_resource() {
43
		return online_resource;
44
	}
45

  
46
	 public abstract OGCClientOperation createOperation(String name);
47
	 public abstract OGCClientOperation createOperation(String name, String onlineResource);
48
	/**
49
	 * Add a new supported operation
50
	 * @param operation
51
	 * The operation to support
52
	 * @param protocol
53
	 * The HTTP protocol (Get or Post)
54
	 */
55
	public void addOperation(String operation, int protocol){
56
		if (protocol == OGCClientOperation.PROTOCOL_GET){
57
			operationsGet.put(operation, createOperation(operation));			
58
		}else if (protocol == OGCClientOperation.PROTOCOL_POST){
59
			operationsPost.put(operation, createOperation(operation));
60
		}
61
	}
62
	
63
	/**
64
	 * Add a new supported operation
65
	 * @param operation
66
	 * The operation to support
67
	 * @param protocol
68
	 * The HTTP protocol (Get or Post)
69
	 * @param onlineResource
70
	 * The online resource
71
	 */
72
	public void addOperation(String operation, int protocol, String onlineResource){
73
		if (protocol == OGCClientOperation.PROTOCOL_GET){
74
			operationsGet.put(operation, createOperation(operation, onlineResource));
75
		}else if (protocol == OGCClientOperation.PROTOCOL_POST){
76
			operationsPost.put(operation, createOperation(operation, onlineResource));
77
		}
78
	}
79
	
80
	/**
81
	 * Gest the online resource for a concrete operation
82
	 * @param operation
83
	 * The operation
84
	 * @param protocol
85
	 * The HTTP protocol (Get or Post)
86
	 * @return
87
	 * The online resource
88
	 */
89
	public String getOnlineResource(String operation, int protocol){
90
		OGCClientOperation op = null;
91
		if (protocol == OGCClientOperation.PROTOCOL_GET){
92
			op = (OGCClientOperation)operationsGet.get(operation);
93
		}else if (protocol == OGCClientOperation.PROTOCOL_POST){
94
			op = (OGCClientOperation)operationsPost.get(operation);
95
		}
96
		if ((op == null) ||
97
				(op.getOnlineResource() == null) || 
98
				(op.getOnlineResource().equals(""))){
99
			return null;
100
		}
101
		return op.getOnlineResource();
102
	}
103
	
104
	/**
105
	 * Gets the online resource for a concrete operation.
106
	 * The default protocol is GET
107
	 * @param operation
108
	 * The operation
109
	 * @return
110
	 * The online resource
111
	 */
112
	public String getOnlineResource(String operation){
113
		return getOnlineResource(operation, OGCClientOperation.PROTOCOL_GET);
114
	}
115
	
116
	/**
117
	 * Get a hash map with the supported operations
118
	 * @return
119
	 */
120
	public Hashtable getSupportedOperationsByName(){
121
		Hashtable operations = new Hashtable();
122
		Iterator getIt = operationsGet.keySet().iterator();
123
		while (getIt.hasNext()){
124
			String id = (String)getIt.next();
125
			OGCClientOperation operation = (OGCClientOperation)operationsGet.get(id);
126
			operations.put(operation.getOperationName(),
127
					operation.getOnlineResource());
128
		}
129
		return operations;
130
	}
131
	
132
	public boolean isOperationSupported(String operationName){
133
	    if (operationsGet.containsKey(operationName)){
134
	        return true;
135
	    }
136
	    if (operationsPost.containsKey(operationName)){
137
            return true;
138
        }
139
	    return false;
140
	}	
141
}
142

  
0 143

  
tags/org.gvsig.desktop-2.0.293/org.gvsig.desktop.compat.cdc/org.gvsig.remoteclient/src/main/java/org/gvsig/remoteclient/taskplanning/retrieving/RequestManager.java
1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
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 3
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
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.remoteclient.taskplanning.retrieving;
25

  
26
import java.io.File;
27
import java.net.MalformedURLException;
28
import java.util.Hashtable;
29
import java.util.TreeMap;
30

  
31
import org.gvsig.remoteclient.taskplanning.IQueue;
32

  
33
/**
34
 * pa administrar les tasques (la hist?ria aquella
35
 *  de que hi haja una cola per a cada servidor)
36
 * @author Luis W. Sevilla (sevilla_lui@gva.es)
37
 */
38
public class RequestManager {
39
	private boolean debug = true;
40
    
41
	private static RequestManager instance;
42
	private TreeMap serversTable = new TreeMap();
43
	private RequestManager() {} // Avoid public instantiation
44
	 
45
	public static RequestManager getInstance() {
46
		if (instance == null)
47
			instance = new RequestManager();
48
		return instance;
49
	}
50
	
51
	
52
	public URLRetrieveTask addURLRequest(URLRequest request, RetrieveListener listener) {
53
		try {
54
			
55
			// TODO canviar per a quetorne el Request antic, que la request guarde el
56
			// seu estat aix? com la llista de listeners
57
			File f = getPreviousDownloadedURLRequest(request);
58
			
59
			if (f!=null) {
60
				// The file was already requested and it is in the cache or
61
				// the download is in process
62
				
63
				// Overwrite the file name with the file in the cache's one.
64
				request.setFileName(f.getAbsolutePath());
65
				System.out.println(request.getUrl()+" is cached at '"+f.getAbsolutePath()+"'");
66
				
67

  
68
				// get this server's task queue
69
				RetrieveQueue serverQueue = (RetrieveQueue) getQueue(request.getHost());
70
				
71
				// Look up the previous cached jobs
72
				
73
				URLRetrieveTask workingTask = serverQueue.getURLPreviousRequest(request);
74
				if (workingTask == null) {
75
					// Task already done. Notify listener
76
					if (debug)
77
						System.err.println("done job found: "+request.getUrl());
78
					RetrieveEvent event = new RetrieveEvent();
79
					event.setType(RetrieveEvent.REQUEST_FINISHED);
80
					listener.transferEventReceived(event);
81
					
82
				} else {
83
					// The task is working yet, will register the listener
84
					
85
					// TODO no va b?... perqu? la cola va buidant-se molt r?pidament
86
					// per a fer que vaja tamb? hi hauria que registrar en cache
87
					// lo que s'est? baixant al principi de l'execute();
88
					if (debug)
89
						System.err.println("working job found: "+request.getUrl());
90
					workingTask.addRetrieveListener(listener);
91
					
92
				}
93
			} else {
94
				// Pick an unrepeatable fileName
95
				String host = request.getHost();
96
				String fileName = request.getFileName();
97
				File tempDir = new File(tempDirectoryPath);
98
				if (!tempDir.exists())
99
					tempDir.mkdir();
100
				String fileNamePrefix = tempDirectoryPath + 
101
											File.separator + host + 
102
											"-" ;
103
				if (fileName.startsWith(fileNamePrefix))
104
					fileName = fileName.substring(fileNamePrefix.length(), fileName.length());
105
				
106
				// get this server's task queue
107
				RetrieveQueue serverQueue = (RetrieveQueue) getQueue(request.getHost());
108
				
109
				
110
				fileName = fileNamePrefix + fileName;
111
				
112
				request.setFileName(fileName);
113
				
114
				// TODO
115
				// jo ac? comprovaria quin protocol, m?tode, host, etc... i crearia un
116
				// objecte que tractara la desc?rrega segons el m?tode que li toca.
117
				// algo en plan Strategy's
118
				//
119
				// per exemple si fem URLRetrieveTask una abstracta i tenim
120
				// 
121
				// GET de tota la vida
122
				// serverQueue.put(new HTTPGetRetrieveTask(request, listener));
123
				//
124
				// POST
125
				// serverQueue.put(new HTTPPostRetrieveTask(request, listener));
126
				//
127
				// FTP
128
				// serverQueue.put(new FTPRetrieveTask(request, listener));
129
				//
130
				// ????Xarxa local?????
131
				// serverQueue.put(new SMBRetrieveTask(request, listener));
132
				
133
				// Enqueue the request and the listener will be notified when done.
134
				URLRetrieveTask task = new URLRetrieveTask(request, listener);
135
				return (URLRetrieveTask) serverQueue.put(task);
136
			}
137
		} catch (MalformedURLException e) {
138
			e.printStackTrace();
139
		}
140
		return null;
141
	}
142
	
143
	private IQueue getQueue(String hostName) {
144
		RetrieveQueue queue = null;
145
		if (serversTable.containsKey(hostName))
146
			queue = (RetrieveQueue) serversTable.get(hostName);
147
		else {
148
			// crea la cola
149
			queue = new RetrieveQueue(hostName);
150
			// pone la cola del server en marcha.
151
		}
152
		return queue;
153
	}
154

  
155

  
156
	private Hashtable downloadedFiles;
157
	private final String tempDirectoryPath = System.getProperty("java.io.tmpdir")+"tmp-andami";
158
	/**
159
     * Remove an URL from the system cache. The file will remain in the file
160
     * system for further eventual uses.
161
     * @param request
162
     */
163
	public void removeURLRequest(URLRequest request) {
164
		if (downloadedFiles != null && downloadedFiles.containsKey(request))
165
			downloadedFiles.remove(request);
166
	}
167
	/**
168
     * Adds an URL to the table of downloaded files for further uses. If the URL
169
     * already exists in the table its filePath value is updated to the new one and
170
     * the old file itself is removed from the file system.
171
     * 
172
     * @param url
173
     * @param filePath
174
     */
175
    protected void addDownloadedURLRequest(URLRequest request, String filePath){
176
        if (downloadedFiles==null)
177
            downloadedFiles = new Hashtable();
178
        String fileName = (String) downloadedFiles.put(request, filePath);
179
        if (fileName!=null){
180
            File f = new File(fileName);
181
            if (f.exists())
182
                f.delete();
183
        }
184
    }
185
    /**
186
     * Returns the content of this URL as a file from the file system.<br>
187
     * <p>
188
     * If the URL has been already downloaded in this session and notified 
189
     * to the system using the static <b>Utilities.addDownloadedURL(URL)</b>
190
     * method, it can be restored faster from the file system avoiding to
191
     * download it again.
192
     * </p>
193
     * @param url
194
     * @return File containing this URL's content or null if no file was found.
195
     */
196
    private File getPreviousDownloadedURLRequest(URLRequest request){
197
        File f = null;
198
        if (downloadedFiles!=null && downloadedFiles.containsKey(request)){
199
            String filePath = (String) downloadedFiles.get(request);
200
            f = new File(filePath);
201
        }
202
        return f;
203
    }
204
    
205
}
0 206

  
tags/org.gvsig.desktop-2.0.293/org.gvsig.desktop.compat.cdc/org.gvsig.remoteclient/src/main/java/org/gvsig/remoteclient/taskplanning/retrieving/RetrieveEvent.java
1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
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 3
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
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.remoteclient.taskplanning.retrieving;
25

  
26
/**
27
 * @author jaume dominguez faus - jaume.dominguez@iver.es
28
 * Luis W. Sevilla (sevilla_lui@gva.es)
29
 */
30
public class RetrieveEvent {
31
	public static final int NOT_STARTED = 0;
32
	public static final int CONNECTING = 1;
33
	public static final int TRANSFERRING = 2;
34
	public static final int REQUEST_FINISHED = 3;
35
	public static final int REQUEST_FAILED = 4;
36
	public static final int REQUEST_CANCELLED = 5;
37
	public static final int POSTPROCESSING = 6;
38
	
39
	/**
40
	 * redundant; use REQUEST_FAILED
41
	 * @deprecated ?
42
	 */
43
	public static final int ERROR = 11;
44
	
45
	private int eventType;
46
	
47
	public void setType(int type) {
48
		eventType = type;
49
	}
50

  
51
	public int getType() {
52
		return eventType;
53
	}
54
}
0 55

  
tags/org.gvsig.desktop-2.0.293/org.gvsig.desktop.compat.cdc/org.gvsig.remoteclient/src/main/java/org/gvsig/remoteclient/taskplanning/retrieving/RetrieveQueue.java
1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
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 3
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
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.remoteclient.taskplanning.retrieving;
25

  
26
import java.util.Date;
27
import java.util.Vector;
28

  
29
import org.gvsig.remoteclient.taskplanning.FIFOTaskPlanner;
30
import org.gvsig.remoteclient.taskplanning.IQueue;
31
import org.gvsig.remoteclient.taskplanning.IRunnableTask;
32
import org.gvsig.remoteclient.taskplanning.ITaskPlanner;
33

  
34
/**
35
 * @author jaume dominguez faus - jaume.dominguez@iver.es
36
 * 		   Luis W. Sevilla (sevilla_lui@gva.es)
37
 */
38
public class RetrieveQueue implements IQueue {
39
	private String hostName;
40
	private Date startTime;
41
	private Vector tasks = new Vector();
42
	private boolean waiting;
43
	private ITaskPlanner taskPlanner;
44
	private Worker worker;
45
	
46
	/**
47
	 * 
48
	 */
49
	public RetrieveQueue(String hName) {
50
		hostName = hName;
51
		startTime = new Date();
52
		worker = new Worker();
53
		new Thread(worker).start();
54
	}
55
	
56
	
57
	public IRunnableTask put(IRunnableTask task) {
58
		tasks.add(task);
59
		if (waiting) {
60
			synchronized (this) {
61
				notifyAll();
62
			}
63
		}
64
		return task;
65
	}
66
	
67
	public IRunnableTask take() {
68
		if (tasks.isEmpty()) {
69
			synchronized (this) {
70
				waiting = true;
71
				try {
72
					wait();
73
				} catch (InterruptedException ie) {
74
					waiting = false;
75
				}
76
			}
77
		}
78
		return getTaskPlanner().nextTask() ;
79
	}
80
	
81
	
82
	
83
	public boolean isEmpty() {
84
		synchronized (this) {
85
			return tasks.isEmpty() && !worker.r.isRunning();
86
		}
87
	}
88
	
89
	
90

  
91
	public ITaskPlanner getTaskPlanner() {
92
		if (taskPlanner == null) {
93
			taskPlanner = new FIFOTaskPlanner(this);
94
		}
95
		return taskPlanner;
96
	}
97

  
98

  
99
	public void setTaskPlanner(ITaskPlanner planner) {
100
		taskPlanner = planner;
101
	}
102

  
103

  
104
	public void pause() {
105
		waiting = true;
106
	}
107

  
108

  
109
	public void resume() {
110
		waiting = false;
111
	}
112

  
113

  
114
	public Vector getTasks() {
115
		return tasks;
116
	}
117

  
118
	private class Worker implements Runnable {
119
		URLRetrieveTask r;
120
		int i = 0; 
121
		public void run() {
122
			while (true) {
123
				r = (URLRetrieveTask) take();
124
				r.execute();
125
			}
126
		}
127
	}
128

  
129
	protected URLRetrieveTask getURLPreviousRequest(URLRequest request) {
130
		// Is the one currently running?
131
		/*URLRetrieveTask aux = (URLRetrieveTask) worker.r;
132
		if (request.equals(aux.getRequest())) {
133
				return aux;
134
		}*/	
135
		// Is one of those in the queue?
136
		for (int i = 0; i < tasks.size(); i++) {
137
			URLRetrieveTask task = (URLRetrieveTask) tasks.get(i);
138
			URLRequest aWorkingRequest = task.getRequest();
139
			if (aWorkingRequest.equals(request)) {
140
				return task;
141
			}
142
		}
143
		return null;
144
	}
145
}
0 146

  
tags/org.gvsig.desktop-2.0.293/org.gvsig.desktop.compat.cdc/org.gvsig.remoteclient/src/main/java/org/gvsig/remoteclient/taskplanning/retrieving/RetrieveListener.java
1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
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 3
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
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.remoteclient.taskplanning.retrieving;
25

  
26

  
27
/**
28
 * @author Luis W. Sevilla (sevilla_lui@gva.es)
29
 */
30
public interface RetrieveListener {
31
	public void transferEventReceived(RetrieveEvent event);
32
}
0 33

  
tags/org.gvsig.desktop-2.0.293/org.gvsig.desktop.compat.cdc/org.gvsig.remoteclient/src/main/java/org/gvsig/remoteclient/taskplanning/retrieving/URLRequest.java
1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
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 3
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
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.remoteclient.taskplanning.retrieving;
25

  
26
import java.net.MalformedURLException;
27
import java.net.URL;
28

  
29
/**
30
 * @author Luis W. Sevilla (sevilla_lui@gva.es)
31
 */
32
public class URLRequest{
33
	public static final String HTTP = "http";
34
	private volatile int hashCode = 0;
35
	public static final int GET = 1;
36
	public static final int POST = 2;
37
	
38
	private int requestType = GET;
39
	private String protocol;
40
	private String host;
41
	private int port = -1;
42
	private String file;
43
	private String fileName;
44
	
45
	public URL getUrl() throws MalformedURLException {
46
		String u = protocol;
47
		u += "://"+host;
48
		if (port != -1)
49
			u += ":"+port;
50
		u += "/"+file;
51
		return new URL(u);
52
	}
53
	/**
54
	 * @return Returns the fileName.
55
	 */
56
	public String getFileName() {
57
		return fileName;
58
	}
59
	/**
60
	 * @param fileName The fileName to set.
61
	 */
62
	public void setFileName(String fileName) {
63
		this.fileName = fileName;
64
	}
65
	/**
66
	 * @return Returns the host.
67
	 */
68
	public String getHost() {
69
		return host;
70
	}
71
	/**
72
	 * @param host The host to set.
73
	 */
74
	public void setHost(String host) {
75
		this.host = host;
76
	}
77
	/**
78
	 * @return Returns the file.
79
	 */
80
	public String getFile() {
81
		return file;
82
	}
83
	/**
84
	 * @param page The file to set.
85
	 */
86
	public void setFile(String page) {
87
		this.file = page;
88
	}
89
	/**
90
	 * @return Returns the protocol.
91
	 */
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff