Skip to content

Commit bb2d798

Browse files
author
Jerome Louvel
committed
- Renamed Directory#setAlphaComparator and setAlphaNumComparator
methods into useAlphaComparator and useAlphaNumComparator with proper deprecation. Suggested by Tim Peierls. - Updated book examples
1 parent dd7b6c8 commit bb2d798

13 files changed

Lines changed: 95 additions & 71 deletions

File tree

build/tmpl/text/changes.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ Changes log
66
- @version-full@ (@release-date@)
77
- Bug fixed
88
- API changes
9+
- Renamed Directory#setAlphaComparator and setAlphaNumComparator
10+
methods into useAlphaComparator and useAlphaNumComparator with
11+
proper deprecation. Suggested by Tim Peierls.
912
- Misc
1013
- Reverted Role equality test to default Java object equality as
1114
equality based on name could cause issues with MemoryRealm
@@ -28,7 +31,7 @@ Changes log
2831
- Fixed retrieval of SSL cipher suites and client certificates
2932
for the internal HTTP client.
3033
- Fixed security bug in the XML extension that allows XML external
31-
entity attacks. Reported by Nicolas Gr�goire,
34+
entity attacks. Reported by Nicolas Gr�goire,
3235
Caleb James DeLisle and Fabio Mancinelli.
3336
- API changes
3437
- Renamed ReadableListener into ReadingListener

modules/org.restlet.example/src/org/restlet/example/book/restlet/ch07/sec1/sub1/MailRepresentation.java renamed to modules/org.restlet.example/src/org/restlet/example/book/restlet/ch07/sec1/sub1/Mail.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
/**
3737
* The mail representation bean.
3838
*/
39-
public class MailRepresentation {
39+
public class Mail {
4040

4141
private String status;
4242

modules/org.restlet.example/src/org/restlet/example/book/restlet/ch07/sec1/sub1/MailClient.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,11 @@ public class MailClient {
4444
public static void main(String[] args) throws Exception {
4545
ClientResource mailClient = new ClientResource(
4646
"http://localhost:8111/accounts/123/mails/abc");
47+
4748
Form form = new Form();
48-
form.add("subject", "Message to J�r�me");
49+
form.add("subject", "Message to Jérôme");
4950
form.add("content", "Doh!\n\nAllo?");
51+
System.out.println(form.getWebRepresentation());
5052
mailClient.put(form).write(System.out);
5153
}
5254

modules/org.restlet.example/src/org/restlet/example/book/restlet/ch07/sec1/sub1/MailServerApplication.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@
3434
package org.restlet.example.book.restlet.ch07.sec1.sub1;
3535

3636
import org.restlet.Application;
37+
import org.restlet.Component;
3738
import org.restlet.Restlet;
38-
import org.restlet.Server;
3939
import org.restlet.data.Protocol;
4040
import org.restlet.routing.Router;
4141

@@ -52,8 +52,10 @@ public class MailServerApplication extends Application {
5252
* @throws Exception
5353
*/
5454
public static void main(String[] args) throws Exception {
55-
Server mailServer = new Server(Protocol.HTTP, 8111);
56-
mailServer.setNext(new MailServerApplication());
55+
Component mailServer = new Component();
56+
mailServer.getClients().add(Protocol.CLAP);
57+
mailServer.getServers().add(Protocol.HTTP, 8111);
58+
mailServer.getDefaultHost().attach(new MailServerApplication());
5759
mailServer.start();
5860
}
5961

@@ -63,8 +65,7 @@ public static void main(String[] args) throws Exception {
6365
@Override
6466
public Restlet createInboundRoot() {
6567
Router router = new Router(getContext());
66-
router.attach(
67-
"http://localhost:8111/accounts/{accountId}/mails/{mailId}",
68+
router.attach("/accounts/{accountId}/mails/{mailId}",
6869
MailServerResource.class);
6970
return router;
7071
}

modules/org.restlet.example/src/org/restlet/example/book/restlet/ch07/sec1/sub1/MailServerResource.java

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,9 @@
4040
import org.restlet.data.Reference;
4141
import org.restlet.ext.freemarker.TemplateRepresentation;
4242
import org.restlet.representation.Representation;
43-
import org.restlet.representation.StringRepresentation;
4443
import org.restlet.resource.ClientResource;
44+
import org.restlet.resource.Get;
45+
import org.restlet.resource.Put;
4546
import org.restlet.resource.ResourceException;
4647
import org.restlet.resource.ServerResource;
4748

@@ -51,10 +52,10 @@
5152
*/
5253
public class MailServerResource extends ServerResource {
5354

54-
@Override
55-
protected Representation get() throws ResourceException {
56-
// Create the mail representation bean
57-
MailRepresentation mail = new MailRepresentation();
55+
@Get
56+
public Representation toHtml() throws ResourceException {
57+
// Create the mail bean
58+
Mail mail = new Mail();
5859
mail.setStatus("received");
5960
mail.setSubject("Message to self");
6061
mail.setContent("Doh!");
@@ -70,20 +71,12 @@ protected Representation get() throws ResourceException {
7071
return new TemplateRepresentation(mailFtl, mail, MediaType.TEXT_HTML);
7172
}
7273

73-
@Override
74-
protected Representation put(Representation input) {
75-
try {
76-
String inputText = input.getText();
77-
System.out.println(inputText);
78-
Form form = new Form(inputText);
79-
80-
for (Parameter entry : form) {
81-
System.out.println(entry.getName() + "=" + entry.getValue());
82-
}
83-
} catch (Exception e) {
84-
e.printStackTrace();
74+
@Put
75+
public String store(Form form) {
76+
for (Parameter entry : form) {
77+
System.out.println(entry.getName() + "=" + entry.getValue());
8578
}
8679

87-
return new StringRepresentation("Mail updated!");
80+
return "Mail updated!";
8881
}
8982
}

modules/org.restlet.example/src/org/restlet/example/book/restlet/ch07/sec1/sub2/CookieAuthenticator.java

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@
5151
import org.restlet.security.ChallengeAuthenticator;
5252
import org.restlet.security.Verifier;
5353

54+
/**
55+
* Naive implementation of a cookie authenticator.
56+
*
57+
* @author Jerome Louvel
58+
*/
5459
public class CookieAuthenticator extends ChallengeAuthenticator {
5560

5661
public CookieAuthenticator(Context context, boolean optional, String realm) {
@@ -66,6 +71,25 @@ public CookieAuthenticator(Context context, String realm) {
6671
super(context, ChallengeScheme.HTTP_COOKIE, realm);
6772
}
6873

74+
@Override
75+
protected void afterHandle(Request request, Response response) {
76+
super.afterHandle(request, response);
77+
Cookie cookie = request.getCookies().getFirst("Credentials");
78+
79+
if (request.getClientInfo().isAuthenticated() && (cookie == null)) {
80+
String identifier = request.getChallengeResponse().getIdentifier();
81+
String secret = new String(request.getChallengeResponse()
82+
.getSecret());
83+
CookieSetting cookieSetting = new CookieSetting("Credentials",
84+
identifier + "=" + secret);
85+
cookieSetting.setAccessRestricted(true);
86+
cookieSetting.setPath("/");
87+
cookieSetting.setComment("Unsecured cookie based authentication");
88+
cookieSetting.setMaxAge(30);
89+
response.getCookieSettings().add(cookieSetting);
90+
}
91+
}
92+
6993
@Override
7094
protected int beforeHandle(Request request, Response response) {
7195
Cookie cookie = request.getCookies().getFirst("Credentials");
@@ -110,23 +134,4 @@ public void challenge(Response response, boolean stale) {
110134
response.setStatus(Status.CLIENT_ERROR_UNAUTHORIZED);
111135
}
112136

113-
@Override
114-
protected void afterHandle(Request request, Response response) {
115-
super.afterHandle(request, response);
116-
Cookie cookie = request.getCookies().getFirst("Credentials");
117-
118-
if (request.getClientInfo().isAuthenticated() && (cookie == null)) {
119-
String identifier = request.getChallengeResponse().getIdentifier();
120-
String secret = new String(request.getChallengeResponse()
121-
.getSecret());
122-
CookieSetting cookieSetting = new CookieSetting("Credentials",
123-
identifier + "=" + secret);
124-
cookieSetting.setAccessRestricted(true);
125-
cookieSetting.setPath("/");
126-
cookieSetting.setComment("Unsecured cookie based authentication");
127-
cookieSetting.setMaxAge(30);
128-
response.getCookieSettings().add(cookieSetting);
129-
}
130-
}
131-
132137
}

modules/org.restlet.example/src/org/restlet/example/book/restlet/ch07/sec1/sub2/MailRepresentation.java renamed to modules/org.restlet.example/src/org/restlet/example/book/restlet/ch07/sec1/sub2/Mail.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
/**
3737
* The mail representation bean.
3838
*/
39-
public class MailRepresentation {
39+
public class Mail {
4040

4141
private String status;
4242

modules/org.restlet.example/src/org/restlet/example/book/restlet/ch07/sec1/sub2/MailClient.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,12 @@ public static void main(String[] args) throws Exception {
4646
ClientResource mailClient = new ClientResource(
4747
"http://localhost:8111/accounts/123/mails/abc");
4848
mailClient.getRequest().getCookies()
49-
.add(new Cookie("Credentials", "scott=tiger"));
49+
.add(new Cookie("Credentials", "chunkylover53=pwd"));
5050

5151
Form form = new Form();
52-
form.add("subject", "Message to J�r�me");
52+
form.add("subject", "Message to Jérôme");
5353
form.add("content", "Doh!\n\nAllo?");
54+
System.out.println(form.getWebRepresentation());
5455
mailClient.put(form).write(System.out);
5556
}
5657

modules/org.restlet.example/src/org/restlet/example/book/restlet/ch07/sec1/sub2/MailServerApplication.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public Restlet createInboundRoot() {
6969
MailServerResource.class);
7070

7171
MapVerifier verifier = new MapVerifier();
72-
verifier.getLocalSecrets().put("scott", "tiger".toCharArray());
72+
verifier.getLocalSecrets().put("chunkylover53", "pwd".toCharArray());
7373

7474
CookieAuthenticator authenticator = new CookieAuthenticator(
7575
getContext(), "Cookie Test");

modules/org.restlet.example/src/org/restlet/example/book/restlet/ch07/sec1/sub2/MailServerResource.java

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,9 @@
4040
import org.restlet.data.Reference;
4141
import org.restlet.ext.freemarker.TemplateRepresentation;
4242
import org.restlet.representation.Representation;
43-
import org.restlet.representation.StringRepresentation;
4443
import org.restlet.resource.ClientResource;
44+
import org.restlet.resource.Get;
45+
import org.restlet.resource.Put;
4546
import org.restlet.resource.ResourceException;
4647
import org.restlet.resource.ServerResource;
4748

@@ -51,10 +52,10 @@
5152
*/
5253
public class MailServerResource extends ServerResource {
5354

54-
@Override
55-
protected Representation get() throws ResourceException {
55+
@Get
56+
public Representation toHtml() throws ResourceException {
5657
// Create the mail bean
57-
MailRepresentation mail = new MailRepresentation();
58+
Mail mail = new Mail();
5859
mail.setStatus("received");
5960
mail.setSubject("Message to self");
6061
mail.setContent("Doh!");
@@ -67,24 +68,15 @@ protected Representation get() throws ResourceException {
6768
+ "/Mail.ftl").get();
6869

6970
// Wraps the bean with a FreeMarker representation
70-
return new TemplateRepresentation(mailFtl,
71-
mail, MediaType.TEXT_HTML);
71+
return new TemplateRepresentation(mailFtl, mail, MediaType.TEXT_HTML);
7272
}
7373

74-
@Override
75-
protected Representation put(Representation input) {
76-
try {
77-
String inputText = input.getText();
78-
System.out.println(inputText);
79-
Form form = new Form(inputText);
80-
81-
for (Parameter entry : form) {
82-
System.out.println(entry.getName() + "=" + entry.getValue());
83-
}
84-
} catch (Exception e) {
85-
e.printStackTrace();
74+
@Put
75+
public String store(Form form) {
76+
for (Parameter entry : form) {
77+
System.out.println(entry.getName() + "=" + entry.getValue());
8678
}
8779

88-
return new StringRepresentation("Mail updated!");
80+
return "Mail updated!";
8981
}
9082
}

0 commit comments

Comments
 (0)