Revision 5273 branches/MULTITHREADING_DEVELOPMENT/libraries/libRemoteServices/src/org/gvsig/remoteClient/wms/WMSProtocolHandler.java

View differences:

WMSProtocolHandler.java
4 4
import java.io.DataInputStream;
5 5
import java.io.File;
6 6
import java.io.FileInputStream;
7
import java.io.FileNotFoundException;
7 8
import java.io.IOException;
8 9
import java.io.InputStream;
9 10
import java.io.StringReader;
......
101 102
		public void transferEventReceived(RetrieveEvent event) {
102 103
			int type = event.getType();
103 104
			
105
			if (type == RetrieveEvent.REQUEST_FINISHED) {
106
				
107
				// TODO revisar no estic segur de que a?? haja d'estar ac?
108
				
109
				File f = new File(event.getFileName());
110
				if (Utilities.isTextFile(f)) {
111
		    		FileInputStream fis;
112
					try {
113
						fis = new FileInputStream(f);
114
					FileChannel fc = fis.getChannel();
115
		    		byte[] data = new byte[(int)fc.size()];   // fc.size returns the size of the file which backs the channel
116
		    		ByteBuffer bb = ByteBuffer.wrap(data);
117
		    		fc.read(bb);
118
		    			    		
119
		    		WMSException wmsEx = null;
120
	               	
121
	            	String exceptionMessage = parseException(data);
122
	                if (exceptionMessage==null)
123
	                {
124
	                 	String error = new String(data);
125
	                	int pos = error.indexOf("<?xml");
126
	                	if (pos!= -1)
127
	                	{
128
	                		String xml = error.substring(pos,error.length());
129
	                		exceptionMessage = parseException(xml.getBytes());
130
	                	}               
131
	                    if (exceptionMessage == null)
132
	                    	exceptionMessage = new String(data);
133
	                	
134
	                }
135
	             	wmsEx = new WMSException(exceptionMessage);
136
	            	wmsEx.setWMSMessage(new String(data));
137
	            	
138
	            	// Since it is an error file, It must be deleted from the cache
139
	            	RequestManager.getInstance().removeURLRequest(mapRequest);
140
	                } catch (FileNotFoundException e) {
141
						// TODO Auto-generated catch block
142
						e.printStackTrace();
143
					} catch (IOException e) {
144
						// TODO Auto-generated catch block
145
						e.printStackTrace();
146
					}
147
		    		
148
	            }	
149
			}
104 150
		}
105 151
    	
106 152
    } ,
......
164 210
				RequestManager.getInstance().removeURLRequest(capabilitiesRequest);
165 211
			capabilitiesRequest = RequestManager.getInstance().addURLRequest(capabilitiesRequest, getCapabilitiesListener);
166 212
			
167
	    } catch(Exception e)
168
		{
169
			//TODO
213
	    } catch(MalformedURLException e) {
170 214
			e.printStackTrace();
171 215
		}
172 216
    }
......
263 307
    	}
264 308
    }
265 309
    
266
    public File getMap(WMSStatus status) throws ServerErrorException, WMSException
