Revision 42721

View differences:

tags/org.gvsig.desktop-2.0.128/org.gvsig.desktop.compat.cdc/pom.xml
1
<?xml version="1.0" encoding="UTF-8"?>
2
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3

  
4
  <modelVersion>4.0.0</modelVersion>
5
  <artifactId>org.gvsig.desktop.compat.cdc</artifactId>
6
  <packaging>pom</packaging>
7
  <name>${project.artifactId}</name>
8
  <parent>
9
      <groupId>org.gvsig</groupId>
10
      <artifactId>org.gvsig.desktop</artifactId>
11
      <version>2.0.128</version>
12
  </parent>
13

  
14
  <build>
15
      <plugins>
16
    
17
      </plugins>
18
  </build>
19

  
20
  <modules>
21
      <module>org.gvsig.i18n</module>
22
      <module>org.gvsig.compat</module>
23
      <module>org.gvsig.projection</module>
24
      <module>org.gvsig.fmap.geometry</module>
25
      <module>org.gvsig.remoteclient</module>
26
      <module>org.gvsig.fmap.dal</module>
27
      <module>org.gvsig.fmap.mapcontext</module>
28
      <module>org.gvsig.metadata.basic</module>
29
  </modules>
30

  
31
  <description>This project group the gvSIG libraries that haven java 1.4 compatibility.</description>
32
</project>
0 33

  
tags/org.gvsig.desktop-2.0.128/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.128/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.128/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.128/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.128/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.128/org.gvsig.desktop.compat.cdc/org.gvsig.remoteclient/src/main/java/org/gvsig/remoteclient/taskplanning/ITaskPlanner.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;
25

  
26
/**
27
 * <p>
28
 * ITaskPlanner provides an interface to program your own task planning. It gives
29
 * you operations for pick a task from the queue according on the criteria that
30
 * you designed.<br>
31
 * </p>
32
 * <p>
33
 * The simplest implementation of ITaskPlanner would be a FIFO task planner (see
34
 * FIFOTaskPlanner.java) which takes jobs from a task queue in the same order
35
 * they were put. But any kind of planner is possible (SJF, LIFO, RoundRobin, etc.). 
36
 * </p>
37
 * @author jaume dominguez faus - jaume.dominguez@iver.es
38
 *
39
 */
40
public interface ITaskPlanner {
41
	/**
42
	 * Takes the next task to be executed.
43
	 * @return IRunnableTask representing the next task to be executed
44
	 */
45
	public IRunnableTask nextTask();
46
	
47
	/**
48
	 * Takes the previous executed task. Notice that it may or may not have
49
	 * sense for specific implementations. For example, in a FIFO-like planner,
50
	 * the task is taken from the queue, executed until it is finished and 
51
	 * removed from the queue. So, there is no previous task.
52
	 * 
53
	 * @return IRunnableTask representing the previous executed task, or null
54
	 * if none.
55
	 * @deprecated (probably this is unuseful and i'll remove it)
56
	 */
57
	public IRunnableTask previousTask();
58
}
0 59

  
tags/org.gvsig.desktop-2.0.128/org.gvsig.desktop.compat.cdc/org.gvsig.remoteclient/src/main/java/org/gvsig/remoteclient/taskplanning/IQueue.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;
25

  
26
import java.util.Vector;
27

  
28
/**
29
 *  <p>
30
 *  You should write your own concrete Queue implementation to hold the tasks in
31
 *  a specific kind of queue.<br>
32
 *  </p>
33
 *  <p>
34
 *  Following the contract, you should also write a task planner that does what 
35
 *  you desire. Task planners are a concrete class of the ITaskPlanner that would 
36
 *  plannify which is the next, the previous,.. task to be done. However, if
37
 *  you don't want to write anything special, just guess a simple FIFO queue<br>
38
 *  </p>
39
 *  
40
 *  @author jaume dominguez faus - jaume.dominguez@iver.es
41
 *  @see ITaskPlanner
42
 */
