aboutsummaryrefslogtreecommitdiff
path: root/src/org/traccar/api/BaseResource.java
diff options
context:
space:
mode:
authorAnton Tananaev <anton.tananaev@gmail.com>2015-12-01 11:30:03 +1300
committerAnton Tananaev <anton.tananaev@gmail.com>2015-12-01 11:30:03 +1300
commit6c444343d14bd3158bb27285efbcb17141166bff (patch)
tree950c882161219618a87a5aedb82cc9cdf00aa52f /src/org/traccar/api/BaseResource.java
parent7575edbee87f9fbe4770c988b469f5915d85bb51 (diff)
downloadtrackermap-server-6c444343d14bd3158bb27285efbcb17141166bff.tar.gz
trackermap-server-6c444343d14bd3158bb27285efbcb17141166bff.tar.bz2
trackermap-server-6c444343d14bd3158bb27285efbcb17141166bff.zip
Remove response builder utility class
Diffstat (limited to 'src/org/traccar/api/BaseResource.java')
-rw-r--r--src/org/traccar/api/BaseResource.java44
1 files changed, 28 insertions, 16 deletions
diff --git a/src/org/traccar/api/BaseResource.java b/src/org/traccar/api/BaseResource.java
index 6051fdb61..9b0c2cf79 100644
--- a/src/org/traccar/api/BaseResource.java
+++ b/src/org/traccar/api/BaseResource.java
@@ -17,6 +17,8 @@ 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;
@@ -24,38 +26,47 @@ import org.traccar.Context;
import org.traccar.helper.Clazz;
import org.traccar.model.User;
-public class BaseResource<T, I> {
+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(ResponseBuilder.badRequest(e));
+ throw new WebApplicationException(
+ Response.status(Response.Status.BAD_REQUEST).entity(getError(e)).build());
}
if (collection == null || collection.isEmpty()) {
- throw new WebApplicationException(ResponseBuilder.notFound());
+ throw new WebApplicationException(Response.status(Response.Status.NOT_FOUND).build());
} else {
return collection;
}
}
- public T getEntity(I id) {
+ 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(ResponseBuilder.badRequest(e));
+ throw new WebApplicationException(Response.status(Response.Status.BAD_REQUEST).entity(getError(e)).build());
}
if (entity == null) {
- throw new WebApplicationException(ResponseBuilder.notFound());
+ throw new WebApplicationException(Response.status(Response.Status.NOT_FOUND).build());
} else {
return entity;
}
@@ -64,37 +75,38 @@ public class BaseResource<T, I> {
public Response postEntity(T entity) {
try {
Context.getDataManager().add(entity);
- return ResponseBuilder.ok(entity);
+ return Response.status(Response.Status.OK).entity(entity).build();
} catch (Exception e) {
- return ResponseBuilder.badRequest(e);
+ return Response.status(Response.Status.BAD_REQUEST).entity(getError(e)).build();
}
}
- public Response putEntity(I id, T entity) {
+ public Response putEntity(long id, T entity) {
try {
Clazz.setId(entity, id);
Context.getDataManager().update(entity);
- return ResponseBuilder.ok(entity);
+ return Response.status(Response.Status.OK).entity(entity).build();
} catch (Exception e) {
- return ResponseBuilder.badRequest(e);
+ return Response.status(Response.Status.BAD_REQUEST).entity(getError(e)).build();
}
}
- public Response deleteEntity(I id) {
+ public Response deleteEntity(long id) {
try {
T entity = Clazz.newInstance(clazz);
Clazz.setId(entity, id);
Context.getDataManager().remove(entity);
- return ResponseBuilder.deleted();
+ return Response.status(Response.Status.NO_CONTENT).build();
} catch (Exception e) {
- return ResponseBuilder.badRequest(e);
+ return Response.status(Response.Status.BAD_REQUEST).entity(getError(e)).build();
}
}
- private void validateSecurityContext(String role, I id) {
+ private void validateSecurityContext(String role, long id) {
UserPrincipal userPrincipal = (UserPrincipal) securityContext.getUserPrincipal();
if (!securityContext.isUserInRole(role) && !userPrincipal.getId().equals(id)) {
- throw new WebApplicationException(ResponseBuilder.forbidden());
+ throw new WebApplicationException(Response.status(Response.Status.FORBIDDEN).build());
}
}
+
}