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.util.Collection;
020import java.util.Enumeration;
021
022import org.apache.cocoon.ProcessingException;
023import org.apache.cocoon.environment.ObjectModelHelper;
024import org.apache.cocoon.environment.Request;
025import org.apache.cocoon.xml.AttributesImpl;
026import org.apache.cocoon.xml.XMLUtils;
027import org.apache.commons.lang.StringUtils;
028import org.xml.sax.SAXException;
029
030/**
031 * Base class for all OAI verb responses.
032 */
033public abstract class AbstractOAIVerbGenerator extends AbstractOAIResponseGenerator
034{
035    @Override
036    protected final void doGenerate() throws IOException, SAXException, ProcessingException
037    {
038        if (_hasBadArguments(getRequiredParameters(), getAllowedParameters()))
039        {
040            XMLUtils.createElement(contentHandler, "request", getURL());
041            generateError("badArgument", "The request includes illegal arguments, is missing required arguments, includes a repeated argument, or values for arguments have an illegal syntax.");
042        }
043        else
044        {
045            Request request = ObjectModelHelper.getRequest(objectModel);
046            
047            AttributesImpl params = new AttributesImpl();
048            
049            Enumeration paramsEnum = request.getParameterNames();
050            while (paramsEnum.hasMoreElements())
051            {
052                String name = (String) paramsEnum.nextElement();
053                params.addCDATAAttribute(name, request.getParameter(name));
054            }
055            
056            XMLUtils.createElement(contentHandler, "request", params, getURL());
057
058            generateVerb();
059        }
060    }
061    
062    /**
063     * Returns a Collection af all mandatory parameters.
064     * @return a Collection af all mandatory parameters.
065     */
066    protected abstract Collection<String> getRequiredParameters();
067
068    /**
069     * Returns a Collection af all possible parameters.
070     * @return a Collection af all possible parameters.
071     */
072    protected abstract Collection<String> getAllowedParameters();
073    
074    /**
075     * Generates SAX events for the current verb.
076     * @throws IOException if an I/O error occurs
077     * @throws SAXException if an error occurs
078     * @throws ProcessingException if an error occurs
079     */
080    protected abstract void generateVerb() throws IOException, SAXException, ProcessingException;
081    
082    private boolean _hasBadArguments(Collection<String> requiredParameters, Collection<String> allowedParameters)
083    {
084        Request request = ObjectModelHelper.getRequest(objectModel);
085        
086        for (String requiredParam : requiredParameters)
087        {
088            if (StringUtils.isEmpty(request.getParameter(requiredParam)))
089            {
090                return true;
091            }
092        }
093        
094        Enumeration params = request.getParameterNames();
095        while (params.hasMoreElements())
096        {
097            String name = (String) params.nextElement();
098            if (!allowedParameters.contains(name) || request.getParameterValues(name).length > 1)
099            {
100                return true;
101            }
102        }
103        
104        return false;
105    }
106}