Revision 39251

View differences:

tags/v2_0_0_Build_2058/libraries/libRemoteServices/src/org/gvsig/remoteclient/IRasterClient.java
1

  
2
package org.gvsig.remoteclient;
3
/**
4
 * <p></p>
5
 * 
6
 */
7
public interface IRasterClient {
8
}
9

  
10

  
0 11

  
tags/v2_0_0_Build_2058/libraries/libRemoteServices/src/org/gvsig/remoteclient/ogc/OGCServiceInformation.java
1
/* gvSIG. Geographic Information System of the Valencian Government
2
*
3
* Copyright (C) 2007-2008 Infrastructures and Transports Department
4
* of the Valencian Government (CIT)
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 2
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
*/
22

  
23
/*
24
* AUTHORS (In addition to CIT):
25
* 2009 Iver T.I.  {{Task}}
26
*/
27
 
28
package org.gvsig.remoteclient.ogc;
29

  
30
import java.util.HashMap;
31
import java.util.Hashtable;
32
import java.util.Iterator;
33

  
34

  
35

  
36
public abstract class OGCServiceInformation {
37
	public String online_resource = null;
38
	protected HashMap operationsGet = new HashMap();
39
	protected HashMap operationsPost = new HashMap();
40
	
41

  
42
	/**
43
	 * @return Returns the online_resource.
44
	 */
45
	 public String getOnline_resource() {
46
		return online_resource;
47
	}
48

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

  
0 146

  
tags/v2_0_0_Build_2058/libraries/libRemoteServices/src/org/gvsig/remoteclient/ogc/OGCClientOperation.java
1
/* gvSIG. Geographic Information System of the Valencian Government
2
*
3
* Copyright (C) 2007-2008 Infrastructures and Transports Department
4
* of the Valencian Government (CIT)
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 2
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
*/
22

  
23
/*
24
* AUTHORS (In addition to CIT):
25
* 2009 Iver T.I.  {{Task}}
26
*/
27
 
28
package org.gvsig.remoteclient.ogc;
29

  
30
import java.util.Hashtable;
31

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

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

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

  
79

  
80

  
0 81

  
tags/v2_0_0_Build_2058/libraries/libRemoteServices/src/org/gvsig/remoteclient/ogc/OGCProtocolHandler.java
1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2005 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41
package org.gvsig.remoteclient.ogc;
42

  
43
import java.io.File;
44
import java.io.FileInputStream;
45
import java.io.IOException;
46

  
47
import org.kxml2.io.KXmlParser;
48
import org.xmlpull.v1.XmlPullParserException;
49

  
50
import org.gvsig.compat.CompatLocator;
51
import org.gvsig.compat.net.Downloader;
52
import org.gvsig.remoteclient.utils.CapabilitiesTags;
53

  
54
public abstract class OGCProtocolHandler {
55
    protected static final Downloader downloader = CompatLocator.getDownloader();
56
    
57
    /**
58
     * procotol handler name
59
     */
60
    protected String name;
61
    /**
62
     * protocol handler version
63
     */
64
    protected String version;
65
    /**
66
     * host of the WMS to connect
67
     */
68
    protected String host;
69
    /**
70
     *  port number of the comunication channel of the WMS to connect
71
     */
72
    protected String port;    
73

  
74
    /**
75
     * @return Returns the host.
76
     */
77
    public String getHost() {
78
        return host;
79
    }
80
    /**
81
     * @param host The host to set.
82
     */
83
    public void setHost(String host) {
84
        this.host = host;
85
    }
86
    /**
87
     * @return Returns the name.
88
     */
89
    public String getName() {
90
        return name;
91
    }
92
    /**
93
     * @param name The name to set.
94
     */
95
    public void setName(String name) {
96
        this.name = name;
97
    }
98
    /**
99
     * @return Returns the port.
100
     */
101
    public String getPort() {
102
        return port;
103
    }
104
    /**
105
     * @param port The port to set.
106
     */
107
    public void setPort(String port) {
108
        this.port = port;
109
    }
110
    /**
111
     * @return Returns the version.
112
     */
113
    public String getVersion() {
114
        return version;
115
    }
116
    /**
117
     * @param version The version to set.
118
     */
119
    public void setVersion(String version) {
120
        this.version = version;
121
    }
122

  
123
    /**
124
     * parses the data retrieved by the Capabilities XML document
125
     */
126
    public abstract boolean parseCapabilities(File f);
127

  
128
    public abstract OGCServiceInformation getServiceInformation();
129

  
130
    /**
131
     * Just for not repeat code. Gets the correct separator according 
132
     * to the server URL
133
     * @param h
134
     * @return
135
     */
136
    protected static String getSymbol(String h) {
137
        String symbol;
138
        if (h.indexOf("?")==-1) 
139
            symbol = "?";
140
        else if (h.indexOf("?")!=h.length()-1)
141
            symbol = "&";
142
        else
143
            symbol = "";
144
        return symbol;
145
    }  
146

  
147
    /**
148
     * Parse an operation into a DcpType tag
149
     * @param parser
150
     * The KXMLParser
151
     * @param operation
152
     * The WFS operation to parse
153
     * @throws IOException 
154
     * @throws XmlPullParserException 
155
     */
156
    protected void parserDcpType(KXmlParser parser, String operation) throws XmlPullParserException, IOException {        
157
        int currentTag;
158
        boolean end = false;
159

  
160
        currentTag = parser.next();
161

  
162
        while (!end) 
163
        {
164
            switch(currentTag)
165
            {
166
            case KXmlParser.START_TAG:
167
                if(parser.getName().compareTo(CapabilitiesTags.HTTP)==0){
168
                    parseHTTPTag(parser, operation);
169
                }	         
170
                break;
171
            case KXmlParser.END_TAG:
172
                if (parser.getName().compareTo(CapabilitiesTags.DCPTYPE) == 0)
173
                    end = true;
174
                break;
175
            case KXmlParser.TEXT:                   
176
                break;
177
            }
178
            if (!end){
179
                currentTag = parser.next();
180
            }
181
        }     
182
    }
183

  
184
    /**
185
     * Parse an operation into a HTTP tag
186
     * @param parser
187
     * The KXMLParser
188
     * @param operation
189
     * The WFS operation to parse
190
     * @throws IOException 
191
     * @throws XmlPullParserException 
192
     */
193
    protected void parseHTTPTag(KXmlParser parser, String operation) throws XmlPullParserException, IOException {        
194
        int currentTag;
195
        boolean end = false;
196

  
197
        currentTag = parser.next();
198
        int protocol = -1;
199

  
200
        while (!end) 
201
        {
202
            switch(currentTag)
203
            {
204
            case KXmlParser.START_TAG:
205
                String value = null;
206
                if(parser.getName().compareTo(CapabilitiesTags.GET)==0){
207
                    protocol = OGCClientOperation.PROTOCOL_GET;
208
                    addOperationByAttribute(parser, operation, protocol);
209
                }else if(parser.getName().compareTo(CapabilitiesTags.POST)==0){
210
                    protocol = OGCClientOperation.PROTOCOL_POST;
211
                    addOperationByAttribute(parser, operation, protocol);
212
                }else if(parser.getName().compareTo(CapabilitiesTags.ONLINERESOURCE)==0){
213
                    addOperationByAttribute(parser, operation, protocol);
214
                }				
215
                break;
216
            case KXmlParser.END_TAG:
217
                if (parser.getName().compareTo(CapabilitiesTags.HTTP) == 0)
218
                    end = true;
219
                break;
220
            case KXmlParser.TEXT:                   
221
                break;
222
            }
223
            if (!end){
224
                currentTag = parser.next();
225
            }
226
        }     
227
    }
228

  
229
    /**
230
     * Add an operation and the online resource 
231
     * @param parser
232
     * The parser
233
     * @param operation
234
     * The operation to add
235
     * @param protocol
236
     * The parser to add
237
     */
238
    protected void addOperationByAttribute(KXmlParser parser, String operation, int protocol){
239
        String value = null;
240
        if (protocol > -1){
241
            for (int i=0 ; i<parser.getAttributeCount() ; i++){
242
                if ((parser.getAttributeName(i).toUpperCase().compareTo(CapabilitiesTags.ONLINERESOURCE.toUpperCase()) == 0) ||
243
                    (parser.getAttributeName(i).equals(CapabilitiesTags.XLINK_HREF))){					
244
                    value = parser.getAttributeValue(i);
245
                }
246
            }								
247
            if (value == null){
248
                getServiceInformation().addOperation(operation, protocol);
249
            }else{
250
                getServiceInformation().addOperation(operation, protocol, value);
251
            }
252
        }  
253
    }
254

  
255
    /**
256
     * Copy the file in a byte array
257
     * @param file
258
     * The file to copy
259
     * @return
260
     * An array of bytes
261
     * @throws IOException
262
     */
263
    protected byte[] fileToBytes(File file) throws IOException{
264
        FileInputStream fis = null;
265
        byte[] bytes = null;
266
        try{
267
            fis = new FileInputStream(file);
268

  
269
            long length = file.length(); 
270
            bytes = new byte[(int)file.length()];
271

  
272
            int offset = 0;
273
            int numRead = 0; 
274
            while (offset < bytes.length && (numRead=fis.read(bytes, offset, bytes.length-offset)) >= 0) 
275
            { 
276
                offset += numRead; 
277
            }
278
        }catch (IOException e) {
279
            throw e;
280
        }finally{
281
            if (fis != null){
282
                fis.close();
283
            }
284
        }         
285
        return bytes;
286
    }
287
}
0 288

  
tags/v2_0_0_Build_2058/libraries/libRemoteServices/src/org/gvsig/remoteclient/ogc/request/OGCRequest.java
1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
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 2
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
 */
22

  
23
/*
24
 * AUTHORS (In addition to CIT):
25
 * 2009 Iver T.I.  {{Task}}
26
 */
27

  
28
package org.gvsig.remoteclient.ogc.request;
29

  
30
import java.io.File;
31
import java.io.IOException;
32
import java.net.ConnectException;
33
import java.net.MalformedURLException;
34
import java.net.URL;
35
import java.net.UnknownHostException;
36

  
37
import org.gvsig.compat.CompatLocator;
38
import org.gvsig.compat.lang.StringUtils;
39
import org.gvsig.compat.net.ICancellable;
40
import org.gvsig.remoteclient.RemoteClientStatus;
41
import org.gvsig.remoteclient.ogc.OGCClientOperation;
42
import org.gvsig.remoteclient.ogc.OGCProtocolHandler;
43
import org.gvsig.remoteclient.utils.Utilities;
44
import org.gvsig.remoteclient.wfs.WFSOperation;
45

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

  
56
	public OGCRequest(RemoteClientStatus status, OGCProtocolHandler protocolHandler) {
57
		super();
58
		this.status = status;
59
		this.protocolHandler = protocolHandler;
60
	}	
61

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

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

  
105
	protected abstract String getHttpGetRequest(String onlineResource);
106

  
107
	protected abstract String getHttpPostRequest(String onlineResource);
108

  
109
	protected abstract String getTempFilePrefix();
110

  
111
	protected abstract String getOperationName();
112

  
113
	protected abstract String getSchemaLocation();	
114

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

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

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

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

  
184
	public boolean isDeleted() {
185
		return isDeleted;
186
	}
187

  
188
	public void setDeleted(boolean isDeleted) {
189
		this.isDeleted = isDeleted;
190
	}
191
	
192
	protected String createXMLStartTag(String tagName){
193
	    return XMLTAG_STARTCHARACTER + tagName + XMLTAG_ENDCHARACTER;
194
	}
195
	
196
   protected String createXMLEndtTag(String tagName){
197
        return XMLTAG_FINISHCHARACTER + tagName + XMLTAG_ENDCHARACTER;
198
    }
199

  
200
}
201

  
0 202

  
tags/v2_0_0_Build_2058/libraries/libRemoteServices/src/org/gvsig/remoteclient/taskplanning/FIFOTaskPlanner.java
1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2005 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41

  
42
/* CVS MESSAGES:
43
*
44
* $Id$
45
* $Log$
46
* Revision 1.1  2006-05-12 07:15:45  jaume
47
* *** empty log message ***
48
*
49
* Revision 1.1  2006/05/11 17:18:06  jaume
50
* Un planificador, monitorizador, de descargas que trabaja en segundo plano
51
*
52
*
53
*/
54
package org.gvsig.remoteclient.taskplanning;
55

  
56
/**
57
 * A simple FIFO task planner. The tasks returned by this planner are executed
58
 * enterely. It does not issue another task until the current is finished. 
59
 * @author jaume
60
 *
61
 */
