Skip to content

Commit c74c58e

Browse files
committed
Improve Controller section (add Extractors sub-section)
1 parent 83732c5 commit c74c58e

File tree

1 file changed

+95
-0
lines changed

1 file changed

+95
-0
lines changed

_posts/2015-03-17-controller.md

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,101 @@ If you use Maven, your `pom.xml` must contains above lines:
148148

149149
```
150150

151+
#### Extractors
152+
153+
With few words a method parameter extractor create a method parameter value from the `RouteContext` and the `MethodParameter` (an useful wrapper over Parameter class from Java).
154+
155+
For example, see the controller route defined with method:
156+
157+
```java
158+
void uriFor(@Param int id, @Param String action, @Header String host, @Session String user);
159+
```
160+
161+
When a request comes for the above route, Pippo uses extractors to extract values for the method's parameters: `id`, `action`, `host` and `user`.
162+
163+
Pippo comes with some builtin extractors that extract values from multiple locations:
164+
165+
- `Param` extracts value from the request parameter (similar with `request.getParamater()`)
166+
- `Header` extracts value from the request header
167+
- `Session` extracts value from a session's attribute
168+
- `Body` extract value from the request body cia a Content Type Engine (e.g. JSON, XML)
169+
170+
They are also other builtin extractors that are not presented in this post.
171+
The beauty of Pippo here is that you can define any extractor you want/need with no effort.
172+
173+
All you must to do is to implement `MethodParameterExtractor` interface:
174+
175+
```java
176+
public interface MethodParameterExtractor {
177+
178+
/**
179+
* Returns true if this extractor is applicable to the given {@link MethodParameter}.
180+
*/
181+
boolean isApplicable(MethodParameter parameter);
182+
183+
/**
184+
* Extract a value from a {@link MethodParameter} for a specified {@link RouteContext}.
185+
*/
186+
Object extract(MethodParameter parameter, RouteContext routeContext);
187+
188+
}
189+
```
190+
191+
Example, `BeanExtractor` that create a bean from the request parameters:
192+
193+
```java
194+
@MetaInfServices
195+
public class BeanExtractor implements MethodParameterExtractor {
196+
197+
@Override
198+
public boolean isApplicable(MethodParameter parameter) {
199+
return parameter.isAnnotationPresent(Bean.class);
200+
}
201+
202+
@Override
203+
public Object extract(MethodParameter parameter, RouteContext routeContext) {
204+
Class<?> parameterType = parameter.getParameterType();
205+
206+
return routeContext.createEntityFromParameters(parameterType);
207+
}
208+
209+
}
210+
```
211+
212+
After you create the new extractor you should add it to Pippo:
213+
214+
- automatically (annotate your extractor with `@MetaInfServices`)
215+
- manually (via `ControllerApplication.addExtractors(extractor)` method)
216+
217+
You are not forced to create an Extractor using only annotation (it's not annotation driven).
218+
219+
Below, I present you a new extractor, `FileItemExtractor` that does't use annotation (it looks after parameter type).
220+
221+
```java
222+
@MetaInfServices
223+
public class FileItemExtractor implements MethodParameterExtractor {
224+
225+
@Override
226+
public boolean isApplicable(MethodParameter parameter) {
227+
return FileItem.class == parameter.getParameterType();
228+
}
229+
230+
@Override
231+
public Object extract(MethodParameter parameter, RouteContext routeContext) {
232+
String name = parameter.getParameterName();
233+
234+
return routeContext.getRequest().getFile(name);
235+
}
236+
237+
}
238+
```
239+
240+
The signature for a possible controller's route that uses a FileItemExtractor can might be:
241+
242+
```java
243+
void upload(FileItem file);
244+
```
245+
151246
#### Parameter name
152247

153248
The Controller module may depend on the `-parameters` flag of the Java 8 javac compiler. This flag embeds the names of method parameters in the generated .class files.

0 commit comments

Comments
 (0)