Revision 3377 trunk/libraries/libRemoteServices/src/org/gvsig/remoteClient/wms/WMSProtocolHandler.java

View differences:

WMSProtocolHandler.java
5 5
import java.io.DataOutputStream;
6 6
import java.io.File;
7 7
import java.io.FileOutputStream;
8
import java.io.IOException;
8 9
import java.io.InputStream;
9 10
import java.io.OutputStream;
11
import java.net.ConnectException;
10 12
import java.net.URL;
13
import java.net.UnknownHostException;
11 14
import java.util.TreeMap;
12 15
import java.util.Vector;
13 16
import org.gvsig.remoteClient.utils.Utilities;
14 17

  
18
import com.devx.io.TempFileManager;
19

  
20

  
15 21
/**
16 22
 * <p> Abstract class that represents handlers to comunicate via WMS protocol.
17 23
 * </p>
......
88 94
			//TODO:
89 95
			//pass this buildXXXRequest to the WMSProtocolHandlerXXX: The request can depend on the WMS version.
90 96
			request = new URL(buildMapRequest(status));
97
	    	byte[] buffer = new byte[1024*256];
98
	    	byte[] imageBytes = null;
99
	    	InputStream is = request.openStream();
100
	    	int readed = 0;
101
	    	
102
	    	for (int i = is.read(buffer); i>0; i = is.read(buffer)){
103
	    		byte[] buffered = new byte[readed+i];
104
	    		for (int j = 0; j < buffered.length; j++) {
105
	    			if (j<readed){
106
	    				buffered[j] = imageBytes[j];
107
	    			}
108
	    			else {
109
	    				buffered[j] = buffer[j-readed];
110
	    			}
111
				}
112
	    		imageBytes = (byte[]) buffered.clone();
113
	    		readed += i;
114
	    		
115
	    	}
116
			
117
			return imageBytes;
118
	    	
91 119
		}
92 120
		catch(Exception e)
93 121
		{
94 122
			// TODO something about the exceptionsssss....!!!
95 123
		}
96 124
		
97
    	try
98
    	{
99
	    
100
    		System.out.println(request.toString());
101
    		File f = new File("map.jpeg");
102
    	  DataOutputStream dos = new DataOutputStream(new FileOutputStream(f));
103
	  	  
104
		  OutputStream bos = null;
105

  
106
	    	byte[] buffer = new byte[1024*256];
107
	    	InputStream is = request.openStream();
108
	    	//long readed = 0;
109
	    	for (int i = is.read(buffer); i>0; i = is.read(buffer)){
110
	    		dos.write(buffer, 0, i);
111
	    		//readed += i;
112
	    	}
113
	    	dos.close();
114
	    	
115
	    return null;
116
    	}
117
    	catch(Exception e)
118
    	{
119
    		//TODO
120
    		//Implement the correct logic to handle all the exceptions.
121
    		return null;
122
    	}
125
		return null;
123 126
    } 
124 127

  
125 128
	/**
......
136 139
		}
137 140
		catch(Exception e)
138 141
		{
139
			// TODO something about the exceptionsssss....!!!
142
			e.printStackTrace();
140 143
		}
141 144
		
142
	   	File f = null;
143
	   	f = new File("tochangethis.txt");
144
	    
145
	    if (!f.exists())
146
	    {
147
	    	try
148
	    	{
149
		    	DataOutputStream dos = new DataOutputStream( new BufferedOutputStream(new FileOutputStream(f)));
150
		    	byte[] buffer = new byte[1024*256];
151
		    	InputStream is = request.openStream();
152
		    	long readed = 0;
153
		    	for (int i = is.read(buffer); i>0; i = is.read(buffer)){
154
		    		dos.write(buffer, 0, i);
155
		    		readed += i;
156
		    	}
157
		    	dos.close();
158
	    	}
159
	    	catch(Exception e)
160
	    	{}
161
	    }
145
//	   	File f = null;
146
//	   	f = new File("tochangethis.txt");
147
//	    
148
//	    if (!f.exists())
149
//	    {
150
//	    	try
151
//	    	{
152
//		    	DataOutputStream dos = new DataOutputStream( new BufferedOutputStream(new FileOutputStream(f)));
153
//		    	byte[] buffer = new byte[1024*256];
154
//		    	InputStream is = request.openStream();
155
//		    	long readed = 0;
156
//		    	for (int i = is.read(buffer); i>0; i = is.read(buffer)){
157
//		    		dos.write(buffer, 0, i);
158
//		    		readed += i;
159
//		    	}
160
//		    	dos.close();
161
//	    	}
162
//	    	catch(Exception e)
163
//	    	{}
164
//	    }
165
		try
166
		{
167
		File f = downloadFile(request,"wms_capabilities.xml");
162 168
	    parse(f);
163
	    //return parse(f);
169
		}
170
		catch(Exception e)
171
		{
172
			//TODO
173
			e.printStackTrace();
174
		}
164 175
    }
165 176

  
166 177
/**
167 178
 * <p>It will send a GetFeatureInfo request to the WMS
168 179
 * Parsing the response and redirecting the info to the WMS client</p>
169 180
 */
