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

View differences:

WMSProtocolHandler.java
16 16

  
17 17
import org.gvsig.remoteClient.exceptions.ServerErrorException;
18 18
import org.gvsig.remoteClient.exceptions.WMSException;
19
import org.gvsig.remoteClient.exceptions.WMSWrongSizeException;
19 20
import org.gvsig.remoteClient.utils.CapabilitiesTags;
20 21
import org.gvsig.remoteClient.utils.ExceptionTags;
21 22
import org.gvsig.remoteClient.utils.Utilities;
......
61 62
    public WMSLayer rootLayer;
62 63
    public Vector srs;
63 64
    
64

  
65
    // abstract methods to implement by the handlers that implement
66
    // the connection to a WMS with certain version.
67
    //    public abstract String getName();   
68
    //    public abstract String getVersion();
69
    
70 65
    /**
71 66
     * parses the data retrieved by the WMS in XML format. 
72 67
     * It will be mostly the WMS Capabilities, but the implementation
......
103 98
    	port = _port;
104 99
    }
105 100

  
101

  
106 102
    /**
103
	 * <p>Builds a GetCapabilities request that is sent to the WMS
104
	 * the response will be parse to extract the data needed by the
105
	 * WMS client</p>
106
	 */
107
    public void getCapabilities()
108
    {		
109
    	URL request = null;
110
		try
111
		{
112
			request = new URL(buildCapabilitiesRequest());
113
		}
114
		catch(Exception e)
115
		{
116
			e.printStackTrace();
117
		}
118
		try
119
		{
120
		File f = com.iver.andami.Utilities.downloadFile(request,"wms_capabilities.xml");
121
	    parse(f);
122
	    } catch(Exception e)
123
		{
124
			//TODO
125
			e.printStackTrace();
126
		}
127
    }
128

  
129
    /**
130
     * <p>It will send a GetFeatureInfo request to the WMS
131
     * Parsing the response and redirecting the info to the WMS client</p>
132
     */
133
    public String getFeatureInfo(WMSStatus status, int x, int y, int featureCount)
134
    {
135
    	URL request = null;
136
    	StringBuffer output = new StringBuffer();
137
    	String outputFormat = new String();
138
    	String ServiceException = "ServiceExceptionReport";    	    	
139
    	StringBuffer sb = new StringBuffer();
140
    	sb.append("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>");
141
    	
142
		try
143
		{
144
			//TODO:
145
			//pass this buildXXXRequest to the WMSProtocolHandlerXXX: The request can depend on the WMS version.
146
			request = new URL(buildGetFeatureInfoRequest(status, x, y));
147
			
148
			//TODO:
149
			//see which output format is being requested. 					
150
		}
151
		catch(Exception e)
152
		{
153
			e.printStackTrace();
154
			return "";
155
		}
156
		
157
    	try
158
    	{	    	    	
159
    		System.out.println(request.toString());	  
160
	    	byte[] buffer = new byte[1024*256];	    	
161
	    	DataInputStream is = new DataInputStream(request.openStream());	    	
162
	    	outputFormat = request.openConnection().getContentType();
163

  
164
	    	for (int i = is.read(buffer); i>0; i = is.read(buffer))
165
	    	{
166
	    		String str = new String(buffer,0,i);
167
	    		output.append(str);	    	
168
	    	}
169
	    		    
170
	    	is.close();
171
		    if ( (outputFormat == null) || (outputFormat.indexOf("xml") != -1)
172
		    		||output.toString().toLowerCase().startsWith("<?xml")
173
		    		||(outputFormat.indexOf("gml") != -1))
174
		    {
175
		    	int tag;
176
		    	KXmlParser kxmlParser = null;
177
		    	kxmlParser = new KXmlParser();	    
178
		    	kxmlParser.setInput(new StringReader(output.toString()));
179
		    	
180
		    	tag = kxmlParser.nextTag();   		
181
 				 while(tag != KXmlParser.END_DOCUMENT)
182
				 {
183
					 switch(tag)
184
					 {
185
						case KXmlParser.START_TAG:		
186
							if (kxmlParser.getName().compareTo(ServiceException)==0)
187
							{
188
					    		sb.append("<INFO>").append(parseException( output.toString().getBytes())).append("</INFO>");
189
					    		return sb.toString();									
190
							}
191
							else if (kxmlParser.getName().compareToIgnoreCase("ERROR")==0)
192
								return output.toString();
193
							else								
194
								sb.append("<" + kxmlParser.getName() + ">\n");
195
							break;
196
						case KXmlParser.END_TAG:	
197
							sb.append("</" + kxmlParser.getName() + ">\n");
198
							break;
199
						case KXmlParser.TEXT:
200
							sb.append(kxmlParser.getText());						
201
						break;
202
					 }
203
    				 tag = kxmlParser.next();
204
    			 }		    		
205
	    		return sb.toString();
206
	    	}
207
	    	else
208
	    	{
209
	    		sb.append("<INFO>").append(PluginServices.getText(this, "info_format_not_supported")).append("</INFO>");
210
	    		return sb.toString();
211
	    	}
212
		}
213
    	catch(XmlPullParserException parserEx)
214
    	{
215
    		if (output.toString().toLowerCase().indexOf("xml") != -1)
216
    		{
217
    			return output.toString().trim();
218
    		}
219
    		else
220
    		{
221
    	   		sb.append("<INFO>").append(PluginServices.getText(this, "info_format_not_supported")).append("</INFO>");
222
        		return sb.toString();
223
    		}
224
    	}
225
    	catch(Exception e)
226
    	{
227
    		e.printStackTrace();
228
    		sb.append("<INFO>").append(PluginServices.getText(this, "info_format_not_supported")).append("</INFO>");
229
    		return sb.toString();
230

  
231
    	}
232
    }
