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

Unified imports. artifacts/trunk@849 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Sun, 28 Mar 2010 15:57:40 +0000
parents 69c84cf7c5d7
children 5332d956729c
line wrap: on
line source
package de.intevation.artifactdatabase.rest;

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

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

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

import org.apache.log4j.Logger;

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

import org.restlet.representation.Representation;

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

/**
 * Base class for the resources of REST interface of the artifact database.
 * Primarily used to unify the logging.
 * @author <a href="mailto:sascha.teichmann@intevation">Sascha L. Teichmann</a>
 */
public class BaseResource
extends      ServerResource
{
    private static Logger logger = Logger.getLogger(BaseResource.class);

    /**
     * Default constructor.
     */
    public BaseResource() {
    }

    /**
     * Overrides the post method of ServerResource to handle some
     * exceptions and to the required logging.
     * The call bridges to #innerPost(Representation) which
     * should be overwitten by the subclasses to do the real
     * request processing.
     * @param requestRepr The incoming represention of the HTTP request.
     * @return The representation produced by #innerPost(Representation).
     * @throws ResourceException Thrown if something went wrong during
     * request processing.
     */
    @Override
    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;
        }
    }

    /**
     * Trivial implementation of innerPost() which is called by
     * #post(Representation) which simply calls super.post(Representation).
     * This should be overwritten by subclasses which need POST support.
     * @param requestRepr The incoming representation of the request.
     * @return The representation produced by super.post(Representation).
     * @throws ResourceException Thrown if something went wrong during
     * request processing.
     */
    protected Representation innerPost(Representation requestRepr)
    throws    ResourceException
    {
        return super.post(requestRepr);
    }

    /**
     * Wrapper around get() of the super class to handle some exceptions
     * and do the corresponing logging. The call is bridged to #innerGet()
     * which should be overwritten by subclasses.
     * @return The representation produced by #innerGet()
     * @throws ResourceException Thrown if something went wrong during
     * request processing.
     */
    @Override
    protected Representation get()
    throws    ResourceException
    {
        try {
            return innerGet();
        }
        catch (ResourceException re) {
            throw re;
        }
        catch (RuntimeException re) {
            logger.error(re.getLocalizedMessage(), re);
            throw re;
        }
    }

    /**
     * Trivial implementaion of innerGet() which simply calls
     * super.get() to produce some output representation. This method
     * should be overwritten by subclasses which need GET support.
     * @return The representation produced by super.get().
     * @throws ResourceException Thrown if something went wrong during
     * request processing.
     */
    protected Representation innerGet()
    throws    ResourceException
    {
        return super.get();
    }

    /**
     * Returns meta information (preferred languages et. al.)
     * of the current HTTP request.
     * @return the meta information
     */
    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