Commit 938f55a3b10e98572b6c3a97a2e0c3363775afc3

Authored by andre.guimaraes
1 parent 6f66ac4b

Interrompe o registro de log do InfoResource

Os serviços contidos na classe InfoResource não serão mais registrados
no log de requests, esses serviços são consultados pelo zabbix para
identificar problemas no broker. O serviço listar requests agora consta
na documentação do broker e com mais opções de filtros.
src/main/java/br/gov/ans/filters/LogRequestFilter.java
... ... @@ -6,6 +6,7 @@ import javax.inject.Inject;
6 6 import javax.servlet.http.HttpServletRequest;
7 7 import javax.ws.rs.container.ContainerRequestContext;
8 8 import javax.ws.rs.container.ContainerRequestFilter;
  9 +import javax.ws.rs.container.ResourceInfo;
9 10 import javax.ws.rs.core.Context;
10 11 import javax.ws.rs.core.SecurityContext;
11 12 import javax.ws.rs.core.UriInfo;
... ... @@ -14,6 +15,7 @@ import javax.ws.rs.ext.Provider;
14 15 import org.jboss.logging.Logger;
15 16 import org.jboss.resteasy.core.ResourceMethodInvoker;
16 17  
  18 +import br.gov.ans.utils.LogIgnore;
