packagees.uvigo.esei.daa.rest;importjava.util.logging.Level;importjava.util.logging.Logger;importjavax.ws.rs.DELETE;importjavax.ws.rs.FormParam;importjavax.ws.rs.GET;importjavax.ws.rs.POST;importjavax.ws.rs.PUT;importjavax.ws.rs.Path;importjavax.ws.rs.PathParam;importjavax.ws.rs.Produces;importjavax.ws.rs.core.MediaType;importjavax.ws.rs.core.Response;importes.uvigo.esei.daa.dao.DAOException;importes.uvigo.esei.daa.dao.PetDAO;importes.uvigo.esei.daa.entities.Pet;/**
* REST resource for managing pets.
*
* @author Martín Vázquez Torres
*/@Path("/pet")@Produces(MediaType.APPLICATION_JSON)publicclassPetResource{privatefinalstaticLoggerLOG=Logger.getLogger(PetResource.class.getName());privatefinalPetDAOdao;/**
* Constructs a new instance of {@link PetResource}.
*///TODO Este constructor tiene que meter aqui el señor id de la url y ya
public PetResource() {
this(new PetDAO());
}
// Needed for testing purposes (parece ser)
PetResource(PetDAO dao) {
this.dao = dao;
}
/**
* Returns a pet with the provided identifier.
*
* @param id the identifier of the pet to retrieve.
* @return a 200 OK response with a pet that has the provided identifier.
* If the identifier does not corresponds with any user, a 400 Bad Request
* response with an error message will be returned. If an error happens
* while retrieving the list, a 500 Internal Server Error response with an
* error message will be returned.
*/
@GET
@Path("/{id}")
public Response get(
@PathParam("id") int id
) {
try {
final Pet pet = this.dao.get(id);
return Response.ok(pet).build();
} catch (IllegalArgumentException iae) {
LOG.log(Level.FINE, "Invalid pet id in get method", iae);
return Response.status(Response.Status.BAD_REQUEST)
.entity(iae.getMessage())
.build();
} catch (DAOException e) {
LOG.log(Level.SEVERE, "Error getting a pet", e);
return Response.serverError()
.entity(e.getMessage())
.build();
}
}
/**
* Returns the complete list of the pets stored in the system.
*
* @return a 200 OK response with the complete list of pets stored in the
* system. If an error happens while retrieving the list, a 500 Internal
* Server Error response with an error message will be returned.
*/
@GET
public Response list() {
try {
return Response.ok(this.dao.list()).build();
} catch (DAOException e) {
LOG.log(Level.SEVERE, "Error listing pets", e);
return Response.serverError().entity(e.getMessage()).build();
}
}
/**
* Sets the ownerID for this webpage.
*
* @param ownerID owner id for the webpage.
* @return ok response
*/
@POST
@Path("/{id}")
public Response cambiarDAO(@PathParam("id") int ownerID) {
dao.setOwnerID(ownerID);
return Response.ok(ownerID).build();
}
/**
* Creates a new pet in the system.
*
* @param name the name of the new pet.
* @return a 200 OK response with a pet that has been created. If the
* name is not provided, a 400 Bad Request response with an
* error message will be returned. If an error happens while retrieving the
* list, a 500 Internal Server Error response with an error message will be
* returned.
*/
@POST
public Response add(
@FormParam("name") String name
) {
try {
final Pet newPet = this.dao.add(name);
return Response.ok(newPet).build();
} catch (IllegalArgumentException iae) {
LOG.log(Level.FINE, "Invalid pet id in add method", iae);
return Response.status(Response.Status.BAD_REQUEST)
.entity(iae.getMessage())
.build();
} catch (DAOException e) {
LOG.log(Level.SEVERE, "Error adding a pet", e);
return Response.serverError()
.entity(e.getMessage())
.build();
}
}
/**
* Modifies the data of a pet.
*
* @param id identifier of the pet to modify.
* @param name the new name of the pet.
* @return a 200 OK response with a pet that has been modified. If the
* identifier does not corresponds with any user or the name is
* not provided, a 400 Bad Request response with an error message will be
* returned. If an error happens while retrieving the list, a 500 Internal
* Server Error response with an error message will be returned.
*/
@PUT
@Path("/{id}")
public Response modify(
@PathParam("id") int id,
@FormParam("name") String name
) {
try {
final Pet modifiedPet = new Pet(id, name, dao.getOwnerID());
this.dao.modify(modifiedPet);
return Response.ok(modifiedPet).build();
} catch (NullPointerException npe) {
final String message = String.format("Invalid data for pet (name: %s)", name);
LOG.log(Level.FINE, message);
return Response.status(Response.Status.BAD_REQUEST)
.entity(message)
.build();
} catch (IllegalArgumentException iae) {
LOG.log(Level.FINE, "Invalid pet id in modify method", iae);
return Response.status(Response.Status.BAD_REQUEST)
.entity(iae.getMessage())
.build();
} catch (DAOException e) {
LOG.log(Level.SEVERE, "Error modifying a pet", e);
return Response.serverError()
.entity(e.getMessage())
.build();
}
}
/**
* Deletes a pet from the system.
*
* @param id the identifier of the pet to be deleted.
* @return a 200 OK response with the identifier of the pet that has
* been deleted. If the identifier does not corresponds with any user, a 400
* Bad Request response with an error message will be returned. If an error
* happens while retrieving the list, a 500 Internal Server Error response
* with an error message will be returned.
*/
@DELETE
@Path("/{id}")
public Response delete(
@PathParam("id") int id
) {
try {
this.dao.delete(id);
return Response.ok(id).build();
} catch (IllegalArgumentException iae) {
LOG.log(Level.FINE, "Invalid pet id in delete method", iae);
return Response.status(Response.Status.BAD_REQUEST)
.entity(iae.getMessage())
.build();
} catch (DAOException e) {
LOG.log(Level.SEVERE, "Error deleting a pet", e);
return Response.serverError()
.entity(e.getMessage())
.build();
}
}
}
packagees.uvigo.esei.daa.rest;importjava.util.logging.Level;importjava.util.logging.Logger;importjavax.ws.rs.DELETE;importjavax.ws.rs.FormParam;importjavax.ws.rs.GET;importjavax.ws.rs.POST;importjavax.ws.rs.PUT;importjavax.ws.rs.Path;importjavax.ws.rs.PathParam;importjavax.ws.rs.Produces;importjavax.ws.rs.core.MediaType;importjavax.ws.rs.core.Response;importes.uvigo.esei.daa.dao.DAOException;importes.uvigo.esei.daa.dao.PetDAO;importes.uvigo.esei.daa.entities.Pet;/**
* REST resource for managing pets.
*
* @author Martín Vázquez Torres
*/@Path("/pet")@Produces(MediaType.APPLICATION_JSON)publicclassPetResource{privatefinalstaticLoggerLOG=Logger.getLogger(PetResource.class.getName());privatefinalPetDAOdao;/**
* Constructs a new instance of {@link PetResource}.
*///TODO Este constructor tiene que meter aqui el señor id de la url y ya
public PetResource() {
this(new PetDAO());
}
// Needed for testing purposes (parece ser)
PetResource(PetDAO dao) {
this.dao = dao;
}
/**
* Returns a pet with the provided identifier.
*
* @param id the identifier of the pet to retrieve.
* @return a 200 OK response with a pet that has the provided identifier.
* If the identifier does not corresponds with any user, a 400 Bad Request
* response with an error message will be returned. If an error happens
* while retrieving the list, a 500 Internal Server Error response with an
* error message will be returned.
*/
@GET
@Path("/{ownerID}/{id}")
public Response get(
@PathParam("ownerID") int ownerID,
@PathParam("id") int id
) {
try {
final Pet pet = this.dao.get(ownerID, id);
return Response.ok(pet).build();
} catch (IllegalArgumentException iae) {
LOG.log(Level.FINE, "Invalid pet id in get method", iae);
return Response.status(Response.Status.BAD_REQUEST)
.entity(iae.getMessage())
.build();
} catch (DAOException e) {
LOG.log(Level.SEVERE, "Error getting a pet", e);
return Response.serverError()
.entity(e.getMessage())
.build();
}
}
/**
* Returns the complete list of the pets stored in the system.
*
* @return a 200 OK response with the complete list of pets stored in the
* system. If an error happens while retrieving the list, a 500 Internal
* Server Error response with an error message will be returned.
*/
@GET
@Path("/{ownerID}")
public Response list(@PathParam("ownerID") int ownerID) {
try {
return Response.ok(this.dao.list(ownerID)).build();
} catch (DAOException e) {
LOG.log(Level.SEVERE, "Error listing pets", e);
return Response.serverError().entity(e.getMessage()).build();
}
}
/*
/**
* Sets the ownerID for this webpage.
*
* @param ownerID owner id for the webpage.
* @return ok response
@POST
@Path("/{id}")
public Response cambiarDAO(@PathParam("id") int ownerID) {
dao.setOwnerID(ownerID);
return Response.ok(ownerID).build();
}
*/
/**
* Creates a new pet in the system.
*
* @param name the name of the new pet.
* @return a 200 OK response with a pet that has been created. If the
* name is not provided, a 400 Bad Request response with an
* error message will be returned. If an error happens while retrieving the
* list, a 500 Internal Server Error response with an error message will be
* returned.
*/
@POST
@Path("/{ownerID}")
public Response add(
@PathParam("ownerID") int ownerID,
@FormParam("name") String name
) {
try {
final Pet newPet = this.dao.add(ownerID, name);
return Response.ok(newPet).build();
} catch (IllegalArgumentException iae) {
LOG.log(Level.FINE, "Invalid pet id in add method", iae);
return Response.status(Response.Status.BAD_REQUEST)
.entity(iae.getMessage())
.build();
} catch (DAOException e) {
LOG.log(Level.SEVERE, "Error adding a pet", e);
return Response.serverError()
.entity(e.getMessage())
.build();
}
}
/**
* Modifies the data of a pet.
*
* @param id identifier of the pet to modify.
* @param name the new name of the pet.
* @return a 200 OK response with a pet that has been modified. If the
* identifier does not corresponds with any user or the name is
* not provided, a 400 Bad Request response with an error message will be
* returned. If an error happens while retrieving the list, a 500 Internal
* Server Error response with an error message will be returned.
*/
@PUT
@Path("/{ownerID}/{id}")
public Response modify(
@PathParam("id") int id,
@PathParam("ownerID") int ownerID,
@FormParam("name") String name
) {
try {
final Pet modifiedPet = new Pet(id, name, ownerID);
this.dao.modify(modifiedPet);
return Response.ok(modifiedPet).build();
} catch (NullPointerException npe) {
final String message = String.format("Invalid data for pet (name: %s)", name);
LOG.log(Level.FINE, message);
return Response.status(Response.Status.BAD_REQUEST)
.entity(message)
.build();
} catch (IllegalArgumentException iae) {
LOG.log(Level.FINE, "Invalid pet id in modify method", iae);
return Response.status(Response.Status.BAD_REQUEST)
.entity(iae.getMessage())
.build();
} catch (DAOException e) {
LOG.log(Level.SEVERE, "Error modifying a pet", e);
return Response.serverError()
.entity(e.getMessage())
.build();
}
}
/**
* Deletes a pet from the system.
*
* @param id the identifier of the pet to be deleted.
* @return a 200 OK response with the identifier of the pet that has
* been deleted. If the identifier does not corresponds with any user, a 400
* Bad Request response with an error message will be returned. If an error
* happens while retrieving the list, a 500 Internal Server Error response
* with an error message will be returned.
*/
@DELETE
@Path("/{ownerID}/{id}")
public Response delete(
@PathParam("id") int id
) {
try {
this.dao.delete(id);
return Response.ok(id).build();
} catch (IllegalArgumentException iae) {
LOG.log(Level.FINE, "Invalid pet id in delete method", iae);
return Response.status(Response.Status.BAD_REQUEST)
.entity(iae.getMessage())
.build();
} catch (DAOException e) {
LOG.log(Level.SEVERE, "Error deleting a pet", e);
return Response.serverError()
.entity(e.getMessage())
.build();
}
}
}