www.pudn.com > gvSIG-1_1-rc1-src.zip > CatalogClient.java



/* gvSIG. Sistema de Información Geográfica de la Generalitat Valenciana
*
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
*
* 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.
*
* For more information, contact:
*
*  Generalitat Valenciana
*   Conselleria d'Infraestructures i Transport
*   Av. Blasco Ibáñez, 50
*   46010 VALENCIA
*   SPAIN
*
*      +34 963862235
*   gvsig@gva.es
*      www.gvsig.gva.es
*
*    or
*
*   IVER T.I. S.A
*   Salamanca 50
*   46005 Valencia
*   Spain
*
*   +34 963163400
*   dac@iver.es
*/
package es.gva.cit.catalogClient;
import es.gva.cit.catalogClient.csw.drivers.CSWCatalogServiceDriver;
import es.gva.cit.catalogClient.drivers.ICatalogServiceDriver;
import es.gva.cit.catalogClient.querys.Query;
import es.gva.cit.catalogClient.schemas.Schemas;
import es.gva.cit.catalogClient.srw.drivers.SRWCatalogServiceDriver;
import com.iver.utiles.swing.jcomboServer.ServerData;
import es.gva.cit.catalogClient.z3950.drivers.Z3950CatalogServiceDriver;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.Socket;
import java.net.URL;
import java.net.UnknownHostException;
import java.util.Collection;

/**
 * This class represents a catalogClient. It must be created to
 * use the catalog service
 * 
 * 
 * @author Jorge Piera Llodra (piera_jor@gva.es)
 */
public class CatalogClient {

/**
 * 
 * 
 */
    private URL url = null;

/**
 * 
 * 
 */
    private String sURL = null;

/**
 * 
 * 
 */
    private String protocol = null;
/**
 * 
 * 
 */
    private ICatalogServiceDriver lnkICatalogServerDriver;

/**
 * 
 * 
 */
    private String serverStatus = null;

/**
 * Constructor
 * 
 * 
 * @param url Server url
 * @param protocol Server protocol
 * @param database Database name. This param only is used when the protocol
 * is z39.50
 */
    public  CatalogClient(String url, String protocol, String database) {        
        setProtocol(protocol);
        setSURL(url);
        //This is necessary. Otherwise it will throw the exception
        if (!(url.regionMatches(0, "http://", 0, 7))) {
            url = "http://" + url;
        }
        try {
            this.setUrl(new URL(url));
            //if protocol is z39.50 we have to add the database to the URL
            if (!(protocol.equals(new String(ServerData.SERVER_SUBTYPE_CATALOG_Z3950)))) {
                database = getUrl().getPath();
            }
            //if protocol is http, we have to set the port manually
            if (getUrl().getPort() == -1) {
                setUrl(new URL("http", getUrl().getHost(), 80, database));
            } else {
                setUrl(new URL("http", getUrl().getHost(), getUrl().getPort(),
                        database));
            }
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            System.out.println("La URL no es correcta");
        }
        //Casting to the respective driver
        if (protocol.equals(new String(ServerData.SERVER_SUBTYPE_CATALOG_SRW))) {
            lnkICatalogServerDriver = new SRWCatalogServiceDriver();
        }
        if (protocol.equals(new String(ServerData.SERVER_SUBTYPE_CATALOG_CSW))) {
            lnkICatalogServerDriver = new CSWCatalogServiceDriver();
        }
        if (protocol.equals(new String(ServerData.SERVER_SUBTYPE_CATALOG_Z3950))) {
            lnkICatalogServerDriver = new Z3950CatalogServiceDriver();
        }
    } 

/**
 * 
 * 
 * 
 * @return 
 */
    public String getCapabilities() {        
        //try to connect
        if (!serverReady()) {
            serverStatus = ("errorServerNotFound");
        } else if (!getLnkICatalogServerDriver().isProtocolSupported(getUrl())) {
            serverStatus = "errorNotSupportedProtocol";
        } else {
            //getCapabilities
            Collection nodesCapabilities = getLnkICatalogServerDriver()
                                              .getCapabilities(getUrl());
            if (nodesCapabilities == null) {
                serverStatus = "errorNotSupportedCapabilities";
            } else {
                //Configure the client
                if (!getLnkICatalogServerDriver().setParameters(nodesCapabilities,getUrl())) {
                    if (!(getLnkICatalogServerDriver()
                                     .getServerAnswerReady().equals(""))) {
                        serverStatus = getLnkICatalogServerDriver().getServerAnswerReady();
                    } else {
                        serverStatus = "errorNotParsedReply";
                    }
                } else {
                    //Show the answer
                    serverStatus = getLnkICatalogServerDriver().getServerAnswerReady();
                    return "2.0.0";
                }
            }
            if (getLnkICatalogServerDriver() instanceof CSWCatalogServiceDriver){
            	((CSWCatalogServiceDriver)getLnkICatalogServerDriver()).setVersion("2.0.1");
            	((CSWCatalogServiceDriver)getLnkICatalogServerDriver()).setService("http://www.opengis.net/cat/csw");   
            	((CSWCatalogServiceDriver)getLnkICatalogServerDriver()).setServerProfile(Schemas.EBRIM);        
               	nodesCapabilities = getLnkICatalogServerDriver().getCapabilities(getUrl());
            	if (nodesCapabilities == null) {
            		serverStatus = "errorNotSupportedCapabilities";
            	} else {
            		//Configure the client
            		if (!getLnkICatalogServerDriver().setParameters(nodesCapabilities,getUrl())) {
            			if (!(getLnkICatalogServerDriver()
            					.getServerAnswerReady().equals(""))) {
            				serverStatus = getLnkICatalogServerDriver().getServerAnswerReady();
            			} else {
            				serverStatus = "errorNotParsedReply";
            			}
            		} else {
            			//Show the answer
            			serverStatus = getLnkICatalogServerDriver().getServerAnswerReady();
            			return "2.0.1";
            		}
            	}
            }
        }
    
        return null;
    } 

/**
 * 
 * 
 * 
 * @return Node array with the retrieved records
 * @param url Server URL
 * @param query It contains the values to do the query (title="XXX",abstract="YYY",...)
 * @param firstRecord Number of the first record to retrieve
 */
    public Collection getRecords(URL url, Query query, int firstRecord) {        
    
        return getLnkICatalogServerDriver().getRecords(url,query,firstRecord);
           
            
    } 

/**
 * This method is used to create a new Query
 * 
 * 
 * @return 
 */
    public Query createNewQuery() {        
        return new Query();   
    } 

/**
 * It tries if the server is ready
 * 
 * 
 * @return boolean
 * true --> server is ready
 * false --> server is not ready
 */
    private boolean serverReady() {        
            try {
                new Socket(this.getUrl().getHost(),this.getUrl().getPort());
            } catch (UnknownHostException e) {
                // TODO Auto-generated catch block
                return false;
            } catch (IOException e) {
                // TODO Auto-generated catch block
                return false;
            }
            return true;
    } 

/**
 * 
 * 
 * 
 * @return Returns the lnkICatalogServerDriver.
 */
    public ICatalogServiceDriver getLnkICatalogServerDriver() {        
        return lnkICatalogServerDriver;
    } 

/**
 * 
 * 
 * 
 * @param lnkICatalogServerDriver The lnkICatalogServerDriver to set.
 */
    public void setLnkICatalogServerDriver(ICatalogServiceDriver lnkICatalogServerDriver) {        
        this.lnkICatalogServerDriver = lnkICatalogServerDriver;
    } 

/**
 * 
 * 
 * 
 * @return Returns the protocol.
 */
    public String getProtocol() {        
        return protocol;
    } 

/**
 * 
 * 
 * 
 * @param protocol The protocol to set.
 */
    public void setProtocol(String protocol) {        
        this.protocol = protocol;
    } 

/**
 * 
 * 
 * 
 * @return Returns the url.
 */
    public URL getUrl() {        
        return url;
    } 

/**
 * 
 * 
 * 
 * @return Returns the sUrl.
 */
    public String getSUrl() {        
        return sURL;
    } 

/**
 * 
 * 
 * 
 * @param url The url to set.
 */
    public void setUrl(URL url) {        
        this.url = url;
    } 

/**
 * 
 * 
 * 
 * @return Returns the sURL.
 */
    public String getSURL() {        
        return sURL;
    } 

/**
 * 
 * 
 * 
 * @param surl The sURL to set.
 */
    public void setSURL(String surl) {        
        sURL = surl;
    } 

/**
 * 
 * 
 * 
 * @return Returns the serverStatus.
 */
    public String getServerStatus() {        
        return serverStatus;
    } 
    
    /**
     * Return true if the services search is supported
     * @return
     */
    public boolean isServicesSeachSupported(){
    	if (url.getHost().equals("laits.gmu.edu")){ 
    		return true;
    	}
    	return false;
    }
 }