001    //$HeadURL: svn+ssh://rbezema@svn.wald.intevation.org/deegree/base/branches/2.2_testing/src/org/deegree/ogcwebservices/csw/manager/HarvesterFactory.java $
002    /*----------------    FILE HEADER  ------------------------------------------
003    
004     This file is part of deegree.
005     Copyright (C) 2001-2008 by:
006     EXSE, Department of Geography, University of Bonn
007     http://www.giub.uni-bonn.de/deegree/
008     lat/lon GmbH
009     http://www.lat-lon.de
010    
011     This library is free software; you can redistribute it and/or
012     modify it under the terms of the GNU Lesser General Public
013     License as published by the Free Software Foundation; either
014     version 2.1 of the License, or (at your option) any later version.
015    
016     This library is distributed in the hope that it will be useful,
017     but WITHOUT ANY WARRANTY; without even the implied warranty of
018     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
019     Lesser General Public License for more details.
020    
021     You should have received a copy of the GNU Lesser General Public
022     License along with this library; if not, write to the Free Software
023     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
024    
025     Contact:
026    
027     Andreas Poth
028     lat/lon GmbH
029     Aennchenstr. 19
030     53115 Bonn
031     Germany
032     E-Mail: poth@lat-lon.de
033    
034     Prof. Dr. Klaus Greve
035     Department of Geography
036     University of Bonn
037     Meckenheimer Allee 166
038     53115 Bonn
039     Germany
040     E-Mail: greve@giub.uni-bonn.de
041    
042     
043     ---------------------------------------------------------------------------*/
044    package org.deegree.ogcwebservices.csw.manager;
045    
046    import java.net.URI;
047    import java.util.Map;
048    
049    import org.deegree.framework.xml.XMLFragment;
050    import org.deegree.ogcwebservices.InvalidParameterValueException;
051    import org.deegree.ogcwebservices.csw.manager.HarvestRepository.ResourceType;
052    
053    /**
054     * returns an concrete instance of
055     * 
056     * @see org.deegree.ogcwebservices.csw.manager.AbstractHarvester that is responsible for performing
057     *      a harvest request against a source type assigned to the request. To decide which concrete
058     *      Harvester is required the resourceType and, if neccessary, source parameter of a harvest
059     *      request will be examinded.
060     * 
061     * @version $Revision: 9345 $
062     * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
063     * @author last edited by: $Author: apoth $
064     * 
065     * @version 1.0. $Revision: 9345 $, $Date: 2007-12-27 17:22:25 +0100 (Do, 27 Dez 2007) $
066     * 
067     * @since 2.0
068     */
069    public class HarvesterFactory {
070    
071        private Map<ResourceType, AbstractHarvester> availableHarvester = null;
072    
073        /**
074         * list of available Harvester
075         * 
076         * @param availableHarvester
077         */
078        HarvesterFactory( Map<ResourceType, AbstractHarvester> availableHarvester ) {
079            this.availableHarvester = availableHarvester;
080        }
081    
082        /**
083         * returns an concrete instance of
084         * 
085         * @see org.deegree.ogcwebservices.csw.manager.AbstractHarvester that is responsible for
086         *      performing a harvest request against a resource type assigned to the request. If no
087         *      Harvester can be found that can be used to haverst the source defined in a harvest
088         *      request an
089         * 
090         * @param request
091         * @return
092         */
093        AbstractHarvester findHarvester( Harvest request )
094                                throws InvalidParameterValueException {
095            URI uri = request.getResourceType();
096            ResourceType st = null;
097            if ( uri == null ) {
098                st = evaluateSource( request.getSource() );
099            } else {
100                String s = uri.toASCIIString();
101                if ( s.equals( "csw:profile" ) ) {
102                    st = ResourceType.csw_profile;
103                } else if ( s.equals( "service" ) ) {
104                    st = ResourceType.service;
105                } else if ( s.equals( "FGDC" ) ) {
106                    st = ResourceType.FGDC;
107                } else if ( s.equals( "dublincore" ) ) {
108                    st = ResourceType.dublincore;
109                } else if ( s.equals( "catalogue" ) ) {
110                    st = ResourceType.catalogue;
111                } else {
112                    String ms = "requested resourceType:" + s + " is not supported by the CS-W";
113                    throw new InvalidParameterValueException( getClass().getName(), ms );
114                }
115            }
116            return availableHarvester.get( st );
117        }
118    
119        /**
120         * determines the type of a metadata resource by evaluation the root element returned when
121         * accessing it.
122         * 
123         * @param source
124         * @return
125         * @throws InvalidParameterValueException
126         */
127        private ResourceType evaluateSource( URI source )
128                                throws InvalidParameterValueException {
129    
130            ResourceType st = null;
131            try {
132                XMLFragment xml = new XMLFragment();
133                xml.load( source.toURL() );
134                String ns = xml.getRootElement().getNamespaceURI();
135                String s = xml.getRootElement().getLocalName();
136                if ( "MD_Metadata".equals( s ) ) {
137                    st = ResourceType.csw_profile;
138                } else if ( "Record".equals( s ) ) {
139                    st = ResourceType.dublincore;
140                } else if ( "WMT_MS_Capabilities".equals( s ) ) {
141                    st = ResourceType.service;
142                } else if ( "WFS_Capabilities".equals( s ) ) {
143                    st = ResourceType.service;
144                } else if ( "WCS_Capabilities".equals( s ) ) {
145                    st = ResourceType.service;
146                }
147                if ( "http://www.opengis.net/cat/csw".equals( ns ) ) {
148                    st = ResourceType.catalogue;
149                }
150            } catch ( Exception e ) {
151                throw new InvalidParameterValueException( "source: " + source
152                                                          + " can not be parsed as XML" );
153            }
154    
155            return st;
156        }
157    
158    }