62
public class FIFOTaskPlanner implements ITaskPlanner {
63
	IQueue queue;
64
	
65
	/**
66
	 * Creates a new instance of FIFOTaskPlanner that will work against the
67
	 * queue passed as paramenter 
68
	 * @param queue, the IQueue to be planned
69
	 */
70
	public FIFOTaskPlanner(IQueue queue) {
71
		this.queue = queue;
72
	}
73

  
74
	
75
	public IRunnableTask nextTask() {
76
		synchronized (this) {
77
			return (IRunnableTask) queue.getTasks().remove(0);
78
		}
79
	}
80
	/**
81
	 * FIFO plans have no previous tasks so, null is always returned.
82
	 */
83
	public IRunnableTask previousTask() {
84
		return null;
85
	}
86

  
87
}
0 88

  
tags/v2_0_0_Build_2058/libraries/libRemoteServices/src/org/gvsig/remoteclient/taskplanning/ITaskPlanner.java
1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2005 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41

  
42
/* CVS MESSAGES:
43
*
44
* $Id$
45
* $Log$
46
* Revision 1.2  2006-05-12 07:45:49  jaume
47
* some warnings removed
48
*
49
* Revision 1.1  2006/05/12 07:15:45  jaume
50
* *** empty log message ***
51
*
52
* Revision 1.1  2006/05/11 17:18:06  jaume
53
* Un planificador, monitorizador, de descargas que trabaja en segundo plano
54
*
55
*
56
*/
57
package org.gvsig.remoteclient.taskplanning;
58

  
59
/**
60
 * <p>
61
 * ITaskPlanner provides an interface to program your own task planning. It gives
62
 * you operations for pick a task from the queue according on the criteria that
63
 * you designed.<br>
64
 * </p>
65
 * <p>
66
 * The simplest implementation of ITaskPlanner would be a FIFO task planner (see
67
 * FIFOTaskPlanner.java) which takes jobs from a task queue in the same order
68
 * they were put. But any kind of planner is possible (SJF, LIFO, RoundRobin, etc.). 
69
 * </p>
70
 * @author jaume dominguez faus - jaume.dominguez@iver.es
71
 *
72
 */
