]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.district.maps/src/org/simantics/maps/tests/WMSTest1.java
Ensure ITileProviders return BufferedImages with compatible ColorModel
[simantics/district.git] / org.simantics.district.maps / src / org / simantics / maps / tests / WMSTest1.java
1 /*******************************************************************************
2  * Copyright (c) 2012 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.maps.tests;
13
14 import java.awt.geom.Rectangle2D;
15 import java.io.File;
16 import java.io.FileOutputStream;
17 import java.io.IOException;
18 import java.io.InputStream;
19 import java.net.HttpURLConnection;
20 import java.net.MalformedURLException;
21 import java.net.URISyntaxException;
22
23 import org.simantics.maps.WebService;
24 import org.simantics.maps.wms.WMSGetMapQuery;
25
26 /**
27  * @author Tuukka Lehtonen
28  */
29 public class WMSTest1 {
30
31     private static String serviceUrl = "http://wms.jpl.nasa.gov/wms.cgi";
32     
33     public static void main(String[] args) throws MalformedURLException, URISyntaxException {
34         // Create a method instance.
35         WebService service = new WebService(serviceUrl);
36         WMSGetMapQuery req = new WMSGetMapQuery(600, 300, new Rectangle2D.Double(-180,-90,360,180), "image/jpeg", "global_mosaic");
37
38         HttpURLConnection connection = null;
39         try {
40                 connection = service.openConnection("", req.toString());
41             // Execute the method.
42             int statusCode = connection.getResponseCode();
43
44             if (statusCode != HttpURLConnection.HTTP_OK) {
45                 System.err.println("Method failed: " + connection.getResponseMessage());
46             }
47
48             // Read the response body.
49             byte[] buf = new byte[16384];
50             int read = 0;
51             
52             InputStream response = connection.getInputStream();
53             FileOutputStream fo = new FileOutputStream(File.createTempFile("wmsresponse", ""));
54             try {
55                 while ((read = response.read(buf)) != -1) {
56                     fo.write(buf, 0, read);
57                 }
58             } finally {
59                 fo.close();
60                 response.close();
61             }
62
63         } catch (IOException e) {
64             System.err.println("Fatal transport error: " + e.getMessage());
65             e.printStackTrace();
66         } finally {
67             // Release the connection.
68             connection.disconnect();
69         }  
70     }
71 }