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 @@ ...@@ -51,11 +51,10 @@
<artifactId>jersey-hk2</artifactId> <artifactId>jersey-hk2</artifactId>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.glassfish.jersey.media</groupId> <groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-binding</artifactId> <artifactId>jersey-media-json-binding</artifactId>
</dependency> </dependency>
<dependency> <dependency>
<groupId>javax.xml.bind</groupId> <groupId>javax.xml.bind</groupId>
......
package university.at.jku.ce.h2dao; package university.at.jku.ce.dao.h2dao;
import java.io.File; import java.io.File;
public class DaoParam { public class DaoParam {
protected static final String DRIVER="org.h2.Driver"; 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 USER="sa";
protected static final String PASSWORD=""; protected static final String PASSWORD="";
protected static final String JDBC_URL="jdbc:h2:C:\\Users\\Michael\\development\\workspace-ce5\\at.jku.ce\\data\\Database"; 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.Connection;
import java.sql.DriverManager; import java.sql.DriverManager;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
...@@ -11,6 +11,7 @@ import java.util.List; ...@@ -11,6 +11,7 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import university.at.jku.ce.dao.StudentDao; 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.Student;
import university.at.jku.ce.model.Study; import university.at.jku.ce.model.Study;
...@@ -46,6 +47,7 @@ public class H2StudentDao implements StudentDao { ...@@ -46,6 +47,7 @@ public class H2StudentDao implements StudentDao {
} catch(SQLException se) { } catch(SQLException se) {
//Handle errors for JDBC //Handle errors for JDBC
se.printStackTrace(); se.printStackTrace();
throw new RuntimeException();
} catch(Exception e) { } catch(Exception e) {
//Handle errors for Class.forName //Handle errors for Class.forName
e.printStackTrace(); e.printStackTrace();
...@@ -64,8 +66,8 @@ public class H2StudentDao implements StudentDao { ...@@ -64,8 +66,8 @@ public class H2StudentDao implements StudentDao {
} //end try } //end try
if (students.size()==1) {return students.get(0);} if (students.size()==1) {return students.get(0);}
else { else {
//log error if (students.size()<1) {throw new NotFoundException();}
return null; else throw new RuntimeException();
} }
} }
...@@ -91,8 +93,8 @@ public class H2StudentDao implements StudentDao { ...@@ -91,8 +93,8 @@ public class H2StudentDao implements StudentDao {
stmt.close(); stmt.close();
con.close(); con.close();
} catch(SQLException se) { } catch(SQLException se) {
//Handle errors for JDBC se.printStackTrace();
se.printStackTrace(); throw new RuntimeException();
} catch(Exception e) { } catch(Exception e) {
//Handle errors for Class.forName //Handle errors for Class.forName
e.printStackTrace(); e.printStackTrace();
...@@ -109,7 +111,8 @@ public class H2StudentDao implements StudentDao { ...@@ -109,7 +111,8 @@ public class H2StudentDao implements StudentDao {
} //end finally try } //end finally try
rs=null; rs=null;
} //end try } //end try
return students; if (students.size()==0) throw new NotFoundException();
return students;
} }
@Override @Override
...@@ -146,6 +149,7 @@ public class H2StudentDao implements StudentDao { ...@@ -146,6 +149,7 @@ public class H2StudentDao implements StudentDao {
} catch(SQLException se) { } catch(SQLException se) {
//Handle errors for JDBC //Handle errors for JDBC
se.printStackTrace(); se.printStackTrace();
throw new RuntimeException();
} catch(Exception e) { } catch(Exception e) {
//Handle errors for Class.forName //Handle errors for Class.forName
e.printStackTrace(); e.printStackTrace();
...@@ -192,6 +196,7 @@ public class H2StudentDao implements StudentDao { ...@@ -192,6 +196,7 @@ public class H2StudentDao implements StudentDao {
} catch(SQLException se) { } catch(SQLException se) {
//Handle errors for JDBC //Handle errors for JDBC
se.printStackTrace(); se.printStackTrace();
throw new RuntimeException();
} catch(Exception e) { } catch(Exception e) {
//Handle errors for Class.forName //Handle errors for Class.forName
e.printStackTrace(); e.printStackTrace();
...@@ -213,7 +218,7 @@ public class H2StudentDao implements StudentDao { ...@@ -213,7 +218,7 @@ public class H2StudentDao implements StudentDao {
@Override @Override
public void removeStudent(int matrNr) { public void removeStudent(int matrNr){
try { try {
// STEP 1: Register JDBC driver // STEP 1: Register JDBC driver
Class.forName(DaoParam.DRIVER); Class.forName(DaoParam.DRIVER);
...@@ -231,11 +236,12 @@ public class H2StudentDao implements StudentDao { ...@@ -231,11 +236,12 @@ public class H2StudentDao implements StudentDao {
prStmt.close(); prStmt.close();
con.close(); con.close();
} catch(SQLException se) { } catch(SQLException se) {
//Handle errors for JDBC se.printStackTrace();
se.printStackTrace(); throw new RuntimeException();
} catch(Exception e) { } catch(Exception e) {
//Handle errors for Class.forName //Handle errors for Class.forName
e.printStackTrace(); e.printStackTrace();
throw new RuntimeException();
} finally { } finally {
//finally block used to close resources //finally block used to close resources
try{ try{
...@@ -277,6 +283,7 @@ public class H2StudentDao implements StudentDao { ...@@ -277,6 +283,7 @@ public class H2StudentDao implements StudentDao {
} catch(SQLException se) { } catch(SQLException se) {
//Handle errors for JDBC //Handle errors for JDBC
se.printStackTrace(); se.printStackTrace();
throw new RuntimeException();
} catch(Exception e) { } catch(Exception e) {
//Handle errors for Class.forName //Handle errors for Class.forName
e.printStackTrace(); e.printStackTrace();
...@@ -293,6 +300,7 @@ public class H2StudentDao implements StudentDao { ...@@ -293,6 +300,7 @@ public class H2StudentDao implements StudentDao {
} //end finally try } //end finally try
rs=null; rs=null;
} //end try } //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.Connection;
import java.sql.DriverManager; import java.sql.DriverManager;
...@@ -12,6 +12,7 @@ import java.util.List; ...@@ -12,6 +12,7 @@ import java.util.List;
import university.at.jku.ce.dao.StudyDao; import university.at.jku.ce.dao.StudyDao;
import university.at.jku.ce.model.Student; import university.at.jku.ce.model.Student;
import university.at.jku.ce.model.Study; import university.at.jku.ce.model.Study;
import university.at.jku.ce.exception.NotFoundException;
public class H2StudyDao implements StudyDao{ public class H2StudyDao implements StudyDao{
...@@ -54,6 +55,7 @@ public class H2StudyDao implements StudyDao{ ...@@ -54,6 +55,7 @@ public class H2StudyDao implements StudyDao{
} catch(SQLException se) { } catch(SQLException se) {
//Handle errors for JDBC //Handle errors for JDBC
se.printStackTrace(); se.printStackTrace();
throw new RuntimeException();
} catch(Exception e) { } catch(Exception e) {
//Handle errors for Class.forName //Handle errors for Class.forName
e.printStackTrace(); e.printStackTrace();
...@@ -70,7 +72,8 @@ public class H2StudyDao implements StudyDao{ ...@@ -70,7 +72,8 @@ public class H2StudyDao implements StudyDao{
} //end finally try } //end finally try
rs=null; rs=null;
} //end try } //end try
return studies; if (studies.size()==0) throw new NotFoundException();
return studies;
} }
@Override @Override
...@@ -116,8 +119,9 @@ public class H2StudyDao implements StudyDao{ ...@@ -116,8 +119,9 @@ public class H2StudyDao implements StudyDao{
} //end try } //end try
if (studies.size()==1) {return studies.get(0);} if (studies.size()==1) {return studies.get(0);}
else { else {
//log error if (studies.size()<1) {throw new NotFoundException();}
return null; else throw new RuntimeException();
} }
} }
...@@ -154,6 +158,7 @@ public class H2StudyDao implements StudyDao{ ...@@ -154,6 +158,7 @@ public class H2StudyDao implements StudyDao{
} catch(SQLException se) { } catch(SQLException se) {
//Handle errors for JDBC //Handle errors for JDBC
se.printStackTrace(); se.printStackTrace();
throw new RuntimeException();
} catch(Exception e) { } catch(Exception e) {
//Handle errors for Class.forName //Handle errors for Class.forName
e.printStackTrace(); e.printStackTrace();
...@@ -199,6 +204,7 @@ public class H2StudyDao implements StudyDao{ ...@@ -199,6 +204,7 @@ public class H2StudyDao implements StudyDao{
} catch(SQLException se) { } catch(SQLException se) {
//Handle errors for JDBC //Handle errors for JDBC
se.printStackTrace(); se.printStackTrace();
throw new RuntimeException();
} catch(Exception e) { } catch(Exception e) {
//Handle errors for Class.forName //Handle errors for Class.forName
e.printStackTrace(); e.printStackTrace();
...@@ -239,6 +245,7 @@ public class H2StudyDao implements StudyDao{ ...@@ -239,6 +245,7 @@ public class H2StudyDao implements StudyDao{
} catch(SQLException se) { } catch(SQLException se) {
//Handle errors for JDBC //Handle errors for JDBC
se.printStackTrace(); se.printStackTrace();
throw new RuntimeException();
} catch(Exception e) { } catch(Exception e) {
//Handle errors for Class.forName //Handle errors for Class.forName
e.printStackTrace(); e.printStackTrace();
......
package university.at.jku.ce.h2dao; package university.at.jku.ce.dao.h2dao;
import java.sql.Connection; import java.sql.Connection;
import java.sql.DriverManager; import java.sql.DriverManager;
...@@ -10,6 +10,7 @@ import java.util.ArrayList; ...@@ -10,6 +10,7 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import university.at.jku.ce.dao.SubjectDao; 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.Study;
import university.at.jku.ce.model.Subject; import university.at.jku.ce.model.Subject;
...@@ -46,6 +47,7 @@ public class H2SubjectDao implements SubjectDao { ...@@ -46,6 +47,7 @@ public class H2SubjectDao implements SubjectDao {
} catch(SQLException se) { } catch(SQLException se) {
//Handle errors for JDBC //Handle errors for JDBC
se.printStackTrace(); se.printStackTrace();
throw new RuntimeException();
} catch(Exception e) { } catch(Exception e) {
//Handle errors for Class.forName //Handle errors for Class.forName
e.printStackTrace(); e.printStackTrace();
...@@ -62,7 +64,8 @@ public class H2SubjectDao implements SubjectDao { ...@@ -62,7 +64,8 @@ public class H2SubjectDao implements SubjectDao {
} //end finally try } //end finally try
rs=null; rs=null;
} //end try } //end try
return subjects; if (subjects.size()==0) throw new NotFoundException();
return subjects;
} }
@Override @Override
...@@ -91,7 +94,8 @@ public class H2SubjectDao implements SubjectDao { ...@@ -91,7 +94,8 @@ public class H2SubjectDao implements SubjectDao {
con.close(); con.close();
} catch(SQLException se) { } catch(SQLException se) {
//Handle errors for JDBC //Handle errors for JDBC
se.printStackTrace(); se.printStackTrace();
throw new RuntimeException();
} catch(Exception e) { } catch(Exception e) {
//Handle errors for Class.forName //Handle errors for Class.forName
e.printStackTrace(); e.printStackTrace();
...@@ -109,7 +113,11 @@ public class H2SubjectDao implements SubjectDao { ...@@ -109,7 +113,11 @@ public class H2SubjectDao implements SubjectDao {
rs=null; rs=null;
} //end try } //end try
if (subjects.size()==1) return subjects.get(0); if (subjects.size()==1) return subjects.get(0);
else return null; else {
if (subjects.size()<1) {throw new NotFoundException();}
else throw new RuntimeException();
}
} }
@Override @Override
...@@ -147,6 +155,7 @@ public class H2SubjectDao implements SubjectDao { ...@@ -147,6 +155,7 @@ public class H2SubjectDao implements SubjectDao {
} catch(SQLException se) { } catch(SQLException se) {
//Handle errors for JDBC //Handle errors for JDBC
se.printStackTrace(); se.printStackTrace();
throw new RuntimeException();
} catch(Exception e) { } catch(Exception e) {
//Handle errors for Class.forName //Handle errors for Class.forName
e.printStackTrace(); e.printStackTrace();
...@@ -190,6 +199,7 @@ public class H2SubjectDao implements SubjectDao { ...@@ -190,6 +199,7 @@ public class H2SubjectDao implements SubjectDao {
} catch(SQLException se) { } catch(SQLException se) {
//Handle errors for JDBC //Handle errors for JDBC
se.printStackTrace(); se.printStackTrace();
throw new RuntimeException();
} catch(Exception e) { } catch(Exception e) {
//Handle errors for Class.forName //Handle errors for Class.forName
e.printStackTrace(); e.printStackTrace();
...@@ -231,6 +241,7 @@ public class H2SubjectDao implements SubjectDao { ...@@ -231,6 +241,7 @@ public class H2SubjectDao implements SubjectDao {
} catch(SQLException se) { } catch(SQLException se) {
//Handle errors for JDBC //Handle errors for JDBC
se.printStackTrace(); se.printStackTrace();
throw new RuntimeException();
} catch(Exception e) { } catch(Exception e) {
//Handle errors for Class.forName //Handle errors for Class.forName
e.printStackTrace(); 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; ...@@ -8,6 +8,7 @@ import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient; import javax.xml.bind.annotation.XmlTransient;
@XmlRootElement
public class Student { public class Student {
private int matrNr; private int matrNr;
......
...@@ -3,10 +3,10 @@ package university.at.jku.ce.model; ...@@ -3,10 +3,10 @@ package university.at.jku.ce.model;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import javax.json.bind.annotation.JsonbTransient;
import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient; import javax.xml.bind.annotation.XmlTransient;
@XmlRootElement
public class Study { public class Study {
private int studyId; private int studyId;
......
...@@ -2,6 +2,7 @@ package university.at.jku.ce.model; ...@@ -2,6 +2,7 @@ package university.at.jku.ce.model;
import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Subject { public class Subject {
private int subjectId; private int subjectId;
private int studyId; private int studyId;
......
package university.at.jku.ce.resource; package university.at.jku.ce.resource;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.GenericEntity; 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.Consumes;
import javax.ws.rs.DELETE; import javax.ws.rs.DELETE;
...