Commit 42d82b31 authored by Michael Schimpelsberger's avatar Michael Schimpelsberger
Browse files

Resourcen und Exceptions als JSON und XML,

cause in Fehlermeldung wenn vorhanden, ansonsten unknown
parent 4bcbc73e
......@@ -15,8 +15,8 @@ public class BadRequestExceptionMapper implements ExceptionMapper<BadRequestExce
@Override
public Response toResponse(BadRequestException exception) {
return Response.status(Status.BAD_REQUEST)
.entity(new Error(exception.getMessage(),400,ExceptionParam.DOKU))
.type(MediaType.APPLICATION_JSON)
.entity(new Error(exception.getMessage(), exception.getCause()==null?"unknown":exception.getCause().toString(), 400,ExceptionParam.DOKU))
.type(ExceptionParam.mediaType)
.build();
}
......
package university.at.jku.ce.exception;
import javax.ws.rs.core.MediaType;
public class ExceptionParam {
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";
protected static MediaType mediaType;
public static MediaType getMediaType() {
return mediaType;
}
public static void setMediaType(MediaType mediaType) {
if (mediaType==null) {ExceptionParam.mediaType=MediaType.APPLICATION_XML_TYPE;}
else ExceptionParam.mediaType = mediaType;
}
}
......@@ -14,9 +14,8 @@ public class GenericExceptionMapper implements ExceptionMapper<RuntimeException>
@Override
public Response toResponse(RuntimeException exception) {
return Response.status(Status.INTERNAL_SERVER_ERROR)
.entity(new Error(ExceptionParam.SERVER_ERROR,500,ExceptionParam.DOKU))
.type(MediaType.APPLICATION_JSON)
.entity(new Error(ExceptionParam.SERVER_ERROR, exception.getCause()==null?"unknown":exception.getCause().toString(), 500,ExceptionParam.DOKU))
.type(ExceptionParam.mediaType)
.build();
}
}
......@@ -15,8 +15,8 @@ public class MethodNotAllowedExceptionMapper implements ExceptionMapper<NotAllow
@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)
.entity(new Error(exception.getMessage(), exception.getCause()==null?"unknown":exception.getCause().toString(), 405,ExceptionParam.DOKU))
.type(ExceptionParam.mediaType)
.build();
}
......
......@@ -15,8 +15,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))
.type(MediaType.APPLICATION_JSON)
.entity(new Error(exception.getMessage(), exception.getCause()==null?"unknown":exception.getCause().toString(), 404,ExceptionParam.DOKU))
.type(ExceptionParam.mediaType)
.build();
}
......
......@@ -5,19 +5,31 @@ import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Error {
private String message;
private String cause;
private int errorCode;
private String documentation;
public Error() {}
public Error (String message, int errorCode, String documentation) {
public Error (String message, String cause, int errorCode, String documentation) {
this.message=message;
this.cause=cause;
this.errorCode=errorCode;
this.documentation=documentation;
}
public String getCause() {
return cause;
}
public void setCause(String cause) {
this.cause = cause;
}
public String getMessage() {
return message;
}
......
......@@ -19,7 +19,7 @@ public class DBInitializerResource {
DBInitializerDao initDao=new H2DBInitializerDao();
@GET
@Produces(MediaType.APPLICATION_JSON)
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public Response dbinit() {
initDao.initDatabase();
return Response.ok().build();
......
package university.at.jku.ce.resource;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.GenericEntity;
import javax.ws.rs.core.HttpHeaders;
import java.net.URI;
import java.util.List;
......@@ -21,6 +22,8 @@ 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 university.at.jku.ce.exception.ExceptionParam;
import javax.ws.rs.BadRequestException;
import university.at.jku.ce.model.Student;
import university.at.jku.ce.model.Study;
......@@ -34,17 +37,19 @@ public class StudentResource {
StudyDao studyDao=new H2StudyDao();
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getStudents() {
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public Response getStudents(@Context HttpHeaders headers) {
ExceptionParam.setMediaType(headers.getMediaType());
List<Student>list= dao.getAllStudents();
GenericEntity<List<Student>> entity=new GenericEntity<List<Student>>(list) {};
return Response.ok(entity).build();
}
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response addStudent(Student student, @Context UriInfo uriInfo) {
@Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public Response addStudent(Student student, @Context UriInfo uriInfo, @Context HttpHeaders headers) {
ExceptionParam.setMediaType(headers.getMediaType());
if (student.getFirstName()==null||student.getFirstName().isEmpty()||student.getLastName()==null||student.getLastName().isEmpty())
throw new BadRequestException();
student= dao.addStudent(student);
......@@ -55,17 +60,19 @@ public class StudentResource {
@GET
@Path("/{studentMatrNr}")
@Produces(MediaType.APPLICATION_JSON)
public Response getStudent(@PathParam("studentMatrNr")int matrNr) {
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public Response getStudent(@PathParam("studentMatrNr")int matrNr, @Context HttpHeaders headers) {
ExceptionParam.setMediaType(headers.getMediaType());
Student student= dao.getStudent(matrNr);
return Response.ok().entity(student).build() ;
}
@PUT
@Path("/{studentMatrNr}")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response updateStudent(@PathParam("studentMatrNr")int matrNr,Student student) {
@Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public Response updateStudent(@PathParam("studentMatrNr")int matrNr,Student student, @Context HttpHeaders headers) {
ExceptionParam.setMediaType(headers.getMediaType());
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
......@@ -75,16 +82,18 @@ public class StudentResource {
@DELETE
@Path("/{studentMatrNr}")
@Produces(MediaType.APPLICATION_JSON)
public Response deleteStudent(@PathParam("studentMatrNr")int matrNr) {
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public Response deleteStudent(@PathParam("studentMatrNr")int matrNr, @Context HttpHeaders headers) {
ExceptionParam.setMediaType(headers.getMediaType());
dao.removeStudent(matrNr);
return Response.noContent().build();
}
@GET
@Path("/{studentMatrNr}/studies")
@Produces(MediaType.APPLICATION_JSON)
public Response getInscriptions(@PathParam("studentMatrNr")int matrNr) {
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public Response getInscriptions(@PathParam("studentMatrNr")int matrNr, @Context HttpHeaders headers) {
ExceptionParam.setMediaType(headers.getMediaType());
List<Study>list= dao.getInscriptions(matrNr);
GenericEntity<List<Study>> entity=new GenericEntity<List<Study>>(list) {};
return Response.ok(entity).build();
......@@ -92,9 +101,10 @@ public class StudentResource {
@POST
@Path("/{studentMatrNr}/studies")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Response addInscription(@PathParam("studentMatrNr")int matrNr, Study study, @Context UriInfo uriInfo) {
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
@Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public Response addInscription(@PathParam("studentMatrNr")int matrNr, Study study, @Context UriInfo uriInfo, @Context HttpHeaders headers) {
ExceptionParam.setMediaType(headers.getMediaType());
if (study.getName()==null||study.getName().isEmpty()||study.getStudyId()<=0)
throw new BadRequestException();
//check if study exists
......@@ -108,18 +118,19 @@ public class StudentResource {
@GET
@Path("/{studentMatrNr}/studies/{studyId}")
@Produces(MediaType.APPLICATION_JSON)
public Response getInscription(@PathParam("studentMatrNr")int matrNr, @PathParam("studyId")int studyId) {
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public Response getInscription(@PathParam("studentMatrNr")int matrNr, @PathParam("studyId")int studyId, @Context HttpHeaders headers) {
ExceptionParam.setMediaType(headers.getMediaType());
Study study= dao.getInscription(matrNr, studyId);
return Response.ok().entity(study).build() ;
}
@DELETE
@Path("/{studentMatrNr}/studies/{studyId}")
@Produces(MediaType.APPLICATION_JSON)
public Response deleteInscription(@PathParam("studentMatrNr")int matrNr,@PathParam("studyId")int studyId ) {
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public Response deleteInscription(@PathParam("studentMatrNr")int matrNr,@PathParam("studyId")int studyId, @Context HttpHeaders headers ) {
ExceptionParam.setMediaType(headers.getMediaType());
dao.removeInscription(matrNr, studyId);
return Response.noContent().build();
}
}
package university.at.jku.ce.resource;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.GenericEntity;
import javax.ws.rs.core.HttpHeaders;
import java.net.URI;
import java.util.List;
......@@ -20,6 +21,8 @@ import javax.ws.rs.core.UriInfo;
import university.at.jku.ce.dao.StudyDao;
import university.at.jku.ce.dao.h2dao.H2StudyDao;
import university.at.jku.ce.exception.ExceptionParam;
import javax.ws.rs.BadRequestException;
import university.at.jku.ce.model.Student;
import university.at.jku.ce.model.Study;
......@@ -33,8 +36,9 @@ public class StudyResource {
StudyDao dao=new H2StudyDao();
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getStudies(@QueryParam("name") String name) {
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public Response getStudies(@QueryParam("name") String name, @Context HttpHeaders headers) {
ExceptionParam.setMediaType(headers.getMediaType());
List<Study>list= dao.getAllStudies(name);
GenericEntity<List<Study>> entity=new GenericEntity<List<Study>>(list) {};
return Response.ok(entity).build();
......@@ -42,11 +46,11 @@ public class StudyResource {
}
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response addStudy(Study study, @Context UriInfo uriInfo) {
@POST
@Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public Response addStudy(Study study, @Context UriInfo uriInfo, @Context HttpHeaders headers) {
ExceptionParam.setMediaType(headers.getMediaType());
if (study.getName()==null||study.getName().isEmpty())
throw new BadRequestException();
study= dao.addStudy(study);
......@@ -58,17 +62,19 @@ public class StudyResource {
@GET
@Path("/{studyId}")
@Produces(MediaType.APPLICATION_JSON)
public Response getStudy(@PathParam("studyId")int studyId) {
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public Response getStudy(@PathParam("studyId")int studyId, @Context HttpHeaders headers) {
ExceptionParam.setMediaType(headers.getMediaType());
Study study= dao.getStudy(studyId);
return Response.ok().entity(study).build() ;
}
@PUT
@Path("/{studyId}")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response updateStudy(@PathParam("studyId")int studyId,Study study) {
@Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public Response updateStudy(@PathParam("studyId")int studyId,Study study, @Context HttpHeaders headers) {
ExceptionParam.setMediaType(headers.getMediaType());
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
......@@ -78,8 +84,9 @@ public class StudyResource {
@DELETE
@Path("/{studyId}")
@Produces(MediaType.APPLICATION_JSON)
public Response deleteStudent(@PathParam("studyId")int studyId) {
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public Response deleteStudent(@PathParam("studyId")int studyId, @Context HttpHeaders headers) {
ExceptionParam.setMediaType(headers.getMediaType());
dao.removeStudy(studyId);
return Response.noContent().build();
}
......
......@@ -13,6 +13,7 @@ import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.GenericEntity;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
......@@ -21,19 +22,22 @@ 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 university.at.jku.ce.exception.ExceptionParam;
import javax.ws.rs.BadRequestException;
import university.at.jku.ce.model.Study;
import university.at.jku.ce.model.Subject;
@Path("/")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
@Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
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, @Context HttpHeaders headers) {
ExceptionParam.setMediaType(headers.getMediaType());
studyDao.getStudy(studyId);
List<Subject>list= dao.getAllSubjects(studyId);
GenericEntity<List<Subject>> entity=new GenericEntity<List<Subject>>(list) {};
......@@ -41,7 +45,8 @@ public class SubjectResource {
}
@POST
public Response addSubject(@PathParam("studyId")int studyId, Subject subject, @Context UriInfo uriInfo) {
public Response addSubject(@PathParam("studyId")int studyId, Subject subject, @Context UriInfo uriInfo, @Context HttpHeaders headers) {
ExceptionParam.setMediaType(headers.getMediaType());
if (subject.getEcts()<=0||subject.getName()==null||subject.getName().isEmpty()) throw new BadRequestException();
subject.setStudyId(studyId);
subject= dao.addSubject(subject);
......@@ -52,7 +57,8 @@ public class SubjectResource {
@PUT
@Path("/{id}")
public Response updateSubject(@PathParam("studyId")int studyId,@PathParam("id")int subjectId, Subject subject) {
public Response updateSubject(@PathParam("studyId")int studyId,@PathParam("id")int subjectId, Subject subject, @Context HttpHeaders headers) {
ExceptionParam.setMediaType(headers.getMediaType());
if (subject.getEcts()<=0||subject.getName()==null||subject.getName().isEmpty()) throw new BadRequestException();
subject.setStudyId(studyId);
subject.setSubjectId(subjectId);
......@@ -64,14 +70,16 @@ public class SubjectResource {
@DELETE
@Path("/{id}")
public Response removeSubject(@PathParam("studyId")int studyId,@PathParam("id")int subjectId) {
public Response removeSubject(@PathParam("studyId")int studyId,@PathParam("id")int subjectId, @Context HttpHeaders headers) {
ExceptionParam.setMediaType(headers.getMediaType());
dao.removeSubject(studyId, subjectId);
return Response.noContent().build();
}
@GET
@Path("/{id}")
public Response getSubject(@PathParam("studyId")int studyId,@PathParam("id")int subjectId) {
public Response getSubject(@PathParam("studyId")int studyId,@PathParam("id")int subjectId, @Context HttpHeaders headers) {
ExceptionParam.setMediaType(headers.getMediaType());
Subject subject=dao.getSubject(studyId, subjectId);
return Response.ok().entity(subject).build();
}
......
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