43
public interface IQueue {
44
	
45
	/**
46
	 * Adds a new task to the queue. The place where the new task will be put its
47
	 * left to the concrete implementation of this interface.
48
	 * @param IRunnableTask task
49
	 */
50
	IRunnableTask put(IRunnableTask task);
51
	
52
	/**
53
	 * Returns the next task by calling the task planner's nextTask() method.
54
	 * @return IRunnableTask with the next task to be executed.
55
	 */
56
	IRunnableTask take();
57
	
58
	/**
59
	 * Returns true if the Queue has no (more) jobs to do.
60
	 * @return
61
	 */
62
	boolean isEmpty();
63
	
64
	/**
65
	 * Returns the task planner currently defined by this queue.
66
	 * @return ITaskPlanner
67
	 */
68
	ITaskPlanner getTaskPlanner();
69
	
70
	/**
71
	 * Sets the TaskPlanner that will decide which of the tasks in the queue will
72
	 * be executed next. A null value should represent a FIFO planner. 
73
	 * @param planner
74
	 */
75
	void setTaskPlanner(ITaskPlanner planner);
76
	
77
	/**
78
	 * Causes the execution of this queue to be paused. The task currently in execution
79
	 * finishes and after it the planner will not issue more tasks until resume() is
80
	 * invoked.
81
	 */
82
	void pause();
83
	
84
	/**
85
	 * Causes the execution of this queue to be resumed. The execution will continue
86
	 * with the next task issued by the planner. It has no effect if the queue was not
87
	 * paused yet. 
88
	 */
89
	void resume();
90

  
91
	/**
92
	 * Returns the set of tasks in a Vector (thread-safe).
93
	 * @return Vector containing the tasks in this queue.
94
	 */
95
	Vector getTasks();
96
}
0 97

  
tags/org.gvsig.desktop-2.0.128/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.128/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.128/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
	 */
92
	public String getProtocol() {
93
		return protocol;
94
	}
95
	/**
96
	 * @param protocol The protocol to set.
97
	 */
98
	public void setProtocol(String protocol) {
99
		this.protocol = protocol;
100
	}
101
	/**
102
	 * @return Returns the requestType.
103
	 */
104
	public int getRequestType() {
105
		return requestType;
106
	}
107
	/**
108
	 * @param requestType The requestType to set.
109
	 */
110
	public void setRequestType(int requestType) {
111
		this.requestType = requestType;
112
	}
113
	/**
114
	 * @return Returns the port.
115
	 */
116
	public int getPort() {
117
		return port;
118
	}
119
	/**
120
	 * @param port The port to set.
121
	 */
122
	public void setPort(int port) {
123
		this.port = port;
124
	}
125

  
126
	public int hashCode() {
127
		if (hashCode == 0) {
128
			int result = 17;
129
			String[] stringFields = new String[] {
130
					fileName,
131
					host,
132
					file,
133
					protocol
134
			};
135
			for (int i = 0; i < stringFields.length; i++) {
136
				if (stringFields[i] != null)
137
					for (int j = 0; j < stringFields[i].length(); j++) 
138
						result = 37*result + (int) stringFields[i].charAt(j);
139
				
140
			}
141
			result = 37*result + port;
142
			result = 37*result + requestType;
143
		}
144
		return hashCode;
145
	}
146
	
