001/*
002 *  Copyright 2012 Anyware Services
003 *
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package org.ametys.odf.oai;
017
018import java.io.IOException;
019import java.text.SimpleDateFormat;
020import java.util.Date;
021import java.util.TimeZone;
022
023import org.apache.cocoon.ProcessingException;
024import org.apache.cocoon.generation.AbstractGenerator;
025import org.apache.cocoon.xml.AttributesImpl;
026import org.apache.cocoon.xml.XMLUtils;
027import org.apache.commons.lang.StringUtils;
028import org.xml.sax.SAXException;
029
030import org.ametys.runtime.config.Config;
031
032/**
033 * Base class for all OAI responses.
034 */
035public abstract class AbstractOAIResponseGenerator extends AbstractGenerator
036{
037    @Override
038    public final void generate() throws IOException, SAXException, ProcessingException
039    {
040        contentHandler.startDocument();
041
042        contentHandler.startPrefixMapping("", "http://www.openarchives.org/OAI/2.0/");
043        contentHandler.startPrefixMapping("xsi", "http://www.w3.org/2001/XMLSchema-instance");
044        AttributesImpl atts = new AttributesImpl();
045        atts.addCDATAAttribute("http://www.w3.org/2001/XMLSchema-instance", "schemaLocation", "xsi:schemaLocation", "http://www.openarchives.org/OAI/2.0/ http://www.openarchives.org/OAI/2.0/OAI-PMH.xsd");
046        XMLUtils.startElement(contentHandler, "OAI-PMH", atts);
047
048        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
049        TimeZone tz = TimeZone.getTimeZone("UTC");
050        formatter.setTimeZone(tz);
051        XMLUtils.createElement(contentHandler, "responseDate", formatter.format(new Date()));
052        
053        doGenerate();
054
055        XMLUtils.endElement(contentHandler, "OAI-PMH");
056
057        contentHandler.endDocument();
058    }
059    
060    /**
061     * Actually generates the OAI response.
062     * @throws IOException if an I/O error occurs
063     * @throws SAXException if an error occurs
064     * @throws ProcessingException if an error occurs
065     */
066    protected abstract void doGenerate() throws IOException, SAXException, ProcessingException;
067
068    /**
069     * Generates SAX events representing an error.
070     * @param errorCode the OAI error code.
071     * @param errorLabel the error label.
072     * @throws SAXException if an error occurs while SAXing the OAI error.
073     */
074    protected void generateError(String errorCode, String errorLabel) throws SAXException
075    {
076        AttributesImpl atts = new AttributesImpl();
077        atts.addCDATAAttribute("code", errorCode);
078        XMLUtils.createElement(contentHandler, "error", atts, errorLabel);
079    }
080    
081    /**
082     * Returns the base URL of the OAI repository.
083     * @return the base URL of the OAI repository.
084     */
085    protected String getURL()
086    {
087        String url = StringUtils.removeEndIgnoreCase(Config.getInstance().getValue("cms.url"), "index.html");
088        
089        StringBuilder result = new StringBuilder(url);
090        
091        if (!url.endsWith("/"))
092        {
093            result.append('/');
094        }
095        
096        return result.append("_odf/OAI").toString();
097    }
098}