git-svn-id: https://svn.d4science-ii.research-infrastructures.eu/gcube/branches/common/common-smartgears/2.1@181336 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
parent
0c90d7a508
commit
d7ad1c6cab
18
pom.xml
18
pom.xml
|
@ -1,4 +1,5 @@
|
|||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
|
@ -110,6 +111,21 @@
|
|||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<!-- https://mvnrepository.com/artifact/org.apache.tomcat/tomcat-catalina -->
|
||||
<dependency>
|
||||
<groupId>org.apache.tomcat</groupId>
|
||||
<artifactId>tomcat-catalina</artifactId>
|
||||
<version>${tomcat.version}</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.tomcat.embed</groupId>
|
||||
<artifactId>tomcat-embed-core</artifactId>
|
||||
<version>${tomcat.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.sun.jersey</groupId>
|
||||
<artifactId>jersey-client</artifactId>
|
||||
|
|
|
@ -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;
|
||||
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
Loading…
Reference in New Issue