view artifact-database/src/main/java/de/intevation/artifactdatabase/rest/BaseResource.java @ 83:8c4638abd518

Installed a SLF4J bridge to route incoming jul logs (used by restlet) to log4j. artifacts/trunk@820 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Mon, 22 Mar 2010 15:54:06 +0000
parents 48d1a9a082c2
children b2e0cb83631c
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 <a href="mailto:sascha.teichmann@intevation">Sascha L. Teichmann</a>
 */
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