73
public interface ITaskPlanner {
74
	/**
75
	 * Takes the next task to be executed.
76
	 * @return IRunnableTask representing the next task to be executed
77
	 */
78
	public IRunnableTask nextTask();
79
	
80
	/**
81
	 * Takes the previous executed task. Notice that it may or may not have
82
	 * sense for specific implementations. For example, in a FIFO-like planner,
83
	 * the task is taken from the queue, executed until it is finished and 
84
	 * removed from the queue. So, there is no previous task.
85
	 * 
86
	 * @return IRunnableTask representing the previous executed task, or null
87
	 * if none.
88
	 * @deprecated (probably this is unuseful and i'll remove it)
89
	 */
90
	public IRunnableTask previousTask();
91
}
0 92

  
tags/v2_0_0_Build_2058/libraries/libRemoteServices/src/org/gvsig/remoteclient/taskplanning/IQueue.java
1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2005 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41

  
42
/* CVS MESSAGES:
43
*
44
* $Id$
45
* $Log$
46
* Revision 1.2  2006-05-16 17:10:27  jaume
47
* *** empty log message ***
48
*
49
* Revision 1.1  2006/05/12 07:15:45  jaume
50
* *** empty log message ***
51
*
52
* Revision 1.1  2006/05/11 17:18:06  jaume
53
* Un planificador, monitorizador, de descargas que trabaja en segundo plano
54
*
55
*
56
*/
57
package org.gvsig.remoteclient.taskplanning;
58

  
59
import java.util.Vector;
60

  
61
/**
62
 *  <p>
63
 *  You should write your own concrete Queue implementation to hold the tasks in
64
 *  a specific kind of queue.<br>
65
 *  </p>
66
 *  <p>
67
 *  Following the contract, you should also write a task planner that does what 
68
 *  you desire. Task planners are a concrete class of the ITaskPlanner that would 
69
 *  plannify which is the next, the previous,.. task to be done. However, if
70
 *  you don't want to write anything special, just guess a simple FIFO queue<br>
71
 *  </p>
72
 *  
73
 *  @author jaume dominguez faus - jaume.dominguez@iver.es
74
 *  @see ITaskPlanner
75
 */
