This is an automated email from the git hooks/post-receive script. New commit to branch develop in repository coselmar. See http://git.codelutin.com/coselmar.git commit 010abb8c63ae381981b6d172b37896edba194aad Author: Yannick Martel <martel@©odelutin.com> Date: Tue Dec 16 16:56:28 2014 +0100 manage error from servers --- .../ifremer/coselmar/services/v1/ErrorAction.java | 27 +++++++++++++++ .../coselmar/services/v1/UsersWebService.java | 7 +++- coselmar-rest/src/main/resources/mapping | 10 +++--- coselmar-ui/src/main/webapp/index.html | 5 --- .../src/main/webapp/js/coselmar-controllers.js | 30 ++++++++++------ coselmar-ui/src/main/webapp/js/coselmar.js | 40 +++++++++++++++++++++- coselmar-ui/src/main/webapp/views/errors/401.html | 7 ++++ coselmar-ui/src/main/webapp/views/errors/403.html | 4 +++ coselmar-ui/src/main/webapp/views/errors/404.html | 4 +++ 9 files changed, 111 insertions(+), 23 deletions(-) diff --git a/coselmar-rest/src/main/java/fr/ifremer/coselmar/services/v1/ErrorAction.java b/coselmar-rest/src/main/java/fr/ifremer/coselmar/services/v1/ErrorAction.java index 50e8a1e..b98e642 100644 --- a/coselmar-rest/src/main/java/fr/ifremer/coselmar/services/v1/ErrorAction.java +++ b/coselmar-rest/src/main/java/fr/ifremer/coselmar/services/v1/ErrorAction.java @@ -44,4 +44,31 @@ public class ErrorAction extends WebMotionController { return render; } + + public Render on404(HttpContext context, Exception e) { + + CoselmarRestUtil.prepareResponse(context); + + Render render = renderError(HttpServletResponse.SC_NOT_FOUND, e.getMessage()); + return render; + + } + + public Render on403(HttpContext context, Exception e) { + + CoselmarRestUtil.prepareResponse(context); + + Render render = renderError(HttpServletResponse.SC_FORBIDDEN, e.getMessage()); + return render; + + } + + public Render on401(HttpContext context, Exception e) { + + CoselmarRestUtil.prepareResponse(context); + + Render render = renderError(HttpServletResponse.SC_UNAUTHORIZED, e.getMessage()); + return render; + + } } diff --git a/coselmar-rest/src/main/java/fr/ifremer/coselmar/services/v1/UsersWebService.java b/coselmar-rest/src/main/java/fr/ifremer/coselmar/services/v1/UsersWebService.java index a2adbca..cd9c84d 100644 --- a/coselmar-rest/src/main/java/fr/ifremer/coselmar/services/v1/UsersWebService.java +++ b/coselmar-rest/src/main/java/fr/ifremer/coselmar/services/v1/UsersWebService.java @@ -282,7 +282,12 @@ public class UsersWebService extends CoselmarWebServiceSupport { Preconditions.checkNotNull(mail); Preconditions.checkNotNull(password); - CoselmarUser user = getCoselmarUserDao().forMailEquals(getCleanMail(mail)).addEquals(CoselmarUser.PROPERTY_ACTIVE, true).findAny(); + CoselmarUser user = getCoselmarUserDao().forMailEquals(getCleanMail(mail)).addEquals(CoselmarUser.PROPERTY_ACTIVE, true).findAnyOrNull(); + + if (user == null) { + throw new InvalidCredentialException("Invalid mail"); + } + String salt = user.getSalt(); checkPassword(user.getPassword(), salt, password); diff --git a/coselmar-rest/src/main/resources/mapping b/coselmar-rest/src/main/resources/mapping index 5c89620..7bd7fe3 100644 --- a/coselmar-rest/src/main/resources/mapping +++ b/coselmar-rest/src/main/resources/mapping @@ -5,7 +5,7 @@ [config] package.filters=fr.ifremer.coselmar.services.filter package.actions=fr.ifremer.coselmar.services.v1 -package.errors=fr.ifremer.coselmar.services.errors +package.errors=fr.ifremer.coselmar.services.v1 server.listener.class=fr.ifremer.coselmar.services.CoselmarRestApplicationListener default.render=fr.ifremer.coselmar.services.CoselmarRender @@ -14,10 +14,10 @@ default.render=fr.ifremer.coselmar.services.CoselmarRender [errors] -#fr.ifremer.coselmar.services.errors.InvalidCredentialException ErrorAction.on401 -#fr.ifremer.coselmar.services.errors.UnauthorizedException ErrorAction.on403 -#fr.ifremer.coselmar.services.CoselmarTechnicalException ErrorAction.on500 -#org.nuiton.topia.persistence.TopiaNoResultException ErrorAction.on404 +fr.ifremer.coselmar.services.errors.InvalidCredentialException ErrorAction.on401 +fr.ifremer.coselmar.services.errors.UnauthorizedException ErrorAction.on403 +fr.ifremer.coselmar.services.CoselmarTechnicalException ErrorAction.on500 +org.nuiton.topia.persistence.TopiaNoResultException ErrorAction.on404 [actions] diff --git a/coselmar-ui/src/main/webapp/index.html b/coselmar-ui/src/main/webapp/index.html index 7a9a43e..e511ed8 100644 --- a/coselmar-ui/src/main/webapp/index.html +++ b/coselmar-ui/src/main/webapp/index.html @@ -100,11 +100,6 @@ </div> <button type="submit" class="btn btn-action" ng-click="login(loginMail, loginPassword)">Sign in</button> </form> - <div ng-init="loginMessage = {fail: false}" class="navbar-form navbar-right"> - <ng-messages for="loginMessage"> - <ng-message when="fail"><div class="alert alert-danger alert-dismissible"><button type="button" class="close" data-dismiss="alert"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>Invalid Credentials</div></ng-message> - </ng-messages> - </div> <form class="navbar-form navbar-right" role="form" ng-if="currentUser"> <div class="form-group">{{currentUser.firstName}} {{currentUser.lastName}} diff --git a/coselmar-ui/src/main/webapp/js/coselmar-controllers.js b/coselmar-ui/src/main/webapp/js/coselmar-controllers.js index 0611296..404a858 100644 --- a/coselmar-ui/src/main/webapp/js/coselmar-controllers.js +++ b/coselmar-ui/src/main/webapp/js/coselmar-controllers.js @@ -24,8 +24,8 @@ var coselmarControllers = angular.module('coselmarControllers', ['ui.bootstrap', 'ui.select', 'ui.bootstrap.tooltip']); // Controller when the main page/view loads -coselmarControllers.controller("HomeCtrl", ['$scope', '$http', '$location', 'userService', 'jwtHelper', - function ($scope, $http, $location, userService, jwtHelper) { +coselmarControllers.controller("HomeCtrl", ['$scope', '$http', '$location', '$routeParams', 'userService', 'jwtHelper', + function ($scope, $http, $location, $routeParams, userService, jwtHelper) { var jwtToken = localStorage.getItem('coselmar-jwt'); if (jwtToken && !jwtHelper.isTokenExpired(jwtToken)) { @@ -43,15 +43,14 @@ coselmarControllers.controller("HomeCtrl", ['$scope', '$http', '$location', 'use localStorage.setItem('coselmar-jwt', successResult.jwt); $scope.currentUser = jwtHelper.decodeToken(successResult.jwt); - // Manage login messages - $scope.loginMessage.fail = false; - - // Redirect on '/' - $location.path('/'); - - }, function(errorResult) { - // Error message for login - $scope.loginMessage.fail = true; + // Redirect on initial request page + if ($location.search()["returnTo"]) { + $location.path($location.search()["returnTo"]); + delete returnTo; + } else { + // Redirect on '/' + $location.path('/'); + } }) } @@ -63,6 +62,15 @@ coselmarControllers.controller("HomeCtrl", ['$scope', '$http', '$location', 'use }]); +// Controller when got 401 +coselmarControllers.controller("unauthorizedCtrl", ['$scope', function ($scope) { + + // Just remove the current stored user + delete $scope.currentUser; + localStorage.removeItem('coselmar-jwt'); + +}]); + // Controller for All Documents View coselmarControllers.controller("DocumentsCtrl", ['$scope', '$route', '$routeParams', '$location', 'documentService', function($scope, $route, $routeParams, $location, documentService){ diff --git a/coselmar-ui/src/main/webapp/js/coselmar.js b/coselmar-ui/src/main/webapp/js/coselmar.js index 8373782..3fe1d28 100644 --- a/coselmar-ui/src/main/webapp/js/coselmar.js +++ b/coselmar-ui/src/main/webapp/js/coselmar.js @@ -69,6 +69,17 @@ coselmarApp.config(['$routeProvider', function($routeProvider) { controller : 'ReferentialCtrl', templateUrl : 'views/referential/referential.html' + // Error pages + }).when('/404', { + templateUrl : 'views/errors/404.html' + + }).when('/403', { + templateUrl : 'views/errors/403.html' + + }).when('/401', { + controller : 'unauthorizedCtrl', + templateUrl : 'views/errors/401.html' + }).otherwise({ redirectTo: '/', @@ -82,4 +93,31 @@ coselmarApp.config(function Config($httpProvider, jwtInterceptorProvider) { return localStorage.getItem('coselmar-jwt'); } $httpProvider.interceptors.push('jwtInterceptor'); -}) \ No newline at end of file +}); + +// Intercept error +coselmarApp.config(function($httpProvider) { + + $httpProvider.interceptors.push(function($q, $location) { + return { + 'responseError': function(rejection) { + + if (rejection.status == 401) { + var returnTo = $location.path(); + $location.path("/401").search('returnTo', returnTo); + + } else if (rejection.status == 403) { + $location.path("/403"); + + } else if (rejection.status == 404) { + $location.path("/404"); + + } else { + $location.path("/"); + + } + return $q.reject(rejection); + } + }; + }); +}); \ No newline at end of file diff --git a/coselmar-ui/src/main/webapp/views/errors/401.html b/coselmar-ui/src/main/webapp/views/errors/401.html new file mode 100644 index 0000000..dcdb0e8 --- /dev/null +++ b/coselmar-ui/src/main/webapp/views/errors/401.html @@ -0,0 +1,7 @@ +<div> + <div> + <h1>Authentication Error</h1> + <p>Error with authentication, please try to log again.</p> + </div> + +</div> \ No newline at end of file diff --git a/coselmar-ui/src/main/webapp/views/errors/403.html b/coselmar-ui/src/main/webapp/views/errors/403.html new file mode 100644 index 0000000..b807ec9 --- /dev/null +++ b/coselmar-ui/src/main/webapp/views/errors/403.html @@ -0,0 +1,4 @@ +<div> + <h1>Not Authorized</h1> + <p>You are not authorized to access this page.</p> +</div> \ No newline at end of file diff --git a/coselmar-ui/src/main/webapp/views/errors/404.html b/coselmar-ui/src/main/webapp/views/errors/404.html new file mode 100644 index 0000000..03041fc --- /dev/null +++ b/coselmar-ui/src/main/webapp/views/errors/404.html @@ -0,0 +1,4 @@ +<div> + <h1>Nothing Here</h1> + <p>The page you try to access does not exist.</p> +</div> \ No newline at end of file -- To stop receiving notification emails like this one, please contact codelutin.com SCM administrator <admin+scm@codelutin.com>.