001    /*
002     * 
003     * $Revision: 13085 $ $Date: 2008-02-06 18:27:24 +0100 (Mi, 06 Feb 2008) $
004     *
005     * This file is part of ***  M y C o R e  ***
006     * See http://www.mycore.de/ for details.
007     *
008     * This program is free software; you can use it, redistribute it
009     * and / or modify it under the terms of the GNU General Public License
010     * (GPL) as published by the Free Software Foundation; either version 2
011     * of the License or (at your option) any later version.
012     *
013     * This program is distributed in the hope that it will be useful, but
014     * WITHOUT ANY WARRANTY; without even the implied warranty of
015     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
016     * GNU General Public License for more details.
017     *
018     * You should have received a copy of the GNU General Public License
019     * along with this program, in a file called gpl.txt or license.txt.
020     * If not, write to the Free Software Foundation Inc.,
021     * 59 Temple Place - Suite 330, Boston, MA  02111-1307 USA
022     */
023    
024    package org.mycore.services.urn;
025    
026    import javax.servlet.ServletException;
027    import javax.servlet.http.HttpServletRequest;
028    import javax.servlet.http.HttpServletResponse;
029    
030    import org.apache.log4j.Logger;
031    import org.mycore.common.MCRConfiguration;
032    import org.mycore.frontend.servlets.MCRServlet;
033    import org.mycore.frontend.servlets.MCRServletJob;
034    
035    /**
036     * This servlet resolves a given URN (urn:nbn:de) from a HTTP request and
037     * redirects the client to the document that is registered for this URN. The URN
038     * can be either given as the query string or as the request path. If the URN is
039     * assigned to a local document, the request is redirected to the frontpage that
040     * displays the document's metadata, as specified by the configuration property
041     * MCR.URN.Resolver.DocumentURL. If the URN is not local, the request is
042     * redirected to another URN resolver, as specified by the configuration
043     * property MCR.URN.Resolver.MasterURL.
044     * 
045     * 
046     * @author Frank Lützenkirchen
047     * @version $Revision: 13085 $ $Date: 2008-02-06 18:27:24 +0100 (Mi, 06 Feb 2008) $
048     */
049    public class MCRURNResolver extends MCRServlet {
050    
051        private final static Logger LOGGER = Logger.getLogger(MCRURNResolver.class);
052    
053        protected String masterURL;
054    
055        protected String documentURL;
056    
057        public void init() throws ServletException {
058            super.init();
059            String base = "MCR.URN.Resolver.";
060            masterURL = MCRConfiguration.instance().getString(base + "MasterURL");
061            documentURL = MCRConfiguration.instance().getString(base + "DocumentURL");
062        }
063    
064        public void doGetPost(MCRServletJob job) throws Exception {
065            HttpServletRequest req = job.getRequest();
066            HttpServletResponse res = job.getResponse();
067    
068            String path = req.getPathInfo();
069            String param = req.getQueryString();
070    
071            String urn = param;
072    
073            if ((urn == null) && (path != null))
074                urn = path.substring(1).trim();
075    
076            if (urn == null) {
077                res.sendError(HttpServletResponse.SC_BAD_REQUEST);
078                return;
079            }
080    
081            LOGGER.info("Resolving URN " + urn);
082    
083            String docID = MCRURNManager.getDocumentIDforURN(urn);
084    
085            if (docID == null)
086                res.sendRedirect(masterURL + urn);
087            else
088                res.sendRedirect(documentURL + docID);
089        }
090    }