Skip to content

Commit f7bb41b

Browse files
committed
Update info Controllers
1 parent 67a6a40 commit f7bb41b

File tree

2 files changed

+190
-54
lines changed

2 files changed

+190
-54
lines changed

_posts/2015-03-17-controller.md

Lines changed: 83 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,28 +15,94 @@ Defining a new controller is simple:
1515

1616
```java
1717
@Path("/contacts")
18+
@Logging
1819
public class ContactsController extends Controller {
1920

20-
@GET("/?")
21-
public void index() {
22-
List<Contact> contacts = contactService.getContacts();
23-
getResponse().bind("contacts", contacts).render("contacts");
21+
private ContactService contactService;
22+
23+
public ContactsController() {
24+
contactService = new InMemoryContactService();
2425
}
25-
26-
@GET("/{id: [0-9]+}")
27-
public void getContact(@Param int id) {
28-
Contact contact = contactService.getContact(id);
29-
getResponse().bind("contact", contact).render(contact);
26+
27+
@GET
28+
@Named("index")
29+
// @Produces(Produces.HTML)
30+
@Metered
31+
@Logging
32+
public void index() {
33+
// inject "user" attribute in session
34+
getRouteContext().setSession("user", "decebal");
35+
36+
// send a template with name "contacts" as response
37+
getResponse()
38+
.bind("contacts", contactService.getContacts())
39+
.render("contacts");
3040
}
3141

32-
@GET("/text")
33-
@Named("text")
42+
@GET("/uriFor/{id: [0-9]+}")
43+
@Named("uriFor")
3444
@Produces(Produces.TEXT)
45+
@Timed
46+
public String uriFor(@Param int id, @Header String host, @Session String user) {
47+
System.out.println("id = " + id);
48+
System.out.println("host = " + host);
49+
System.out.println("user = " + user);
50+
51+
Map<String, Object> parameters = new HashMap<>();
52+
parameters.put("id", id);
53+
54+
String uri = getApplication().getRouter().uriFor("api.get", parameters);
55+
56+
return "id = " + id + "; uri = " + uri;
57+
}
58+
59+
@GET("/api")
60+
@Named("api.getAll")
61+
@Produces(Produces.JSON)
3562
@NoCache
36-
public void complex(@Param int id, @Param String action, @Header String host, @Session String user) {
37-
// do something
63+
public List<Contact> getAll() {
64+
return contactService.getContacts();
65+
}
66+
67+
@GET("/api/{id: [0-9]+}")
68+
@Named("api.get")
69+
@Produces(Produces.JSON)
70+
public Contact get(@Param int id) {
71+
return contactService.getContact(id);
72+
}
73+
74+
}
75+
```
76+
77+
```java
78+
@Path("/files")
79+
public class FilesController extends Controller {
80+
81+
@GET
82+
public void index() {
83+
// send a template with name "files" as response
84+
getRouteContext().render("files");
3885
}
39-
86+
87+
@GET("/download")
88+
public File download() {
89+
// send a file as response
90+
return new File("pom.xml");
91+
}
92+
93+
@POST("/upload")
94+
@Produces(Produces.TEXT)
95+
public String upload(FileItem file) {
96+
// send a text (the info about uploaded file) as response
97+
// return file.toString();
98+
return new StringBuilder()
99+
.append(file.getName()).append("\n")
100+
.append(file.getSubmittedFileName()).append("\n")
101+
.append(file.getSize()).append("\n")
102+
.append(file.getContentType())
103+
.toString();
104+
}
105+
40106
}
41107
```
42108

@@ -51,7 +117,9 @@ public class MyApplication extends ControllerApplication {
51117
// add controller(s)
52118
addControllers(ContactsController.class); // one instance for EACH request
53119
// or
54-
addControllers(new ContactsController()); // one instance for ALL requests
120+
addControllers(new ContactsController()); // one instance for ALL requests
121+
122+
addControllers(FilesController.class);
55123
}
56124

57125
}

index.md

Lines changed: 107 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -33,76 +33,142 @@ public class BasicApplication extends Application {
3333

3434
@Override
3535
protected void onInit() {
36-
// send 'Hello World' as response
36+
// send 'Hello World' as response
3737
GET("/", routeContext -> routeContext.send("Hello World"));
3838

39-
// send a file as response
40-
GET("/file", routeContext -> routeContext.send(new File("pom.xml"));
39+
// send a file as response
40+
GET("/file", routeContext -> routeContext.send(new File("pom.xml")));
4141

4242
// send a json as response
4343
GET("/json", routeContext -> {
44-
Contact contact = createContact();
45-
routeContext.json().send(contact);
44+
Contact contact = createContact();
45+
routeContext.json().send(contact);
4646
});
4747

4848
// send xml as response
4949
GET("/xml", routeContext -> {
50-
Contact contact = createContact();
51-
routeContext.xml().send(contact);
50+
Contact contact = createContact();
51+
routeContext.xml().send(contact);
5252
});
53-
53+
5454
// send an object and negotiate the Response content-type, default to XML
5555
GET("/negotiate", routeContext -> {
56-
Contact contact = createContact();
57-
routeContext.xml().negotiateContentType().send(contact);
56+
Contact contact = createContact();
57+
routeContext.xml().negotiateContentType().send(contact);
5858
});
59-
60-
// send a template as response
59+
60+
// send a template with name "hello" as response
6161
GET("/template", routeContext -> {
62-
routeContext.setLocal("greeting", "Hello");
63-
routeContext.render("hello");
64-
});
62+
routeContext.setLocal("greeting", "Hello");
63+
routeContext.render("hello");
64+
});
65+
}
66+
67+
private Contact createContact() {
68+
return new Contact()
69+
.setId(12345)
70+
.setName("John")
71+
.setPhone("0733434435")
72+
.setAddress("Sunflower Street, No. 6");
6573
}
6674

