Revision 39595

View differences:

branches/v2_0_0_prep/libraries/libRemoteServices/src/org/gvsig/remoteclient/wms/WMSProtocolHandler.java
2 2

  
3 3
import java.io.ByteArrayInputStream;
4 4
import java.io.File;
5
import java.io.FileInputStream;
5 6
import java.io.FileReader;
6 7
import java.io.IOException;
7 8
import java.io.InputStream;
8 9
import java.net.URL;
9 10
import java.net.URLConnection;
11
import java.nio.ByteBuffer;
12
import java.nio.channels.FileChannel;
10 13
import java.util.ArrayList;
14
import java.util.Iterator;
11 15
import java.util.StringTokenizer;
12 16
import java.util.TreeMap;
13 17

  
......
247 251
		}
248 252
    }
249 253

  
250
    public File getLegendGraphic(WMSStatus status, String layerName, ICancellable cancel) throws ServerErrorException, WMSException
254
    public File getLegendGraphic_old(WMSStatus status, String layerName, ICancellable cancel) throws ServerErrorException, WMSException
251 255
    {
252 256
    	try
253 257
		{
......
289 293
		}
290 294
    }
291 295
    
296
    public File getLegendGraphic(WMSStatus status, String layerName, ICancellable cancel) 
297
    	throws ServerErrorException, WMSException {
298
    	URL request = null;
299
		try {
300
			request = new URL(buildGetLegendGraphicRequest(status, layerName));
301
			System.out.println(request);
302
            File f = Utilities.downloadFile(request, "wmsGetLegendGraphic", cancel);
303
	    	if (f== null)
304
	    		return null;
305
            if (Utilities.isTextFile(f)) {
306
	    		FileInputStream fis = new FileInputStream(f);
307
	    		FileChannel fc = fis.getChannel();
308
	    		byte[] data = new byte[(int)fc.size()];
309
	    		ByteBuffer bb = ByteBuffer.wrap(data);
310
	    		fc.read(bb);
311

  
312
	    		WMSException wmsEx = null;
313

  
314
            	String exceptionMessage = parseException(data);
315
                if (exceptionMessage == null) {
316
                 	String error = new String(data);
317
                	int pos = error.indexOf("<?xml");
318
                	if (pos!= -1) {
319
                		String xml = error.substring(pos,error.length());
320
                		exceptionMessage = parseException(xml.getBytes());
321
                	}
322
                    if (exceptionMessage == null)
323
                    	exceptionMessage = new String(data);
324

  
325
                }
326
             	wmsEx = new WMSException(exceptionMessage);
327
            	wmsEx.setWMSMessage(new String(data));
328
            	Utilities.removeURL(request);
329
                throw wmsEx;
330
            }
331
			return f;
332
		} catch(IOException e) {
333
			e.printStackTrace();
334
            throw new ServerErrorException();
335
		}
336
    }
337
    
338
    /**
339
     * @return string that represents the url for getting the wms legend
340
     * If the layer has the object layer-->style-->legendurl that url will be returned 
341
     * otherwise builds the getLegendGraphic according to the OGC WMS Specifications
342
     * 
343
     */
344
    private String buildGetLegendGraphicRequest(WMSStatus status, String layerName) {
345
    	//TODO: deal with more than one layer    	
346
    	WMSLayer lyr = (WMSLayer) this.layers.get(layerName);
347
    	WMSStyle sty =null;
348
    	if (lyr != null){
349
    		Iterator it = lyr.getStyles().iterator();
350
    		while (it.hasNext()){
351
    			sty = (WMSStyle) it.next();
352
    			if (sty.getName().equals(status.getStyles().get(0).toString())){    				
353
    				return sty.getLegendURLOnlineResourceHRef();
354
    			}
355
    		}
356
    	}
357
    	//TODO: pass by parameter the legend output format?
358
		StringBuffer req = new StringBuffer();
359
		String symbol = null;
360
		String onlineResource = null;
361

  
362
		if (status.getOnlineResource() == null)
363
			onlineResource = getHost();
364
		else
365
			onlineResource = status.getOnlineResource();
366
		symbol = getSymbol(onlineResource);
367

  
368
		req.append(onlineResource + symbol + "REQUEST=GetLegendGraphic&SERVICE=WMS&VERSION=").append(getVersion());
369
        req.append("&LAYER=" + layerName).append("&TRANSPARENT=TRUE").append("&FORMAT=image/png");
370
        String aux = req.toString().replaceAll(" ", "%20");
371
        System.out.println("GetLegendGraphic url:" + aux);
372
		return aux;
373
    }
374
    
292 375
    public URL getMapURL(WMSStatus status, ICancellable cancel) throws ServerErrorException, WMSException {
293 376
    	try {
294 377
			WMSGetMapRequest request = createGetMapRequest(status);

Also available in: Unified diff