147
	public boolean equals(Object o) {
148
		if (this == o)
149
			return true;
150
		if (!(o instanceof URLRequest))
151
			return false;
152

  
153
		URLRequest other = (URLRequest) o;
154
		
155
		String[] stringFields = new String[] {
156
				// this.fileName, NO!!! Do not check file name
157
				this.host,
158
				this.file,
159
				this.protocol
160
		};
161
		String[] othersStringField = new String[] {
162
				// other.fileName, NO!!! Do not check file name
163
				other.host,
164
				other.file,
165
				other.protocol
166
		};
167
		for (int i = 0; i < stringFields.length; i++) {
168
			if (stringFields[i] == null && othersStringField[i]!=null) return false;
169
			if (stringFields[i] == null && othersStringField[i]!=null) return false;
170
			if (stringFields[i] != null && othersStringField[i]!=null) 
171
				if (!stringFields[i].equals(othersStringField[i])) return false;
172
		}
173
		
174
		if (this.port != other.port) return false;
175
		if (this.requestType != other.requestType) return false;
176
		return true;
177
	}
178
}
0 179

  
tags/org.gvsig.desktop-2.0.128/org.gvsig.desktop.compat.cdc/org.gvsig.remoteclient/src/main/java/org/gvsig/remoteclient/taskplanning/retrieving/URLRetrieveTask.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
 * Based on portions of code from EarthFlicks
28
 * http://today.java.net/lpt/a/212
29
 */
30

  
31
import java.io.BufferedOutputStream;
32
import java.io.DataOutputStream;
33
import java.io.File;
34
import java.io.FileOutputStream;
35
import java.io.IOException;
36
import java.io.InputStream;
37
import java.net.MalformedURLException;
38
import java.net.URL;
39
import java.util.Iterator;
40
import java.util.Vector;
41

  
42
import org.gvsig.remoteclient.taskplanning.IRunnableTask;
43

  
44
/**
45
 * Clase para bajar ficheros en un thread independiente.
46
 */
47
public class URLRetrieveTask  implements IRunnableTask {
48
	
49
	private boolean running, cancelled;
50
	private URLRequest request;
51
	private Vector listeners = new Vector();
52
	private RetrieveEvent event = new RetrieveEvent();
53
	private InputStream is;
54
	
55
	/**
56
	 * 
57
	 */
58
	public URLRetrieveTask(URLRequest request, RetrieveListener listener) {
59
		this.request = request;
60
		addRetrieveListener(listener);
61
		running = cancelled = false;
62
	}
63
	
64

  
65
	public void execute() {
66
		event.setType(RetrieveEvent.NOT_STARTED);
67
		fireEvent();
68
		cancelled = false;
69
		running= true;
70
		
71
		long t = System.currentTimeMillis();
72
		File f = new File(request.getFileName()+System.currentTimeMillis());
73
		while (f.exists()) {
74
			t++;
75
			f = new File(request.getFileName()+t);
76
		}
77
		URL url;
78
		try {
79
			event.setType(RetrieveEvent.CONNECTING);
80
			fireEvent();
81
			url = request.getUrl();
82
			request.setFileName(f.getAbsolutePath());
83
			System.out.println("downloading '"+url+"' to: "+f.getAbsolutePath());
84
			
85
			DataOutputStream dos = new DataOutputStream( new BufferedOutputStream(new FileOutputStream(f)));
86
			byte[] buffer = new byte[1024*256];
87
			fireEvent();
88
			is = url.openStream();
89
			event.setType(RetrieveEvent.TRANSFERRING);
90
			fireEvent();
91
			if (!cancelled) {
92
				long readed = 0;
93
				for (int i = is.read(buffer); !cancelled && i>0; i = is.read(buffer)){
94
					dos.write(buffer, 0, i);
95
					readed += i;
96
				}
97
				
98
				dos.close();
99
			}
100
			
101
			if (cancelled) {
102
				// Bad incomplete file, delete it.
103
				System.out.println("download cancelled ("+url+")");
104
				f.delete();
105
			} else {
106
				// register job in the cache
107
				// TODO aix? deuria d'estar al principi per a poder capturar
108
				// treballs que s'estan fent per? que encara no s'han acabat
109
				synchronized (this) {
110
					RequestManager.getInstance().addDownloadedURLRequest(request, f.getAbsolutePath());
111
				}
112
			}
113
			
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff