www.pudn.com > gvSIG-1_1-rc1-src.zip > CSWCatalogServiceDriver.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.csw.drivers;
import es.gva.cit.catalogClient.csw.parsers.CswCapabilitiesParser;
import es.gva.cit.catalogClient.csw.parsers.CswSupportedProtocolOperations;
import es.gva.cit.catalogClient.drivers.AbstractCatalogServiceDriver;
import es.gva.cit.catalogClient.metadataXML.XMLNode;
import es.gva.cit.catalogClient.metadataXML.XMLTree;
import es.gva.cit.catalogClient.metadataXML.XMLTreeNumberOfRecordsAnswer;
import es.gva.cit.catalogClient.protocols.HTTPGetProtocol;
import es.gva.cit.catalogClient.protocols.HTTPPostProtocol;
import es.gva.cit.catalogClient.protocols.SOAPProtocol;
import es.gva.cit.catalogClient.querys.Query;
import es.gva.cit.catalogClient.schemas.Schemas;
import java.net.URL;
import java.util.Collection;
import org.apache.commons.httpclient.NameValuePair;

/**
 * This class implements the CSW protocol.
 * 
 * 
 * @author Jorge Piera Llodra (piera_jor@gva.es)
 * @see http://portal.opengeospatial.org/files/?artifact_id=5929&version=1
 */
