/* Package GeoTools/WMS Implementation * Copyright (C) 2001 Cameron Shorter (camerons@users.sourceforge.net) * Artur Hefczyc (kobit@users.sourceforge.net) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * * $Id: RemoteConnection.java,v 1.11 2002/01/17 20:52:24 kobit Exp $ * $Author: kobit $ * $Date: 2002/01/17 20:52:24 $ */ package uk.ac.leeds.ccg.ogc; import java.io.*; import java.net.*; import org.xml.sax.*; import wttools.protocols.*; import wttools.protocols.ifc.*; /** * Class RemoteConnection represents single connection * to remote server or proxy server. All remote connections to * OGC services should be done through this class instances.
* Methods of this class are capable of server name resolving and * making decision if connection should realized through the * server or through the proxy service.
* For non blocking other services this class should be designed * to work as a separate thread. * * @author Cameron Shorter * @author Artur Hefczyc * @version $Revision: 1.11 $ */ public class RemoteConnection { /* Connection constants */ /** * Constant RESPONSE_ERROR indicates that connection http error * occured. We probably have no additional data from WMS. */ public static final int RESPONSE_ERROR = -1; /** * Constant CONTENT_UNKNOWN indicates that http connection * was successful but we got unexpected content. It can contain useful * data or not and we can't determine data type. */ public static final int CONTENT_UNKNOWN = 0; /** * Constant CONTENT_BASIC_XML indicates that http connection * was successful but we got unexpected content. It can contain useful data. */ public static final int CONTENT_BASIC_XML = 1; /** * Constant CONTENT_CAPABILITIES indicates that http connection * was successful and we got content with capabilities encoded in XML format. */ public static final int CONTENT_CAPABILITIES = 2; /** * Constant CONTENT_GML indicates that http connection * was successful and we got content encoded in GML format. */ public static final int CONTENT_GML = 3; /** * Constant CONTENT_SE indicates that http connection * was successful but some error ocured in WMS server and we got * service exception encoded in XML format. */ public static final int CONTENT_SE = 4; /** * Constant CONTENT_HTML indicates that http connection was * successful but we got html page. Probably remote server is not * a conforming WMS server. */ public static final int CONTENT_HTML = 5; protected final static boolean DEBUG = true; private String server = null; private String proxy = null; private wttools.protocols.URI url = null; private ProtocolIfc connection = null; public RemoteConnection(String server, String proxy) { if(server != null) this.server = server; if(proxy != null) this.proxy = proxy; } protected void initConnection(wttools.protocols.URI uri) throws IOException { url = uri; prln("initConn:1 "+connection+" "+uri.toString()); connection = ProtocolFactory.createProtocol(uri, DEBUG); connection.setStreamReadTimeout(60*1000); prln("initConn:2 "+connection+" "+uri.toString()); } public int sendQuery(String query) throws IOException, MalformedURLException { prln("send qu:1 "+server+" "+query); if(server.indexOf('?') == -1) url = new wttools.protocols.URI(server+"?"+query); else url = new wttools.protocols.URI(server+"&"+query); prln("send qu:2 "+url.toString()); return sendRequest(new URL(url.toString())); } public int sendRequest(URL url) throws IOException { prln("send req: URL="+url.toString()); if (connection == null) { initConnection(new wttools.protocols.URI(url)); } // end of if (connection == null) else { connection.setURI(new wttools.protocols.URI(url)); } // end of else of if (connection == null) System.out.println("end of sendReq "+connection); return identifyResponse(connection); } public InputStream getInputStream() throws IOException { if(connection != null) return connection.getInputStream(); else return null; } public byte[] getByteData() throws IOException { if (connection != null) { return connection.getContentData(); } // end of if (connection != null) else { return null; } // end of if (connection != null)else } public void saveContent(String fileName) throws IOException { connection.saveContent(fileName); // byte[] buffer = new byte[1024*8]; // FileOutputStream out = new FileOutputStream(fileName, false); // InputStream in = getInputStream(); // int bytes = 0; // do { // bytes = in.read(buffer); // if(bytes > 0) out.write(buffer, 0, bytes); // } while(bytes > 0); // out.close(); } /* Protected methods */ protected int identifyResponse(ProtocolIfc conn) throws IOException { if (conn.getConnectionResult() != conn.CODE_CONNECTION_OK) { return RESPONSE_ERROR; } // end of if (conn.getConnectionResult() != conn.CODE_CONNECTION_OK) // if(conn instanceof HttpURLConnection) // if(((HttpURLConnection)conn).getResponseCode() < 200 || // ((HttpURLConnection)conn).getResponseCode() >= 300) // return RESPONSE_ERROR; if(conn.getContentType() == null) return CONTENT_UNKNOWN; String content = conn.getContentType().toLowerCase(); if(content.equals(OGCConstants.MT_XML_TEXT) || content.equals(OGCConstants.MT_XML_OGC_GENERIC)) return CONTENT_BASIC_XML; if(content.equals(OGCConstants.MT_XML_CAPABILITIES)) return CONTENT_CAPABILITIES; if(content.equals(OGCConstants.MT_XML_GML)) return CONTENT_GML; if(content.equals(OGCConstants.MT_XML_SE) || content.equals(OGCConstants.MT_XML_SE_INIMAGE) || content.equals(OGCConstants.MT_XML_SE_BLANK)) return CONTENT_SE; if(content.equals(OGCConstants.MT_HTML)) return CONTENT_HTML; return CONTENT_UNKNOWN; } protected static void prln(String str) { System.err.println(str); } } /* * Changes in file: * * $Log: RemoteConnection.java,v $ * Revision 1.11 2002/01/17 20:52:24 kobit * URI bug in RemoteConnection corrected * * Revision 1.10 2001/12/20 15:27:23 ianturton * various fixes and small mods to make WMSExample actually run. Can now * display an image if you remember to choose gif or jpg as output. Still * crashes if you zoom in. * * Revision 1.9 2001/12/11 12:26:58 ianturton * fixed malformed url in send query. * * Revision 1.8 2001/11/20 17:01:45 kobit * Added byte[] getContentData() support * * Revision 1.7 2001/11/18 15:57:54 kobit * Use of simple-jprotocols package instead of java.net, DTD validating switched OFF, default version if WMS implementation is set to 1.0.0 * * Revision 1.6 2001/10/24 09:40:18 kobit * Added LOG cvs keyword to the end of file * * */