76
public interface IQueue {
77
	
78
	/**
79
	 * Adds a new task to the queue. The place where the new task will be put its
80
	 * left to the concrete implementation of this interface.
81
	 * @param IRunnableTask task
82
	 */
83
	IRunnableTask put(IRunnableTask task);
84
	
85
	/**
86
	 * Returns the next task by calling the task planner's nextTask() method.
87
	 * @return IRunnableTask with the next task to be executed.
88
	 */
89
	IRunnableTask take();
90
	
91
	/**
92
	 * Returns true if the Queue has no (more) jobs to do.
93
	 * @return
94
	 */
95
	boolean isEmpty();
96
	
97
	/**
98
	 * Returns the task planner currently defined by this queue.
99
	 * @return ITaskPlanner
100
	 */
101
	ITaskPlanner getTaskPlanner();
102
	
103
	/**
104
	 * Sets the TaskPlanner that will decide which of the tasks in the queue will
105
	 * be executed next. A null value should represent a FIFO planner. 
106
	 * @param planner
107
	 */
108
	void setTaskPlanner(ITaskPlanner planner);
109
	
110
	/**
111
	 * Causes the execution of this queue to be paused. The task currently in execution
112
	 * finishes and after it the planner will not issue more tasks until resume() is
113
	 * invoked.
114
	 */
115
	void pause();
116
	
117
	/**
118
	 * Causes the execution of this queue to be resumed. The execution will continue
119
	 * with the next task issued by the planner. It has no effect if the queue was not
120
	 * paused yet. 
121
	 */
122
	void resume();
123

  
124
	/**
125
	 * Returns the set of tasks in a Vector (thread-safe).
126
	 * @return Vector containing the tasks in this queue.
127
	 */
128
	Vector getTasks();
129
}
0 130

  
tags/v2_0_0_Build_2058/libraries/libRemoteServices/src/org/gvsig/remoteclient/taskplanning/retrieving/RequestManager.java
1
/*
2
 * Created on 01-oct-2005
3
 */
