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