diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2015-12-01 14:37:26 +1300 |
---|---|---|
committer | Anton Tananaev <anton.tananaev@gmail.com> | 2015-12-01 14:37:26 +1300 |
commit | a4fb24732188e4e0b7cc44079eea02c85e142028 (patch) | |
tree | a2fdc0e6f92d7bfafe2f4e39b01133c2a09fc748 /src/org/traccar/api/BaseResource.java | |
parent | 40a607faa34d3c21d7b6d8275279fc8ce3c96980 (diff) | |
download | trackermap-server-a4fb24732188e4e0b7cc44079eea02c85e142028.tar.gz trackermap-server-a4fb24732188e4e0b7cc44079eea02c85e142028.tar.bz2 trackermap-server-a4fb24732188e4e0b7cc44079eea02c85e142028.zip |
Refactor base and other resource classes
Diffstat (limited to 'src/org/traccar/api/BaseResource.java')
-rw-r--r-- | src/org/traccar/api/BaseResource.java | 95 |
1 files changed, 1 insertions, 94 deletions
diff --git a/src/org/traccar/api/BaseResource.java b/src/org/traccar/api/BaseResource.java index 9b0c2cf79..5a05c6732 100644 --- a/src/org/traccar/api/BaseResource.java +++ b/src/org/traccar/api/BaseResource.java @@ -15,98 +15,5 @@ */ package org.traccar.api; -import java.sql.SQLException; -import java.util.Collection; -import java.util.HashMap; -import java.util.Map; -import javax.ws.rs.WebApplicationException; -import javax.ws.rs.core.Response; -import javax.ws.rs.core.SecurityContext; -import org.traccar.Context; -import org.traccar.helper.Clazz; -import org.traccar.model.User; - -public class BaseResource<T> { - - private static final String ERROR_KEY = "error"; - - private final Class<T> clazz = Clazz.getGenericArgumentType(getClass()); - - @javax.ws.rs.core.Context - private SecurityContext securityContext; - - private static Map<String, String> getError(Exception e) { - Map<String, String> error = new HashMap<>(); - error.put(ERROR_KEY, e.getMessage()); - return error; - } - - public Collection<T> getEntities() { - Collection<T> collection; - try { - collection = Context.getDataManager().get(clazz); - } catch (SQLException e) { - throw new WebApplicationException( - Response.status(Response.Status.BAD_REQUEST).entity(getError(e)).build()); - } - if (collection == null || collection.isEmpty()) { - throw new WebApplicationException(Response.status(Response.Status.NOT_FOUND).build()); - } else { - return collection; - } - } - - public T getEntity(long id) { - validateSecurityContext(User.ROLE_USER, id); - T entity = Clazz.newInstance(clazz); - try { - Clazz.setId(entity, id); - entity = Context.getDataManager().get(entity); - } catch (Exception e) { - throw new WebApplicationException(Response.status(Response.Status.BAD_REQUEST).entity(getError(e)).build()); - } - if (entity == null) { - throw new WebApplicationException(Response.status(Response.Status.NOT_FOUND).build()); - } else { - return entity; - } - } - - public Response postEntity(T entity) { - try { - Context.getDataManager().add(entity); - return Response.status(Response.Status.OK).entity(entity).build(); - } catch (Exception e) { - return Response.status(Response.Status.BAD_REQUEST).entity(getError(e)).build(); - } - } - - public Response putEntity(long id, T entity) { - try { - Clazz.setId(entity, id); - Context.getDataManager().update(entity); - return Response.status(Response.Status.OK).entity(entity).build(); - } catch (Exception e) { - return Response.status(Response.Status.BAD_REQUEST).entity(getError(e)).build(); - } - } - - public Response deleteEntity(long id) { - try { - T entity = Clazz.newInstance(clazz); - Clazz.setId(entity, id); - Context.getDataManager().remove(entity); - return Response.status(Response.Status.NO_CONTENT).build(); - } catch (Exception e) { - return Response.status(Response.Status.BAD_REQUEST).entity(getError(e)).build(); - } - } - - private void validateSecurityContext(String role, long id) { - UserPrincipal userPrincipal = (UserPrincipal) securityContext.getUserPrincipal(); - if (!securityContext.isUserInRole(role) && !userPrincipal.getId().equals(id)) { - throw new WebApplicationException(Response.status(Response.Status.FORBIDDEN).build()); - } - } - +public class BaseResource { } |