170
    public String getFeatureInfo(WMSStatus status, int x, int y)
181
    public String getFeatureInfo(WMSStatus status, int x, int y, int featureCount)
171 182
    {
172 183
    	URL request = null;
173 184
		try
......
214 225
    private String buildCapabilitiesRequest()
215 226
    {
216 227
		String req = new String();
217
		
218
		req = req + getHost() + "REQUEST=GetCapabilities&SERVICE=WMS&";
228
		String host = getHost();
229
		if (!host.endsWith("?"))
230
			req = req + getHost() + "?REQUEST=GetCapabilities&SERVICE=WMS&";
231
		else
232
			req = req + getHost() + "REQUEST=GetCapabilities&SERVICE=WMS&";
219 233
		req = req + "VERSION=" + getVersion();
220 234
		req += ("&EXCEPTIONS=XML");
221 235
		return req;
......
227 241
    private String buildGetFeatureInfoRequest(WMSStatus status, int x, int y)
228 242
    {
229 243
		String req = new String();
230
		
231
		req = req + getHost() + "REQUEST=GetFeatureInfo&SERVICE=WMS&";
244
		if (!host.endsWith("?"))
245
			req = req + getHost() + "?REQUEST=GetFeatureInfo&SERVICE=WMS&";
246
		else
247
			req = req + getHost() + "REQUEST=GetFeatureInfo&SERVICE=WMS&";
232 248
		req = req + "QUERY_LAYERS="+Utilities.Vector2CS(status.getLayerNames()); 
233 249
		req = req + "&VERSION=" + getVersion() + "&";
234 250
		req = req + getPartialQuery(status);
......
247 263
    private String buildMapRequest(WMSStatus status)
248 264
    {
249 265
		String req = new String();
250
		
251
		req = req + getHost() + "REQUEST=GetMap&SERVICE=WMS&";
266
		if (!host.endsWith("?"))
267
			req = req + getHost() + "?REQUEST=GetMap&SERVICE=WMS&";
268
		else
269
			req = req + getHost() + "REQUEST=GetMap&SERVICE=WMS&";
252 270
		req = req + "VERSION=" + getVersion() + "&";
253 271
		req = req + getPartialQuery(status);
254 272
       if (status.getExceptionFormat() != null) {
......
355 373
            email = new String();
356 374
            formats = new Vector();       	
357 375
        }
358
     }    
376
     }   
377
    
378
    /**
379
     * Downloads an URL into a temporary file that is removed the next time the 
380
     * tempFileManager class is called, which means the next time gvSIG is launched.
381
     * 
382
     * @param url
383
     * @param name
384
     * @return
385
     * @throws IOException
386
     * @throws ServerErrorResponseException
387
     * @throws ConnectException
388
     * @throws UnknownHostException
389
     */
390
    private File downloadFile(URL url, String name) throws IOException,ConnectException, UnknownHostException{
391
    	File f = null;
392

  
393
	    try{
394
	    	f = TempFileManager.createTempFile(name, "tmp");
395
	    	System.out.println("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\ndownloading '"+url.toString()+"' to: "+f.getAbsolutePath());
396
			 
397
	    	f.deleteOnExit();
398
		    
399
	    } catch (IOException io) {
400
	    	io.printStackTrace();
401
	    }
402
	    DataOutputStream dos = new DataOutputStream( new BufferedOutputStream(new FileOutputStream(f)));
403
	    byte[] buffer = new byte[1024*256];
404
	    InputStream is = url.openStream();
405
	    long readed = 0;
406
	    for (int i = is.read(buffer); i>0; i = is.read(buffer)){
407
	    	dos.write(buffer, 0, i);
408
	    	readed += i;
409
	    }
410
	    dos.close();
411
	    /*if (!isNotAnException(f))
412
	    	// SI que es una excepci?n
413
	    	throw new ServerErrorResponseException();*/
414
	    return f;
415
	}
416

  
359 417
 }

Also available in: Unified diff