From d7ad1c6cab9ba697666f47a4e9aa464182cc5f8e Mon Sep 17 00:00:00 2001 From: Lucio Lelii Date: Fri, 26 Jul 2019 14:57:19 +0000 Subject: [PATCH] git-svn-id: https://svn.d4science-ii.research-infrastructures.eu/gcube/branches/common/common-smartgears/2.1@181336 82a268e6-3cf1-43bd-a215-b396298e98cf --- pom.xml | 24 +++++- .../smartgears/managers/RequestManager.java | 6 +- .../utils/GcubeAccountingValve.java | 85 +++++++++++++++++++ 3 files changed, 108 insertions(+), 7 deletions(-) create mode 100644 src/main/java/org/gcube/smartgears/utils/GcubeAccountingValve.java diff --git a/pom.xml b/pom.xml index 9bf40e8..a3ee785 100644 --- a/pom.xml +++ b/pom.xml @@ -1,4 +1,5 @@ - 4.0.0 @@ -12,7 +13,7 @@ common-smartgears 2.1.9-SNAPSHOT SmartGears - + @@ -24,7 +25,7 @@ - + distro 7.0.42 @@ -54,7 +55,7 @@ org.gcube.data.publishing document-store-lib - + org.gcube.accounting accounting-lib @@ -110,6 +111,21 @@ test + + + org.apache.tomcat + tomcat-catalina + ${tomcat.version} + provided + + + + org.apache.tomcat.embed + tomcat-embed-core + ${tomcat.version} + test + + com.sun.jersey jersey-client diff --git a/src/main/java/org/gcube/smartgears/managers/RequestManager.java b/src/main/java/org/gcube/smartgears/managers/RequestManager.java index 4138e27..2cb2b3d 100644 --- a/src/main/java/org/gcube/smartgears/managers/RequestManager.java +++ b/src/main/java/org/gcube/smartgears/managers/RequestManager.java @@ -1,7 +1,8 @@ package org.gcube.smartgears.managers; -import static org.gcube.smartgears.Constants.*; -import static org.gcube.smartgears.handlers.application.request.RequestError.*; +import static org.gcube.smartgears.Constants.WILDCARD; +import static org.gcube.smartgears.handlers.application.request.RequestError.application_error; +import static org.gcube.smartgears.handlers.application.request.RequestError.request_not_authorized_error; import java.io.IOException; import java.util.ArrayList; @@ -28,7 +29,6 @@ import org.gcube.smartgears.handlers.application.RequestHandler; import org.gcube.smartgears.handlers.application.ResponseEvent; import org.gcube.smartgears.handlers.application.request.RequestError; import org.gcube.smartgears.handlers.application.request.RequestException; -import org.gcube.smartgears.utils.Utils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/src/main/java/org/gcube/smartgears/utils/GcubeAccountingValve.java b/src/main/java/org/gcube/smartgears/utils/GcubeAccountingValve.java new file mode 100644 index 0000000..632b74d --- /dev/null +++ b/src/main/java/org/gcube/smartgears/utils/GcubeAccountingValve.java @@ -0,0 +1,85 @@ +package org.gcube.smartgears.utils; + +import java.io.IOException; + +import javax.servlet.ServletException; + +import org.apache.catalina.connector.Request; +import org.apache.catalina.connector.Response; +import org.apache.catalina.valves.ValveBase; +import org.gcube.accounting.datamodel.UsageRecord.OperationResult; +import org.gcube.accounting.datamodel.usagerecords.ServiceUsageRecord; +import org.gcube.accounting.persistence.AccountingPersistence; +import org.gcube.accounting.persistence.AccountingPersistenceFactory; +import org.gcube.common.scope.api.ScopeProvider; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class GcubeAccountingValve extends ValveBase { + + private static Logger log = LoggerFactory.getLogger(GcubeAccountingValve.class); + + private String infra; + private String serviceClass; + private String serviceName; + private String hostAndPort; + + public void setInfra(String infra) { + this.infra = infra; + } + + public void setServiceClass(String serviceClass) { + this.serviceClass = serviceClass; + } + + public void setServiceName(String serviceName) { + this.serviceName = serviceName; + } + + public void setHostAndPort(String hostAndPort) { + this.hostAndPort = hostAndPort; + } + + @Override + public void invoke(Request request, Response response) throws IOException, ServletException { + try { + String callerIp = request.getHeader("x-forwarded-for"); + if (callerIp == null) { + callerIp = request.getRemoteAddr(); + } + + boolean success = response.getStatus()<400; + ScopeProvider.instance.set(infra); + AccountingPersistenceFactory.setFallbackLocation("/tmp"); + AccountingPersistence persistence = AccountingPersistenceFactory.getPersistence(); + ServiceUsageRecord serviceUsageRecord = new ServiceUsageRecord(); + try{ + + serviceUsageRecord.setConsumerId("UNKNOWN"); + serviceUsageRecord.setCallerQualifier("UNKNOWN"); + serviceUsageRecord.setScope(infra); + serviceUsageRecord.setServiceClass(serviceClass); + serviceUsageRecord.setServiceName(serviceName); + serviceUsageRecord.setDuration(200l); + serviceUsageRecord.setHost(hostAndPort); + serviceUsageRecord.setCalledMethod(request.getRequestURI()); + serviceUsageRecord.setCallerHost(callerIp); + serviceUsageRecord.setOperationResult(success?OperationResult.SUCCESS:OperationResult.FAILED); + persistence.account(serviceUsageRecord); + log.info("Request: {} {} {} {} ", infra, request.getContextPath(), request.getRequestURI(), success); + }catch(Exception ex){ + log.warn("invalid record passed to accounting ",ex); + }finally { + ScopeProvider.instance.reset(); + } + + }catch (Exception e) { + log.error("error executing valve", e); + } + getNext().invoke(request, response); + } + + + + +}