ingo@100: /* ingo@100: * Copyright (c) 2010 by Intevation GmbH ingo@100: * ingo@100: * This program is free software under the LGPL (>=v2.1) ingo@100: * Read the file LGPL.txt coming with the software for details ingo@100: * or visit http://www.gnu.org/licenses/ if it does not exist. ingo@100: */ ingo@100: sascha@40: package de.intevation.artifactdatabase.rest; sascha@40: sascha@93: import de.intevation.artifactdatabase.DefaultCallMeta; sascha@93: import de.intevation.artifactdatabase.DefaultPreferredLocale; sascha@93: ingo@131: import de.intevation.artifacts.ArtifactDatabase; sascha@48: import de.intevation.artifacts.CallMeta; sascha@48: import de.intevation.artifacts.PreferredLocale; sascha@48: sascha@93: import java.util.Iterator; sascha@93: import java.util.List; sascha@93: sascha@40: import org.apache.log4j.Logger; sascha@40: sascha@93: import org.restlet.data.ClientInfo; sascha@93: import org.restlet.data.Language; sascha@93: import org.restlet.data.Preference; sascha@40: sascha@40: import org.restlet.representation.Representation; sascha@40: sascha@93: import org.restlet.resource.ResourceException; sascha@93: import org.restlet.resource.ServerResource; sascha@48: sascha@40: /** sascha@88: * Base class for the resources of REST interface of the artifact database. sascha@88: * Primarily used to unify the logging. sascha@77: * @author Sascha L. Teichmann sascha@40: */ sascha@40: public class BaseResource sascha@40: extends ServerResource sascha@40: { sascha@40: private static Logger logger = Logger.getLogger(BaseResource.class); sascha@40: sascha@88: /** sascha@88: * Default constructor. sascha@88: */ sascha@40: public BaseResource() { sascha@40: } sascha@40: sascha@88: /** sascha@88: * Overrides the post method of ServerResource to handle some sascha@88: * exceptions and to the required logging. sascha@88: * The call bridges to #innerPost(Representation) which sascha@88: * should be overwitten by the subclasses to do the real sascha@88: * request processing. sascha@88: * @param requestRepr The incoming represention of the HTTP request. sascha@88: * @return The representation produced by #innerPost(Representation). sascha@88: * @throws ResourceException Thrown if something went wrong during sascha@88: * request processing. sascha@88: */ sascha@88: @Override sascha@40: protected Representation post(Representation requestRepr) sascha@40: throws ResourceException sascha@40: { sascha@40: try { sascha@40: return innerPost(requestRepr); sascha@40: } sascha@40: catch (ResourceException re) { sascha@40: throw re; sascha@40: } sascha@40: catch (RuntimeException re) { sascha@40: logger.error(re.getLocalizedMessage(), re); sascha@40: throw re; sascha@40: } sascha@40: } sascha@40: sascha@88: /** sascha@88: * Trivial implementation of innerPost() which is called by sascha@88: * #post(Representation) which simply calls super.post(Representation). sascha@88: * This should be overwritten by subclasses which need POST support. sascha@88: * @param requestRepr The incoming representation of the request. sascha@88: * @return The representation produced by super.post(Representation). sascha@88: * @throws ResourceException Thrown if something went wrong during sascha@88: * request processing. sascha@88: */ sascha@40: protected Representation innerPost(Representation requestRepr) sascha@40: throws ResourceException sascha@40: { sascha@40: return super.post(requestRepr); sascha@40: } sascha@40: sascha@88: /** sascha@88: * Wrapper around get() of the super class to handle some exceptions sascha@88: * and do the corresponing logging. The call is bridged to #innerGet() sascha@88: * which should be overwritten by subclasses. sascha@88: * @return The representation produced by #innerGet() sascha@88: * @throws ResourceException Thrown if something went wrong during sascha@88: * request processing. sascha@88: */ sascha@88: @Override sascha@47: protected Representation get() sascha@40: throws ResourceException sascha@40: { sascha@40: try { sascha@40: return innerGet(); sascha@40: } sascha@40: catch (ResourceException re) { sascha@40: throw re; sascha@40: } sascha@40: catch (RuntimeException re) { sascha@40: logger.error(re.getLocalizedMessage(), re); sascha@40: throw re; sascha@40: } sascha@40: } sascha@40: sascha@88: /** sascha@88: * Trivial implementaion of innerGet() which simply calls sascha@88: * super.get() to produce some output representation. This method sascha@88: * should be overwritten by subclasses which need GET support. sascha@88: * @return The representation produced by super.get(). sascha@88: * @throws ResourceException Thrown if something went wrong during sascha@88: * request processing. sascha@88: */ sascha@40: protected Representation innerGet() sascha@40: throws ResourceException sascha@40: { sascha@40: return super.get(); sascha@40: } sascha@48: sascha@88: /** sascha@88: * Returns meta information (preferred languages et. al.) sascha@88: * of the current HTTP request. sascha@88: * @return the meta information sascha@88: */ sascha@48: protected CallMeta getCallMeta() { sascha@48: ClientInfo clientInfo = getClientInfo(); sascha@48: sascha@48: List> pl = clientInfo.getAcceptedLanguages(); sascha@48: sascha@48: PreferredLocale [] languages = new PreferredLocale[pl.size()]; sascha@48: sascha@48: int index = 0; sascha@48: sascha@94: for (Preference p: pl) { sascha@48: String lang = p.getMetadata().getName(); sascha@48: float quality = p.getQuality(); sascha@86: languages[index++] = new DefaultPreferredLocale(lang, quality); sascha@48: } sascha@48: sascha@48: return new DefaultCallMeta(languages); sascha@48: } ingo@131: ingo@131: ingo@131: /** ingo@131: * Returns the artifact database stored in the context of the REST ingo@131: * application. ingo@131: * ingo@131: * @return the artifact database. ingo@131: */ ingo@131: protected ArtifactDatabase getArtifactDatabase() { ingo@131: return (ArtifactDatabase) getContext().getAttributes().get("database"); ingo@131: } sascha@40: } sascha@88: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :