Skip to content

Commit 1185f23

Browse files
author
jmcx
committed
Added support for POST on contacts server resource.
1 parent c1cf467 commit 1185f23

3 files changed

Lines changed: 56 additions & 0 deletions

File tree

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@
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;
2831
import org.restlet.tutorial.representation.ContactListRepresentation;
32+
import org.restlet.tutorial.representation.ContactRepresentation;
2933
import org.restlet.tutorial.representation.ContactWithCompanyListRepresentation;
3034

3135
public interface ContactListResource {
@@ -35,5 +39,9 @@ public interface ContactListResource {
3539

3640
@Get("?strategy=load")
3741
public ContactWithCompanyListRepresentation getContactsLoad();
42+
43+
@Post
44+
public ContactRepresentation add(ContactRepresentation contactReprIn)
45+
throws BadEntityException;
3846

3947
}

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

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

32+
import org.restlet.data.Status;
3233
import org.restlet.resource.ResourceException;
3334
import org.restlet.resource.ServerResource;
3435
import org.restlet.tutorial.WebApiTutorial;
36+
import org.restlet.tutorial.core.exception.BadEntityException;
3537
import org.restlet.tutorial.core.exception.NotFoundException;
3638
import org.restlet.tutorial.core.util.ResourceUtils;
3739
import org.restlet.tutorial.persistence.ContactPersistence;
@@ -207,4 +209,49 @@ public ContactWithCompanyListRepresentation getContactsLoad()
207209
}
208210

209211
}
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+
}
210257
}

modules/org.restlet.tutorial.webapi/src/main/java/org/restlet/tutorial/utils/ContactUtils.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public static Contact toContact(ContactRepresentation cRepr) {
3636
contact.setFirstName(cRepr.getFirstName());
3737
contact.setName(cRepr.getName());
3838
contact.setAge(cRepr.getAge());
39+
contact.setEmail(cRepr.getEmail());
3940
return contact;
4041
}
4142
return null;

0 commit comments

Comments
 (0)