17 19 import br.gov.ans.utils.LogIntegracaoUtil;
18 20 import br.gov.ans.utils.MessageUtils;
19 21  
... ... @@ -30,6 +32,9 @@ public class LogRequestFilter implements ContainerRequestFilter{
30 32 private UriInfo uriInfo;
31 33  
32 34 @Context
  35 + private ResourceInfo resourceInfo;
  36 +
  37 + @Context
33 38 private HttpServletRequest request;
34 39  
35 40 @Context
... ... @@ -41,8 +46,10 @@ public class LogRequestFilter implements ContainerRequestFilter{
41 46 @Override
42 47 public void filter(ContainerRequestContext context) throws IOException{
43 48 request.setCharacterEncoding("UTF-8");
44   -
45   - audit.registrarLog(getUserName(),uriInfo.getAbsolutePath().toString(), getMethodName(context));
  49 +
  50 + if(isLoggable()){
  51 + audit.registrarLog(getUserName(),uriInfo.getAbsolutePath().toString(), getMethodName(context));
  52 + }
46 53 }
47 54  
48 55 public String getMethodName(ContainerRequestContext context){
... ... @@ -60,4 +67,15 @@ public class LogRequestFilter implements ContainerRequestFilter{
60 67 }
61 68 }
62 69  
  70 + private boolean isLoggable(){
  71 + if(resourceInfo.getResourceClass().isAnnotationPresent(LogIgnore.class)){
  72 + return false;
  73 + }
  74 +
  75 + if(resourceInfo.getResourceMethod().isAnnotationPresent(LogIgnore.class)){
  76 + return false;
  77 + }
  78 +
  79 + return true;
  80 + }
63 81 }
... ...
src/main/java/br/gov/ans/integracao/sei/dao/LogIntegracaoSistemicaDAO.java 0 → 100644
... ... @@ -0,0 +1,50 @@
  1 +package br.gov.ans.integracao.sei.dao;
  2 +
  3 +import static br.gov.ans.integracao.sei.utils.Util.setPaginacaoQuery;
  4 +import static br.gov.ans.integracao.sei.utils.Util.setQueryParameters;
  5 +import static br.gov.ans.integracao.sei.utils.Util.andOrWhere;
  6 +
  7 +import java.util.HashMap;
  8 +import java.util.List;
  9 +
  10 +import javax.persistence.EntityManager;
  11 +import javax.persistence.PersistenceContext;
  12 +import javax.persistence.PersistenceContextType;
  13 +import javax.persistence.Query;
  14 +
  15 +import org.apache.commons.lang3.StringUtils;
  16 +
  17 +import br.gov.ans.modelo.LogIntegracaoSistemica;
  18 +
  19 +public class LogIntegracaoSistemicaDAO {
  20 +
  21 + @PersistenceContext(unitName = "sei_broker_pu", type = PersistenceContextType.EXTENDED)
  22 + private EntityManager em;
  23 +
  24 + @SuppressWarnings("unchecked")
  25 + public List<LogIntegracaoSistemica> getLogs(String operacao, String origem, Integer pagina, Integer qtdRegistros){
  26 + HashMap<String,Object> parametros = new HashMap<String,Object>();
  27 + StringBuilder sql = new StringBuilder("SELECT l FROM LogIntegracaoSistemica l ");
  28 +
  29 + if(StringUtils.isNotBlank(operacao)){
  30 + sql.append(andOrWhere(sql));
  31 + sql.append("l.operacao = :operacao ");
  32 + parametros.put("operacao", operacao);
  33 + }
  34 +
  35 + if(StringUtils.isNotBlank(origem)){
  36 + sql.append(andOrWhere(sql));
  37 + sql.append("l.origem = :origem ");
  38 + parametros.put("origem", origem);
  39 + }
  40 +
  41 + sql.append("order by l.data desc ");
  42 +
  43 + Query query = em.createQuery(sql.toString());
  44 +
  45 + setPaginacaoQuery(query, pagina, qtdRegistros);
  46 + setQueryParameters(query, parametros);
  47 +
  48 + return query.getResultList();
  49 + }
  50 +}
... ...
src/main/java/br/gov/ans/integracao/sei/rest/InfoResource.java
1 1 package br.gov.ans.integracao.sei.rest;
2 2  
3 3 import static br.gov.ans.integracao.sei.utils.Util.parseInt;
4   -import static br.gov.ans.integracao.sei.utils.Util.setPaginacaoQuery;
5 4  
6 5 import java.util.List;
7 6  
... ... @@ -18,15 +17,17 @@ import javax.ws.rs.core.MediaType;
18 17  
19 18 import org.jboss.logging.Logger;
20 19  
21   -import br.gov.ans.dao.DAO;
22 20 import br.gov.ans.exceptions.BusinessException;
23 21 import br.gov.ans.factories.qualifiers.PropertiesInfo;
24 22 import br.gov.ans.integracao.sei.client.SeiPortTypeProxy;
  23 +import br.gov.ans.integracao.sei.dao.LogIntegracaoSistemicaDAO;
25 24 import br.gov.ans.integracao.sei.modelo.Operacao;
26 25 import br.gov.ans.integracao.sei.utils.Constantes;
27 26 import br.gov.ans.modelo.LogIntegracaoSistemica;
  27 +import br.gov.ans.utils.LogIgnore;
28 28 import br.gov.ans.utils.MessageUtils;
29 29  
  30 +@LogIgnore
30 31 @Path("/info")
31 32 public class InfoResource {
32 33  
... ... @@ -49,8 +50,8 @@ public class InfoResource {
49 50 @PersistenceContext(unitName = "sei_broker_pu", type = PersistenceContextType.EXTENDED)
50 51 private EntityManager emOracle;
51 52  
52   - @Inject
53   - private DAO<LogIntegracaoSistemica> dao;
  53 + @Inject
  54 + private LogIntegracaoSistemicaDAO dao;
54 55  
55 56 /**
56 57 * @api {get} /info/versao Consultar versão
... ... @@ -181,16 +182,39 @@ public class InfoResource {
181 182 }
182 183 }
183 184  
184   - @SuppressWarnings("unchecked")
  185 + /**
  186 + * @api {get} /info/requests Listar Requests
  187 + * @apiName getUltimosRequests
  188 + * @apiGroup Info
  189 + * @apiVersion 2.0.0
  190 + *
  191 + * @apiDescription Lista os requests recebidos pelo broker.
  192 + *
  193 + *
  194 + * @apiParam (Query Parameters) {String} [operacao] nome do método acessado
  195 + * @apiParam (Query Parameters) {String} [origem] usuário que originou a requisição
  196 + * @apiParam (Query Parameters) {int} [pag=1] número da página
  197 + * @apiParam (Query Parameters) {int} [itens=50] quantidade de itens listados por página
  198 + *
  199 + * @apiExample {curl} Exemplo de requisição:
  200 + * curl -i http://<host>/sei-broker/service/info/requests
  201 + *
  202 + * @apiSuccess {String} mensagem Mensagem de sucesso.
  203 + *
  204 + * @apiErrorExample {json} Error-Response:
  205 + * HTTP/1.1 500 Internal Server Error
  206 + * {
  207 + * "error":"Mensagem de erro."
  208 + * "code":"código do erro"
  209 + * }
  210 + */
185 211 @GET
186 212 @Path("/requests")
187 213 @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
188   - public List<LogIntegracaoSistemica> getUltimosRequests(@QueryParam("pag") String pagina, @QueryParam("itens") String qtdRegistros) throws BusinessException{
189   - Query query = dao.createNamedQuery("LogIntegracaoSistemica.ultimosRequests", null);
190   -
191   - setPaginacaoQuery(query, pagina == null? null:parseInt(pagina), qtdRegistros == null? null : parseInt(qtdRegistros));
  214 + public List<LogIntegracaoSistemica> getUltimosRequests(@QueryParam("pag") String pagina, @QueryParam("itens") String qtdRegistros,
  215 + @QueryParam("operacao") String operacao, @QueryParam("origem") String origem) throws BusinessException{
192 216  
193   - return query.getResultList();
  217 + return dao.getLogs(operacao, origem, pagina == null? null:parseInt(pagina), qtdRegistros == null? null : parseInt(qtdRegistros));
194 218 }
195 219  
196 220 }
... ...
src/main/java/br/gov/ans/integracao/sei/utils/Util.java
... ... @@ -160,6 +160,14 @@ public class Util {
160 160 query.setMaxResults(qtdRegistros);
161 161 }
162 162  
  163 + public static String andOrWhere(StringBuilder sql){
  164 + if(sql.toString().contains("WHERE ")){
  165 + return "AND ";
  166 + }
  167 +
  168 + return "WHERE ";
  169 + }
  170 +
163 171 public static String getOnlyNumbers(String string) throws Exception{
164 172 return string.replaceAll(REGEX_SOMENTE_NUMEROS,"");
165 173 }
... ...
src/main/java/br/gov/ans/utils/LogIgnore.java 0 → 100644
... ... @@ -0,0 +1,15 @@
  1 +package br.gov.ans.utils;
  2 +
  3 +import java.lang.annotation.Documented;
  4 +import java.lang.annotation.ElementType;
  5 +import java.lang.annotation.Retention;
  6 +import java.lang.annotation.RetentionPolicy;
  7 +import java.lang.annotation.Target;
  8 +
  9 +
  10 +@Documented
  11 +@Retention(RetentionPolicy.RUNTIME)
  12 +@Target({ElementType.TYPE, ElementType.METHOD})
  13 +public @interface LogIgnore {
  14 +
  15 +}
0 16 \ No newline at end of file
... ...
src/main/webapp/WEB-INF/sei-broker-ds.xml
... ... @@ -25,7 +25,7 @@
25 25 <share-prepared-statements>false</share-prepared-statements>
26 26 </statement>
27 27 </datasource>
28   - <datasource jta="false" jndi-name="java:/jdbc/sei-mysql" pool-name="jdbc/sei-mysql" enabled="true" use-java-context="true" use-ccm="false" statistics-enabled="true">
  28 + <datasource jta="true" jndi-name="java:/jdbc/sei-mysql" pool-name="jdbc/sei-mysql" enabled="true" use-java-context="true" use-ccm="false" statistics-enabled="true">
29 29 <connection-url>${br.gov.ans.seiBroker.db.mysql.connectionUrl}</connection-url>
30 30 <driver-class>com.mysql.jdbc.Driver</driver-class>
31 31 <driver>com.mysql</driver>
... ...
src/main/webapp/api-docs/api_data.js
... ... @@ -4933,6 +4933,82 @@ define({ &quot;api&quot;: [
4933 4933 },
4934 4934 {
4935 4935 "type": "get",
  4936 + "url": "/info/requests",
  4937 + "title": "Listar Requests",
  4938 + "name": "getUltimosRequests",
  4939 + "group": "Info",
  4940 + "version": "2.0.0",
  4941 + "description": "<p>Lista os requests recebidos pelo broker.</p>",
  4942 + "parameter": {
  4943 + "fields": {
  4944 + "Query Parameters": [
  4945 + {
  4946 + "group": "Query Parameters",
  4947 + "type": "String",
  4948 + "optional": true,
  4949 + "field": "operacao",
  4950 + "description": "<p>nome do método acessado</p>"
  4951 + },
  4952 + {
  4953 + "group": "Query Parameters",
  4954 + "type": "String",
  4955 + "optional": true,
  4956 + "field": "origem",
  4957 + "description": "<p>usuário que originou a requisição</p>"
  4958 + },
  4959 + {
  4960 + "group": "Query Parameters",
  4961 + "type": "int",
  4962 + "optional": true,
  4963 + "field": "pag",
  4964 + "defaultValue": "1",
  4965 + "description": "<p>número da página</p>"
  4966 + },
  4967 + {
  4968 + "group": "Query Parameters",
  4969 + "type": "int",
  4970 + "optional": true,
  4971 + "field": "itens",
  4972 + "defaultValue": "50",
  4973 + "description": "<p>quantidade de itens listados por página</p>"
  4974 + }
  4975 + ]
  4976 + }
  4977 + },
  4978 + "examples": [
  4979 + {
  4980 + "title": "Exemplo de requisição:",
  4981 + "content": "curl -i http://<host>/sei-broker/service/info/requests",
  4982 + "type": "curl"
  4983 + }
  4984 + ],
  4985 + "success": {
  4986 + "fields": {
  4987 + "Success 200": [
  4988 + {
  4989 + "group": "Success 200",
  4990 + "type": "String",
  4991 + "optional": false,
  4992 + "field": "mensagem",
  4993 + "description": "<p>Mensagem de sucesso.</p>"
  4994 + }
  4995 + ]
  4996 + }
  4997 + },
  4998 + "error": {
  4999 + "examples": [
  5000 + {
  5001 + "title": "Error-Response:",
  5002 + "content": "HTTP/1.1 500 Internal Server Error\n{\n\t\"error\":\"Mensagem de erro.\"\n\t\"code\":\"código do erro\"\n}",
  5003 + "type": "json"
  5004 + }
  5005 + ]
  5006 + },
  5007 + "filename": "sei-broker/src/main/java/br/gov/ans/integracao/sei/rest/InfoResource.java",
  5008 + "groupTitle": "Info"
  5009 + },
  5010 + {
  5011 + "type": "get",
4936 5012 "url": "/info/conexoes/mysql",
4937 5013 "title": "Testar conexão MySQL",
4938 5014 "name": "testMySQLConnection",
... ...
src/main/webapp/api-docs/api_data.json
... ... @@ -4933,6 +4933,82 @@
4933 4933 },
4934 4934 {
4935 4935 "type": "get",
  4936 + "url": "/info/requests",
  4937 + "title": "Listar Requests",
  4938 + "name": "getUltimosRequests",
  4939 + "group": "Info",
  4940 + "version": "2.0.0",
  4941 + "description": "<p>Lista os requests recebidos pelo broker.</p>",
  4942 + "parameter": {
  4943 + "fields": {
  4944 + "Query Parameters": [
  4945 + {
  4946 + "group": "Query Parameters",
  4947 + "type": "String",
  4948 + "optional": true,
  4949 + "field": "operacao",
  4950 + "description": "<p>nome do método acessado</p>"
  4951 + },
  4952 + {
  4953 + "group": "Query Parameters",
  4954 + "type": "String",
  4955 + "optional": true,
  4956 + "field": "origem",
  4957 + "description": "<p>usuário que originou a requisição</p>"
  4958 + },
  4959 + {
  4960 + "group": "Query Parameters",
  4961 + "type": "int",
  4962 + "optional": true,
  4963 + "field": "pag",
  4964 + "defaultValue": "1",
  4965 + "description": "<p>número da página</p>"
  4966 + },
  4967 + {
  4968 + "group": "Query Parameters",
  4969 + "type": "int",
  4970 + "optional": true,
  4971 + "field": "itens",
  4972 + "defaultValue": "50",
  4973 + "description": "<p>quantidade de itens listados por página</p>"
  4974 + }
  4975 + ]
  4976 + }
  4977 + },
  4978 + "examples": [
  4979 + {
  4980 + "title": "Exemplo de requisição:",
  4981 + "content": "curl -i http://<host>/sei-broker/service/info/requests",
  4982 + "type": "curl"
  4983 + }
  4984 + ],
  4985 + "success": {
  4986 + "fields": {
  4987 + "Success 200": [
  4988 + {
  4989 + "group": "Success 200",
  4990 + "type": "String",
  4991 + "optional": false,
  4992 + "field": "mensagem",
  4993 + "description": "<p>Mensagem de sucesso.</p>"
  4994 + }
  4995 + ]
  4996 + }
  4997 + },
  4998 + "error": {
  4999 + "examples": [
  5000 + {
  5001 + "title": "Error-Response:",
  5002 + "content": "HTTP/1.1 500 Internal Server Error\n{\n\t\"error\":\"Mensagem de erro.\"\n\t\"code\":\"código do erro\"\n}",
  5003 + "type": "json"
  5004 + }
  5005 + ]
  5006 + },
  5007 + "filename": "sei-broker/src/main/java/br/gov/ans/integracao/sei/rest/InfoResource.java",
  5008 + "groupTitle": "Info"
  5009 + },
  5010 + {
  5011 + "type": "get",
4936 5012 "url": "/info/conexoes/mysql",
4937 5013 "title": "Testar conexão MySQL",
4938 5014 "name": "testMySQLConnection",
... ...
src/main/webapp/api-docs/api_project.js
... ... @@ -8,7 +8,7 @@ define({
8 8 "apidoc": "0.2.0",
9 9 "generator": {
10 10 "name": "apidoc",
11   - "time": "2018-04-25T13:34:37.392Z",
  11 + "time": "2018-04-26T12:30:30.895Z",
12 12 "url": "http://apidocjs.com",
13 13 "version": "0.15.1"
14 14 }
... ...
src/main/webapp/api-docs/api_project.json
... ... @@ -8,7 +8,7 @@
8 8 "apidoc": "0.2.0",
9 9 "generator": {
10 10 "name": "apidoc",
11   - "time": "2018-04-25T13:34:37.392Z",
  11 + "time": "2018-04-26T12:30:30.895Z",
12 12 "url": "http://apidocjs.com",
13 13 "version": "0.15.1"
14 14 }
... ...