Commit d658374e authored by Michael Schimpelsberger's avatar Michael Schimpelsberger
Browse files

Status Codes laut Testplan und entsprechende ExceptionMapper

implementiert
parent f895f0d9
......@@ -10,9 +10,10 @@ import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import university.at.jku.ce.dao.StudentDao;
import university.at.jku.ce.exception.NotFoundException;
import javax.ws.rs.BadRequestException;
import javax.ws.rs.NotFoundException;
import javax.ws.rs.ext.Provider;
import university.at.jku.ce.model.Student;
import university.at.jku.ce.model.Study;
......@@ -239,7 +240,7 @@ public class H2StudentDao implements StudentDao {
con.close();
} catch(SQLException se) {
se.printStackTrace();
throw new RuntimeException();
throw new BadRequestException();
} catch(Exception e) {
//Handle errors for Class.forName
e.printStackTrace();
......@@ -329,7 +330,7 @@ public class H2StudentDao implements StudentDao {
} catch(SQLException se) {
//Handle errors for JDBC
se.printStackTrace();
throw new RuntimeException();
throw new BadRequestException();
} catch(Exception e) {
//Handle errors for Class.forName
e.printStackTrace();
......@@ -427,7 +428,7 @@ public class H2StudentDao implements StudentDao {
con.close();
} catch(SQLException se) {
se.printStackTrace();
throw new RuntimeException();
throw new BadRequestException();
} catch(Exception e) {
//Handle errors for Class.forName
e.printStackTrace();
......
......@@ -12,7 +12,10 @@ import java.util.List;
import university.at.jku.ce.dao.StudyDao;
import university.at.jku.ce.model.Student;
import university.at.jku.ce.model.Study;
import university.at.jku.ce.exception.NotFoundException;
import javax.ws.rs.BadRequestException;
import javax.ws.rs.NotFoundException;
import javax.ws.rs.ext.Provider;
public class H2StudyDao implements StudyDao{
......@@ -246,10 +249,11 @@ public class H2StudyDao implements StudyDao{
} catch(SQLException se) {
//Handle errors for JDBC
se.printStackTrace();
throw new RuntimeException();
throw new BadRequestException();
} catch(Exception e) {
//Handle errors for Class.forName
e.printStackTrace();
e.printStackTrace();
throw new RuntimeException();
} finally {
//finally block used to close resources
try{
......
......@@ -10,7 +10,10 @@ import java.util.ArrayList;
import java.util.List;
import university.at.jku.ce.dao.SubjectDao;
import university.at.jku.ce.exception.NotFoundException;
import javax.ws.rs.BadRequestException;
import javax.ws.rs.NotFoundException;
import javax.ws.rs.ext.Provider;
import university.at.jku.ce.model.Study;
import university.at.jku.ce.model.Subject;
......@@ -47,7 +50,7 @@ public class H2SubjectDao implements SubjectDao {
} catch(SQLException se) {
//Handle errors for JDBC
se.printStackTrace();
throw new RuntimeException();
throw new BadRequestException();
} catch(Exception e) {
//Handle errors for Class.forName
e.printStackTrace();
......@@ -154,7 +157,7 @@ public class H2SubjectDao implements SubjectDao {
} catch(SQLException se) {
//Handle errors for JDBC
se.printStackTrace();
throw new RuntimeException();
throw new BadRequestException();
} catch(Exception e) {
//Handle errors for Class.forName
e.printStackTrace();
......@@ -242,10 +245,11 @@ public class H2SubjectDao implements SubjectDao {
} catch(SQLException se) {
//Handle errors for JDBC
se.printStackTrace();
throw new RuntimeException();
throw new BadRequestException();
} catch(Exception e) {
//Handle errors for Class.forName
e.printStackTrace();
throw new RuntimeException();
} finally {
//finally block used to close resources
try{
......
package university.at.jku.ce.exception;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;
import javax.ws.rs.BadRequestException;
import university.at.jku.ce.model.Error;
@Provider
public class BadRequestExceptionMapper implements ExceptionMapper<BadRequestException> {
@Override
public Response toResponse(BadRequestException exception) {
return Response.status(Status.BAD_REQUEST)
.entity(new Error(exception.getMessage(),400,ExceptionParam.DOKU))
.type(MediaType.APPLICATION_JSON)
.build();
}
}
package university.at.jku.ce.exception;
public class ExceptionParam {
protected static final String NOT_FOUND="Resource not found";
protected static final String SERVER_ERROR="Internal server error";
protected static final String SERVER_ERROR="HTTP 500 Internal Server Error";
protected static final String DOKU="https://en.wikipedia.org/wiki/List_of_HTTP_status_codes";
}
package university.at.jku.ce.exception;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;
import javax.ws.rs.NotAllowedException;
import university.at.jku.ce.model.Error;
@Provider
public class MethodNotAllowedExceptionMapper implements ExceptionMapper<NotAllowedException>{
@Override
public Response toResponse(NotAllowedException exception) {
return Response.status(Status.METHOD_NOT_ALLOWED)
.entity(new Error(exception.getMessage(),405,ExceptionParam.DOKU))
.type(MediaType.APPLICATION_JSON)
.build();
}
}
package university.at.jku.ce.exception;
public class NotFoundException extends RuntimeException {
/**
*
*/
private static final long serialVersionUID = 127383111965174818L;
public NotFoundException() {
super (ExceptionParam.NOT_FOUND);
}
}
package university.at.jku.ce.exception;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;
import university.at.jku.ce.model.Error;
import javax.ws.rs.NotFoundException;
@Provider
public class NotFoundExceptionMapper implements ExceptionMapper<NotFoundException> {
......@@ -12,7 +14,8 @@ public class NotFoundExceptionMapper implements ExceptionMapper<NotFoundExceptio
@Override
public Response toResponse(NotFoundException exception) {
return Response.status(Status.NOT_FOUND)
.entity(new Error(exception.getMessage(),404,ExceptionParam.DOKU))
.entity(new Error(exception.getMessage(),404,ExceptionParam.DOKU))
.type(MediaType.APPLICATION_JSON)
.build();
}
......
......@@ -18,7 +18,10 @@ import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
import university.at.jku.ce.dao.StudentDao;
import university.at.jku.ce.dao.StudyDao;
import university.at.jku.ce.dao.h2dao.H2StudentDao;
import university.at.jku.ce.dao.h2dao.H2StudyDao;
import javax.ws.rs.BadRequestException;
import university.at.jku.ce.model.Student;
import university.at.jku.ce.model.Study;
......@@ -28,6 +31,7 @@ public class StudentResource {
StudentDao dao =new H2StudentDao();
StudyDao studyDao=new H2StudyDao();
@GET
@Produces(MediaType.APPLICATION_JSON)
......@@ -41,6 +45,8 @@ public class StudentResource {
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response addStudent(Student student, @Context UriInfo uriInfo) {
if (student.getFirstName()==null||student.getFirstName().isEmpty()||student.getLastName()==null||student.getLastName().isEmpty())
throw new BadRequestException();
student= dao.addStudent(student);
String newMatrNr=String.valueOf(student.getMatrNr());
URI uri=uriInfo.getAbsolutePathBuilder().path(newMatrNr).build();
......@@ -60,6 +66,8 @@ public class StudentResource {
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response updateStudent(@PathParam("studentMatrNr")int matrNr,Student student) {
if (student.getFirstName()==null||student.getFirstName().isEmpty()||student.getLastName()==null||student.getLastName().isEmpty())
throw new BadRequestException();
student.setMatrNr(matrNr); //if User does not send correct id it is still used correct id from URL
student= dao.updateStudent(student);
return Response.ok().entity(student).build();
......@@ -87,6 +95,12 @@ public class StudentResource {
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Response addInscription(@PathParam("studentMatrNr")int matrNr, Study study, @Context UriInfo uriInfo) {
if (study.getName()==null||study.getName().isEmpty()||study.getStudyId()<=0)
throw new BadRequestException();
//check if study exists
Study temp=studyDao.getStudy(study.getStudyId());
//if study exists but has different name: BadRequest
if (!temp.getName().equals(study.getName())) {throw new BadRequestException();}
study= dao.addInscription(matrNr, study);
URI uri=uriInfo.getAbsolutePathBuilder().path(String.valueOf(study.getStudyId())).build();
return Response.created(uri).entity(study).build();
......
......@@ -20,6 +20,7 @@ import javax.ws.rs.core.UriInfo;
import university.at.jku.ce.dao.StudyDao;
import university.at.jku.ce.dao.h2dao.H2StudyDao;
import javax.ws.rs.BadRequestException;
import university.at.jku.ce.model.Student;
import university.at.jku.ce.model.Study;
......@@ -45,6 +46,8 @@ public class StudyResource {
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response addStudy(Study study, @Context UriInfo uriInfo) {
if (study.getName()==null||study.getName().isEmpty())
throw new BadRequestException();
study= dao.addStudy(study);
String newStudyId=String.valueOf(study.getStudyId());
URI uri=uriInfo.getAbsolutePathBuilder().path(newStudyId).build();
......@@ -65,6 +68,8 @@ public class StudyResource {
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response updateStudy(@PathParam("studyId")int studyId,Study study) {
if (study.getName()==null||study.getName().isEmpty())
throw new BadRequestException();
study.setStudyId(studyId); //if User does not send correct id it is still used correct id from URL
study= dao.updateStudy(study);
return Response.ok().entity(study).build();
......
......@@ -17,8 +17,11 @@ import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
import university.at.jku.ce.dao.StudyDao;
import university.at.jku.ce.dao.SubjectDao;
import university.at.jku.ce.dao.h2dao.H2StudyDao;
import university.at.jku.ce.dao.h2dao.H2SubjectDao;
import javax.ws.rs.BadRequestException;
import university.at.jku.ce.model.Study;
import university.at.jku.ce.model.Subject;
......@@ -27,9 +30,11 @@ import university.at.jku.ce.model.Subject;
@Consumes(MediaType.APPLICATION_JSON)
public class SubjectResource {
private SubjectDao dao=new H2SubjectDao();
private StudyDao studyDao=new H2StudyDao();
@GET
public Response getAllSubjects(@PathParam("studyId")int studyId) {
public Response getAllSubjects(@PathParam("studyId")int studyId) {
studyDao.getStudy(studyId);
List<Subject>list= dao.getAllSubjects(studyId);
GenericEntity<List<Subject>> entity=new GenericEntity<List<Subject>>(list) {};
return Response.ok(entity).build();
......@@ -37,6 +42,7 @@ public class SubjectResource {
@POST
public Response addSubject(@PathParam("studyId")int studyId, Subject subject, @Context UriInfo uriInfo) {
if (subject.getEcts()<=0||subject.getName()==null||subject.getName().isEmpty()) throw new BadRequestException();
subject.setStudyId(studyId);
subject= dao.addSubject(subject);
String newSubjectId=String.valueOf(subject.getSubjectId());
......@@ -47,6 +53,7 @@ public class SubjectResource {
@PUT
@Path("/{id}")
public Response updateSubject(@PathParam("studyId")int studyId,@PathParam("id")int subjectId, Subject subject) {
if (subject.getEcts()<=0||subject.getName()==null||subject.getName().isEmpty()) throw new BadRequestException();
subject.setStudyId(studyId);
subject.setSubjectId(subjectId);
subject= dao.updateSubject(subject);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment