Commit 960aea41 authored by Michael Schimpelsberger's avatar Michael Schimpelsberger
Browse files

http Status Codes und Header Informationen implementiert

parent 32f5b52f
No preview for this file type
This diff is collapsed.
......@@ -51,11 +51,10 @@
<artifactId>jersey-hk2</artifactId>
</dependency>
<dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-binding</artifactId>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
......
package university.at.jku.ce.h2dao;
package university.at.jku.ce.dao.h2dao;
import java.io.File;
public class DaoParam {
protected static final String DRIVER="org.h2.Driver";
//protected static final String JDBC_URL="jdbc:h2:file:./test";
protected static final String USER="sa";
protected static final String PASSWORD="";
protected static final String JDBC_URL="jdbc:h2:C:\\Users\\Michael\\development\\workspace-ce5\\at.jku.ce\\data\\Database";
......
package university.at.jku.ce.h2dao;
package university.at.jku.ce.dao.h2dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
......@@ -11,6 +11,7 @@ import java.util.List;
import java.util.Map;
import university.at.jku.ce.dao.StudentDao;
import university.at.jku.ce.exception.NotFoundException;
import university.at.jku.ce.model.Student;
import university.at.jku.ce.model.Study;
......@@ -46,6 +47,7 @@ public class H2StudentDao implements StudentDao {
} catch(SQLException se) {
//Handle errors for JDBC
se.printStackTrace();
throw new RuntimeException();
} catch(Exception e) {
//Handle errors for Class.forName
e.printStackTrace();
......@@ -64,8 +66,8 @@ public class H2StudentDao implements StudentDao {
} //end try
if (students.size()==1) {return students.get(0);}
else {
//log error
return null;
if (students.size()<1) {throw new NotFoundException();}
else throw new RuntimeException();
}
}
......@@ -91,8 +93,8 @@ public class H2StudentDao implements StudentDao {
stmt.close();
con.close();
} catch(SQLException se) {
//Handle errors for JDBC
se.printStackTrace();
se.printStackTrace();
throw new RuntimeException();
} catch(Exception e) {
//Handle errors for Class.forName
e.printStackTrace();
......@@ -109,7 +111,8 @@ public class H2StudentDao implements StudentDao {
} //end finally try
rs=null;
} //end try
return students;
if (students.size()==0) throw new NotFoundException();
return students;
}
@Override
......@@ -146,6 +149,7 @@ public class H2StudentDao implements StudentDao {
} catch(SQLException se) {
//Handle errors for JDBC
se.printStackTrace();
throw new RuntimeException();
} catch(Exception e) {
//Handle errors for Class.forName
e.printStackTrace();
......@@ -192,6 +196,7 @@ public class H2StudentDao implements StudentDao {
} catch(SQLException se) {
//Handle errors for JDBC
se.printStackTrace();
throw new RuntimeException();
} catch(Exception e) {
//Handle errors for Class.forName
e.printStackTrace();
......@@ -213,7 +218,7 @@ public class H2StudentDao implements StudentDao {
@Override
public void removeStudent(int matrNr) {
public void removeStudent(int matrNr){
try {
// STEP 1: Register JDBC driver
Class.forName(DaoParam.DRIVER);
......@@ -231,11 +236,12 @@ public class H2StudentDao implements StudentDao {
prStmt.close();
con.close();
} catch(SQLException se) {
//Handle errors for JDBC
se.printStackTrace();
se.printStackTrace();
throw new RuntimeException();
} catch(Exception e) {
//Handle errors for Class.forName
e.printStackTrace();
throw new RuntimeException();
} finally {
//finally block used to close resources
try{
......@@ -277,6 +283,7 @@ public class H2StudentDao implements StudentDao {
} catch(SQLException se) {
//Handle errors for JDBC
se.printStackTrace();
throw new RuntimeException();
} catch(Exception e) {
//Handle errors for Class.forName
e.printStackTrace();
......@@ -293,6 +300,7 @@ public class H2StudentDao implements StudentDao {
} //end finally try
rs=null;
} //end try
return inscriptions;
if (inscriptions.size()==0) throw new NotFoundException();
return inscriptions;
}
}
package university.at.jku.ce.h2dao;
package university.at.jku.ce.dao.h2dao;
import java.sql.Connection;
import java.sql.DriverManager;
......@@ -12,6 +12,7 @@ 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;
public class H2StudyDao implements StudyDao{
......@@ -54,6 +55,7 @@ public class H2StudyDao implements StudyDao{
} catch(SQLException se) {
//Handle errors for JDBC
se.printStackTrace();
throw new RuntimeException();
} catch(Exception e) {
//Handle errors for Class.forName
e.printStackTrace();
......@@ -70,7 +72,8 @@ public class H2StudyDao implements StudyDao{
} //end finally try
rs=null;
} //end try
return studies;
if (studies.size()==0) throw new NotFoundException();
return studies;
}
@Override
......@@ -116,8 +119,9 @@ public class H2StudyDao implements StudyDao{
} //end try
if (studies.size()==1) {return studies.get(0);}
else {
//log error
return null;
if (studies.size()<1) {throw new NotFoundException();}
else throw new RuntimeException();
}
}
......@@ -154,6 +158,7 @@ public class H2StudyDao implements StudyDao{
} catch(SQLException se) {
//Handle errors for JDBC
se.printStackTrace();
throw new RuntimeException();
} catch(Exception e) {
//Handle errors for Class.forName
e.printStackTrace();
......@@ -199,6 +204,7 @@ public class H2StudyDao implements StudyDao{
} catch(SQLException se) {
//Handle errors for JDBC
se.printStackTrace();
throw new RuntimeException();
} catch(Exception e) {
//Handle errors for Class.forName
e.printStackTrace();
......@@ -239,6 +245,7 @@ public class H2StudyDao implements StudyDao{
} catch(SQLException se) {
//Handle errors for JDBC
se.printStackTrace();
throw new RuntimeException();
} catch(Exception e) {
//Handle errors for Class.forName
e.printStackTrace();
......
package university.at.jku.ce.h2dao;
package university.at.jku.ce.dao.h2dao;
import java.sql.Connection;
import java.sql.DriverManager;
......@@ -10,6 +10,7 @@ import java.util.ArrayList;
import java.util.List;
import university.at.jku.ce.dao.SubjectDao;
import university.at.jku.ce.exception.NotFoundException;
import university.at.jku.ce.model.Study;
import university.at.jku.ce.model.Subject;
......@@ -46,6 +47,7 @@ public class H2SubjectDao implements SubjectDao {
} catch(SQLException se) {
//Handle errors for JDBC
se.printStackTrace();
throw new RuntimeException();
} catch(Exception e) {
//Handle errors for Class.forName
e.printStackTrace();
......@@ -62,7 +64,8 @@ public class H2SubjectDao implements SubjectDao {
} //end finally try
rs=null;
} //end try
return subjects;
if (subjects.size()==0) throw new NotFoundException();
return subjects;
}
@Override
......@@ -91,7 +94,8 @@ public class H2SubjectDao implements SubjectDao {
con.close();
} catch(SQLException se) {
//Handle errors for JDBC
se.printStackTrace();
se.printStackTrace();
throw new RuntimeException();
} catch(Exception e) {
//Handle errors for Class.forName
e.printStackTrace();
......@@ -109,7 +113,11 @@ public class H2SubjectDao implements SubjectDao {
rs=null;
} //end try
if (subjects.size()==1) return subjects.get(0);
else return null;
else {
if (subjects.size()<1) {throw new NotFoundException();}
else throw new RuntimeException();
}
}
@Override
......@@ -147,6 +155,7 @@ public class H2SubjectDao implements SubjectDao {
} catch(SQLException se) {
//Handle errors for JDBC
se.printStackTrace();
throw new RuntimeException();
} catch(Exception e) {
//Handle errors for Class.forName
e.printStackTrace();
......@@ -190,6 +199,7 @@ public class H2SubjectDao implements SubjectDao {
} catch(SQLException se) {
//Handle errors for JDBC
se.printStackTrace();
throw new RuntimeException();
} catch(Exception e) {
//Handle errors for Class.forName
e.printStackTrace();
......@@ -231,6 +241,7 @@ public class H2SubjectDao implements SubjectDao {
} catch(SQLException se) {
//Handle errors for JDBC
se.printStackTrace();
throw new RuntimeException();
} catch(Exception e) {
//Handle errors for Class.forName
e.printStackTrace();
......
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 DOKU="https://en.wikipedia.org/wiki/List_of_HTTP_status_codes";
}
package university.at.jku.ce.exception;
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;
@Provider
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))
.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.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;
@Provider
public class NotFoundExceptionMapper implements ExceptionMapper<NotFoundException> {
@Override
public Response toResponse(NotFoundException exception) {
return Response.status(Status.NOT_FOUND)
.entity(new Error(exception.getMessage(),404,ExceptionParam.DOKU))
.build();
}
}
package university.at.jku.ce.model;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Error {
private String message;
private int errorCode;
private String documentation;
public Error() {}
public Error (String message, int errorCode, String documentation) {
this.message=message;
this.errorCode=errorCode;
this.documentation=documentation;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public int getErrorCode() {
return errorCode;
}
public void setErrorCode(int errorCode) {
this.errorCode = errorCode;
}
public String getDocumentation() {
return documentation;
}
public void setDocumentation(String documentation) {
this.documentation = documentation;
}
}
......@@ -8,6 +8,7 @@ import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;
@XmlRootElement
public class Student {
private int matrNr;
......
......@@ -3,10 +3,10 @@ package university.at.jku.ce.model;
import java.util.HashMap;
import java.util.Map;
import javax.json.bind.annotation.JsonbTransient;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;
@XmlRootElement
public class Study {
private int studyId;
......
......@@ -2,6 +2,7 @@ package university.at.jku.ce.model;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Subject {
private int subjectId;
private int studyId;
......
package university.at.jku.ce.resource;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.GenericEntity;
import java.util.List;
import java.net.URI;
import java.util.List;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
......@@ -13,23 +15,24 @@ import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
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.h2dao.H2StudentDao;
import university.at.jku.ce.model.Student;
import university.at.jku.ce.model.Study;
import university.at.jku.ce.service.StudentService;
import university.at.jku.ce.service.StudentServiceImpl;
@Path("/students")
public class StudentResource {
StudentService service=new StudentServiceImpl();
StudentDao dao =new H2StudentDao();
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getStudents() {
List<Student>list= service.getAllStudents();
List<Student>list= dao.getAllStudents();
GenericEntity<List<Student>> entity=new GenericEntity<List<Student>>(list) {};
return Response.ok(entity).build();
}
......@@ -37,38 +40,46 @@ public class StudentResource {
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Student addStudent(Student student) {
return service.addStudent(student);
public Response addStudent(Student student, @Context UriInfo uriInfo) {
student= dao.addStudent(student);
String newMatrNr=String.valueOf(student.getMatrNr());
URI uri=uriInfo.getAbsolutePathBuilder().path(newMatrNr).build();
return Response.created(uri).entity(student).build();
}
@GET
@Path("/{studentMatrNr}")
@Produces(MediaType.APPLICATION_JSON)
public Student getStudent(@PathParam("studentMatrNr")int matrNr) {
return service.getStudent(matrNr);
public Response getStudent(@PathParam("studentMatrNr")int matrNr) {
Student student= dao.getStudent(matrNr);
return Response.ok().entity(student).build() ;
}
@PUT
@Path("/{studentMatrNr}")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Student updateStudent(@PathParam("studentMatrNr")int matrNr,Student student) {
public Response updateStudent(@PathParam("studentMatrNr")int matrNr,Student student) {
student.setMatrNr(matrNr); //if User does not send correct id it is still used correct id from URL
return service.updateStudent(student);
student= dao.updateStudent(student);
return Response.ok().entity(student).build();
}
@DELETE
@Path("/{studentMatrNr}")
@Produces(MediaType.APPLICATION_JSON)
public void deleteStudent(@PathParam("studentMatrNr")int matrNr) {
service.removeStudent(matrNr);
public Response deleteStudent(@PathParam("studentMatrNr")int matrNr) {
dao.removeStudent(matrNr);
return Response.noContent().build();
}
@GET
@Path("/{studentMatrNr}/inscriptions")
@Produces(MediaType.APPLICATION_JSON)
public List<Study> getInscriptions(@PathParam("studentMatrNr")int matrNr) {
return service.getInscriptions(matrNr);
public Response getInscriptions(@PathParam("studentMatrNr")int matrNr) {
List<Study>list= dao.getInscriptions(matrNr);
GenericEntity<List<Study>> entity=new GenericEntity<List<Study>>(list) {};
return Response.ok(entity).build();
}
}
package university.at.jku.ce.resource;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.GenericEntity;
import java.util.List;
import java.net.URI;
import java.util.List;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
......@@ -14,9 +16,12 @@ import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
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.h2dao.H2StudyDao;
import university.at.jku.ce.model.Student;
import university.at.jku.ce.model.Study;
import university.at.jku.ce.service.StudyService;
import university.at.jku.ce.service.StudyServiceImpl;
......@@ -24,13 +29,12 @@ import university.at.jku.ce.service.StudyServiceImpl;
public class StudyResource {
StudyService service=new StudyServiceImpl();
StudyDao dao=new H2StudyDao();
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getStudies(@QueryParam("name") String name) {
List<Study>list= service.getAllStudies(name);
public Response getStudies(@QueryParam("name") String name) {
List<Study>list= dao.getAllStudies(name);
GenericEntity<List<Study>> entity=new GenericEntity<List<Study>>(list) {};
return Response.ok(entity).build();
......@@ -40,32 +44,38 @@ public class StudyResource {
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Study addStudy(Study study) {
return service.addStudy(study);
public Response addStudy(Study study, @Context UriInfo uriInfo) {
study= dao.addStudy(study);
String newStudyId=String.valueOf(study.getStudyId());
URI uri=uriInfo.getAbsolutePathBuilder().path(newStudyId).build();
return Response.created(uri).entity(study).build();
}
@GET
@Path("/{studyId}")
@Produces(MediaType.APPLICATION_JSON)
public Study getStudy(@PathParam("studyId")int studyId) {
return service.getStudy(studyId);
public Response getStudy(@PathParam("studyId")int studyId) {
Study study= dao.getStudy(studyId);
return Response.ok().entity(study).build() ;
}
@PUT
@Path("/{studyId}")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Study updateStudy(@PathParam("studyId")int studyId,Study study) {
public Response updateStudy(@PathParam("studyId")int studyId,Study study) {
study.setStudyId(studyId); //if User does not send correct id it is still used correct id from URL
return service.updateStudy(study);
study= dao.updateStudy(study);
return Response.ok().entity(study).build();
}
@DELETE
@Path("/{studyId}")
@Produces(MediaType.APPLICATION_JSON)
public void deleteStudent(@PathParam("studyId")int studyId) {
service.removeStudy(studyId);