67-
private Contact createContact() {
68-
return new Contact()
69-
.setId(12345)
70-
.setName("John")
71-
.setPhone("0733434435")
72-
.setAddress("Sunflower Street, No. 6");
73-
}
74-
7575
}
76-
```
76+
```
7777

7878
#### 1.2 Controllers approach
7979

8080
Define controller(s):
8181

8282
```java
8383
@Path("/contacts")
84+
@Logging
8485
public class ContactsController extends Controller {
8586

86-
@GET("/?")
87-
public void index() {
88-
List<Contact> contacts = contactService.getContacts();
89-
getResponse().bind("contacts", contacts).render("contacts");
87+
private ContactService contactService;
88+
89+
public ContactsController() {
90+
contactService = new InMemoryContactService();
9091
}
91-
92-
@GET("/{id: [0-9]+}")
93-
public void getContact(@Param int id) {
94-
Contact contact = contactService.getContact(id);
95-
getResponse().bind("contact", contact).render(contact);
92+
93+
@GET
94+
@Named("index")
95+
// @Produces(Produces.HTML)
96+
@Metered
97+
@Logging
98+
public void index() {
99+
// inject "user" attribute in session
100+
getRouteContext().setSession("user", "decebal");
101+
102+
// send a template with name "contacts" as response
103+
getResponse()
104+
.bind("contacts", contactService.getContacts())
105+
.render("contacts");
96106
}
97107

98-
@GET("/text")
99-
@Named("text")
108+
@GET("/uriFor/{id: [0-9]+}")
109+
@Named("uriFor")
100110
@Produces(Produces.TEXT)
111+
@Timed
112+
public String uriFor(@Param int id, @Header String host, @Session String user) {
113+
System.out.println("id = " + id);
114+
System.out.println("host = " + host);
115+
System.out.println("user = " + user);
116+
117+
Map<String, Object> parameters = new HashMap<>();
118+
parameters.put("id", id);
119+
120+
String uri = getApplication().getRouter().uriFor("api.get", parameters);
121+
122+
return "id = " + id + "; uri = " + uri;
123+
}
124+
125+
@GET("/api")
126+
@Named("api.getAll")
127+
@Produces(Produces.JSON)
101128
@NoCache
102-
public void complex(@Param int id, @Param String action, @Header String host, @Session String user) {
103-
// do something
129+
public List<Contact> getAll() {
130+
return contactService.getContacts();
104131
}
105-
132+
133+
@GET("/api/{id: [0-9]+}")
134+
@Named("api.get")
135+
@Produces(Produces.JSON)
136+
public Contact get(@Param int id) {
137+
return contactService.getContact(id);
138+
}
139+
140+
}
141+
```
142+
143+
```java
144+
@Path("/files")
145+
public class FilesController extends Controller {
146+
147+
@GET
148+
public void index() {
149+
// send a template with name "files" as response
150+
getRouteContext().render("files");
151+
}
152+
153+
@GET("/download")
154+
public File download() {
155+
// send a file as response
156+
return new File("pom.xml");
157+
}
158+
159+
@POST("/upload")
160+
@Produces(Produces.TEXT)
161+
public String upload(FileItem file) {
162+
// send a text (the info about uploaded file) as response
163+
// return file.toString();
164+
return new StringBuilder()
165+
.append(file.getName()).append("\n")
166+
.append(file.getSubmittedFileName()).append("\n")
167+
.append(file.getSize()).append("\n")
168+
.append(file.getContentType())
169+
.toString();
170+
}
171+
106172
}
107173
```
108174

@@ -115,7 +181,9 @@ public class BasicApplication extends ControllerApplication {
115181
protected void onInit() {
116182
addControllers(ContactsController.class); // one instance for EACH request
117183
// OR
118-
addControllers(new ContactsController()); // one instance for ALL requests
184+
addControllers(new ContactsController()); // one instance for ALL requests
185+
186+
addControllers(FilesController.class);
119187
}
120188

121189
}

0 commit comments

Comments
 (0)