public class CSWCatalogServiceDriver extends AbstractCatalogServiceDriver {
private String service = "CSW";
/**
 * 
 * 
 */
    private String responseHandler = null;

/**
 * 
 * 
 */
    private String hopCount = null;

/**
 * 
 * 
 */
    private String distributedSearch = null;

/**
 * 
 * 
 */
    private String constraint = null;

/**
 * 
 * 
 */
    private String[] CONSTRAINTLANGUAGE = null;

/**
 * 
 * 
 */
    private String[] elementSetName = null;

/**
 * 
 * 
 */
    private String[] typeNames = null;

/**
 * 
 * 
 */
    private String[] resultType = null;

/**
 * 
 * 
 */
    private String[] NAMESPACE = null;

/**
 * 
 * 
 */
    private String version = "2.0.0";
/**
 * 
 * 
 */
    private CswSupportedProtocolOperations operations = null;
/*
     * (non-Javadoc)
     *
     * @see ICatalogServerDriver#GetCapabilities(java.lang.String,
     *      java.lang.String)
     */

/**
 * 
 * 
 * 
 * @return 
 * @param url 
 */
    public Collection getCapabilities(URL url) {        
        Collection nodes = new java.util.ArrayList();
        CSWMessages messages = new CSWMessages(this);
        CswCapabilitiesParser parser = new CswCapabilitiesParser(this,url);
        System.out.println("**************GET*************");
        nodes = new HTTPGetProtocol().doQuery(url, messages.getHTTPGETCapabilities(true), 0);
        if ((nodes != null) && (parser.getExceptionCode((XMLNode)nodes.toArray()[0]).equals("8001")) &&
                (parser.getExceptionSubCode((XMLNode)nodes.toArray()[0]).equals("5002")))
            nodes = new HTTPGetProtocol().doQuery(url, messages.getHTTPGETCapabilities(false), 0);
        
        if ((nodes == null) || 
        		((nodes.size() >0) && (((XMLNode)nodes.toArray()[0])==null)) ||
        		((nodes.size() >0) && (((XMLNode)nodes.toArray()[0]).getName().equals("html")))) {
            System.out.println("**************POST*************");
            System.out.println(messages.getHTTPPOSTCapabilities());
            nodes = new HTTPPostProtocol().doQuery(url,
                    messages.getHTTPPOSTCapabilities(), 0);
            if (nodes == null) {
                System.out.println("**************SOAP*************");
                nodes = new SOAPProtocol().doQuery(url,
                        messages.getSOAPCapabilities(), 0);
                if (nodes == null) {
                    return null;
                } else {
                    setCommunicationProtocol("SOAP");
                }
            } else {
                setCommunicationProtocol("POST");
            }
        } else {
            setCommunicationProtocol("GET");
        }
        
       return nodes;
    } 

/**
 * this method implements the describeRecords operation
 * 
 * 
 * @return 
 * @param url 
 */
    public XMLNode[] describeRecords(URL url) {        
        //return new HTTPPostProtocol().doQuery(url, getMessageDescribeRecords(),0);
        return null;
    } 
/*
     * (non-Javadoc)
     *
     * @see ICatalogServerDriver#GetRecords(java.lang.String, java.lang.String,
     *      java.lang.String, int, int, java.lang.String, java.lang.String)
     */

/**
 * 
 * 
 * 
 * @return 
 * @param url 
 * @param query 
 * @param firstRecord 
 */
    public Collection getRecords(URL url, Query query, int firstRecord) {        
        setQuery(query);
        CSWMessages messages = new CSWMessages(this);
        CSWMessages.setUrl(url);
        Collection answerNodes = new java.util.ArrayList();
        Collection nodes = new java.util.ArrayList();
        
        if (getOperations().getGetRecords()==null)
        	return null;
      
        if (getServerProfile().equals(Schemas.ISO19115)){
        	 if (getService().equals("CSW")){
        		 firstRecord = firstRecord - 1;
        	 }
        }
    
        
        for (int i=0 ; i tree with information of the answer
 * Next positions -> One result for each position
 * 
 * 
 * @return 
 * @param numberOfRecords Number of records returned by the query
 * @param firstRecord Number of the first record
 * @param node Tree returned by the server
 * @param url Server URL
 */
    private Collection createAnswerTree(int numberOfRecords, int firstRecord, XMLNode node, URL url) {        
        Collection answerNodes = new java.util.ArrayList();
      
        XMLNode[] auxNodes = cutMetadata(node);
        if (getServerProfile().equals(Schemas.EBRIM)) {
        	 if (!url.getHost().equals("laits.gmu.edu")){
        		 auxNodes = getEbRIMNodes(auxNodes, url);
        	 }
        }
        //Solves some problems with the maxNumberOfRecords attribute
        numberOfRecords = auxNodes.length;
        answerNodes.add(XMLTreeNumberOfRecordsAnswer.getNode(numberOfRecords,
                firstRecord, firstRecord + numberOfRecords));
        
        for (int i = 1;
                (i <= numberOfRecords) && (i <= 10) &&
                (i <= (numberOfRecords - firstRecord + 1)); i++)
            answerNodes.add(auxNodes[i - 1]);
        return answerNodes;
    } 

/**
 * This function retrieve the nodes for one ebRIM answer
 * 
 * 
 * @return Medatada Nodes.
 * @param nodes Server URL
 * @param url 
 */
    private XMLNode[] getEbRIMNodes(XMLNode[] nodes, URL url) {        
        XMLNode[] auxNodes = new XMLNode[nodes.length];
        for (int i = 0; i < nodes.length; i++) {
            String id = XMLTree.searchAtribute(nodes[i], "id");
            auxNodes[i] = (XMLNode)new HTTPGetProtocol().doQuery(url,
                    getEbRIMRequestParameters(id), 0).toArray()[0];
        }
        return auxNodes;
    } 

/**
 * It Returns the parameters needed by getExtrinsicContent
 * 
 * 
 * @return 
 * @param id Record id
 */
    private NameValuePair[] getEbRIMRequestParameters(String id) {        
        NameValuePair nvp1 = new NameValuePair("request", "getExtrinsicContent");
        NameValuePair nvp2 = new NameValuePair("id", id);
        return new NameValuePair[] { nvp1, nvp2 };
    } 

/**
 * This function returns the number of records that have been retrieved.
 * It Reads a Node value.
 * 
 * 
 * @return The number of records
 * @param node The answer tree
 */
    private int getNumberOfRecords(XMLNode node) {        
    	int numberOfRecords = getNumberOfRecords(node,"csw:SearchResults","numberOfRecordsMatched");
    	if (numberOfRecords == -1)
    		numberOfRecords = getNumberOfRecords(node,"SearchResults","numberOfRecordsMatched");
    	
    	return numberOfRecords;
    } 

/**
 * This function finds and separates metadata from a full tree
 * 
 * 
 * @return An array of trees that contain metadata.
 * @param node full tree
 */
    private XMLNode[] cutMetadata(XMLNode node) {        
        XMLNode[] auxNodes = XMLTree.searchMultipleNode(node,
                "csw:SearchResults->brief:MD_Metadata");
        if (auxNodes.length == 0) {
            auxNodes = XMLTree.searchMultipleNode(node,
                    "SearchResults->ebrim:Organization");
        }
        if (auxNodes.length == 0) {
            auxNodes = XMLTree.searchMultipleNode(node,
                    "csw:SearchResults->csw:SummaryRecord");
        }
        if (auxNodes.length == 0) {
            auxNodes = XMLTree.searchMultipleNode(node,
                    "csw:SearchResults->csw:Record");
        }
        if (auxNodes.length == 0) {
            auxNodes = XMLTree.searchMultipleNode(node,
                    "csw:SearchResults->DS_DataSet");
        }
        
        if (auxNodes.length == 0) {
            auxNodes = XMLTree.searchMultipleNode(node,
                    "SearchResults->wrs:WRSExtrinsicObject");
        }
        
        if (auxNodes.length == 0) {
            auxNodes = XMLTree.searchMultipleNode(node,
                    "SearchResults->WRSExtrinsicObject");
        }        
        
        if (auxNodes.length == 0) {
            auxNodes = XMLTree.searchMultipleNode(node,
                    "csw:SearchResults->dc:metadata");
        }
        
        if (auxNodes.length == 0) {
            auxNodes = XMLTree.searchMultipleNode(node,
                    "csw:SearchResults->iso19115:MD_Metadata");
        }
        if (auxNodes.length == 0) {
            auxNodes = XMLTree.searchMultipleNode(node,
                    "csw:SearchResults->laitscsw:DataGranule");
        }          
        if (auxNodes.length == 0) {
            auxNodes = XMLTree.searchMultipleNode(node,
                    "csw:SearchResults->rim:Service");
        }  
        return auxNodes;
    } 
/*
     * (non-Javadoc)
     *
     * @see catalogClient.ICatalogServerDriver#setParameters(java.util.Properties)
     */

/**
 * 
 * 
 * 
 * @return 
 * @param nodes 
 * @param url 
 */
    public boolean setParameters(Collection nodes, URL url) {        
        return new CswCapabilitiesParser(this,url).parse((XMLNode)nodes.toArray()[0]);
    } 

/**
 * 
 * 
 * 
 * @param typeNames The typeNames to set.
 */
    public void setTypeNames(String[] typeNames) {        
        this.typeNames = typeNames;
    } 
/* (non-Javadoc)
     * @see es.gva.cit.catalogClient.drivers.ICatalogServiveDriver#isTheProtocol(java.net.URL)
     */

/**
 * 
 * 
 * 
 * @return 
 * @param url 
 */
    public boolean isProtocolSupported(URL url) {        
        return true;
    } 

/**
 * 
 * 
 * 
 * @return Returns the distributedSearch.
 */
    public String getDistributedSearch() {        
        return distributedSearch;
    } 

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

/**
 * 
 * 
 * 
 * @return Returns the hopCount.
 */
    public String getHopCount() {        
        return hopCount;
    } 

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

/**
 * 
 * 
 * 
 * @return Returns the nAMESPACE.
 */
    public String[] getNAMESPACE() {        
        return NAMESPACE;
    } 

/**
 * 
 * 
 * 
 * @param namespace The nAMESPACE to set.
 */
    public void setNAMESPACE(String[] namespace) {        
        NAMESPACE = namespace;
    } 

/**
 * 
 * 
 * 
 * @return Returns the responseHandler.
 */
    public String getResponseHandler() {        
        return responseHandler;
    } 

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

/**
 * 
 * 
 * 
 * @return Returns the resultType.
 */
    public String[] getResultType() {        
        return resultType;
    } 

/**
 * 
 * 
 * 
 * @param resultType The resultType to set.
 */
    public void setResultType(String[] resultType) {        
        this.resultType = resultType;
    } 

/**
 * 
 * 
 * 
 * @return Returns the typeNames.
 */
    public String[] getTypeNames() {        
        return typeNames;
    } 

/**
 * 
 * 
 * 
 * @return Returns the version.
 */
    public String getVersion() {        
        return version;
    } 

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

/**
 * 
 * 
 * 
 * @return Returns the constraint.
 */
    public String getConstraint() {        
        return constraint;
    } 

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

/**
 * 
 * 
 * 
 * @return Returns the cONSTRAINTLANGUAGE.
 */
    public String[] getCONSTRAINTLANGUAGE() {        
        return CONSTRAINTLANGUAGE;
    } 

/**
 * 
 * 
 * 
 * @param constraintlanguage The cONSTRAINTLANGUAGE to set.
 */
    public void setCONSTRAINTLANGUAGE(String[] constraintlanguage) {        
        CONSTRAINTLANGUAGE = constraintlanguage;
    } 

/**
 * 
 * 
 * 
 * @return Returns the elementSetName.
 */
    public String[] getElementSetName() {        
        return elementSetName;
    } 

/**
 * 
 * 
 * 
 * @param elementSetName The elementSetName to set.
 */
    public void setElementSetName(String[] elementSetName) {        
        this.elementSetName = elementSetName;
    } 

/**
 * 
 * 
 * 
 * @return Returns the operations.
 */
    public CswSupportedProtocolOperations getOperations() {        
		return operations;
    } 

/**
 * 
 * 
 * 
 * @param operations The operations to set.
 */
    public void setOperations(CswSupportedProtocolOperations operations) {        
		this.operations = operations;
    } 
/* (non-Javadoc)
     * @see es.gva.cit.catalogClient.drivers.ICatalogServiceDriver#getServerPofile()
     */

/**
 * 
 * 
 * 
 * @return 
 */
    public String getQueryProfile() {        
        // TODO Auto-generated method stub
        return getServerProfile();
    }

public String getService() {
	return service;
}

public void setService(String service) {
	this.service = service;
} 
 }