233
    /**
107 234
     * <p>Builds a GetMap request that is sent to the WMS
108 235
     * the response (image) will be redirect to the
109 236
     * WMS client</p>
110
     */
111
    
237
     */   
112 238
    public byte[] getMap(WMSStatus status) throws ServerErrorException, WMSException
113 239
    {        
114 240
    	URL request = null;
......
120 246
			URLConnection conn = request.openConnection();
121 247
			System.out.println(request.toString());
122 248
            String type = conn.getContentType();
123
            
124
            	
249
                        	
125 250
	    	byte[] imageBytes = null;
126 251
	    	byte[] buffer = new byte[1024*256];
127 252
            InputStream is = conn.getInputStream();
......
147 272
	    	if ((type !=null && !type.subSequence(0,5).equals("image")) 
148 273
	    		||(Utilities.isTextData(imageBytes)))
149 274
	    	{	    		
275
               	WMSException wmsEx = null;
276
               	
150 277
            	String exceptionMessage = parseException(imageBytes);
151
                if (exceptionMessage!=null)
278
                if (exceptionMessage==null)
152 279
                {
153
                    throw new WMSException(exceptionMessage);
154
                }
155
                else
156
                {
157
                	String error = new String(imageBytes);
280
                 	String error = new String(imageBytes);
158 281
                	int pos = error.indexOf("<?xml");
159 282
                	if (pos!= -1)
160 283
                	{
161 284
                		String xml = error.substring(pos,error.length());
162 285
                		exceptionMessage = parseException(xml.getBytes());
163
                        if (exceptionMessage!=null)
164
                            throw new WMSException(exceptionMessage);
165
                        else
166
                        	throw new ServerErrorException(new String(imageBytes));
286
                        if (exceptionMessage == null)
287
                        	exceptionMessage = new String(imageBytes);
167 288
                	}
168
                	throw new ServerErrorException(new String(imageBytes));
169 289
                }
290
             	//WMSWrongSizeException.check(exceptionMessage);               
291
                // error desconocido.
292
            	wmsEx = new WMSException(exceptionMessage);
293
            	wmsEx.setWMSMessage(new String(imageBytes));
294
                throw wmsEx;
170 295
            }
171
			return imageBytes;
172
	    	
296
			return imageBytes;	    	
173 297
		}
174 298
		catch(IOException e)
175 299
		{
......
215 339
                 //kxmlParser.require(KXmlParser.END_DOCUMENT, null, null);
216 340
            }
217 341
        }
218
        catch(XmlPullParserException parser_ex){
219
            //System.out.println(parser_ex.getMessage());
342
        catch(XmlPullParserException parser_ex){ 
220 343
            parser_ex.printStackTrace();
221 344
        }
222 345
        catch (IOException ioe) {           
......
228 351
        }
229 352
        return message;
230 353
    }
231

  
232
    /**
233
	 * <p>Builds a GetCapabilities request that is sent to the WMS
234
	 * the response will be parse to extract the data needed by the
235
	 * WMS client</p>
236
	 */
237
    public void getCapabilities()
238
    {		
239
    	URL request = null;
240
		try
241
		{
242
			request = new URL(buildCapabilitiesRequest());
243
		}
244
		catch(Exception e)
245
		{
246
			e.printStackTrace();
247
		}
248
		try
249
		{
250
		File f = com.iver.andami.Utilities.downloadFile(request,"wms_capabilities.xml");
251
	    parse(f);
252
	    } catch(Exception e)
253
		{
254
			//TODO
255
			e.printStackTrace();
256
		}
257
    }
258

  
259
    /**
260
     * <p>It will send a GetFeatureInfo request to the WMS
261
     * Parsing the response and redirecting the info to the WMS client</p>
262
     */
