Commit 65801f5f authored by Franz Reischl's avatar Franz Reischl
Browse files

Fixed XML return of serveral things

parent a00c0d1b
package university.at.jku.ce.dao;
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.model.User;
......@@ -32,7 +31,7 @@ public interface StudentDao {
public List<User> getAllUsers();
public List<H2StudentDao.UserName> getAllUserNames();
public List<User> getAllUserNames();
public void addUser(int id, Student student);
......
......@@ -610,8 +610,8 @@ public class H2StudentDao implements StudentDao {
}
@Override
public List<UserName> getAllUserNames(){
List<UserName> users=new ArrayList<>();
public List<User> getAllUserNames(){
List<User> users=new ArrayList<>();
try {
// STEP 1: Register JDBC driver
Class.forName(DaoParam.DRIVER);
......@@ -624,7 +624,7 @@ public class H2StudentDao implements StudentDao {
prStmt = con.prepareStatement(sql);
rs=prStmt.executeQuery();
while (rs.next()) {
users.add(new UserName(rs.getInt(1),rs.getString(2)));
users.add(new User(rs.getInt(1),rs.getString(2)));
}
// STEP 4: Clean-up environment
......
......@@ -18,6 +18,17 @@ public class User {
this.password = password;
this.role = role;
}
public User(Integer id, String username, String role) {
super();
this.id=id;
this.username = username;
this.role = role;
}
public User(Integer id, String username) {
super();
this.id=id;
this.username = username;
}
......
package university.at.jku.ce.model;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Version {
private final static String VERSION_NUMBER = "2.0";
private String versionNumber;
public Version(){
versionNumber = VERSION_NUMBER;
}
// public String getVersion(){return VERSION_NUMBER;}
public static String getVersion(){
return VERSION_NUMBER;
public String getVersionNumber(){
return versionNumber;
}
public void setVersionNumber(String version){
this.versionNumber = version;
}
}
package university.at.jku.ce.resource;
import university.at.jku.ce.authentication.BasicAuthenticationUtil;
import university.at.jku.ce.dao.StudentDao;
import university.at.jku.ce.dao.h2dao.H2StudentDao;
import university.at.jku.ce.exception.ExceptionParam;
import university.at.jku.ce.model.User;
import javax.annotation.security.PermitAll;
import javax.annotation.security.RolesAllowed;
import javax.ws.rs.*;
import javax.ws.rs.core.*;
import java.util.List;
@Path("/users")
public class UserResource {
private static final String AUTHORIZATION_PROPERTY = "Authorization";
StudentDao dao = new H2StudentDao();
/**
* Get a list of registered usernames and their corresponding user IDs
* @param headers Framework headers
* @return List of usernames
*/
@PermitAll
@GET
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public Response getUsers(@Context HttpHeaders headers) {
ExceptionParam.setMediaType(headers.getMediaType());
List<H2StudentDao.UserName> list = dao.getAllUserNames();
GenericEntity<List<H2StudentDao.UserName>> entity = new GenericEntity<List<H2StudentDao.UserName>>(list) {
};
return Response.ok(entity).build();
}
/**
* Get the detailed user information of a specific user
* @param userId ID of specified user
* @param headers Framework headers
* @return User details
*/
@RolesAllowed({"STUDENT", "ADMIN"})
@GET
@Path("/{userId}")
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public Response getUserDetails(@PathParam("userId") int userId, @Context HttpHeaders headers) {
ExceptionParam.setMediaType(headers.getMediaType());
List<String> authorization = headers.getRequestHeader(AUTHORIZATION_PROPERTY);
BasicAuthenticationUtil.AuthInfo authInfo = BasicAuthenticationUtil.authorize(authorization);
String username = authInfo.getUser();
// Get the User object to be compared with login credentials
User userByName = dao.getUser(username);
if (userByName.getRole().equals("STUDENT") && !userByName.getId().equals(userId)) throw new ForbiddenException();
if (userId <= 0) throw new BadRequestException();
// Get the requested User object that will be returned (Can be different in case of admin)
User userById = dao.getUser(userId);
GenericEntity<User> entity = new GenericEntity<User>(userById) {
};
return Response.ok(entity).build();
}
}
package university.at.jku.ce.resource;
import university.at.jku.ce.authentication.BasicAuthenticationUtil;
import university.at.jku.ce.dao.StudentDao;
import university.at.jku.ce.dao.h2dao.H2StudentDao;
import university.at.jku.ce.exception.ExceptionParam;
import university.at.jku.ce.model.User;
import javax.annotation.security.PermitAll;
import javax.annotation.security.RolesAllowed;
import javax.ws.rs.*;
import javax.ws.rs.core.*;
import java.util.List;
@Path("/users")
public class UserResource {
private static final String AUTHORIZATION_PROPERTY = "Authorization";
StudentDao dao = new H2StudentDao();
/**
* Get a list of registered usernames and their corresponding user IDs
* @param headers Framework headers
* @return List of usernames
*/
@PermitAll
@GET
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public Response getUsers(@Context HttpHeaders headers) {
ExceptionParam.setMediaType(headers.getMediaType());
List<User> list = dao.getAllUserNames();
GenericEntity<List<User>> entity = new GenericEntity<List<User>>(list) {
};
return Response.ok(entity).build();
}
/**
* Get the detailed user information of a specific user
* @param userId ID of specified user
* @param headers Framework headers
* @return User details
*/
@RolesAllowed({"STUDENT", "ADMIN"})
@GET
@Path("/{userId}")
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public Response getUserDetails(@PathParam("userId") int userId, @Context HttpHeaders headers) {
ExceptionParam.setMediaType(headers.getMediaType());
List<String> authorization = headers.getRequestHeader(AUTHORIZATION_PROPERTY);
BasicAuthenticationUtil.AuthInfo authInfo = BasicAuthenticationUtil.authorize(authorization);
String username = authInfo.getUser();
// Get the User object to be compared with login credentials
User userByName = dao.getUser(username);
if (userByName.getRole().equals("STUDENT") && !userByName.getId().equals(userId)) throw new ForbiddenException();
if (userId <= 0) throw new BadRequestException();
// Get the requested User object that will be returned (Can be different in case of admin)
User userById = dao.getUser(userId);
GenericEntity<User> entity = new GenericEntity<User>(userById) {
};
return Response.ok(entity).build();
}
}
......@@ -8,7 +8,6 @@ import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.*;
import java.util.HashMap;
@Path("/version")
public class VersionResource {
......@@ -18,9 +17,8 @@ public class VersionResource {
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public Response getUsers(@Context HttpHeaders headers) {
ExceptionParam.setMediaType(headers.getMediaType());
HashMap<String, String> versionNumber = new HashMap<>();
versionNumber.put("version", Version.getVersion());
GenericEntity<HashMap<String, String>> entity = new GenericEntity<HashMap<String, String>>(versionNumber) {
Version version = new Version();
GenericEntity<Version> entity = new GenericEntity<Version>(version) {
};
return Response.ok(entity).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