Skip to content

Commit d4865f0

Browse files
committed
move local attributes from response to request fix jooby-project#7
1 parent f3ddef7 commit d4865f0

File tree

11 files changed

+282
-152
lines changed

11 files changed

+282
-152
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package org.jooby;
2+
3+
import java.util.Map;
4+
import java.util.Optional;
5+
6+
import javax.annotation.Nonnull;
7+
8+
/**
9+
* Local attributes for {@link Request} and {@link Session}.
10+
*
11+
* @author edgar
12+
*/
13+
public interface Locals {
14+
15+
/**
16+
* Get a named object. If the object isn't found this method returns an empty
17+
* optional.
18+
*
19+
* @param name A local var's name.
20+
* @param <T> Target type.
21+
* @return A value or empty optional.
22+
*/
23+
@Nonnull
24+
<T> Optional<T> get(final @Nonnull String name);
25+
26+
/**
27+
* @return An immutable copy of local attributes.
28+
*/
29+
@Nonnull
30+
Map<String, Object> attributes();
31+
32+
/**
33+
* Test if the var name exists inside the local attributes.
34+
*
35+
* @param name A local var's name.
36+
* @return True, for existing locals.
37+
*/
38+
default boolean isSet(final @Nonnull String name) {
39+
return get(name).isPresent();
40+
}
41+
42+
/**
43+
* Set a local using a the given name. If a local already exists, it will be replaced
44+
* with the new value. Keep in mind that ONLY none null values are allowed.
45+
*
46+
* @param name A local var's name.
47+
* @param value A local values.
48+
* @return This locals.
49+
*/
50+
@Nonnull Locals set(final @Nonnull String name, final @Nonnull Object value);
51+
52+
/**
53+
* Remove a local value (if any) from session locals.
54+
*
55+
* @param name A local var's name.
56+
* @param <T> A local type.
57+
* @return Existing value or empty optional.
58+
*/
59+
<T> Optional<T> unset(final String name);
60+
61+
/**
62+
* Unset/remove all the session data.
63+
*
64+
* @return This locals.
65+
*/
66+
Locals unset();
67+
68+
}

jooby/src/main/java/org/jooby/Request.java

Lines changed: 66 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
* @author edgar
4040
* @since 0.1.0
4141
*/
42-
public interface Request {
42+
public interface Request extends Locals {
4343

4444
/**
4545
* Forwarding request.
@@ -50,165 +50,191 @@ public interface Request {
5050
class Forwarding implements Request {
5151

5252
/** Target request. */
53-
private Request request;
53+
private Request req;
5454

5555
/**
5656
* Creates a new {@link Forwarding} request.
5757
*
5858
* @param request A target request.
5959
*/
6060
public Forwarding(final @Nonnull Request request) {
61-
this.request = requireNonNull(request, "A HTTP request is required.");
61+
this.req = requireNonNull(request, "A HTTP request is required.");
6262
}
6363

6464
@Override
6565
public String path() {
66-
return request.path();
66+
return req.path();
6767
}
6868

6969
@Override
7070
public Verb verb() {
71-
return request.verb();
71+
return req.verb();
7272
}
7373

7474
@Override
7575
public MediaType type() {
76-
return request.type();
76+
return req.type();
7777
}
7878

7979
@Override
8080
public List<MediaType> accept() {
81-
return request.accept();
81+
return req.accept();
8282
}
8383

8484
@Override
8585
public Optional<MediaType> accepts(final List<MediaType> types) {
86-
return request.accepts(types);
86+
return req.accepts(types);
8787
}
8888

8989
@Override
9090
public Optional<MediaType> accepts(final MediaType... types) {
91-
return request.accepts(types);
91+
return req.accepts(types);
9292
}
9393

9494
@Override
9595
public Optional<MediaType> accepts(final String... types) {
96-
return request.accepts(types);
96+
return req.accepts(types);
9797
}
9898

9999
@Override
100100
public Map<String, Mutant> params() throws Exception {
101-
return request.params();
101+
return req.params();
102102
}
103103

104104
@Override
105105
public Mutant param(final String name) throws Exception {
106-
return request.param(name);
106+
return req.param(name);
107107
}
108108

109109
@Override
110110
public Mutant header(final String name) {
111-
return request.header(name);
111+
return req.header(name);
112112
}
113113

114114
@Override
115115
public Map<String, Mutant> headers() {
116-
return request.headers();
116+
return req.headers();
117117
}
118118

119119
@Override
120120
public Optional<Cookie> cookie(final String name) {
121-
return request.cookie(name);
121+
return req.cookie(name);
122122
}
123123

124124
@Override
125125
public List<Cookie> cookies() {
126-
return request.cookies();
126+
return req.cookies();
127127
}
128128

129129
@Override
130130
public <T> T body(final TypeLiteral<T> type) throws Exception {
131-
return request.body(type);
131+
return req.body(type);
132132
}
133133

134134
@Override
135135
public <T> T body(final Class<T> type) throws Exception {
136-
return request.body(type);
136+
return req.body(type);
137137
}
138138

139139
@Override
140140
public <T> T getInstance(final Class<T> type) {
141-
return request.getInstance(type);
141+
return req.getInstance(type);
142142
}
143143

144144
@Override
145145
public <T> T getInstance(final TypeLiteral<T> type) {
146-
return request.getInstance(type);
146+
return req.getInstance(type);
147147
}
148148

149149
@Override
150150
public <T> T getInstance(final Key<T> key) {
151-
return request.getInstance(key);
151+
return req.getInstance(key);
152152
}
153153

154154
@Override
155155
public Charset charset() {
156-
return request.charset();
156+
return req.charset();
157157
}
158158

159159
@Override
160160
public long length() {
161-
return request.length();
161+
return req.length();
162162
}
163163

164164
@Override
165165
public Locale locale() {
166-
return request.locale();
166+
return req.locale();
167167
}
168168

169169
@Override
170170
public String ip() {
171-
return request.ip();
171+
return req.ip();
172172
}
173173

174174
@Override
175175
public Route route() {
176-
return request.route();
176+
return req.route();
177177
}
178178

179179
@Override
180180
public Session session() {
181-
return request.session();
181+
return req.session();
182182
}
183183

184184
@Override
185185
public Optional<Session> ifSession() {
186-
return request.ifSession();
186+
return req.ifSession();
187187
}
188188

189189
@Override
190190
public String hostname() {
191-
return request.hostname();
191+
return req.hostname();
192192
}
193193

194194
@Override
195195
public String protocol() {
196-
return request.protocol();
196+
return req.protocol();
197197
}
198198

199199
@Override
200200
public boolean secure() {
201-
return request.secure();
201+
return req.secure();
202202
}
203203

204204
@Override
205205
public boolean xhr() {
206-
return request.xhr();
206+
return req.xhr();
207+
}
208+
209+
@Override
210+
public Map<String, Object> attributes() {
211+
return req.attributes();
212+
}
213+
214+
@Override
215+
public <T> Optional<T> get(final String name) {
216+
return req.get(name);
217+
}
218+
219+
@Override
220+
public Request set(final String name, final Object value) {
221+
req.set(name, value);
222+
return this;
223+
}
224+
225+
@Override
226+
public Request unset() {
227+
return this;
228+
}
229+
230+
@Override
231+
public <T> Optional<T> unset(final String name) {
232+
return req.unset(name);
207233
}
208234

209235
@Override
210236
public String toString() {
211-
return request.toString();
237+
return req.toString();
212238
}
213239

214240
/**
@@ -221,7 +247,7 @@ public static Request unwrap(final @Nonnull Request req) {
221247
requireNonNull(req, "A request is required.");
222248
Request root = req;
223249
while (root instanceof Forwarding) {
224-
root = ((Forwarding) root).request;
250+
root = ((Forwarding) root).req;
225251
}
226252
return root;
227253
}
@@ -623,4 +649,10 @@ default boolean xhr() {
623649
*/
624650
boolean secure();
625651

652+
@Override
653+
Request set(String name, Object value);
654+
655+
@Override
656+
Request unset();
657+
626658
}

jooby/src/main/java/org/jooby/Response.java

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import java.io.Reader;
3030
import java.nio.charset.Charset;
3131
import java.util.Date;
32-
import java.util.Map;
3332
import java.util.Optional;
3433

3534
import javax.annotation.Nonnull;
@@ -195,22 +194,6 @@ public Response length(final int length) {
195194
return this;
196195
}
197196

198-
@Override
199-
public <T> T local(final String name) {
200-
return response.local(name);
201-
}
202-
203-
@Override
204-
public Response local(final String name, final Object value) {
205-
response.local(name, value);
206-
return this;
207-
}
208-
209-
@Override
210-
public Map<String, Object> locals() {
211-
return response.locals();
212-
}
213-
214197
@Override
215198
public Optional<MediaType> type() {
216199
return response.type();
@@ -837,32 +820,4 @@ default Response status(final int status) {
837820
*/
838821
boolean committed();
839822

840-
/**
841-
* Response local variables are scoped to the request, and therefore only available to the view(s)
842-
* rendered during that request / response cycle.
843-
*
844-
* @return Immutable version of local variables.
845-
*/
846-
@Nonnull
847-
Map<String, Object> locals();
848-
849-
/**
850-
* Get a local variable by it's name.
851-
*
852-
* @param name A var's name
853-
* @param <T> A local type.
854-
* @return A local.
855-
*/
856-
@Nonnull
857-
<T> T local(@Nonnull String name);
858-
859-
/**
860-
* Put a local using a var's name. It override any existing local.
861-
*
862-
* @param name A var's name
863-
* @param value A var's value.
864-
* @return This response.
865-
*/
866-
Response local(@Nonnull String name, @Nonnull Object value);
867-
868823
}

0 commit comments

Comments
 (0)