Skip to content

Commit 41cab5a

Browse files
author
Jerome Louvel
committed
- Fixed implementation
1 parent 8764f8f commit 41cab5a

7 files changed

Lines changed: 170 additions & 163 deletions

File tree

modules/org.restlet.tutorial.webapi/src/main/java/org/restlet/tutorial/WebApiTutorial.java

Lines changed: 141 additions & 152 deletions
Original file line numberDiff line numberDiff line change
@@ -48,157 +48,146 @@
4848

4949
public class WebApiTutorial extends Application {
5050

51-
public static Logger logger = Engine.getLogger(WebApiTutorial.class);
52-
53-
public static final String PING = "Version: 1.0.0 running";
54-
55-
public static void main(String[] args) throws Exception {
56-
logger.info("Contacts application starting...");
57-
58-
PersistenceService.initialize();
59-
60-
// Attach application to http://localhost:9000/v1
61-
Component c = new Component();
62-
c.getServers().add(Protocol.HTTP, 9000);
63-
64-
// Declare logger
65-
// Declare client connector based on the classloader
66-
c.getClients().add(Protocol.CLAP);
67-
// Look for the log configuration file in the current classloader
68-
c.getLogService().setLogPropertiesRef("clap:///logging.properties");
69-
70-
c.getDefaultHost().attach("/v1", new WebApiTutorial());
71-
72-
c.start();
73-
74-
logger.info("Contacts application started on port 9000");
75-
logger.info("URL: http://localhost:9000/v1");
76-
}
77-
78-
/*
79-
* Define role names
80-
*/
81-
public static final String ROLE_ADMIN = "admin";
82-
83-
public static final String ROLE_OWNER = "owner";
84-
85-
public static final String ROLE_USER = "user";
86-
87-
/*
88-
* Define route constants
89-
*/
90-
public static final String ROUTE_COMPANIES = "/companies";
91-
92-
public static final String ROUTE_CONTACTS = "/contacts";
93-
94-
/*
95-
* Define SQL State code constants
96-
*/
97-
public static final String SQL_STATE_23000_DUPLICATE = "23000";
98-
99-
public WebApiTutorial() {
100-
setName("WebAPITutorial");
101-
setDescription("Full Web API tutorial");
102-
103-
getRoles().add(new Role(this, ROLE_ADMIN));
104-
getRoles().add(new Role(this, ROLE_OWNER));
105-
getRoles().add(new Role(this, ROLE_USER));
106-
}
107-
108-
@Override
109-
public Restlet createInboundRoot() {
110-
111-
Router publicRouter = publicResources();
112-
113-
// Create the api router, protected by a guard
114-
ChallengeAuthenticator apiGuard = createApiGuard();
115-
Router apiRouter = createApiRouter();
116-
apiGuard.setNext(apiRouter);
117-
118-
publicRouter.attachDefault(apiGuard);
119-
120-
return publicRouter;
121-
}
122-
123-
public Router publicResources() {
124-
Router router = new Router();
125-
126-
router.attach("/ping", PingServerResource.class);
127-
128-
// Attach Swagger Specifications
129-
attachSwaggerSpecification1(router);
130-
attachSwaggerSpecification2(router);
131-
return router;
132-
}
133-
134-
private ChallengeAuthenticator createApiGuard() {
135-
136-
ChallengeAuthenticator apiGuard = new ChallengeAuthenticator(
137-
getContext(), ChallengeScheme.HTTP_BASIC, "realm");
138-
139-
// Create in-memory users and roles.
140-
MemoryRealm realm = new MemoryRealm();
141-
User owner = new User("owner", "owner");
142-
realm.getUsers().add(owner);
143-
realm.map(owner, Role.get(this, ROLE_OWNER));
144-
realm.map(owner, Role.get(this, ROLE_USER));
145-
User admin = new User("admin", "admin");
146-
realm.getUsers().add(admin);
147-
realm.map(admin, Role.get(this, ROLE_ADMIN));
148-
realm.map(admin, Role.get(this, ROLE_OWNER));
149-
realm.map(admin, Role.get(this, ROLE_USER));
150-
User user = new User("user", "user");
151-
realm.getUsers().add(user);
152-
realm.map(user, Role.get(this, ROLE_USER));
153-
154-
// - Verifier : checks authentication
155-
// - Enroler : to check authorization (roles)
156-
apiGuard.setVerifier(realm.getVerifier());
157-
apiGuard.setEnroler(realm.getEnroler());
158-
159-
// Provide your own authentication checks by extending SecretVerifier or
160-
// LocalVerifier classes
161-
// Extend the Enroler class in order to assign roles for an
162-
// authenticated user
163-
164-
return apiGuard;
165-
}
166-
167-
private Router createApiRouter() {
168-
169-
// Attach server resources to the given URL template.
170-
// For instance, CompanyListServerResource is attached
171-
// to http://localhost:9000/v1/companies
172-
// and to http://localhost:9000/v1/companies/
173-
Router router = new Router(getContext());
174-
router.attach(ROUTE_COMPANIES, CompanyListServerResource.class);
175-
router.attach(ROUTE_COMPANIES + "/", CompanyListServerResource.class);
176-
router.attach(ROUTE_COMPANIES + "/{id}", CompanyServerResource.class);
177-
router.attach(ROUTE_CONTACTS, ContactListServerResource.class);
178-
router.attach(ROUTE_CONTACTS + "/", ContactListServerResource.class);
179-
router.attach(ROUTE_CONTACTS + "/{email}", ContactServerResource.class);
180-
181-
// List of contacts for a company
182-
router.attach(ROUTE_COMPANIES + "/{id}" + ROUTE_CONTACTS,
183-
ContactListServerResource.class);
184-
router.attach(ROUTE_COMPANIES + "/{id}" + ROUTE_CONTACTS + "/",
185-
ContactListServerResource.class);
186-
187-
return router;
188-
}
189-
190-
private void attachSwaggerSpecification1(Router router) {
191-
SwaggerSpecificationRestlet swaggerSpecificationRestlet = new SwaggerSpecificationRestlet(
192-
this);
193-
swaggerSpecificationRestlet.setBasePath("http://myapp.com/api/");
194-
swaggerSpecificationRestlet.attach(router);
195-
}
196-
197-
private void attachSwaggerSpecification2(Router router) {
198-
Swagger2SpecificationRestlet swagger2SpecificationRestlet = new Swagger2SpecificationRestlet(
199-
this);
200-
swagger2SpecificationRestlet.setBasePath("http://myapp.com/api/");
201-
swagger2SpecificationRestlet.attach(router);
202-
}
51+
public static void main(String[] args) throws Exception {
52+
LOGGER.info("Contacts application starting...");
53+
54+
PersistenceService.initialize();
55+
56+
// Attach application to http://localhost:9000/v1
57+
Component c = new Component();
58+
c.getServers().add(Protocol.HTTP, 9000);
59+
60+
// Declare client connector based on the classloader
61+
c.getClients().add(Protocol.CLAP);
62+
63+
// Look for the log configuration file in the current classloader
64+
c.getLogService().setLogPropertiesRef("clap:///logging.properties");
65+
66+
c.getDefaultHost().attach("/v1", new WebApiTutorial());
67+
68+
c.start();
69+
70+
LOGGER.info("Sample Web API started");
71+
LOGGER.info("URL: http://localhost:9000/v1");
72+
}
73+
74+
public static final Logger LOGGER = Engine.getLogger(WebApiTutorial.class);
75+
public static final String PING = "Version: 1.0.0 running";
76+
77+
/*
78+
* Define role names
79+
*/
80+
public static final String ROLE_ADMIN = "admin";
81+
public static final String ROLE_OWNER = "owner";
82+
public static final String ROLE_USER = "user";
83+
84+
/*
85+
* Define SQL State code constants
86+
*/
87+
public static final String SQL_STATE_23000_DUPLICATE = "23000";
88+
89+
public WebApiTutorial() {
90+
setName("WebAPITutorial");
91+
setDescription("Full Web API tutorial");
92+
93+
getRoles().add(new Role(this, ROLE_ADMIN));
94+
getRoles().add(new Role(this, ROLE_OWNER));
95+
getRoles().add(new Role(this, ROLE_USER));
96+
}
97+
98+
private void attachSwaggerSpecification1(Router router) {
99+
SwaggerSpecificationRestlet swaggerSpecificationRestlet = new SwaggerSpecificationRestlet(
100+
this);
101+
swaggerSpecificationRestlet.setBasePath("http://myapp.com/api/");
102+
swaggerSpecificationRestlet.attach(router);
103+
}
104+
105+
private void attachSwaggerSpecification2(Router router) {
106+
Swagger2SpecificationRestlet swagger2SpecificationRestlet = new Swagger2SpecificationRestlet(
107+
this);
108+
swagger2SpecificationRestlet.setBasePath("http://myapp.com/api/");
109+
swagger2SpecificationRestlet.attach(router);
110+
}
111+
112+
private ChallengeAuthenticator createApiGuard() {
113+
114+
ChallengeAuthenticator apiGuard = new ChallengeAuthenticator(
115+
getContext(), ChallengeScheme.HTTP_BASIC, "realm");
116+
117+
// Create in-memory users and roles.
118+
MemoryRealm realm = new MemoryRealm();
119+
User owner = new User("owner", "owner");
120+
realm.getUsers().add(owner);
121+
realm.map(owner, Role.get(this, ROLE_OWNER));
122+
realm.map(owner, Role.get(this, ROLE_USER));
123+
User admin = new User("admin", "admin");
124+
realm.getUsers().add(admin);
125+
realm.map(admin, Role.get(this, ROLE_ADMIN));
126+
realm.map(admin, Role.get(this, ROLE_OWNER));
127+
realm.map(admin, Role.get(this, ROLE_USER));
128+
User user = new User("user", "user");
129+
realm.getUsers().add(user);
130+
realm.map(user, Role.get(this, ROLE_USER));
131+
132+
// - Verifier : checks authentication
133+
// - Enroler : to check authorization (roles)
134+
apiGuard.setVerifier(realm.getVerifier());
135+
apiGuard.setEnroler(realm.getEnroler());
136+
137+
// Provide your own authentication checks by extending SecretVerifier or
138+
// LocalVerifier classes
139+
// Extend the Enroler class in order to assign roles for an
140+
// authenticated user
141+
142+
return apiGuard;
143+
}
144+
145+
private Router createApiRouter() {
146+
147+
// Attach server resources to the given URL template.
148+
// For instance, CompanyListServerResource is attached
149+
// to http://localhost:9000/v1/companies
150+
// and to http://localhost:9000/v1/companies/
151+
Router router = new Router(getContext());
152+
153+
router.attach("/companies", CompanyListServerResource.class);
154+
router.attach("/companies/", CompanyListServerResource.class);
155+
router.attach("/companies/{id}", CompanyServerResource.class);
156+
router.attach("/companies/{id}/contacts",
157+
ContactListServerResource.class);
158+
router.attach("/companies/{id}/contacts/",
159+
ContactListServerResource.class);
160+
161+
router.attach("/contacts", ContactListServerResource.class);
162+
router.attach("/contacts/", ContactListServerResource.class);
163+
router.attach("/contacts/{email}", ContactServerResource.class);
164+
return router;
165+
}
166+
167+
@Override
168+
public Restlet createInboundRoot() {
169+
170+
Router publicRouter = publicResources();
171+
172+
// Create the api router, protected by a guard
173+
ChallengeAuthenticator apiGuard = createApiGuard();
174+
Router apiRouter = createApiRouter();
175+
apiGuard.setNext(apiRouter);
176+
177+
publicRouter.attachDefault(apiGuard);
178+
179+
return publicRouter;
180+
}
181+
182+
public Router publicResources() {
183+
Router router = new Router();
184+
185+
router.attach("/ping", PingServerResource.class);
186+
187+
// Attach Swagger Specifications
188+
attachSwaggerSpecification1(router);
189+
attachSwaggerSpecification2(router);
190+
return router;
191+
}
203192

204193
}

modules/org.restlet.tutorial.webapi/src/main/java/org/restlet/tutorial/core/exception/BadEntityException.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,12 @@
4646
@Status(422)
4747
public class BadEntityException extends BusinessException {
4848

49-
private List<String> globalMessages;
49+
/**
50+
*
51+
*/
52+
private static final long serialVersionUID = 1L;
53+
54+
private List<String> globalMessages;
5055

5156
private List<FieldError> fieldErrors;
5257

modules/org.restlet.tutorial.webapi/src/main/java/org/restlet/tutorial/core/exception/BadParameterException.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,12 @@
3636
@Status(400)
3737
public class BadParameterException extends BusinessException {
3838

39-
public BadParameterException(String message) {
39+
/**
40+
*
41+
*/
42+
private static final long serialVersionUID = 1L;
43+
44+
public BadParameterException(String message) {
4045
super(400, message);
4146
}
4247
}

modules/org.restlet.tutorial.webapi/src/main/java/org/restlet/tutorial/core/exception/BusinessException.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,11 @@
4242
@JsonIgnoreProperties({ "cause", "localizedMessage", "stackTrace", "suppressed" })
4343
public abstract class BusinessException extends RuntimeException {
4444

45-
private int status;
45+
/**
46+
*
47+
*/
48+
private static final long serialVersionUID = 1L;
49+
private int status;
4650

4751
public BusinessException(int status, String message) {
4852
super(message);

modules/org.restlet.tutorial.webapi/src/main/java/org/restlet/tutorial/core/exception/NotFoundException.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,12 @@
3636
@Status(404)
3737
public class NotFoundException extends BusinessException {
3838

39-
public NotFoundException(String message) {
39+
/**
40+
*
41+
*/
42+
private static final long serialVersionUID = 1L;
43+
44+
public NotFoundException(String message) {
4045
super(404, message);
4146
}
4247
}

modules/org.restlet.tutorial.webapi/src/main/java/org/restlet/tutorial/core/util/ResourceUtils.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import org.restlet.data.Status;
2828
import org.restlet.resource.ResourceException;
2929
import org.restlet.resource.ServerResource;
30-
import org.restlet.tutorial.WebApiTutorial;
3130
import org.restlet.tutorial.core.exception.BadEntityException;
3231
import org.restlet.tutorial.representation.CompanyRepresentation;
3332

@@ -61,7 +60,7 @@ public static void checkRole(ServerResource serverResource, String role)
6160
* @return The URL of the resource that represents a company.
6261
*/
6362
public static String getCompanyUrl(String id) {
64-
return WebApiTutorial.ROUTE_COMPANIES + "/" + id;
63+
return "/companies/" + id;
6564
}
6665

6766
/**
@@ -72,7 +71,7 @@ public static String getCompanyUrl(String id) {
7271
* @return The URL of the resource that represents a contact.
7372
*/
7473
public static String getContactUrl(String email) {
75-
return WebApiTutorial.ROUTE_CONTACTS + "/" + email;
74+
return "/contacts/" + email;
7675
}
7776

7877
/**

0 commit comments

Comments
 (0)