Ignore:
Timestamp:
01/06/14 17:20:05 (10 years ago)
Author:
olhsha
Message:

files INSTALL, UPDATED, CHANGES and README are corrected. The bug with the wrong server diagnostic (403 instead of 404), when a resource's give id is not found, is fixed.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/rest/UserResource.java

    r4207 r4217  
    2727import java.util.UUID;
    2828import javax.servlet.http.HttpServletRequest;
     29import javax.servlet.http.HttpServletResponse;
    2930import javax.ws.rs.Consumes;
    3031import javax.ws.rs.GET;
     
    5152@Component
    5253@Path("/users")
    53 @Transactional(rollbackFor={Exception.class, SQLException.class, IOException.class, ParserConfigurationException.class})
     54@Transactional(rollbackFor = {Exception.class, SQLException.class, IOException.class, ParserConfigurationException.class})
    5455public class UserResource {
     56
    5557    @Autowired
    5658    private DBIntegrityService dbIntegrityService;
    5759    @Context
    5860    private HttpServletRequest httpServletRequest;
     61    @Context
     62    private HttpServletResponse httpServletResponse;
    5963    @Context
    6064    private UriInfo uriInfo;
     
    6670    public UserResource() {
    6771    }
    68    
     72
    6973    @GET
    7074    @Produces(MediaType.TEXT_XML)
    7175    @Path("{userid: " + BackendConstants.regExpIdentifier + "}")
    72     @Secured("ROLE_USER")   
    73     @Transactional(readOnly=true)
    74     public JAXBElement<User> getUser(@PathParam("userid") String ExternalIdentifier) throws SQLException {
    75          dbIntegrityService.setServiceURI(uriInfo.getBaseUri().toString());
     76    @Secured("ROLE_USER")
     77    @Transactional(readOnly = true)
     78    public JAXBElement<User> getUser(@PathParam("userid") String ExternalIdentifier) throws SQLException, IOException {
     79        dbIntegrityService.setServiceURI(uriInfo.getBaseUri().toString());
    7680        final Number userID = dbIntegrityService.getUserInternalIdentifier(UUID.fromString(ExternalIdentifier));
    77         final User user = dbIntegrityService.getUser(userID);
    78         return new ObjectFactory().createUser(user);
     81        if (userID != null) {
     82            final User user = dbIntegrityService.getUser(userID);
     83            return new ObjectFactory().createUser(user);
     84        } else {
     85            httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, "The user with the given id is not found in the database");
     86            return null;
     87        }
    7988    }
    80    
     89
    8190    @GET
    8291    @Produces(MediaType.TEXT_XML)
    8392    @Path("/info")
    84     @Secured("ROLE_USER")   
    85     @Transactional(readOnly=true)
    86     public JAXBElement<User> getUserByInfo(@QueryParam("email") String email) throws SQLException {
     93    @Secured("ROLE_USER")
     94    @Transactional(readOnly = true)
     95    public JAXBElement<User> getUserByInfo(@QueryParam("email") String email) throws SQLException, IOException {
    8796        dbIntegrityService.setServiceURI(uriInfo.getBaseUri().toString());
    8897        final User user = dbIntegrityService.getUserByInfo(email);
    89         return new ObjectFactory().createUser(user);
     98        if (user != null) {
     99            return new ObjectFactory().createUser(user);
     100        } else {
     101            httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, "The user with the given info is not found in the database");
     102            return null;
     103        }
    90104    }
    91    
     105
    92106    @GET
    93107    @Produces(MediaType.TEXT_XML)
    94108    @Path("{userid: " + BackendConstants.regExpIdentifier + "}/current")
    95     @Secured("ROLE_USER")   
    96     @Transactional(readOnly=true)
    97     public JAXBElement<CurrentUserInfo> getCurrentUserInfo(@PathParam("userid") String ExternalIdentifier){
     109    @Secured("ROLE_USER")
     110    @Transactional(readOnly = true)
     111    public JAXBElement<CurrentUserInfo> getCurrentUserInfo(@PathParam("userid") String ExternalIdentifier) throws IOException {
    98112        dbIntegrityService.setServiceURI(uriInfo.getBaseUri().toString());
    99113        final Number userID = dbIntegrityService.getUserInternalIdentifier(UUID.fromString(ExternalIdentifier));
    100         final CurrentUserInfo userInfo = new CurrentUserInfo();
    101         userInfo.setRef(dbIntegrityService.getUserURI(userID));
    102         userInfo.setCurrentUser(ifLoggedIn(userID));
    103         return new ObjectFactory().createCurrentUserInfo(userInfo);
     114        if (userID != null) {
     115            final CurrentUserInfo userInfo = new CurrentUserInfo();
     116            userInfo.setRef(dbIntegrityService.getUserURI(userID));
     117            userInfo.setCurrentUser(ifLoggedIn(userID));
     118            return new ObjectFactory().createCurrentUserInfo(userInfo);
     119        } else {
     120            httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, "The user with the given id is not found in the database");
     121            return null;
     122        }
    104123    }
    105    
     124
    106125    @POST
    107126    @Consumes(MediaType.TEXT_XML)
     
    110129    @Secured("ROLE_ADMIN")
    111130    public JAXBElement<User> addUser(@PathParam("userid") String remoteId, User user) throws SQLException {
    112         dbIntegrityService.setServiceURI(uriInfo.getBaseUri().toString());       
     131        dbIntegrityService.setServiceURI(uriInfo.getBaseUri().toString());
    113132        final Number userID = dbIntegrityService.addUser(user, remoteId);
    114133        final User addedUser = dbIntegrityService.getUser(userID);
    115134        return new ObjectFactory().createUser(addedUser);
    116135    }
    117    
     136
    118137    @PUT
    119138    @Consumes(MediaType.TEXT_XML)
     
    121140    @Path("")
    122141    @Secured("ROLE_ADMIN")
    123     public JAXBElement<User> updateUser(User user){
    124         dbIntegrityService.setServiceURI(uriInfo.getBaseUri().toString());       
     142    public JAXBElement<User> updateUser(User user) throws IOException{
     143        dbIntegrityService.setServiceURI(uriInfo.getBaseUri().toString());
    125144        final Number userID = dbIntegrityService.updateUser(user);
    126         final User addedUser = dbIntegrityService.getUser(userID);
    127         return new ObjectFactory().createUser(addedUser);
     145        if (userID != null) {
     146            final User addedUser = dbIntegrityService.getUser(userID);
     147            return new ObjectFactory().createUser(addedUser);
     148        } else {
     149            httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, "The user with the given id is not found in the database");
     150            return null;
     151        }
    128152    }
    129    
    130    
    131    
    132     private boolean ifLoggedIn(Number userID){
     153
     154    private boolean ifLoggedIn(Number userID) {
    133155        return httpServletRequest.getRemoteUser().equals(dbIntegrityService.getUserRemoteID(userID));
    134156    }
Note: See TracChangeset for help on using the changeset viewer.