view artifact-database/src/main/java/de/intevation/artifactdatabase/rest/BaseResource.java @ 72:b01e47317a78

Added '/services' url to web server to list the available services in XML. artifacts/trunk@600 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Fri, 22 Jan 2010 11:40:26 +0000
parents 41c225c8bd41
children 48d1a9a082c2
line wrap: on
line source
package de.intevation.artifactdatabase.rest;

import de.intevation.artifacts.CallMeta;
import de.intevation.artifacts.PreferredLocale;

import org.apache.log4j.Logger;

import org.restlet.resource.ServerResource;
import org.restlet.resource.ResourceException;

import org.restlet.representation.Representation;

import org.restlet.data.Language;
import org.restlet.data.ClientInfo;
import org.restlet.data.Preference;

import java.util.List;
import java.util.Iterator;

import de.intevation.artifactdatabase.DefaultCallMeta;
import de.intevation.artifactdatabase.DefaultPreferredLocale;

/**
 * @author Sascha L. Teichmann (sascha.teichmann@intevation)
 */
public class BaseResource
extends      ServerResource
{
    private static Logger logger = Logger.getLogger(BaseResource.class);

    public BaseResource() {
    }

    protected Representation post(Representation requestRepr)
    throws    ResourceException
    {
        try {
            return innerPost(requestRepr);
        }
        catch (ResourceException re) {
            throw re;
        }
        catch (RuntimeException re) {
            logger.error(re.getLocalizedMessage(), re);
            throw re;
        }
    }

    protected Representation innerPost(Representation requestRepr)
    throws    ResourceException
    {
        return super.post(requestRepr);
    }

    protected Representation get()
    throws    ResourceException
    {
        try {
            return innerGet();
        }
        catch (ResourceException re) {
            throw re;
        }
        catch (RuntimeException re) {
            logger.error(re.getLocalizedMessage(), re);
            throw re;
        }
    }

    protected Representation innerGet()
    throws    ResourceException
    {
        return super.get();
    }

    protected CallMeta getCallMeta() {
        ClientInfo clientInfo = getClientInfo();

        List<Preference<Language>> pl = clientInfo.getAcceptedLanguages();

        PreferredLocale [] languages = new PreferredLocale[pl.size()];

        int index = 0;

        for (Iterator<Preference<Language>> iter = pl.iterator(); iter.hasNext();) {
            Preference p = iter.next();
            String lang    = p.getMetadata().getName();
            float  quality = p.getQuality();
            languages[index++] = new DefaultPreferredLocale(lang, quality); 
        }

        return new DefaultCallMeta(languages);
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8:

http://dive4elements.wald.intevation.org