Commit 039b775f3d911f0030d4a5268384c46ff1dbbb87

Authored by Cleverson Sacramento
1 parent 80eba845
Exists in master

FWK-223: Atualizar o arquétipo com os novos experimentos

Task-Url: https://demoiselle.atlassian.net/browse/FWK-223
archetype/html-rest/src/main/resources/archetype-resources/src/main/java/rest/AuthREST.java
... ... @@ -6,7 +6,6 @@ import javax.inject.Inject;
6 6 import javax.validation.constraints.NotNull;
7 7 import javax.validation.constraints.Size;
8 8 import javax.ws.rs.Consumes;
9   -import javax.ws.rs.GET;
10 9 import javax.ws.rs.POST;
11 10 import javax.ws.rs.Path;
12 11 import javax.ws.rs.Produces;
... ... @@ -24,25 +23,28 @@ public class AuthREST {
24 23 private SecurityContext securityContext;
25 24  
26 25 @POST
  26 + @Path("login")
27 27 @ValidatePayload
28   - @Produces("application/json")
29 28 @Consumes("application/json")
30   - public void login(CredentialsData data) {
  29 + @Produces("application/json")
  30 + public Principal login(CredentialsBody body) {
31 31 Credentials credentials = Beans.getReference(Credentials.class);
32   - credentials.setUsername(data.username);
33   - credentials.setPassword(data.password);
  32 + credentials.setUsername(body.username);
  33 + credentials.setPassword(body.password);
34 34  
35 35 securityContext.login();
  36 + return securityContext.getUser();
36 37 }
37 38  
38   - @GET
  39 + @POST
39 40 @LoggedIn
40   - @Produces("application/json")
41   - public Principal getLoggedInUser() {
42   - return securityContext.getUser();
  41 + @Path("logout")
  42 + @ValidatePayload
  43 + public void logout() {
  44 + securityContext.logout();
43 45 }
44 46  
45   - public static class CredentialsData {
  47 + public static class CredentialsBody {
46 48  
47 49 @NotNull(message = "{required.field}")
48 50 @Size(min = 1, message = "{required.field}")
... ...
archetype/html-rest/src/main/resources/archetype-resources/src/main/java/rest/BookmarkREST.java
... ... @@ -65,10 +65,10 @@ public class BookmarkREST {
65 65 @ValidatePayload
66 66 @Produces("application/json")
67 67 @Consumes("application/json")
68   - public Response insert(Bookmark entity, @Context UriInfo uriInfo) throws Exception {
69   - checkId(entity);
  68 + public Response insert(Bookmark body, @Context UriInfo uriInfo) throws Exception {
  69 + checkId(body);
70 70  
71   - String id = bc.insert(entity).getId().toString();
  71 + String id = bc.insert(body).getId().toString();
72 72 URI location = uriInfo.getRequestUriBuilder().path(id).build();
73 73  
74 74 return Response.created(location).entity(id).build();
... ... @@ -81,12 +81,12 @@ public class BookmarkREST {
81 81 @ValidatePayload
82 82 @Produces("application/json")
83 83 @Consumes("application/json")
84   - public void update(@PathParam("id") Long id, Bookmark entity) throws Exception {
85   - checkId(entity);
  84 + public void update(@PathParam("id") Long id, Bookmark body) throws Exception {
  85 + checkId(body);
86 86 load(id);
87 87  
88   - entity.setId(id);
89   - bc.update(entity);
  88 + body.setId(id);
  89 + bc.update(body);
90 90 }
91 91  
92 92 @DELETE
... ...
archetype/html-rest/src/main/resources/archetype-resources/src/main/webapp/js/controller/bookmark-list.js
1 1 $(function() {
2 2 $("#new").focus();
3   -
4 3 BookmarkProxy.findAll().done(findAllOk);
5 4  
6 5 MetadataProxy.getDemoiselleVersion().done(function(data) {
... ...
archetype/html-rest/src/main/resources/archetype-resources/src/main/webapp/js/controller/login.js
... ... @@ -10,24 +10,26 @@ $(function() {
10 10  
11 11 $("[id$='-message']").hide();
12 12  
13   - var data = {
  13 + var credentials = {
14 14 'username' : $("#username").val().trim(),
15 15 'password' : $("#password").val().trim()
16 16 };
17 17  
18   - AuthProxy.login(data).done(loginOk).fail(loginFail);
  18 + AuthProxy.login(credentials).done(loginOk).fail(loginFail);
19 19 });
20 20 });
21 21  
22   -function loginOk(data, status, request) {
23   - App.setToken(request.getResponseHeader('Set-Token'));
24   - location.href = "home.html";
  22 +function loginOk(data, textStatus, jqXHR) {
  23 + App.auth.setToken(jqXHR.getResponseHeader('Set-Token'));
  24 + App.auth.setLoggedInUser(data);
  25 +
  26 + App.restoreSavedLocation();
25 27 }
26 28  
27   -function loginFail(request) {
28   - switch (request.status) {
  29 +function loginFail(jqXHR, textStatus, errorThrown) {
  30 + switch (jqXHR.status) {
29 31 case 401:
30   - $("#global-message").html(request.responseText).show();
  32 + $("#global-message").html(jqXHR.responseText).show();
31 33 break;
32 34  
33 35 case 422:
... ... @@ -35,7 +37,7 @@ function loginFail(request) {
35 37 var id = $(this).attr('id');
36 38 var message = null;
37 39  
38   - $.each(request.responseJSON, function(index, value) {
  40 + $.each(jqXHR.responseJSON, function(index, value) {
39 41 if (id == value.property) {
40 42 message = value.message;
41 43 return;
... ...
archetype/html-rest/src/main/resources/archetype-resources/src/main/webapp/js/controller/menu.js
1 1 $(function() {
2 2 $("#menu").load("menu.html", function() {
3   - AuthProxy.getUser().done(getUserOk);
  3 + $("#username").html(App.auth.getLoggedInUser().name);
4 4  
5   - $("#logout").on("click", function() {
6   - App.removeToken();
7   - location.href = "index.html";
  5 + $("#logout").click(function(event) {
  6 + event.preventDefault();
  7 + AuthProxy.logout().done(logoutOk);
8 8 });
9 9 });
10 10 });
11 11  
12   -function getUserOk(data) {
13   - $("#username").html(data.name);
  12 +function logoutOk() {
  13 + App.auth.clearAuthentication();
  14 + location.href = "";
14 15 }
... ...
archetype/html-rest/src/main/resources/archetype-resources/src/main/webapp/js/lib/app.js
1   -$.ajaxSetup({
2   - error : function(request) {
3   - switch (request.status) {
4   - case 401:
5   - bootbox.alert("Você não está autenticado!", function() {
6   - location.href = "login.html";
7   - });
8   -
9   - break;
10   - }
11   - }
12   -});
13   -
14 1 var App = {
15 2  
16   - tokenKey : "Token",
  3 + savedLocationKey : "Saved Location",
17 4  
18   - getToken : function() {
19   - return sessionStorage.getItem(this.tokenKey);
  5 + restoreSavedLocation : function() {
  6 + var url = sessionStorage.getItem(this.savedLocationKey);
  7 + location.href = (url ? url : "");
20 8 },
21 9  
22   - setToken : function(token) {
23   - console.log(token);
24   - sessionStorage.setItem(this.tokenKey, token);
  10 + saveLocation : function(url) {
  11 + sessionStorage.setItem(this.savedLocationKey, url);
25 12 },
26 13  
27   - setHeader : function(request) {
28   - request.setRequestHeader("Authorization", "Token " + App.getToken());
29   - },
30   -
31   - removeToken : function() {
32   - sessionStorage.removeItem(this.tokenKey);
33   - $.removeCookie("Token");
  14 + clearSavedLocation : function() {
  15 + sessionStorage.removeItem(this.savedLocationKey);
34 16 },
35 17  
36 18 getUrlParameterByName : function(name) {
37 19 name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
38 20 var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"), results = regex.exec(location.search);
39 21 return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
  22 + },
  23 +
  24 + auth : {
  25 + tokenKey : "Token",
  26 +
  27 + userKey : "User",
  28 +
  29 + getLoggedInUser : function() {
  30 + return JSON.parse(sessionStorage.getItem(this.userKey));
  31 + },
  32 +
  33 + setLoggedInUser : function(user) {
  34 + sessionStorage.setItem(this.userKey, JSON.stringify(user));
  35 + },
  36 +
  37 + isLoggedIn : function() {
  38 + return this.getToken() != null;
  39 + },
  40 +
  41 + getToken : function() {
  42 + return sessionStorage.getItem(this.tokenKey);
  43 + },
  44 +
  45 + setToken : function(token) {
  46 + sessionStorage.setItem(this.tokenKey, token);
  47 + },
  48 +
  49 + clearAuthentication : function() {
  50 + sessionStorage.removeItem(this.userKey);
  51 + sessionStorage.removeItem(this.tokenKey);
  52 + },
  53 +
  54 + setHeader : function(request) {
  55 + request.setRequestHeader("Authorization", "Token " + this.getToken());
  56 + }
  57 + },
  58 +
  59 + handling : {
  60 + handle401 : function(request) {
  61 + App.auth.clearAuthentication();
  62 + App.saveLocation(location.href);
  63 + location.href = "login.html";
  64 + },
  65 +
  66 + handle422 : function(request) {
  67 + var elements = $("form input, form select, form textarea").get().reverse();
  68 +
  69 + $(elements).each(function() {
  70 + var id = $(this).attr('id');
  71 + var messages = [];
  72 +
  73 + $.each(request.responseJSON, function(index, value) {
  74 + var aux = value.property ? value.property : "global";
  75 +
  76 + if (id == aux) {
  77 + messages.push(value.message);
  78 + return;
  79 + }
  80 + });
  81 +
  82 + if (!id) {
  83 + return;
  84 + }
  85 +
  86 + var message = $("#" + id.replace(".", "\\.") + "-message");
  87 +
  88 + if (messages.length > 1) {
  89 + message.empty();
  90 + var ul = message.append("<ul></ul>")
  91 +
  92 + while (messages.length > 0) {
  93 + ul.append("<li>" + messages.pop() + "</li>");
  94 + }
  95 +
  96 + message.show();
  97 + $(this).focus();
  98 +
  99 + } else if (messages.length == 1) {
  100 + message.html(messages.pop()).show();
  101 + $(this).focus();
  102 +
  103 + } else {
  104 + message.hide();
  105 + }
  106 + });
  107 + },
  108 +
  109 + handle500 : function(request) {
  110 + alert("Ocorreu um erro interno no servidor e o processamento não foi concluído. Informe ao administrador pelo e-mail: contato@soumaisaventura.com.br");
  111 + }
40 112 }
41 113 };
  114 +
  115 +$.ajaxSetup({
  116 + error : function(request) {
  117 + switch (request.status) {
  118 + case 401:
  119 + App.handling.handle401(request);
  120 + break;
  121 +
  122 + case 422:
  123 + App.handling.handle422(request);
  124 + break;
  125 +
  126 + case 500:
  127 + App.handling.handle500(request);
  128 + break;
  129 + }
  130 + }
  131 +});
... ...
archetype/html-rest/src/main/resources/archetype-resources/src/main/webapp/js/proxy/auth.js
... ... @@ -2,23 +2,20 @@ var AuthProxy = {
2 2  
3 3 url : "api/auth",
4 4  
5   - login : function($credentials) {
  5 + login : function(credentials) {
6 6 return $.ajax({
7   - url : this.url,
  7 + url : this.url + "/login",
8 8 type : "POST",
9   - data : JSON.stringify($credentials),
  9 + data : JSON.stringify(credentials),
10 10 contentType : "application/json",
11 11 error : function() {}
12 12 });
13 13 },
14 14  
15   - getUser : function() {
  15 + logout : function() {
16 16 return $.ajax({
17   - url : this.url,
18   - type : "GET",
19   - beforeSend : function(request) {
20   - App.setHeader(request)
21   - }
  17 + url : this.url + "/logout",
  18 + type : "POST"
22 19 });
23 20 }
24 21 };
... ...
archetype/html-rest/src/main/resources/archetype-resources/src/main/webapp/js/proxy/bookmark.js
... ... @@ -6,54 +6,54 @@ var BookmarkProxy = {
6 6 return $.ajax({
7 7 type : "GET",
8 8 url : this.url,
9   - beforeSend : function(request) {
10   - App.setHeader(request)
  9 + beforeSend : function(jqXHR) {
  10 + App.auth.setHeader(jqXHR)
11 11 }
12 12 });
13 13 },
14 14  
15   - load : function($id) {
  15 + load : function(id) {
16 16 return $.ajax({
17 17 type : "GET",
18   - url : this.url + "/" + $id,
19   - beforeSend : function(request) {
20   - App.setHeader(request)
  18 + url : this.url + "/" + id,
  19 + beforeSend : function(jqXHR) {
  20 + App.auth.setHeader(jqXHR)
21 21 }
22 22 });
23 23 },
24 24  
25   - insert : function($data) {
  25 + insert : function(bookmark) {
26 26 return $.ajax({
27 27 type : "POST",
28 28 url : this.url,
29   - data : JSON.stringify($data),
  29 + data : JSON.stringify(bookmark),
30 30 contentType : "application/json",
31   - beforeSend : function(request) {
32   - App.setHeader(request)
  31 + beforeSend : function(jqXHR) {
  32 + App.auth.setHeader(jqXHR)
33 33 }
34 34 });
35 35 },
36 36  
37   - update : function($id, $data) {
  37 + update : function(id, bookmark) {
38 38 return $.ajax({
39 39 type : "PUT",
40   - url : this.url + "/" + $id,
41   - data : JSON.stringify($data),
  40 + url : this.url + "/" + id,
  41 + data : JSON.stringify(bookmark),
42 42 contentType : "application/json",
43   - beforeSend : function(request) {
44   - App.setHeader(request)
  43 + beforeSend : function(jqXHR) {
  44 + App.auth.setHeader(jqXHR)
45 45 }
46 46 });
47 47 },
48 48  
49   - remove : function($ids) {
  49 + remove : function(ids) {
50 50 return $.ajax({
51 51 type : "DELETE",
52 52 url : this.url,
53   - data : JSON.stringify($ids),
  53 + data : JSON.stringify(ids),
54 54 contentType : "application/json",
55   - beforeSend : function(request) {
56   - App.setHeader(request)
  55 + beforeSend : function(jqXHR) {
  56 + App.auth.setHeader(jqXHR)
57 57 }
58 58 });
59 59 }
... ...
archetype/html-rest/src/main/resources/archetype-resources/src/main/webapp/js/proxy/metadata.js
... ... @@ -9,10 +9,10 @@ var MetadataProxy = {
9 9 });
10 10 },
11 11  
12   - getMessage : function($key) {
  12 + getMessage : function(key) {
13 13 return $.ajax({
14 14 type : "GET",
15   - url : this.url + "/message/" + $key
  15 + url : this.url + "/message/" + key
16 16 });
17 17 },
18 18  
... ...