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.Arrays;
020import java.util.Collection;
021import java.util.Set;
022
023import org.apache.avalon.framework.service.ServiceException;
024import org.apache.avalon.framework.service.ServiceManager;
025import org.apache.avalon.framework.service.Serviceable;
026import org.apache.cocoon.ProcessingException;
027import org.apache.cocoon.environment.ObjectModelHelper;
028import org.apache.cocoon.environment.Request;
029import org.apache.cocoon.xml.XMLUtils;
030import org.apache.commons.lang.StringUtils;
031import org.xml.sax.SAXException;
032
033import org.ametys.runtime.i18n.I18nizableText;
034
035/**
036 * <code>ListSets</code> generator.
037 */
038public class ListSetsGenerator extends AbstractOAIVerbGenerator implements Serviceable
039{
040    private OaiSetExtensionPoint _oaiSetEP;
041
042    public void service(ServiceManager serviceManager) throws ServiceException
043    {
044        _oaiSetEP = (OaiSetExtensionPoint) serviceManager.lookup(OaiSetExtensionPoint.ROLE);
045    }
046    
047    @Override
048    protected Collection<String> getRequiredParameters()
049    {
050        return Arrays.asList("verb");
051    }
052
053    @Override
054    protected Collection<String> getAllowedParameters()
055    {
056        return Arrays.asList("verb", "resumptionToken");
057    }
058
059    @Override
060    protected void generateVerb() throws IOException, SAXException, ProcessingException
061    {
062        Request request = ObjectModelHelper.getRequest(objectModel);
063
064        String token = request.getParameter("resumptionToken");
065        
066        if (StringUtils.isNotEmpty(token))
067        {
068            generateError("badResumptionToken ", "This repository does not support resumption tokens");
069            return;
070        }
071        
072        Set<String> extensionsIds = _oaiSetEP.getExtensionsIds();
073        
074        XMLUtils.startElement(contentHandler, "ListSets");
075        
076        for (String extensionId : extensionsIds)
077        {
078            OaiSet oaiSet = _oaiSetEP.getExtension(extensionId);
079            
080            XMLUtils.startElement(contentHandler, "set");
081            
082            XMLUtils.createElement(contentHandler, "setSpec", extensionId);
083            oaiSet.getName().toSAX(contentHandler, "setName");
084            
085            I18nizableText description = oaiSet.getDescription();
086            if (description != null)
087            {
088                description.toSAX(contentHandler, "setDescription");
089            }
090            
091            XMLUtils.endElement(contentHandler, "set");
092        }
093        
094        XMLUtils.endElement(contentHandler, "ListSets");
095    }
096
097}