comparison artifact-database/src/main/java/org/dive4elements/artifactdatabase/rest/BaseResource.java @ 473:d0ac790a6c89 dive4elements-move

Moved directories to org.dive4elements
author Sascha L. Teichmann <teichmann@intevation.de>
date Thu, 25 Apr 2013 10:57:18 +0200
parents artifact-database/src/main/java/de/intevation/artifactdatabase/rest/BaseResource.java@320a81983c8d
children 415df0fc4fa1
comparison
equal deleted inserted replaced
472:783cc1b6b615 473:d0ac790a6c89
1 /*
2 * Copyright (c) 2010 by Intevation GmbH
3 *
4 * This program is free software under the LGPL (>=v2.1)
5 * Read the file LGPL.txt coming with the software for details
6 * or visit http://www.gnu.org/licenses/ if it does not exist.
7 */
8
9 package de.intevation.artifactdatabase.rest;
10
11 import de.intevation.artifactdatabase.DefaultCallMeta;
12 import de.intevation.artifactdatabase.DefaultPreferredLocale;
13
14 import de.intevation.artifacts.ArtifactDatabase;
15 import de.intevation.artifacts.CallMeta;
16 import de.intevation.artifacts.PreferredLocale;
17
18 import java.util.List;
19
20 import org.apache.log4j.Logger;
21
22 import org.restlet.data.ClientInfo;
23 import org.restlet.data.Language;
24 import org.restlet.data.Preference;
25
26 import org.restlet.representation.Representation;
27
28 import org.restlet.resource.ResourceException;
29 import org.restlet.resource.ServerResource;
30
31 /**
32 * Base class for the resources of REST interface of the artifact database.
33 * Primarily used to unify the logging.
34 * @author <a href="mailto:sascha.teichmann@intevation">Sascha L. Teichmann</a>
35 */
36 public class BaseResource
37 extends ServerResource
38 {
39 private static Logger logger = Logger.getLogger(BaseResource.class);
40
41 /**
42 * Default constructor.
43 */
44 public BaseResource() {
45 }
46
47 /**
48 * Overrides the post method of ServerResource to handle some
49 * exceptions and to the required logging.
50 * The call bridges to #innerPost(Representation) which
51 * should be overwitten by the subclasses to do the real
52 * request processing.
53 * @param requestRepr The incoming represention of the HTTP request.
54 * @return The representation produced by #innerPost(Representation).
55 * @throws ResourceException Thrown if something went wrong during
56 * request processing.
57 */
58 @Override
59 protected Representation post(Representation requestRepr)
60 throws ResourceException
61 {
62 try {
63 return innerPost(requestRepr);
64 }
65 catch (ResourceException re) {
66 throw re;
67 }
68 catch (RuntimeException re) {
69 logger.error(re.getLocalizedMessage(), re);
70 throw re;
71 }
72 }
73
74 /**
75 * Trivial implementation of innerPost() which is called by
76 * #post(Representation) which simply calls super.post(Representation).
77 * This should be overwritten by subclasses which need POST support.
78 * @param requestRepr The incoming representation of the request.
79 * @return The representation produced by super.post(Representation).
80 * @throws ResourceException Thrown if something went wrong during
81 * request processing.
82 */
83 protected Representation innerPost(Representation requestRepr)
84 throws ResourceException
85 {
86 return super.post(requestRepr);
87 }
88
89 /**
90 * Wrapper around get() of the super class to handle some exceptions
91 * and do the corresponing logging. The call is bridged to #innerGet()
92 * which should be overwritten by subclasses.
93 * @return The representation produced by #innerGet()
94 * @throws ResourceException Thrown if something went wrong during
95 * request processing.
96 */
97 @Override
98 protected Representation get()
99 throws ResourceException
100 {
101 try {
102 return innerGet();
103 }
104 catch (ResourceException re) {
105 throw re;
106 }
107 catch (RuntimeException re) {
108 logger.error(re.getLocalizedMessage(), re);
109 throw re;
110 }
111 }
112
113 /**
114 * Trivial implementaion of innerGet() which simply calls
115 * super.get() to produce some output representation. This method
116 * should be overwritten by subclasses which need GET support.
117 * @return The representation produced by super.get().
118 * @throws ResourceException Thrown if something went wrong during
119 * request processing.
120 */
121 protected Representation innerGet()
122 throws ResourceException
123 {
124 return super.get();
125 }
126
127 /**
128 * Returns meta information (preferred languages et. al.)
129 * of the current HTTP request.
130 * @return the meta information
131 */
132 protected CallMeta getCallMeta() {
133 ClientInfo clientInfo = getClientInfo();
134
135 List<Preference<Language>> pl = clientInfo.getAcceptedLanguages();
136
137 PreferredLocale [] languages = new PreferredLocale[pl.size()];
138
139 int index = 0;
140
141 for (Preference<Language> p: pl) {
142 String lang = p.getMetadata().getName();
143 float quality = p.getQuality();
144 languages[index++] = new DefaultPreferredLocale(lang, quality);
145 }
146
147 return new DefaultCallMeta(languages);
148 }
149
150
151 /**
152 * Returns the artifact database stored in the context of the REST
153 * application.
154 *
155 * @return the artifact database.
156 */
157 protected ArtifactDatabase getArtifactDatabase() {
158 return (ArtifactDatabase) getContext().getAttributes().get("database");
159 }
160 }
161 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org