263
    public String getFeatureInfo(WMSStatus status, int x, int y, int featureCount)
264
    {
265
    	URL request = null;
266
    	StringBuffer output = new StringBuffer();
267
    	String outputFormat = new String();
268
    	String ServiceException = "ServiceExceptionReport";    	
269
    	
270
    	StringBuffer sb = new StringBuffer();
271
    	sb.append("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>");
272
    	
273
		try
274
		{
275
			//TODO:
276
			//pass this buildXXXRequest to the WMSProtocolHandlerXXX: The request can depend on the WMS version.
277
			request = new URL(buildGetFeatureInfoRequest(status, x, y));
278
			
279
			//TODO:
280
			//see which output format is being requested. 					
281
		}
282
		catch(Exception e)
283
		{
284
			e.printStackTrace();
285
		}
286
		
287
    	try
288
    	{	    	    	
289
    		System.out.println(request.toString());
290
    		//File f = new File("c:\\featureInfo.xml");
291
    		//DataOutputStream dos = new DataOutputStream(new FileOutputStream(f)); 	  
292
	    	byte[] buffer = new byte[1024*256];	    	
293
	    	DataInputStream is = new DataInputStream(request.openStream());	    	
294
	    	outputFormat = request.openConnection().getContentType();
295

  
296
	    	for (int i = is.read(buffer); i>0; i = is.read(buffer)){
297
	    		//dos.write(buffer, 0, i);
298
	    		String str = new String(buffer,0,i);
299
	    		//System.out.println("str:" + str);
300
	    		output.append(str);	    	
301
	    		//System.out.println("output:" +output.toString());
302
	    	}
303
	    		    
304
	    	//dos.close();
305
	    	is.close();
306
		    if ( (outputFormat == null) || (outputFormat.indexOf("xml") != -1)
307
		    		||output.toString().toLowerCase().startsWith("<?xml")
308
		    		||(outputFormat.indexOf("gml") != -1))
309
		    {
310
		    	int tag;
311
		    	KXmlParser kxmlParser = null;
312
		    	kxmlParser = new KXmlParser();	    
313
		    	//kxmlParser.setInput(new FileReader(f));
314
		    	kxmlParser.setInput(new StringReader(output.toString()));
315
		    	
316
				tag = kxmlParser.nextTag();
317
	    		//if ( kxmlParser.getEventType() != KXmlParser.END_DOCUMENT ) 
318
	    		{    		
319
	 				 while(tag != KXmlParser.END_DOCUMENT)
320
					 {
321
						 switch(tag)
322
						 {
323
							case KXmlParser.START_TAG:		
324
								if (kxmlParser.getName().compareTo(ServiceException)==0)
325
								{
326
						    		sb.append("<INFO>").append(parseException( output.toString().getBytes())).append("</INFO>");
327
						    		return sb.toString();									
328
								}
329
								else if (kxmlParser.getName().compareToIgnoreCase("ERROR")==0)
330
									return output.toString();
331
								else								
332
									sb.append("<" + kxmlParser.getName() + ">\n");
333
								break;
334
							case KXmlParser.END_TAG:	
335
								sb.append("</" + kxmlParser.getName() + ">\n");
336
								break;
337
							case KXmlParser.TEXT:
338
								sb.append(kxmlParser.getText());						
339
							break;
340
						 }
341
	    				 tag = kxmlParser.next();
342
	    			 }
343
	    		}	
344
	    		//System.out.println(sb.toString());
345
	    		return sb.toString();
346
	    	}
347
	    	else
348
	    	{
349
	    		sb.append("<INFO>").append(PluginServices.getText(this, "info_format_not_supported")).append("</INFO>");
350
	    		return sb.toString();
351
	    	}
352
		}
353
    	catch(XmlPullParserException parserEx)
354
    	{
355
    		if (output.toString().toLowerCase().indexOf("xml") != -1)
356
    			return output.toString().trim();
357
    		else
358
    		{
359
    	   		sb.append("<INFO>").append(PluginServices.getText(this, "info_format_not_supported")).append("</INFO>");
360
        		return sb.toString();
361
    		}
362
    	}
363
    	catch(Exception e)
364
    	{
365
    		e.printStackTrace();
366
    		sb.append("<INFO>").append(PluginServices.getText(this, "info_format_not_supported")).append("</INFO>");
367
    		return sb.toString();
368

  
369
    	}
370
    }
371 354
    
372 355
    /**
373 356
     * Builds the GetCapabilitiesRequest according to the OGC WMS Specifications
......
483 466
        if (status.getTransparency()) {
484 467
            req += "&TRANSPARENT=TRUE";
485 468
        }
486

  
487 469
//
488 470
//        if (status.getBGColor() != null) {
489 471
//            req += ("&COLOR=" + status.getBGColor());

Also available in: Unified diff