267
    {        
268
    	URL request = null;
310
    public void getMap(WMSStatus status) throws ServerErrorException, WMSException
311
    {      
269 312
		try
270 313
		{
271
			//TODO:
272
			//pass this buildXXXRequest to the WMSProtocolHandlerXXX: The request can depend on the WMS version.
273
			request = new URL(buildMapRequest(status));
274
            
275
            File f = com.iver.andami.Utilities.downloadFile(request, "wmsGetMap");            		    	
276
	    	
277
            if (Utilities.isTextFile(f)) {
278
	    		FileInputStream fis = new FileInputStream(f);
279
	    		FileChannel fc = fis.getChannel();
280
	    		byte[] data = new byte[(int)fc.size()];   // fc.size returns the size of the file which backs the channel
281
	    		ByteBuffer bb = ByteBuffer.wrap(data);
282
	    		fc.read(bb);
283
	    			    		
284
	    		WMSException wmsEx = null;
285
               	
286
            	String exceptionMessage = parseException(data);
287
                if (exceptionMessage==null)
288
                {
289
                 	String error = new String(data);
290
                	int pos = error.indexOf("<?xml");
291
                	if (pos!= -1)
292
                	{
293
                		String xml = error.substring(pos,error.length());
294
                		exceptionMessage = parseException(xml.getBytes());
295
                	}               
296
                    if (exceptionMessage == null)
297
                    	exceptionMessage = new String(data);
298
                	
299
                }
300
             	wmsEx = new WMSException(exceptionMessage);
301
            	wmsEx.setWMSMessage(new String(data));
302
            	
303
            	// Since it is an error file, It must be deleted from the cache
304
            	com.iver.andami.Utilities.removeURL(request);
305
                throw wmsEx;
306
            }
307
			return f;	    	
308
		}
309
		catch(IOException e)
310
		{
314
			mapRequest = buildMapRequest(status);
315
			mapRequest.setFileName("wmsGetMap");
316
			mapRequest = RequestManager.getInstance().addURLRequest(mapRequest, getMapListener);
317
			
318
	    } catch(MalformedURLException e) {
311 319
			e.printStackTrace();
312
            throw new ServerErrorException();
313 320
		}
314 321
    } 
315 322
    
......
387 394
    private URLRequest buildCapabilitiesRequest(WMSStatus status) throws MalformedURLException
388 395
    {
389 396
		URLRequest req = new URLRequest();
390
		String symbol = null;
391 397
		StringBuffer u = new StringBuffer();
392 398
		String onlineResource;
393 399
		if (status == null || status.getOnlineResource() == null)
......
439 445

  
440 446
    /**
441 447
     * Builds the GetMapRequest according to the OGC WMS Specifications
448
     * @throws MalformedURLException 
442 449
     */
443
    private String buildMapRequest(WMSStatus status)
450
    private URLRequest buildMapRequest(WMSStatus status) throws MalformedURLException
444 451
    { 
445
		StringBuffer req = new StringBuffer();
446
		String symbol = null;
452
		URLRequest req = new URLRequest();
453
		StringBuffer u = new StringBuffer();
447 454
		String onlineResource = null;
448 455
		
449 456
		if (status.getOnlineResource() == null)
450 457
			onlineResource = getHost();
451 458
		else 
452 459
			onlineResource = status.getOnlineResource();
453
		symbol = getSymbol(onlineResource);
454
        
455
		req.append(onlineResource + symbol + "REQUEST=GetMap&SERVICE=WMS&VERSION=").append(getVersion()).append("&");
456
		req.append(getPartialQuery(status));
460
		u.append(onlineResource +  getSymbol(onlineResource) + "REQUEST=GetMap&SERVICE=WMS&VERSION=").append(getVersion()).append("&");
461
		u.append(getPartialQuery(status));
457 462
        if (status.getExceptionFormat() != null) {
458
            req.append("&EXCEPTIONS=" + status.getExceptionFormat());
463
            u.append("&EXCEPTIONS=" + status.getExceptionFormat());
459 464
        } else {
460
            req.append("&EXCEPTIONS=XML");
465
            u.append("&EXCEPTIONS=XML");
461 466
        }
462
		return req.toString().replaceAll(" ", "%20");
467
        URL url = new URL(u.toString());
468
		
469
        req.setHost(url.getHost());
470
		req.setRequestType(URLRequest.GET);
471
		req.setProtocol(URLRequest.HTTP);
472
		req.setFile(url.getFile());
473
		int myPort = url.getPort();
474
		if (myPort != -1 && myPort != 80 )
475
			req.setPort(myPort);
476
		
477
		return req;
463 478
    }
464 479
    
465 480
    /**

Also available in: Unified diff