@@ -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
8080Define controller(s):
8181
8282``` java
8383@Path (" /contacts" )
84+ @Logging
8485public 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