4
package org.gvsig.remoteclient.taskplanning.retrieving;
5

  
6
import java.io.File;
7
import java.net.MalformedURLException;
8
import java.util.Hashtable;
9
import java.util.TreeMap;
10

  
11
import org.gvsig.remoteclient.taskplanning.IQueue;
12

  
13
/**
14
 * pa administrar les tasques (la hist?ria aquella
15
 *  de que hi haja una cola per a cada servidor)
16
 * @author Luis W. Sevilla (sevilla_lui@gva.es)
17
 */
18
public class RequestManager {
19
	private boolean debug = true;
20
    
21
	private static RequestManager instance;
22
	private TreeMap serversTable = new TreeMap();
23
	private RequestManager() {} // Avoid public instantiation
24
	 
25
	public static RequestManager getInstance() {
26
		if (instance == null)
27
			instance = new RequestManager();
28
		return instance;
29
	}
30
	
31
	
32
	public URLRetrieveTask addURLRequest(URLRequest request, RetrieveListener listener) {
33
		try {
34
			
35
			// TODO canviar per a quetorne el Request antic, que la request guarde el
36
			// seu estat aix? com la llista de listeners
37
			File f = getPreviousDownloadedURLRequest(request);
38
			
39
			if (f!=null) {
40
				// The file was already requested and it is in the cache or
41
				// the download is in process
42
				
43
				// Overwrite the file name with the file in the cache's one.
44
				request.setFileName(f.getAbsolutePath());
45
				System.out.println(request.getUrl()+" is cached at '"+f.getAbsolutePath()+"'");
46
				
47

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

  
135

  
136
	private Hashtable downloadedFiles;
137
	private final String tempDirectoryPath = System.getProperty("java.io.tmpdir")+"tmp-andami";
138
	/**
139
     * Remove an URL from the system cache. The file will remain in the file
140
     * system for further eventual uses.
141
     * @param request
142
     */
143
	public void removeURLRequest(URLRequest request) {
144
		if (downloadedFiles != null && downloadedFiles.containsKey(request))
145
			downloadedFiles.remove(request);
146
	}
147
	/**
148
     * Adds an URL to the table of downloaded files for further uses. If the URL
149
     * already exists in the table its filePath value is updated to the new one and
150
     * the old file itself is removed from the file system.
151
     * 
152
     * @param url
153
     * @param filePath
154
     */
155
    protected void addDownloadedURLRequest(URLRequest request, String filePath){
156
        if (downloadedFiles==null)
157
            downloadedFiles = new Hashtable();
158
        String fileName = (String) downloadedFiles.put(request, filePath);
159
        if (fileName!=null){
160
            File f = new File(fileName);
161
            if (f.exists())
162
                f.delete();
163
        }
164
    }
165
    /**
166
     * Returns the content of this URL as a file from the file system.<br>
167
     * <p>
168
     * If the URL has been already downloaded in this session and notified 
169
     * to the system using the static <b>Utilities.addDownloadedURL(URL)</b>
170
     * method, it can be restored faster from the file system avoiding to
171
     * download it again.
172
     * </p>
173
     * @param url
174
     * @return File containing this URL's content or null if no file was found.
175
     */
176
    private File getPreviousDownloadedURLRequest(URLRequest request){
177
        File f = null;
178
        if (downloadedFiles!=null && downloadedFiles.containsKey(request)){
179
            String filePath = (String) downloadedFiles.get(request);
180
            f = new File(filePath);
181
        }
182
        return f;
183
    }
184
    
185
}
0 186

  
tags/v2_0_0_Build_2058/libraries/libRemoteServices/src/org/gvsig/remoteclient/taskplanning/retrieving/RetrieveEvent.java
1
/*
2
 * Created on 01-oct-2005
3
 */
4
package org.gvsig.remoteclient.taskplanning.retrieving;
5

  
6
/**
7
 * @author jaume dominguez faus - jaume.dominguez@iver.es
8
 * Luis W. Sevilla (sevilla_lui@gva.es)
9
 */
10
public class RetrieveEvent {
11
	public static final int NOT_STARTED = 0;
12
	public static final int CONNECTING = 1;
13
	public static final int TRANSFERRING = 2;
14
	public static final int REQUEST_FINISHED = 3;
15
	public static final int REQUEST_FAILED = 4;
16
	public static final int REQUEST_CANCELLED = 5;
17
	public static final int POSTPROCESSING = 6;
18
	
19
	/**
20
	 * redundant; use REQUEST_FAILED
21
	 * @deprecated ?
22
	 */
23
	public static final int ERROR = 11;
24
	
25
	private int eventType;
26
	
27
	public void setType(int type) {
28
		eventType = type;
29
	}
30

  
31
	public int getType() {
32
		return eventType;
33
	}
34
}
0 35

  
tags/v2_0_0_Build_2058/libraries/libRemoteServices/src/org/gvsig/remoteclient/taskplanning/retrieving/RetrieveQueue.java
1
/*
2
 * Created on 01-oct-2005
3
 */
4
package org.gvsig.remoteclient.taskplanning.retrieving;
5

  
6
import java.util.Date;
7
import java.util.Vector;
8

  
9
import org.gvsig.remoteclient.taskplanning.FIFOTaskPlanner;
10
import org.gvsig.remoteclient.taskplanning.IQueue;
11
import org.gvsig.remoteclient.taskplanning.IRunnableTask;
12
import org.gvsig.remoteclient.taskplanning.ITaskPlanner;
13

  
14
/**
15
 * @author jaume dominguez faus - jaume.dominguez@iver.es
16
 * 		   Luis W. Sevilla (sevilla_lui@gva.es)
17
 */
18
public class RetrieveQueue implements IQueue {
19
	private String hostName;
20
	private Date startTime;
21
	private Vector tasks = new Vector();
22
	private boolean waiting;
23
	private ITaskPlanner taskPlanner;
24
	private Worker worker;
25
	
26
	/**
27
	 * 
28
	 */
29
	public RetrieveQueue(String hName) {
30
		hostName = hName;
31
		startTime = new Date();
32
		worker = new Worker();
33
		new Thread(worker).start();
34
	}
35
	
36
	
37
	public IRunnableTask put(IRunnableTask task) {
38
		tasks.add(task);
39
		if (waiting) {
40
			synchronized (this) {
41
				notifyAll();
42
			}
43
		}
44
		return task;
45
	}
46
	
47
	public IRunnableTask take() {
48
		if (tasks.isEmpty()) {
49
			synchronized (this) {
50
				waiting = true;
51
				try {
52
					wait();
53
				} catch (InterruptedException ie) {
54
					waiting = false;
55
				}
56
			}
57
		}
58
		return getTaskPlanner().nextTask() ;
59
	}
60
	
61
	
62
	
63
	public boolean isEmpty() {
64
		synchronized (this) {
65
			return tasks.isEmpty() && !worker.r.isRunning();
66
		}
67
	}
68
	
69
	
70

  
71
	public ITaskPlanner getTaskPlanner() {
72
		if (taskPlanner == null) {
73
			taskPlanner = new FIFOTaskPlanner(this);
74
		}
75
		return taskPlanner;
76
	}
77

  
78

  
79
	public void setTaskPlanner(ITaskPlanner planner) {
80
		taskPlanner = planner;
81
	}
82

  
83

  
84
	public void pause() {
85
		waiting = true;
86
	}
87

  
88

  
89
	public void resume() {
90
		waiting = false;
91
	}
92

  
93

  
94
	public Vector getTasks() {
95
		return tasks;
96
	}
97

  
98
	private class Worker implements Runnable {
99
		URLRetrieveTask r;
100
		int i = 0; 
101
		public void run() {
102
			while (true) {
103
				r = (URLRetrieveTask) take();
104
				r.execute();
105
			}
106
		}
107
	}
108

  
109
	protected URLRetrieveTask getURLPreviousRequest(URLRequest request) {
110
		// Is the one currently running?
111
		/*URLRetrieveTask aux = (URLRetrieveTask) worker.r;
112
		if (request.equals(aux.getRequest())) {
113
				return aux;
114
		}*/	
115
		// Is one of those in the queue?
116
		for (int i = 0; i < tasks.size(); i++) {
117
			URLRetrieveTask task = (URLRetrieveTask) tasks.get(i);
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff