Skip to content

Commit 0bc42ac

Browse files
author
jmcx
committed
Removed support of POST on contacts resource and fixed PUT.
1 parent 1185f23 commit 0bc42ac

3 files changed

Lines changed: 14 additions & 57 deletions

File tree

modules/org.restlet.tutorial.webapi/src/main/java/org/restlet/tutorial/resource/ContactListResource.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,7 @@
2525
package org.restlet.tutorial.resource;
2626

2727
import org.restlet.resource.Get;
28-
import org.restlet.resource.Post;
29-
import org.restlet.tutorial.core.exception.BadEntityException;
30-
import org.restlet.tutorial.representation.CompanyRepresentation;
3128
import org.restlet.tutorial.representation.ContactListRepresentation;
32-
import org.restlet.tutorial.representation.ContactRepresentation;
3329
import org.restlet.tutorial.representation.ContactWithCompanyListRepresentation;
3430

3531
public interface ContactListResource {
@@ -39,9 +35,5 @@ public interface ContactListResource {
3935

4036
@Get("?strategy=load")
4137
public ContactWithCompanyListRepresentation getContactsLoad();
42-
43-
@Post
44-
public ContactRepresentation add(ContactRepresentation contactReprIn)
45-
throws BadEntityException;
4638

4739
}

modules/org.restlet.tutorial.webapi/src/main/java/org/restlet/tutorial/resource/server/ContactListServerResource.java

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,9 @@
2929
import java.util.List;
3030
import java.util.logging.Level;
3131

32-
import org.restlet.data.Status;
3332
import org.restlet.resource.ResourceException;
3433
import org.restlet.resource.ServerResource;
3534
import org.restlet.tutorial.WebApiTutorial;
36-
import org.restlet.tutorial.core.exception.BadEntityException;
3735
import org.restlet.tutorial.core.exception.NotFoundException;
3836
import org.restlet.tutorial.core.util.ResourceUtils;
3937
import org.restlet.tutorial.persistence.ContactPersistence;
@@ -209,49 +207,4 @@ public ContactWithCompanyListRepresentation getContactsLoad()
209207
}
210208

211209
}
212-
213-
@Override
214-
public ContactRepresentation add(ContactRepresentation contactReprIn)
215-
throws BadEntityException {
216-
217-
getLogger().finer("Add a new contact.");
218-
219-
// Check authorization
220-
ResourceUtils.checkRole(this, WebApiTutorial.ROLE_USER);
221-
getLogger().finer("User allowed to add a contact.");
222-
223-
// Check entity
224-
ResourceUtils.notNull(contactReprIn);
225-
getLogger().finer("Contact checked");
226-
227-
try {
228-
229-
// Convert ContactRepresentation to Contact
230-
Contact contactIn = ContactUtils.toContact(contactReprIn);
231-
232-
// Add new contact in DB and retrieve created contact
233-
Contact contactOut = contactPersistence.add(contactIn);
234-
235-
// Convert company to CompanyRepresentation
236-
ContactRepresentation result = ContactUtils
237-
.toContactRepresentation(contactOut);
238-
239-
// Set location of created resource and status to created (201)
240-
getResponse().setLocationRef(
241-
ResourceUtils.getContactUrl(contactOut.getId()));
242-
getResponse().setStatus(Status.SUCCESS_CREATED);
243-
244-
getLogger().finer("Contact successfully added.");
245-
246-
return result;
247-
} catch (SQLException ex) {
248-
getLogger().log(Level.WARNING, "Error when adding a contact", ex);
249-
if (WebApiTutorial.SQL_STATE_23000_DUPLICATE.equals(ex
250-
.getSQLState())) {
251-
throw new BadEntityException(
252-
"Can't add a contact due to integrity constraint violation.");
253-
}
254-
throw new ResourceException(ex);
255-
}
256-
}
257210
}

modules/org.restlet.tutorial.webapi/src/main/java/org/restlet/tutorial/resource/server/ContactServerResource.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ protected void doInit() {
8484
setExisting(true);
8585
} else {
8686
setExisting(false);
87-
throw new NotFoundException("No contact with email: " + email);
8887
}
8988
} catch (SQLException ex) {
9089
throw new ResourceException(ex);
@@ -96,7 +95,12 @@ protected void doInit() {
9695
}
9796

9897
public ContactRepresentation getContact() {
99-
getLogger().finer("Retrieve a contact");
98+
99+
getLogger().finer("Retrieve a contact");
100+
101+
if (contact == null){
102+
throw new NotFoundException("No contact with email: " + email);
103+
}
100104

101105
// Check authorization
102106
ResourceUtils.checkRole(this, WebApiTutorial.ROLE_USER);
@@ -118,6 +122,10 @@ public ContactWithCompanyRepresentation getContactLoad() {
118122

119123
getLogger().finer("Load a contact");
120124

125+
if (contact == null){
126+
throw new NotFoundException("No contact with email: " + email);
127+
}
128+
121129
// Check authorization
122130
ResourceUtils.checkRole(this, WebApiTutorial.ROLE_USER);
123131
getLogger().finer("User allowed to load a contact.");
@@ -145,6 +153,10 @@ public ContactWithCompanyRepresentation getContactLoad() {
145153
public void remove() throws NotFoundException {
146154

147155
getLogger().finer("Remove a contact.");
156+
157+
if (contact == null){
158+
throw new NotFoundException("No contact with email: " + email);
159+
}
148160

149161
// Check authorization
150162
ResourceUtils.checkRole(this, WebApiTutorial.ROLE_OWNER);

0 commit comments

Comments
 (0)