Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Refactor service resources.

  • Loading branch information...
commit bd820e412e48c6c94a9500d45c696fb6d8a2d08e 1 parent 4b2d5b1
@shsu authored
View
6 src/main/java/ca/bcit/infosys/comp4911/access/UserDao.java
@@ -14,9 +14,7 @@
public class UserDao {
@PersistenceContext(unitName = "comp4911")
- EntityManager em;
-
- public static final String USER = User.class.getSimpleName();
+ private EntityManager em;
public void create(final User user) {
em.persist(user);
@@ -35,7 +33,7 @@ public void delete(final User user) {
}
public List<User> getAll() {
- TypedQuery<User> query = em.createQuery("select u from " + USER + " u",
+ TypedQuery<User> query = em.createQuery("select u from User u",
User.class);
return query.getResultList();
}
View
25 src/main/java/ca/bcit/infosys/comp4911/application/UserTokens.java
@@ -1,6 +1,7 @@
package ca.bcit.infosys.comp4911.application;
import ca.bcit.infosys.comp4911.helper.SH;
+import com.google.common.base.Charsets;
import com.google.common.base.Strings;
import com.google.common.collect.Maps;
import com.google.common.io.BaseEncoding;
@@ -31,25 +32,29 @@ public String generateToken(final int userID) {
return token;
}
- public int verifyTokenAndReturnUserID(final String token) throws WebApplicationException {
- if (Strings.isNullOrEmpty(token)) {
- throw new WebApplicationException(SH.corsResponse(401));
- }
+ public boolean clearToken(final String headerToken, final String queryToken) {
+ return tokensForAuthenticatedUserID.remove(processHeaderQueryToken(headerToken,queryToken)) != null;
+ }
- Integer userID = tokensForAuthenticatedUserID.get(token);
+ public int verifyTokenAndReturnUserID(final String headerToken, final String queryToken) throws WebApplicationException {
+ Integer userID = tokensForAuthenticatedUserID.get(processHeaderQueryToken(headerToken,queryToken));
if (userID == null) {
- throw new WebApplicationException(SH.corsResponse(401));
+ throw new WebApplicationException(Response.Status.UNAUTHORIZED);
}
return userID;
}
- public boolean clearToken(String tokenToBeCleared) {
- if (Strings.isNullOrEmpty(tokenToBeCleared)) {
- return false;
+ private String processHeaderQueryToken(final String headerToken, final String queryToken) {
+ if (!Strings.isNullOrEmpty(headerToken)) {
+ String decodedToken = new String(
+ BaseEncoding.base64().decode(headerToken.substring("Basic ".length())), Charsets.UTF_8);
+ return decodedToken.substring(0, decodedToken.length() - 1);
+ } else if (!Strings.isNullOrEmpty(queryToken)) {
+ return queryToken;
}
- return tokensForAuthenticatedUserID.remove(tokenToBeCleared) != null;
+ throw new WebApplicationException(Response.Status.UNAUTHORIZED);
}
}
View
22 src/main/java/ca/bcit/infosys/comp4911/helper/SH.java
@@ -10,28 +10,18 @@
*/
public class SH {
public static final String AUTHORIZATION_STRING = "Authorization";
- public static final String TOKEN = "token";
+ public static final String TOKEN_STRING = "token";
- public static javax.ws.rs.core.Response corsResponse(Integer code) {
+ public static javax.ws.rs.core.Response response(Integer code) {
return javax.ws.rs.core.Response.status(code).build();
}
- public static javax.ws.rs.core.Response corsResponseWithEntity(Integer code, Object entity) {
- return javax.ws.rs.core.Response.status(code).entity(entity).build();
- }
-
- public static String processHeaderQueryToken(final String headerToken, final String queryToken) {
- String token = null;
-
- if (!Strings.isNullOrEmpty(headerToken)) {
- String decodedToken = new String(
- BaseEncoding.base64().decode(headerToken.substring("Basic ".length())), Charsets.UTF_8);
- token = decodedToken.substring(0, decodedToken.length() - 1);
- } else if (!Strings.isNullOrEmpty(queryToken)) {
- token = queryToken;
+ public static javax.ws.rs.core.Response responseWithEntity(int code, Object entity) {
+ if (entity == null) {
+ response(code);
}
- return token;
+ return javax.ws.rs.core.Response.status(code).entity(entity).build();
}
public static Integer getCurrentWeek() {
View
45 src/main/java/ca/bcit/infosys/comp4911/services/PayRateResource.java
@@ -14,57 +14,58 @@
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
+import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.Date;
import java.util.List;
-/**
- * Created by Graeme on 2/8/14.
- */
@Path("/pay_rates")
public class PayRateResource {
@EJB
- PayRateDao payRateDao;
+ private PayRateDao payRateDao;
@EJB
- UserTokens userTokens;
+ private UserTokens userTokens;
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response retrieveAllPayRates(
- @HeaderParam(SH.AUTHORIZATION_STRING) final String token) {
- int userId = userTokens.verifyTokenAndReturnUserID((token));
+ @HeaderParam(SH.AUTHORIZATION_STRING) final String headerToken,
+ @QueryParam(SH.TOKEN_STRING) final String queryToken) {
+ int userId = userTokens.verifyTokenAndReturnUserID(headerToken, queryToken);
- return SH.corsResponseWithEntity(200, payRateDao.getAllPayRates());
+ return SH.responseWithEntity(200, payRateDao.getAllPayRates());
}
@POST
@Consumes(MediaType.APPLICATION_JSON)
public Response createPayRate(
- @HeaderParam(SH.AUTHORIZATION_STRING) final String token,
+ @HeaderParam(SH.AUTHORIZATION_STRING) final String headerToken,
+ @QueryParam(SH.TOKEN_STRING) final String queryToken,
final PayRate payRate) {
- int userId = userTokens.verifyTokenAndReturnUserID((token));
+ int userId = userTokens.verifyTokenAndReturnUserID(headerToken, queryToken);
payRateDao.create(payRate);
- return SH.corsResponse(201);
+ return SH.response(201);
}
@GET
@Path("{pay_level}")
@Produces(MediaType.APPLICATION_JSON)
public Response retrieveAllPayLevelRates(
- @HeaderParam(SH.AUTHORIZATION_STRING) final String token,
+ @HeaderParam(SH.AUTHORIZATION_STRING) final String headerToken,
+ @QueryParam(SH.TOKEN_STRING) final String queryToken,
@PathParam("pay_level") String payLevelName) {
- int userId = userTokens.verifyTokenAndReturnUserID((token));
+ int userId = userTokens.verifyTokenAndReturnUserID(headerToken, queryToken);
List<PayRate> payRates = payRateDao.getAllPayRatesByLevel(payLevelName);
if (payRates == null) {
- return SH.corsResponse(404);
+ return SH.response(404);
}
- return SH.corsResponseWithEntity(200, payRates);
+ return SH.responseWithEntity(200, payRates);
}
/**
@@ -74,10 +75,13 @@ public Response retrieveAllPayLevelRates(
@Path("{pay_level}/{year}")
@Consumes(MediaType.APPLICATION_JSON)
public Response updatePayRate(
- @HeaderParam(SH.AUTHORIZATION_STRING) final String token,
+ @HeaderParam(SH.AUTHORIZATION_STRING) final String headerToken,
+ @QueryParam(SH.TOKEN_STRING) final String queryToken,
@PathParam("id") Integer id,
final String payLevel,
final Date year) {
+ int userId = userTokens.verifyTokenAndReturnUserID(headerToken, queryToken);
+
return null;
}
@@ -85,16 +89,17 @@ public Response updatePayRate(
@Path("{pay_level}/{year}")
@Produces(MediaType.APPLICATION_JSON)
public Response retrievePayRate(
- @HeaderParam(SH.AUTHORIZATION_STRING) final String token,
+ @HeaderParam(SH.AUTHORIZATION_STRING) final String headerToken,
+ @QueryParam(SH.TOKEN_STRING) final String queryToken,
@PathParam("pay_level") final String payLevel,
@PathParam("year") final String year) {
- int userId = userTokens.verifyTokenAndReturnUserID((token));
+ int userId = userTokens.verifyTokenAndReturnUserID(headerToken, queryToken);
PayRate payRate = payRateDao.getPayRateByLevelAndYear(payLevel, year);
if (payRate == null) {
- return SH.corsResponse(404);
+ return SH.response(404);
}
- return SH.corsResponseWithEntity(200, payRate);
+ return SH.responseWithEntity(200, payRate);
}
}
View
37 src/main/java/ca/bcit/infosys/comp4911/services/ProjectAssignmentResource.java
@@ -17,63 +17,64 @@
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
+import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
-/**
- * Created by Graeme on 2/11/14.
- */
@Path("/projects/{id}/assignments")
public class ProjectAssignmentResource {
@EJB
- UserTokens userTokens;
+ private UserTokens userTokens;
@EJB
- ProjectDao projectDao;
+ private ProjectDao projectDao;
@EJB
- ProjectAssignmentDao projectAssignmentDao;
+ private ProjectAssignmentDao projectAssignmentDao;
@EJB
- UserDao userDao;
+ private UserDao userDao;
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getAllProjectAssignments(
- @HeaderParam(SH.AUTHORIZATION_STRING) final String token) {
- int userId = userTokens.verifyTokenAndReturnUserID((token));
+ @HeaderParam(SH.AUTHORIZATION_STRING) final String headerToken,
+ @QueryParam(SH.TOKEN_STRING) final String queryToken) {
+ int userId = userTokens.verifyTokenAndReturnUserID(headerToken, queryToken);
- return SH.corsResponseWithEntity(200, projectAssignmentDao.getAll());
+ return SH.responseWithEntity(200, projectAssignmentDao.getAll());
}
@POST
@Consumes(MediaType.APPLICATION_JSON)
public Response createProjectAssignments(
- @HeaderParam(SH.AUTHORIZATION_STRING) final String token,
+ @HeaderParam(SH.AUTHORIZATION_STRING) final String headerToken,
+ @QueryParam(SH.TOKEN_STRING) final String queryToken,
final ProjectAssignment projectAssignment) {
- int userId = userTokens.verifyTokenAndReturnUserID((token));
+ int userId = userTokens.verifyTokenAndReturnUserID(headerToken, queryToken);
projectAssignmentDao.create(projectAssignment);
- return SH.corsResponse(201);
+ return SH.response(201);
}
@PUT
@Path("{user_id}")
@Consumes(MediaType.APPLICATION_JSON)
public Response updateProjectAssignment(
- @HeaderParam(SH.AUTHORIZATION_STRING) final String token,
+ @HeaderParam(SH.AUTHORIZATION_STRING) final String headerToken,
+ @QueryParam(SH.TOKEN_STRING) final String queryToken,
@PathParam("user_id") String id, //I added this. ProjectDao.read needs a string becuase you need to input the
//the project name. This needs to be fixed. I just need it to compile though.
- ProjectAssignment projectAssignment) {
- int userId = userTokens.verifyTokenAndReturnUserID((token));
+ final ProjectAssignment projectAssignment) {
+ int userId = userTokens.verifyTokenAndReturnUserID(headerToken, queryToken);
Project update = projectDao.read(id);
if (update == null) {
- return SH.corsResponse(404);
+ return SH.response(404);
}
projectAssignmentDao.update(projectAssignment);
- return SH.corsResponse(200);
+ return SH.response(200);
}
}
View
55 src/main/java/ca/bcit/infosys/comp4911/services/ProjectResource.java
@@ -17,97 +17,100 @@
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
+import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
-/**
- * Created by Graeme on 2/8/14.
- */
@Path("/projects")
public class ProjectResource {
@EJB
- ProjectDao projectDao;
+ private ProjectDao projectDao;
@EJB
- WorkPackageAssignmentDao workPackageAssignmentDao;
+ private WorkPackageAssignmentDao workPackageAssignmentDao;
@EJB
- ProjectAssignmentDao projectAssignmentDao;
+ private ProjectAssignmentDao projectAssignmentDao;
@EJB
- UserDao userDao;
+ private UserDao userDao;
@EJB
- UserTokens userTokens;
+ private UserTokens userTokens;
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response retrieveAllProjects(
- @HeaderParam(SH.AUTHORIZATION_STRING) final String token) {
- int userId = userTokens.verifyTokenAndReturnUserID((token));
+ @HeaderParam(SH.AUTHORIZATION_STRING) final String headerToken,
+ @QueryParam(SH.TOKEN_STRING) final String queryToken) {
+ int userId = userTokens.verifyTokenAndReturnUserID(headerToken, queryToken);
- return SH.corsResponseWithEntity(200, projectDao.getAll());
+ return SH.responseWithEntity(200, projectDao.getAll());
}
@POST
@Consumes(MediaType.APPLICATION_JSON)
public Response createProject(
- @HeaderParam(SH.AUTHORIZATION_STRING) final String token,
+ @HeaderParam(SH.AUTHORIZATION_STRING) final String headerToken,
+ @QueryParam(SH.TOKEN_STRING) final String queryToken,
final Project project) {
- int userId = userTokens.verifyTokenAndReturnUserID((token));
+ int userId = userTokens.verifyTokenAndReturnUserID(headerToken, queryToken);
projectDao.create(project);
- return SH.corsResponse(201);
+ return SH.response(201);
}
@GET
@Path("{project_number}")
@Produces(MediaType.APPLICATION_JSON)
public Response retrieveProject(
- @HeaderParam(SH.AUTHORIZATION_STRING) final String token,
+ @HeaderParam(SH.AUTHORIZATION_STRING) final String headerToken,
+ @QueryParam(SH.TOKEN_STRING) final String queryToken,
@PathParam("project_number") String id) {
- int userId = userTokens.verifyTokenAndReturnUserID((token));
+ int userId = userTokens.verifyTokenAndReturnUserID(headerToken, queryToken);
Project project = projectDao.read(id);
if (project == null) {
- return SH.corsResponse(404);
+ return SH.response(404);
}
- return SH.corsResponseWithEntity(200, project);
+ return SH.responseWithEntity(200, project);
}
@PUT
@Path("{project_number}")
@Consumes(MediaType.APPLICATION_JSON)
public Response updateProject(
- @HeaderParam(SH.AUTHORIZATION_STRING) final String token,
+ @HeaderParam(SH.AUTHORIZATION_STRING) final String headerToken,
+ @QueryParam(SH.TOKEN_STRING) final String queryToken,
@PathParam("project_number") String id,
final Project Project) {
- int userId = userTokens.verifyTokenAndReturnUserID((token));
+ int userId = userTokens.verifyTokenAndReturnUserID(headerToken, queryToken);
Project check = projectDao.read(id);
if (check == null) {
- return SH.corsResponse(404);
+ return SH.response(404);
}
projectDao.update(Project);
- return SH.corsResponse(200);
+ return SH.response(200);
}
@GET
@Path("{project_number}/users")
@Produces(MediaType.APPLICATION_JSON)
public Response getUsersForProject(
- @HeaderParam(SH.AUTHORIZATION_STRING) final String token,
+ @HeaderParam(SH.AUTHORIZATION_STRING) final String headerToken,
+ @QueryParam(SH.TOKEN_STRING) final String queryToken,
@PathParam("project_number") final String id) {
- int userId = userTokens.verifyTokenAndReturnUserID((token));
+ int userId = userTokens.verifyTokenAndReturnUserID(headerToken, queryToken);
Project check = projectDao.read(id);
if (check == null) {
- return SH.corsResponse(404);
+ return SH.response(404);
}
- return SH.corsResponseWithEntity(200, projectAssignmentDao.getAllUsers(id));
+ return SH.responseWithEntity(200, projectAssignmentDao.getAllUsers(id));
}
}
View
51 src/main/java/ca/bcit/infosys/comp4911/services/StatusReportResource.java
@@ -16,6 +16,7 @@
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
+import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
@@ -26,90 +27,94 @@
public class StatusReportResource {
@EJB
- UserTokens userTokens;
+ private UserTokens userTokens;
@EJB
- WorkPackageDao workPackageDao;
+ private WorkPackageDao workPackageDao;
@EJB
- WorkPackageStatusReportDao workPackageStatusReportDao;
+ private WorkPackageStatusReportDao workPackageStatusReportDao;
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response retrieveWorkPackageResponseReports(
- @HeaderParam(SH.AUTHORIZATION_STRING) final String token,
+ @HeaderParam(SH.AUTHORIZATION_STRING) final String headerToken,
+ @QueryParam(SH.TOKEN_STRING) final String queryToken,
@PathParam("id") final String id) {
- int userId = userTokens.verifyTokenAndReturnUserID((token));
+ int userId = userTokens.verifyTokenAndReturnUserID(headerToken, queryToken);
WorkPackage check = workPackageDao.read(id);
if (check == null) {
- return SH.corsResponse(404);
+ return SH.response(404);
}
- return SH.corsResponseWithEntity(200, workPackageStatusReportDao.getAll());
+ return SH.responseWithEntity(200, workPackageStatusReportDao.getAll());
}
@POST
@Consumes(MediaType.APPLICATION_JSON)
public Response createWorkPackageResponseReport(
- @HeaderParam(SH.AUTHORIZATION_STRING) final String token,
+ @HeaderParam(SH.AUTHORIZATION_STRING) final String headerToken,
+ @QueryParam(SH.TOKEN_STRING) final String queryToken,
@PathParam("id") String id,
- WorkPackageStatusReport workPackageStatusReport) {
- int userId = userTokens.verifyTokenAndReturnUserID((token));
+ final WorkPackageStatusReport workPackageStatusReport) {
+ int userId = userTokens.verifyTokenAndReturnUserID(headerToken, queryToken);
WorkPackage check = workPackageDao.read(id);
if (check == null) {
- return SH.corsResponse(404);
+ return SH.response(404);
}
workPackageStatusReportDao.create(workPackageStatusReport);
- return SH.corsResponse(201);
+ return SH.response(201);
}
@GET
@Path("{report_id}")
@Produces(MediaType.APPLICATION_JSON)
public Response retrieveWorkPackageStatusReport(
- @HeaderParam(SH.AUTHORIZATION_STRING) final String token,
+ @HeaderParam(SH.AUTHORIZATION_STRING) final String headerToken,
+ @QueryParam(SH.TOKEN_STRING) final String queryToken,
@PathParam("id") String workPackageId,
@PathParam("report_id") Integer reportId) {
- int userId = userTokens.verifyTokenAndReturnUserID((token));
+ int userId = userTokens.verifyTokenAndReturnUserID(headerToken, queryToken);
WorkPackage check = workPackageDao.read(workPackageId);
if (check == null) {
- return SH.corsResponse(404);
+ return SH.response(404);
}
WorkPackageStatusReport workPackageStatusReport = workPackageStatusReportDao.read(reportId);
if (workPackageStatusReport == null) {
- return SH.corsResponse(404);
+ return SH.response(404);
}
- return SH.corsResponseWithEntity(200, workPackageStatusReport);
+ return SH.responseWithEntity(200, workPackageStatusReport);
}
@PUT
@Path("{report_id}")
@Consumes(MediaType.APPLICATION_JSON)
public Response updateWorkPackageResponseReport(
- @HeaderParam(SH.AUTHORIZATION_STRING) final String token,
+ @HeaderParam(SH.AUTHORIZATION_STRING) final String headerToken,
+ @QueryParam(SH.TOKEN_STRING) final String queryToken,
@PathParam("id") String workPackageId,
@PathParam("report_id") Integer reportId,
- WorkPackageStatusReport workPackageStatusReport) {
- int userId = userTokens.verifyTokenAndReturnUserID((token));
+ final WorkPackageStatusReport workPackageStatusReport) {
+ int userId = userTokens.verifyTokenAndReturnUserID(headerToken, queryToken);
WorkPackage check = workPackageDao.read(workPackageId);
if (check == null) {
- return SH.corsResponse(404);
+ return SH.response(404);
}
WorkPackageStatusReport checkReport = workPackageStatusReportDao.read(reportId);
if (checkReport == null) {
- return SH.corsResponse(404);
+ return SH.response(404);
}
workPackageStatusReportDao.update(workPackageStatusReport);
- return SH.corsResponse(200);
+ return SH.response(200);
}
}
View
2  src/main/java/ca/bcit/infosys/comp4911/services/StatusResource.java
@@ -30,7 +30,7 @@ public Response jsonTest() {
JSONObject jsonObject = new JSONObject();
jsonObject.put("test", "success");
- return SH.corsResponseWithEntity(200, jsonObject.toString());
+ return SH.responseWithEntity(200, jsonObject.toString());
}
}
View
42 src/main/java/ca/bcit/infosys/comp4911/services/TimesheetResource.java
@@ -25,23 +25,24 @@
public class TimesheetResource {
@EJB
- TimesheetDao timesheetDao;
+ private TimesheetDao timesheetDao;
@EJB
- UserTokens userTokens;
+ private UserTokens userTokens;
// we're going to have to mix this in with the timesheetRowDao as well
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response retrieveAllTimesheets(
- @HeaderParam(SH.AUTHORIZATION_STRING) final String token,
+ @HeaderParam(SH.AUTHORIZATION_STRING) final String headerToken,
+ @QueryParam(SH.TOKEN_STRING) final String queryToken,
@QueryParam("filter") final String filter) {
- int userId = 1; //userTokens.verifyTokenAndReturnUserID((token));
+ int userId = userTokens.verifyTokenAndReturnUserID(headerToken, queryToken);
if (filter != null) {
if (filter.equals("current")) {
Timesheet timesheet = timesheetDao.getByDate(SH.getCurrentWeek(), SH.getCurrentYear(), userId);
- return SH.corsResponseWithEntity(200, timesheet);
+ return SH.responseWithEntity(200, timesheet);
}
if (filter.equals("default")) {
//Timesheet timesheet = timesheetDao.getByDate(54, userId); Not sure where we'll store the default sheet
@@ -49,18 +50,19 @@ public Response retrieveAllTimesheets(
}
// if no filter return all the timesheets
- return SH.corsResponseWithEntity(200, timesheetDao.getAll());
+ return SH.responseWithEntity(200, timesheetDao.getAll());
}
@POST
@Consumes(MediaType.APPLICATION_JSON)
public Response createTimesheet(
- //@HeaderParam(SH.AUTHORIZATION_STRING) final String token
- Timesheet timesheet
- ) {
- int userId = 1; //userTokens.verifyTokenAndReturnUserID((token))
+ @HeaderParam(SH.AUTHORIZATION_STRING) final String headerToken,
+ @QueryParam(SH.TOKEN_STRING) final String queryToken,
+ final Timesheet timesheet) {
+ int userId = userTokens.verifyTokenAndReturnUserID(headerToken, queryToken);
timesheetDao.create(timesheet);
- return SH.corsResponse(201);
+
+ return SH.response(201);
}
/**
@@ -70,31 +72,33 @@ public Response createTimesheet(
@Path("{id}")
@Produces(MediaType.APPLICATION_JSON)
public Response retrieveTimesheet(
- @HeaderParam(SH.AUTHORIZATION_STRING) final String token,
+ @HeaderParam(SH.AUTHORIZATION_STRING) final String headerToken,
+ @QueryParam(SH.TOKEN_STRING) final String queryToken,
@PathParam("id") Integer id) {
- int userId = userTokens.verifyTokenAndReturnUserID((token));
+ int userId = userTokens.verifyTokenAndReturnUserID(headerToken, queryToken);
Timesheet timesheet = timesheetDao.read(id);
if (timesheet == null) {
- return SH.corsResponse(404);
+ return SH.response(404);
}
- return SH.corsResponseWithEntity(200, timesheet);
+ return SH.responseWithEntity(200, timesheet);
}
@PUT
@Path("{id}")
@Consumes(MediaType.APPLICATION_JSON)
public Response updateTimesheet(
- @HeaderParam(SH.AUTHORIZATION_STRING) final String token,
+ @HeaderParam(SH.AUTHORIZATION_STRING) final String headerToken,
+ @QueryParam(SH.TOKEN_STRING) final String queryToken,
@PathParam("id") Integer id, Timesheet timesheet) {
- int userId = userTokens.verifyTokenAndReturnUserID((token));
+ int userId = userTokens.verifyTokenAndReturnUserID(headerToken, queryToken);
Timesheet update = timesheetDao.read(id);
if (update == null) {
- return SH.corsResponse(404);
+ return SH.response(404);
}
timesheetDao.update(timesheet);
- return SH.corsResponse(200);
+ return SH.response(200);
}
}
View
48 src/main/java/ca/bcit/infosys/comp4911/services/UserResource.java
@@ -44,11 +44,10 @@
@Produces(MediaType.APPLICATION_JSON)
public Response retrieveAuthenticatedUserInfo(
@HeaderParam(SH.AUTHORIZATION_STRING) final String headerToken,
- @QueryParam(SH.TOKEN) final String queryToken) {
- String token = SH.processHeaderQueryToken(headerToken, queryToken);
- int userId = userTokens.verifyTokenAndReturnUserID(token);
+ @QueryParam(SH.TOKEN_STRING) final String queryToken) {
+ int userId = userTokens.verifyTokenAndReturnUserID(headerToken, queryToken);
- return SH.corsResponseWithEntity(200, userDao.read(userId));
+ return SH.responseWithEntity(200, userDao.read(userId));
}
@Path("/token")
@@ -57,7 +56,7 @@ public Response retrieveAuthenticatedUserInfo(
public Response retrieveToken(
@HeaderParam(SH.AUTHORIZATION_STRING) final String headerAuth) {
if (Strings.isNullOrEmpty(headerAuth)) {
- throw new WebApplicationException(SH.corsResponse(401));
+ throw new WebApplicationException(Response.Status.UNAUTHORIZED);
}
String decodedCredentials = new String(
@@ -65,16 +64,16 @@ public Response retrieveToken(
String[] credentials = decodedCredentials.split(":");
if (credentials.length != 2) {
- throw new WebApplicationException(SH.corsResponse(400));
+ throw new WebApplicationException(Response.Status.BAD_REQUEST);
}
- for(String credential:credentials){
- if(Strings.isNullOrEmpty(credential)){
- throw new WebApplicationException(SH.corsResponse(400));
+ for (String credential : credentials) {
+ if (Strings.isNullOrEmpty(credential)) {
+ throw new WebApplicationException(SH.response(400));
}
}
- return SH.corsResponseWithEntity(200, performLoginAndGenerateTokenInJSON(credentials[0], credentials[1]));
+ return SH.responseWithEntity(200, performLoginAndGenerateTokenInJSON(credentials[0], credentials[1]));
}
@@ -85,10 +84,10 @@ public Response retrieveToken(
public Response retrieveToken(User user) {
if (user == null || Strings.isNullOrEmpty(user.getUsername()) ||
Strings.isNullOrEmpty(user.getPassword())) {
- throw new WebApplicationException(SH.corsResponse(400));
+ throw new WebApplicationException(Response.Status.BAD_REQUEST);
}
- return SH.corsResponseWithEntity(200, performLoginAndGenerateTokenInJSON(user.getUsername(), user.getPassword()));
+ return SH.responseWithEntity(200, performLoginAndGenerateTokenInJSON(user.getUsername(), user.getPassword()));
}
@@ -96,44 +95,39 @@ public Response retrieveToken(User user) {
@DELETE
public Response invalidateToken(
@HeaderParam(SH.AUTHORIZATION_STRING) final String headerToken,
- @QueryParam(SH.TOKEN) final String queryToken) {
- String token = SH.processHeaderQueryToken(headerToken, queryToken);
- if (!userTokens.clearToken(token)) {
- throw new WebApplicationException(SH.corsResponse(401));
- }
+ @QueryParam(SH.TOKEN_STRING) final String queryToken) {
+ userTokens.clearToken(headerToken, queryToken);
- return SH.corsResponse(204);
+ return SH.response(204);
}
@Path("/projects")
@GET
public Response retrieveAllProjectsAssignedToUser(
@HeaderParam(SH.AUTHORIZATION_STRING) final String headerToken,
- @QueryParam(SH.TOKEN) final String queryToken) {
- String token = SH.processHeaderQueryToken(headerToken, queryToken);
- int userId = userTokens.verifyTokenAndReturnUserID(token);
+ @QueryParam(SH.TOKEN_STRING) final String queryToken) {
+ int userId = userTokens.verifyTokenAndReturnUserID(headerToken, queryToken);
// TODO: Get projects of user.
- return SH.corsResponseWithEntity(200,projectDao.getAll());
+ return SH.responseWithEntity(200, projectDao.getAll());
}
@Path("/work_packages")
@GET
public Response retrieveAllWorkPackagesAssignedToUser(
@HeaderParam(SH.AUTHORIZATION_STRING) final String headerToken,
- @QueryParam(SH.TOKEN) final String queryToken) {
- String token = SH.processHeaderQueryToken(headerToken, queryToken);
- int userId = userTokens.verifyTokenAndReturnUserID(token);
+ @QueryParam(SH.TOKEN_STRING) final String queryToken) {
+ int userId = userTokens.verifyTokenAndReturnUserID(headerToken, queryToken);
// TODO: Get work packages of user.
- return SH.corsResponseWithEntity(200,workPackageDao.getAll());
+ return SH.responseWithEntity(200, workPackageDao.getAll());
}
private String performLoginAndGenerateTokenInJSON(final String username, final String password) {
Optional<User> authenticatedUser = userDao.authenticate(username, password);
if (!authenticatedUser.isPresent()) {
- throw new WebApplicationException(SH.corsResponse(401));
+ throw new WebApplicationException(Response.Status.UNAUTHORIZED);
}
// Create a response with userId and token
View
32 src/main/java/ca/bcit/infosys/comp4911/services/UserTimesheetsResource.java
@@ -2,7 +2,6 @@
import ca.bcit.infosys.comp4911.access.TimesheetDao;
import ca.bcit.infosys.comp4911.application.UserTokens;
-import ca.bcit.infosys.comp4911.domain.Timesheet;
import ca.bcit.infosys.comp4911.helper.SH;
import javax.ejb.EJB;
@@ -14,7 +13,6 @@
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
-import java.util.List;
@Path("/user/timesheets")
public class UserTimesheetsResource {
@@ -29,17 +27,16 @@
@Produces(MediaType.APPLICATION_JSON)
public Response retrieveAllTimesheetsForUser(
@HeaderParam(SH.AUTHORIZATION_STRING) final String headerToken,
- @QueryParam(SH.TOKEN) final String queryToken,
+ @QueryParam(SH.TOKEN_STRING) final String queryToken,
@QueryParam("filter") final String timesheetsFilter) {
- String token = SH.processHeaderQueryToken(headerToken, queryToken);
- int userId = userTokens.verifyTokenAndReturnUserID(token);
+ int userId = userTokens.verifyTokenAndReturnUserID(headerToken, queryToken);
if (timesheetsFilter.equals("current")) {
- return SH.corsResponseWithEntity(200, timesheetDao.getByDate(
+ return SH.responseWithEntity(200, timesheetDao.getByDate(
SH.getCurrentWeek(), SH.getCurrentYear(), userId));
} else {
// TODO: Get timesheet for user.
- return SH.corsResponseWithEntity(200, timesheetDao.getAll());
+ return SH.responseWithEntity(200, timesheetDao.getAll());
}
}
@@ -48,13 +45,12 @@ public Response retrieveAllTimesheetsForUser(
@Produces(MediaType.APPLICATION_JSON)
public Response createTimesheetForUser(
@HeaderParam(SH.AUTHORIZATION_STRING) final String headerToken,
- @QueryParam(SH.TOKEN) final String queryToken) {
- String token = SH.processHeaderQueryToken(headerToken, queryToken);
- int userId = userTokens.verifyTokenAndReturnUserID(token);
+ @QueryParam(SH.TOKEN_STRING) final String queryToken) {
+ int userId = userTokens.verifyTokenAndReturnUserID(headerToken, queryToken);
//TODO: create a new timesheet, persist it and return the timesheet in the body.
- return SH.corsResponseWithEntity(201, null);
+ return SH.responseWithEntity(201, null);
}
@Path("/rejected")
@@ -62,11 +58,10 @@ public Response createTimesheetForUser(
@Produces(MediaType.APPLICATION_JSON)
public Response retrieveAllRejectedTimesheetsForUser(
@HeaderParam(SH.AUTHORIZATION_STRING) final String headerToken,
- @QueryParam(SH.TOKEN) final String queryToken) {
- String token = SH.processHeaderQueryToken(headerToken, queryToken);
- int userId = userTokens.verifyTokenAndReturnUserID(token);
+ @QueryParam(SH.TOKEN_STRING) final String queryToken) {
+ int userId = userTokens.verifyTokenAndReturnUserID(headerToken, queryToken);
- return SH.corsResponseWithEntity(200, timesheetDao.getRejected(userId));
+ return SH.responseWithEntity(200, timesheetDao.getRejected(userId));
}
@Path("/to_be_approved")
@@ -74,12 +69,11 @@ public Response retrieveAllRejectedTimesheetsForUser(
@Produces(MediaType.APPLICATION_JSON)
public Response retrieveAllTimesheetsNeedApprovalByUser(
@HeaderParam(SH.AUTHORIZATION_STRING) final String headerToken,
- @QueryParam(SH.TOKEN) final String queryToken) {
- String token = SH.processHeaderQueryToken(headerToken, queryToken);
- int userId = userTokens.verifyTokenAndReturnUserID(token);
+ @QueryParam(SH.TOKEN_STRING) final String queryToken) {
+ int userId = userTokens.verifyTokenAndReturnUserID(headerToken, queryToken);
//TODO
- return SH.corsResponseWithEntity(200, null);
+ return SH.responseWithEntity(200, null);
}
}
View
38 src/main/java/ca/bcit/infosys/comp4911/services/UsersResource.java
@@ -22,64 +22,66 @@
public class UsersResource {
@EJB
- UserDao userDao;
+ private UserDao userDao;
@EJB
- UserTokens userTokens;
+ private UserTokens userTokens;
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response retrieveAllUsers(
- @HeaderParam(SH.AUTHORIZATION_STRING) final String token
- //@QueryParam("filter") final String filter
- ) {
- int userId = userTokens.verifyTokenAndReturnUserID(token);
- return SH.corsResponseWithEntity(200, userDao.getAll());
+ @HeaderParam(SH.AUTHORIZATION_STRING) final String headerToken,
+ @QueryParam(SH.TOKEN_STRING) final String queryToken) {
+ int userId = userTokens.verifyTokenAndReturnUserID(headerToken, queryToken);
+ return SH.responseWithEntity(200, userDao.getAll());
}
@POST
@Consumes(MediaType.APPLICATION_JSON)
public Response createUser(
- @HeaderParam(SH.AUTHORIZATION_STRING) final String token,
+ @HeaderParam(SH.AUTHORIZATION_STRING) final String headerToken,
+ @QueryParam(SH.TOKEN_STRING) final String queryToken,
final User user) {
- int userId = userTokens.verifyTokenAndReturnUserID(token);
+ int userId = userTokens.verifyTokenAndReturnUserID(headerToken, queryToken);
userDao.create(user);
- return SH.corsResponse(201);
+ return SH.response(201);
}
@GET
@Path("{id}")
@Produces(MediaType.APPLICATION_JSON)
public Response retrieveUser(
- @HeaderParam(SH.AUTHORIZATION_STRING) final String token,
+ @HeaderParam(SH.AUTHORIZATION_STRING) final String headerToken,
+ @QueryParam(SH.TOKEN_STRING) final String queryToken,
@PathParam("id") final Integer id) {
- int userId = userTokens.verifyTokenAndReturnUserID(token);
+ int userId = userTokens.verifyTokenAndReturnUserID(headerToken, queryToken);
User user = userDao.read(id);
if (user == null) {
- return SH.corsResponse(404);
+ return SH.response(404);
}
- return SH.corsResponseWithEntity(200, user);
+ return SH.responseWithEntity(200, user);
}
@PUT
@Path("{id}")
@Consumes(MediaType.APPLICATION_JSON)
public Response updateUser(
- @HeaderParam(SH.AUTHORIZATION_STRING) final String token,
+ @HeaderParam(SH.AUTHORIZATION_STRING) final String headerToken,
+ @QueryParam(SH.TOKEN_STRING) final String queryToken,
@PathParam("id") final Integer id,
final User user) {
- int userId = userTokens.verifyTokenAndReturnUserID(token);
+ int userId = userTokens.verifyTokenAndReturnUserID(headerToken, queryToken);
User check = userDao.read(id);
if (check == null) {
- return SH.corsResponse(404);
+ return SH.response(404);
}
userDao.update(user);
- return SH.corsResponse(200);
+ return SH.response(200);
}
}
View
42 src/main/java/ca/bcit/infosys/comp4911/services/WorkPackageAssignmentResource.java
@@ -18,6 +18,7 @@
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
+import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.List;
@@ -28,72 +29,75 @@
@Path("/work_packages/{id}/assignments")
public class WorkPackageAssignmentResource {
@EJB
- UserTokens userTokens;
+ private UserTokens userTokens;
@EJB
- WorkPackageAssignmentDao workPackageAssignmentDao;
+ private WorkPackageAssignmentDao workPackageAssignmentDao;
@EJB
- WorkPackageDao workPackageDao;
+ private WorkPackageDao workPackageDao;
@EJB
- UserDao userDao;
+ private UserDao userDao;
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getAllWorkPackageAssignments(
- @HeaderParam(SH.AUTHORIZATION_STRING) final String token) {
- int userId = userTokens.verifyTokenAndReturnUserID(token);
+ @HeaderParam(SH.AUTHORIZATION_STRING) final String headerToken,
+ @QueryParam(SH.TOKEN_STRING) final String queryToken) {
+ int userId = userTokens.verifyTokenAndReturnUserID(headerToken, queryToken);
- return SH.corsResponseWithEntity(200, workPackageAssignmentDao.getAll());
+ return SH.responseWithEntity(200, workPackageAssignmentDao.getAll());
}
@POST
@Consumes(MediaType.APPLICATION_JSON)
public Response createWorkPackageAssignment(
- @HeaderParam(SH.AUTHORIZATION_STRING) final String token,
+ @HeaderParam(SH.AUTHORIZATION_STRING) final String headerToken,
+ @QueryParam(SH.TOKEN_STRING) final String queryToken,
@PathParam("id") Integer id,
- WorkPackageAssignment workPackageAssignment) {
- int userId = userTokens.verifyTokenAndReturnUserID((token));
+ final WorkPackageAssignment workPackageAssignment) {
+ int userId = userTokens.verifyTokenAndReturnUserID(headerToken, queryToken);
WorkPackageAssignment update = workPackageAssignmentDao.read(id);
if (update == null) {
- return SH.corsResponse(404);
+ return SH.response(404);
}
workPackageAssignmentDao.create(workPackageAssignment);
- return SH.corsResponse(201);
+ return SH.response(201);
}
@PUT
@Path("{user_id}")
@Consumes(MediaType.APPLICATION_JSON)
public Response updateWorkPackageAssignment(
- @HeaderParam(SH.AUTHORIZATION_STRING) final String token,
+ @HeaderParam(SH.AUTHORIZATION_STRING) final String headerToken,
+ @QueryParam(SH.TOKEN_STRING) final String queryToken,
@PathParam("user_id") Integer id,
@PathParam("id") String wpId,
- WorkPackageAssignment workPackageAssignment) {
- int userId = userTokens.verifyTokenAndReturnUserID((token));
+ final WorkPackageAssignment workPackageAssignment) {
+ int userId = userTokens.verifyTokenAndReturnUserID(headerToken, queryToken);
WorkPackage workPackage = workPackageDao.read(wpId);
if (workPackage == null) {
- return SH.corsResponse(404);
+ return SH.response(404);
}
User user = userDao.read(id);
if (user == null) {
- return SH.corsResponse(404);
+ return SH.response(404);
}
// See WorkPackageAssignmentDao for explanation
List<WorkPackageAssignment> wpAssignmentList =
workPackageAssignmentDao.getByUserAndWorkPackage(workPackage, user);
if (wpAssignmentList == null) {
- return SH.corsResponse(404);
+ return SH.response(404);
}
workPackageAssignmentDao.update(workPackageAssignment);
- return SH.corsResponse(200);
+ return SH.response(200);
}
}
View
55 src/main/java/ca/bcit/infosys/comp4911/services/WorkPackageResource.java
@@ -16,90 +16,93 @@
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
+import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
-/**
- * Created by Graeme on 2/8/14.
- */
@Path("/work_packages")
public class WorkPackageResource {
@EJB
- WorkPackageDao workPackageDao;
+ private WorkPackageDao workPackageDao;
@EJB
- WorkPackageStatusReportDao workPackageStatusReportDao;
+ private WorkPackageStatusReportDao workPackageStatusReportDao;
@EJB
- UserTokens userTokens;
+ private UserTokens userTokens;
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response retrieveAllWorkPackages(
- @HeaderParam(SH.AUTHORIZATION_STRING) final String token) {
- int userId = userTokens.verifyTokenAndReturnUserID((token));
+ @HeaderParam(SH.AUTHORIZATION_STRING) final String headerToken,
+ @QueryParam(SH.TOKEN_STRING) final String queryToken) {
+ int userId = userTokens.verifyTokenAndReturnUserID(headerToken, queryToken);
- return SH.corsResponseWithEntity(200, workPackageDao.getAll());
+ return SH.responseWithEntity(200, workPackageDao.getAll());
}
@GET
@Path("{workpackage_number}")
@Produces(MediaType.APPLICATION_JSON)
public Response retrieveWorkPackage(
- @HeaderParam(SH.AUTHORIZATION_STRING) final String token,
+ @HeaderParam(SH.AUTHORIZATION_STRING) final String headerToken,
+ @QueryParam(SH.TOKEN_STRING) final String queryToken,
@PathParam("workpackage_number") String workpackageNumber) {
- int userId = userTokens.verifyTokenAndReturnUserID((token));
+ int userId = userTokens.verifyTokenAndReturnUserID(headerToken, queryToken);
WorkPackage workPackage = workPackageDao.read(workpackageNumber);
if (workPackage == null) {
- return SH.corsResponse(404);
+ return SH.response(404);
}
- return SH.corsResponseWithEntity(200, workPackage);
+ return SH.responseWithEntity(200, workPackage);
}
@POST
@Consumes(MediaType.APPLICATION_JSON)
public Response createPayRate(
- @HeaderParam(SH.AUTHORIZATION_STRING) final String token,
- WorkPackage workPackage) {
- int userId = userTokens.verifyTokenAndReturnUserID((token));
+ @HeaderParam(SH.AUTHORIZATION_STRING) final String headerToken,
+ @QueryParam(SH.TOKEN_STRING) final String queryToken,
+ final WorkPackage workPackage) {
+ int userId = userTokens.verifyTokenAndReturnUserID(headerToken, queryToken);
workPackageDao.create(workPackage);
- return SH.corsResponse(201);
+ return SH.response(201);
}
@PUT
@Path("{workpackage_number}")
@Consumes(MediaType.APPLICATION_JSON)
public Response updateWorkPackage(
- @HeaderParam(SH.AUTHORIZATION_STRING) final String token,
+ @HeaderParam(SH.AUTHORIZATION_STRING) final String headerToken,
+ @QueryParam(SH.TOKEN_STRING) final String queryToken,
@PathParam("workpackage_number") String workpackageNumber,
- WorkPackage workPackage) {
- int userId = userTokens.verifyTokenAndReturnUserID((token));
+ final WorkPackage workPackage) {
+ int userId = userTokens.verifyTokenAndReturnUserID(headerToken, queryToken);
WorkPackage workPackageUpdate = workPackageDao.read(workpackageNumber);
if (workPackageUpdate == null) {
- return SH.corsResponse(404);
+ return SH.response(404);
}
workPackageDao.update(workPackage);
- return SH.corsResponse(200);
+ return SH.response(200);
}
@DELETE
@Path("{workpackage_number}")
public Response deleteWorkPackage(
- @HeaderParam(SH.AUTHORIZATION_STRING) final String token,
+ @HeaderParam(SH.AUTHORIZATION_STRING) final String headerToken,
+ @QueryParam(SH.TOKEN_STRING) final String queryToken,
@PathParam("workpackage_number") String workpackageNumber) {
- int userId = userTokens.verifyTokenAndReturnUserID((token));
+ int userId = userTokens.verifyTokenAndReturnUserID(headerToken, queryToken);
WorkPackage workPackage = workPackageDao.read(workpackageNumber);
if (workPackage == null) {
- return SH.corsResponse(404);
+ return SH.response(404);
}
workPackageDao.delete(workPackage);
- return SH.corsResponse(204);
+ return SH.response(204);
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.