merge for release 4.13

git-svn-id: https://svn.d4science-ii.research-infrastructures.eu/gcube/branches/common/common-smartgears/2.1@176609 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Lucio Lelii 2019-01-16 14:37:56 +00:00
parent 1b60aa2d44
commit 55d3199615
15 changed files with 228 additions and 291 deletions

View File

@ -1,4 +1,9 @@
<ReleaseNotes> <ReleaseNotes>
<Changeset component="common-smartgears-2.1.7" date="2017-01-16">
<Change>Added Proxy Address to Application Configuration</Change>
<Change>Added protocol to Container Configuration (http by default)</Change>
<Change>Changed the logs in accounting handler to log error or success and eventually error code</Change>
</Changeset>
<Changeset component="common-smartgears-2.1.5" date="2017-07-18"> <Changeset component="common-smartgears-2.1.5" date="2017-07-18">
<Change>Added ThreadLocal InnerMethodName to set method name from application</Change> <Change>Added ThreadLocal InnerMethodName to set method name from application</Change>
</Changeset> </Changeset>

View File

@ -10,7 +10,7 @@
<groupId>org.gcube.core</groupId> <groupId>org.gcube.core</groupId>
<artifactId>common-smartgears</artifactId> <artifactId>common-smartgears</artifactId>
<version>2.1.6-SNAPSHOT</version> <version>2.1.7-SNAPSHOT</version>
<name>SmartGears</name> <name>SmartGears</name>
<dependencyManagement> <dependencyManagement>

View File

@ -20,17 +20,7 @@ public interface ApplicationConfiguration {
*/ */
Mode mode(); Mode mode();
/**
* Returns true if the application is secure (accessible only in https).
* @return secure or not
*/
boolean secure();
/**
* Returns true if the application is behind a proxy.
* @return secure or not
*/
ApplicationConfiguration proxied(boolean proxied);
/** /**
* Returns the context path of the application * Returns the context path of the application
@ -53,9 +43,7 @@ public interface ApplicationConfiguration {
* @return this configuration * @return this configuration
*/ */
ApplicationConfiguration mode(Mode mode); ApplicationConfiguration mode(Mode mode);
ApplicationConfiguration secure(boolean value);
/** /**
* Returns the name of the application. * Returns the name of the application.
@ -110,6 +98,9 @@ public interface ApplicationConfiguration {
*/ */
ApplicationConfiguration description(String description); ApplicationConfiguration description(String description);
ProxyAddress proxyAddress();
ApplicationConfiguration proxyAddress(ProxyAddress proxyaddress);
/** /**
* Returns the tokens in which the application operates when it first starts. * Returns the tokens in which the application operates when it first starts.
@ -173,5 +164,4 @@ public interface ApplicationConfiguration {
} }

View File

@ -42,9 +42,6 @@ public class BridgedApplicationConfiguration implements ApplicationConfiguration
log.trace("setting persistence location for {} @ {}",application.name(), dir.getAbsolutePath()); log.trace("setting persistence location for {} @ {}",application.name(), dir.getAbsolutePath());
} }
if (container.proxyAddress()==null)
application.proxied(false);
} }
public ApplicationConfiguration inner() { public ApplicationConfiguration inner() {
@ -92,6 +89,10 @@ public class BridgedApplicationConfiguration implements ApplicationConfiguration
public String description() { public String description() {
return application.description(); return application.description();
} }
public ProxyAddress proxyAddress() {
return application.proxyAddress();
}
public ApplicationConfiguration description(String description) { public ApplicationConfiguration description(String description) {
return application.description(description); return application.description(description);
@ -130,15 +131,6 @@ public class BridgedApplicationConfiguration implements ApplicationConfiguration
application.merge(config); application.merge(config);
} }
@Override
public boolean secure() {
return application.secure();
}
@Override
public ApplicationConfiguration secure(boolean value) {
return application.secure(value);
}
@Override @Override
public Set<String> startTokens() { public Set<String> startTokens() {
@ -150,10 +142,6 @@ public class BridgedApplicationConfiguration implements ApplicationConfiguration
return application.startTokens(tokens); return application.startTokens(tokens);
} }
@Override
public ApplicationConfiguration proxied(boolean proxied) {
return application.proxied(proxied);
}
@Override @Override
public boolean proxied() { public boolean proxied() {
@ -170,5 +158,10 @@ public class BridgedApplicationConfiguration implements ApplicationConfiguration
return application.includes(includes); return application.includes(includes);
} }
@Override
public ApplicationConfiguration proxyAddress(ProxyAddress proxyaddress) {
return application.proxyAddress(proxyaddress);
}
} }

View File

@ -37,16 +37,10 @@ public class DefaultApplicationConfiguration implements ApplicationConfiguration
@XmlAttribute @XmlAttribute
private Mode mode = Mode.online; private Mode mode = Mode.online;
@XmlAttribute(name="isSecure")
private boolean secure = false;
@XmlAttribute(name="context") @XmlAttribute(name="context")
String context; String context;
@XmlAttribute(name="proxied")
private boolean proxied = true;
@XmlElement(name="name" , required=true) @XmlElement(name="name" , required=true)
@NotNull @NotNull
String name; String name;
@ -65,6 +59,10 @@ public class DefaultApplicationConfiguration implements ApplicationConfiguration
@XmlElement(name="description") @XmlElement(name="description")
String description=""; String description="";
@XmlElementRef
@IsValid
ProxyAddress proxyAddress;
@XmlElementRef @XmlElementRef
Set<Exclude> excludes= new LinkedHashSet<Exclude>(); Set<Exclude> excludes= new LinkedHashSet<Exclude>();
@ -79,6 +77,7 @@ public class DefaultApplicationConfiguration implements ApplicationConfiguration
public Set<Exclude> excludes() { public Set<Exclude> excludes() {
return excludes; return excludes;
} }
@Override @Override
public Set<Include> includes() { public Set<Include> includes() {
@ -91,11 +90,6 @@ public class DefaultApplicationConfiguration implements ApplicationConfiguration
public Mode mode() { public Mode mode() {
return mode; return mode;
} }
@Override
public boolean secure() {
return secure;
}
@Override @Override
public String name() { public String name() {
@ -113,6 +107,10 @@ public class DefaultApplicationConfiguration implements ApplicationConfiguration
return this; return this;
} }
public ProxyAddress proxyAddress() {
return proxyAddress;
}
@Override @Override
public ApplicationConfiguration excludes(Exclude ... excludes) { public ApplicationConfiguration excludes(Exclude ... excludes) {
this.excludes=new HashSet<Exclude>(Arrays.asList(excludes)); this.excludes=new HashSet<Exclude>(Arrays.asList(excludes));
@ -135,17 +133,7 @@ public class DefaultApplicationConfiguration implements ApplicationConfiguration
public String serviceClass() { public String serviceClass() {
return group; return group;
} }
@Override
public boolean proxied() {
return proxied;
}
@Override
public ApplicationConfiguration proxied(boolean proxied) {
this.proxied = proxied;
return this;
}
@Override @Override
public ApplicationConfiguration serviceClass(String group) { public ApplicationConfiguration serviceClass(String group) {
@ -186,7 +174,10 @@ public class DefaultApplicationConfiguration implements ApplicationConfiguration
return this; return this;
} }
@Override
public boolean proxied() {
return proxyAddress!=null;
}
@Override @Override
public Persistence persistence() { public Persistence persistence() {
@ -198,10 +189,10 @@ public class DefaultApplicationConfiguration implements ApplicationConfiguration
this.persistenceManager=manager; this.persistenceManager=manager;
return this; return this;
} }
@Override @Override
public ApplicationConfiguration secure(boolean value) { public ApplicationConfiguration proxyAddress(ProxyAddress proxyaddress) {
this.secure=value; this.proxyAddress = proxyaddress;
return this; return this;
} }
@ -243,89 +234,4 @@ public class DefaultApplicationConfiguration implements ApplicationConfiguration
} }
@Override
public String toString() {
return "DefaultApplicationConfiguration [mode=" + mode + ", secure="
+ secure + ", context=" + context + ", proxied=" + proxied
+ ", name=" + name + ", group=" + group + ", version="
+ version + ", tokens=" + tokens + ", description="
+ description + ", excludes=" + excludes
+ ", persistenceManager=" + persistenceManager + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((context == null) ? 0 : context.hashCode());
result = prime * result + ((description == null) ? 0 : description.hashCode());
result = prime * result + ((excludes == null) ? 0 : excludes.hashCode());
result = prime * result + ((group == null) ? 0 : group.hashCode());
result = prime * result + ((mode == null) ? 0 : mode.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + ((persistenceManager == null) ? 0 : persistenceManager.hashCode());
result = prime * result + ((tokens == null) ? 0 : tokens.hashCode());
result = prime * result + ((version == null) ? 0 : version.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
DefaultApplicationConfiguration other = (DefaultApplicationConfiguration) obj;
if (context == null) {
if (other.context != null)
return false;
} else if (!context.equals(other.context))
return false;
if (description == null) {
if (other.description != null)
return false;
} else if (!description.equals(other.description))
return false;
if (excludes == null) {
if (other.excludes != null)
return false;
} else if (!excludes.equals(other.excludes))
return false;
if (group == null) {
if (other.group != null)
return false;
} else if (!group.equals(other.group))
return false;
if (mode != other.mode)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (persistenceManager == null) {
if (other.persistenceManager != null)
return false;
} else if (!persistenceManager.equals(other.persistenceManager))
return false;
if (tokens == null) {
if (other.tokens != null)
return false;
} else if (!tokens.equals(other.tokens))
return false;
if (version == null) {
if (other.version != null)
return false;
} else if (!version.equals(other.version))
return false;
if (secure!=other.secure)
return false;
return true;
}
} }

View File

@ -0,0 +1,97 @@
package org.gcube.smartgears.configuration.application;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import org.gcube.common.validator.annotations.NotNull;
@XmlRootElement(name="proxy")
public class ProxyAddress {
@XmlAttribute
String protocol = "http";
@XmlElement
@NotNull
String hostname;
@XmlElement
Integer port;
public String hostname() {
return hostname;
}
public ProxyAddress hostname(String hostname) {
this.hostname = hostname;
return this;
}
public Integer port() {
return port;
}
public ProxyAddress port(int port) {
this.port = port;
return this;
}
public String protocol() {
return protocol;
}
public ProxyAddress protocol(String protocol) {
this.protocol = protocol;
return this;
}
@Override
public String toString() {
return "ProxyAddress [protocol=" + protocol + ", hostname=" + hostname + ", port=" + port + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((hostname == null) ? 0 : hostname.hashCode());
result = prime * result + ((port == null) ? 0 : port.hashCode());
result = prime * result + ((protocol == null) ? 0 : protocol.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
ProxyAddress other = (ProxyAddress) obj;
if (hostname == null) {
if (other.hostname != null)
return false;
} else if (!hostname.equals(other.hostname))
return false;
if (port == null) {
if (other.port != null)
return false;
} else if (!port.equals(other.port))
return false;
if (protocol == null) {
if (other.protocol != null)
return false;
} else if (!protocol.equals(other.protocol))
return false;
return true;
}
}

View File

@ -42,28 +42,25 @@ public class ContainerConfiguration {
private Mode mode = Mode.online; private Mode mode = Mode.online;
@XmlElement @XmlElement
@NotNull @NotNull @IsValid
String hostname; String hostname;
@XmlElement @XmlElement
@NotNull @NotNull
Integer port; Integer port;
@XmlElementRef
@IsValid
ProxyAddress proxyAddress;
@XmlElement(name ="authentication-endpoint") @XmlElement(name ="authentication-endpoint")
String authenticationEnpoint = null; String authenticationEnpoint = null;
@XmlElement(name ="secure-port") @XmlElement(name ="protocol")
Integer securePort; @NotNull @IsValid
String protocol="http";
@XmlElement @XmlElement
boolean authorizeChildrenContext = false; boolean authorizeChildrenContext = false;
@XmlElement @XmlElement
@NotNull @NotNull@IsValid
String infrastructure; String infrastructure;
@XmlElement @XmlElement
@ -186,6 +183,8 @@ public class ContainerConfiguration {
return this; return this;
} }
/** /**
* Returns the host name of the container. * Returns the host name of the container.
* @return the host name; * @return the host name;
@ -217,8 +216,8 @@ public class ContainerConfiguration {
* Returns the port at which the container is listening for requests. * Returns the port at which the container is listening for requests.
* @return the port * @return the port
*/ */
public Integer securePort() { public String protocol() {
return securePort; return protocol;
} }
@ -241,8 +240,8 @@ public class ContainerConfiguration {
return this; return this;
} }
public ContainerConfiguration securePort(int port) { public ContainerConfiguration protocol(String protocol) {
this.securePort=port; this.protocol=protocol;
return this; return this;
} }
@ -276,15 +275,6 @@ public class ContainerConfiguration {
return this; return this;
} }
public ProxyAddress proxyAddress() {
return proxyAddress;
}
public ContainerConfiguration setProxyaddress(ProxyAddress proxyaddress) {
this.proxyAddress = proxyaddress;
return this;
}
/** /**
* Returns the persistence manager of the container. * Returns the persistence manager of the container.
@ -420,8 +410,6 @@ public class ContainerConfiguration {
return true; return true;
} }
} }
@ -430,32 +418,18 @@ public class ContainerConfiguration {
public int hashCode() { public int hashCode() {
final int prime = 31; final int prime = 31;
int result = 1; int result = 1;
result = prime * result result = prime * result + ((allowedContext == null) ? 0 : allowedContext.hashCode());
+ ((allowedContext == null) ? 0 : allowedContext.hashCode());
result = prime * result + ((apps == null) ? 0 : apps.hashCode()); result = prime * result + ((apps == null) ? 0 : apps.hashCode());
result = prime result = prime * result + ((authenticationEnpoint == null) ? 0 : authenticationEnpoint.hashCode());
* result
+ ((authenticationEnpoint == null) ? 0 : authenticationEnpoint
.hashCode());
result = prime * result + (authorizeChildrenContext ? 1231 : 1237); result = prime * result + (authorizeChildrenContext ? 1231 : 1237);
result = prime * result result = prime * result + ((hostname == null) ? 0 : hostname.hashCode());
+ ((hostname == null) ? 0 : hostname.hashCode()); result = prime * result + ((infrastructure == null) ? 0 : infrastructure.hashCode());
result = prime * result
+ ((infrastructure == null) ? 0 : infrastructure.hashCode());
result = prime * result + ((mode == null) ? 0 : mode.hashCode()); result = prime * result + ((mode == null) ? 0 : mode.hashCode());
result = prime result = prime * result + ((persistenceManager == null) ? 0 : persistenceManager.hashCode());
* result
+ ((persistenceManager == null) ? 0 : persistenceManager
.hashCode());
result = prime * result + ((port == null) ? 0 : port.hashCode()); result = prime * result + ((port == null) ? 0 : port.hashCode());
result = prime * result result = prime * result + ((properties == null) ? 0 : properties.hashCode());
+ ((properties == null) ? 0 : properties.hashCode()); result = prime * result + ((protocol == null) ? 0 : protocol.hashCode());
result = prime * result result = prime * result + (int) (publicationFrequency ^ (publicationFrequency >>> 32));
+ ((proxyAddress == null) ? 0 : proxyAddress.hashCode());
result = prime * result
+ (int) (publicationFrequency ^ (publicationFrequency >>> 32));
result = prime * result
+ ((securePort == null) ? 0 : securePort.hashCode());
result = prime * result + ((site == null) ? 0 : site.hashCode()); result = prime * result + ((site == null) ? 0 : site.hashCode());
result = prime * result + ((tokens == null) ? 0 : tokens.hashCode()); result = prime * result + ((tokens == null) ? 0 : tokens.hashCode());
return result; return result;
@ -514,18 +488,13 @@ public class ContainerConfiguration {
return false; return false;
} else if (!properties.equals(other.properties)) } else if (!properties.equals(other.properties))
return false; return false;
if (proxyAddress == null) { if (protocol == null) {
if (other.proxyAddress != null) if (other.protocol != null)
return false; return false;
} else if (!proxyAddress.equals(other.proxyAddress)) } else if (!protocol.equals(other.protocol))
return false; return false;
if (publicationFrequency != other.publicationFrequency) if (publicationFrequency != other.publicationFrequency)
return false; return false;
if (securePort == null) {
if (other.securePort != null)
return false;
} else if (!securePort.equals(other.securePort))
return false;
if (site == null) { if (site == null) {
if (other.site != null) if (other.site != null)
return false; return false;
@ -539,7 +508,13 @@ public class ContainerConfiguration {
return true; return true;
} }
@Override
public String toString() {
return "ContainerConfiguration [mode=" + mode + ", hostname=" + hostname + ", port=" + port + ", authenticationEnpoint=" + authenticationEnpoint + ", protocol=" + protocol
+ ", authorizeChildrenContext=" + authorizeChildrenContext + ", infrastructure=" + infrastructure
+ ", site=" + site + ", tokens=" + tokens + ", allowedContext=" + allowedContext + ", apps=" + apps
+ ", properties=" + properties + ", publicationFrequency=" + publicationFrequency
+ ", persistenceManager=" + persistenceManager + "]";
}
} }

View File

@ -1,52 +0,0 @@
package org.gcube.smartgears.configuration.container;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import org.gcube.common.validator.annotations.NotNull;
@XmlRootElement(name="proxy")
public class ProxyAddress {
@XmlAttribute
boolean secure = false;
@XmlElement
@NotNull
String hostname;
@XmlElement
@NotNull
int port;
public String hostname() {
return hostname;
}
public ProxyAddress hostname(String hostname) {
this.hostname = hostname;
return this;
}
public int port() {
return port;
}
public ProxyAddress port(int port) {
this.port = port;
return this;
}
public boolean secure() {
return secure;
}
public ProxyAddress secure(boolean secure) {
this.secure = secure;
return this;
}
}

View File

@ -12,11 +12,15 @@ import org.gcube.common.resources.gcore.HostingNode;
import org.gcube.smartgears.configuration.application.ApplicationConfiguration; import org.gcube.smartgears.configuration.application.ApplicationConfiguration;
import org.gcube.smartgears.configuration.container.ContainerConfiguration; import org.gcube.smartgears.configuration.container.ContainerConfiguration;
import org.gcube.smartgears.context.application.ApplicationContext; import org.gcube.smartgears.context.application.ApplicationContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ProfileBuilder { public class ProfileBuilder {
private static List<String> servletExcludes = Arrays.asList("default","jsp"); private static List<String> servletExcludes = Arrays.asList("default","jsp");
private static final Logger log = LoggerFactory.getLogger(ProfileBuilder.class);
private ApplicationContext context; private ApplicationContext context;
public ProfileBuilder(ApplicationContext context) { public ProfileBuilder(ApplicationContext context) {
@ -43,18 +47,18 @@ public class ProfileBuilder {
.status((context.lifecycle().state().remoteForm())); .status((context.lifecycle().state().remoteForm()));
endpoint.profile().endpoints().clear(); endpoint.profile().endpoints().clear();
String baseAddress; String baseAddress;
if (configuration.proxied()){ if (configuration.proxied()){
String protocol = container.proxyAddress().secure()? "https://": "http://"; String protocol = configuration.proxyAddress().protocol();
int port = container.proxyAddress().port(); String port = configuration.proxyAddress().port()!=null?":"+configuration.proxyAddress().port():"";
baseAddress=String.format("%s%s:%d%s", protocol , container.proxyAddress().hostname(), port,context.application().getContextPath()); baseAddress=String.format("%s://%s%s%s", protocol , configuration.proxyAddress().hostname(), port,context.application().getContextPath());
} else { } else {
String protocol = configuration.secure()? "https://": "http://"; String protocol = container.protocol();
int port = configuration.secure()?container.securePort(): container.port(); int port = container.port();
baseAddress=String.format("%s%s:%d%s", protocol , container.hostname(), port,context.application().getContextPath()); baseAddress=String.format("%s://%s:%d%s", protocol , container.hostname(), port,context.application().getContextPath());
} }
for (ServletRegistration servlet : context.application().getServletRegistrations().values()) for (ServletRegistration servlet : context.application().getServletRegistrations().values())

View File

@ -69,19 +69,21 @@ public class RequestAccounting extends RequestHandler {
String callerIp = e.request().getHeader("x-forwarded-for"); String callerIp = e.request().getHeader("x-forwarded-for");
if(callerIp==null) if(callerIp==null)
callerIp=e.request().getRemoteHost(); callerIp=e.request().getRemoteHost();
boolean success = e.response().getStatus()<400;
generateAccounting(caller,callerQualifier,callerIp==null?"UNKNOWN":callerIp , context); generateAccounting(caller,callerQualifier,callerIp==null?"UNKNOWN":callerIp , success, context);
log.info("REQUEST SERVED ON {}:{}({}) CALLED FROM {}@{} IN SCOPE {} FINISHED IN {} millis", log.info("REQUEST SERVED ON {}:{}({}) CALLED FROM {}@{} IN SCOPE {} {}(CODE {}) IN {} millis",
context.configuration().name(),context.configuration().serviceClass(), InnerMethodName.instance.get(), context.configuration().name(),context.configuration().serviceClass(), InnerMethodName.instance.get(),
caller, callerIp, ScopeProvider.instance.get(), System.currentTimeMillis()-startCallThreadLocal.get()); caller, callerIp, ScopeProvider.instance.get(), success?"SUCCEDED":"FAILED", e.response().getStatus(), System.currentTimeMillis()-startCallThreadLocal.get());
startCallThreadLocal.remove(); startCallThreadLocal.remove();
InnerMethodName.instance.reset(); InnerMethodName.instance.reset();
if (resetScope) if (resetScope)
ScopeProvider.instance.reset(); ScopeProvider.instance.reset();
} }
void generateAccounting(String caller, String callerQualifier, String remoteHost, ApplicationContext context){ void generateAccounting(String caller, String callerQualifier, String remoteHost, boolean success, ApplicationContext context){
AccountingPersistenceFactory.setFallbackLocation(context.container().persistence().location()); AccountingPersistenceFactory.setFallbackLocation(context.container().persistence().location());
AccountingPersistence persistence = AccountingPersistenceFactory.getPersistence(); AccountingPersistence persistence = AccountingPersistenceFactory.getPersistence();
ServiceUsageRecord serviceUsageRecord = new ServiceUsageRecord(); ServiceUsageRecord serviceUsageRecord = new ServiceUsageRecord();
@ -92,11 +94,11 @@ public class RequestAccounting extends RequestHandler {
serviceUsageRecord.setScope(ScopeProvider.instance.get()); serviceUsageRecord.setScope(ScopeProvider.instance.get());
serviceUsageRecord.setServiceClass(context.configuration().serviceClass()); serviceUsageRecord.setServiceClass(context.configuration().serviceClass());
serviceUsageRecord.setServiceName(context.configuration().name()); serviceUsageRecord.setServiceName(context.configuration().name());
serviceUsageRecord.setHost(context.container().configuration().hostname()+":"+context.container().configuration().port()); serviceUsageRecord.setHost(context.container().configuration().hostname()+":"+context.container().configuration().port());
serviceUsageRecord.setCalledMethod(InnerMethodName.instance.get()); serviceUsageRecord.setCalledMethod(InnerMethodName.instance.get());
serviceUsageRecord.setCallerHost(remoteHost); serviceUsageRecord.setCallerHost(remoteHost);
serviceUsageRecord.setOperationResult(OperationResult.SUCCESS); serviceUsageRecord.setOperationResult(success?OperationResult.SUCCESS:OperationResult.FAILED);
serviceUsageRecord.setDuration(System.currentTimeMillis()-startCallThreadLocal.get()); serviceUsageRecord.setDuration(System.currentTimeMillis()-startCallThreadLocal.get());
persistence.account(serviceUsageRecord); persistence.account(serviceUsageRecord);

View File

@ -78,11 +78,11 @@ public class ApplicationManager {
context.configuration().validate(); context.configuration().validate();
if (context.configuration().secure() && /* if (context.configuration().secure() &&
container.configuration().securePort()==null) container.configuration().securePort()==null)
throw new IllegalStateException( throw new IllegalStateException(
String.format("Application %s cannot be managed because is declared as secure without a secure connector port declared in the container", context.application().getContextPath())); String.format("Application %s cannot be managed because is declared as secure without a secure connector port declared in the container", context.application().getContextPath()));
*/
context.configuration().startTokens(generateTokensForApplication(container)); context.configuration().startTokens(generateTokensForApplication(container));

View File

@ -17,6 +17,7 @@ import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import org.gcube.common.authorization.library.exception.AuthorizationException;
import org.gcube.smartgears.configuration.application.Exclude; import org.gcube.smartgears.configuration.application.Exclude;
import org.gcube.smartgears.configuration.application.Include; import org.gcube.smartgears.configuration.application.Include;
import org.gcube.smartgears.context.application.ApplicationContext; import org.gcube.smartgears.context.application.ApplicationContext;
@ -27,6 +28,7 @@ import org.gcube.smartgears.handlers.application.RequestHandler;
import org.gcube.smartgears.handlers.application.ResponseEvent; import org.gcube.smartgears.handlers.application.ResponseEvent;
import org.gcube.smartgears.handlers.application.request.RequestError; import org.gcube.smartgears.handlers.application.request.RequestError;
import org.gcube.smartgears.handlers.application.request.RequestException; import org.gcube.smartgears.handlers.application.request.RequestException;
import org.gcube.smartgears.utils.Utils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -80,25 +82,27 @@ public class RequestManager implements Filter {
// create a per-request context with temporary properties // create a per-request context with temporary properties
ApplicationContext ctx = new DefaultApplicationContext(context); ApplicationContext ctx = new DefaultApplicationContext(context);
RequestEvent event = new RequestEvent(servlet, ctx, httprequest,httpresponse); RequestEvent event = new RequestEvent(servlet, ctx, httprequest,httpresponse);
try { try {
pipeline.forward(event); pipeline.forward(event);
} }
catch(Throwable t) { catch(Throwable t) {
log.error("error in doFilter, forward",t);
handleError(httprequest,httpresponse,t); handleError(httprequest,httpresponse,t);
return; return;
} }
try{ try{
// dispatch to other filters for this servlet // dispatch to other filters for this servlet
chain.doFilter(request, response); chain.doFilter(request, response);
}catch(Throwable t){ }catch(ServletException t){
t.printStackTrace(); log.error("error in doFilter",t.getRootCause());
handleError(httprequest,httpresponse,t); handleError(httprequest,httpresponse,t.getRootCause());
} }
ResponseEvent responseEvent = new ResponseEvent(servlet, ctx, httprequest, httpresponse); ResponseEvent responseEvent = new ResponseEvent(servlet, ctx, httprequest, httpresponse);
try { try {
@ -106,10 +110,11 @@ public class RequestManager implements Filter {
pipeline.reverse().forward(responseEvent); pipeline.reverse().forward(responseEvent);
} }
catch(Throwable t) { catch(Throwable t) {
t.printStackTrace(); log.error("error in doFilter, reverse",t);
handleError(httprequest,httpresponse,t); handleError(httprequest,httpresponse,t);
return; return;
} }
} }
} }
@ -172,7 +177,7 @@ public class RequestManager implements Filter {
} }
return new ArrayList<>(); return new ArrayList<>();
} }
return handlersToFilter; return handlersToFilter;
} }
@ -241,22 +246,29 @@ public class RequestManager implements Filter {
}*/ }*/
private void handleError(HttpServletRequest request, HttpServletResponse response,Throwable t) throws IOException { private void handleError(HttpServletRequest request, HttpServletResponse response,Throwable t) throws IOException {
if(t instanceof AuthorizationException) {
response.setStatus(HttpServletResponse.SC_FORBIDDEN);
response.getWriter().write("Error (Forbidden) : "+t.getMessage()+"\nStacktrace:\n");
t.printStackTrace(response.getWriter());
response.flushBuffer();
} else {
RequestError error = t instanceof RequestException? RequestError error = t instanceof RequestException?
RequestException.class.cast(t).error(): RequestException.class.cast(t).error():
application_error; application_error;
if (error == application_error) {
response.sendError(error.code(),error.message());
}else {
if (error == request_not_authorized_error){
response.setHeader("WWW-Authenticate", "Basic realm=\"Smartgears\"");
log.info("setting WWW-Authenticate to response header");
}
response.getWriter().write("Error ("+error.code()+") : "+error.message()+"\nStacktrace:\n");
t.printStackTrace(response.getWriter());
response.setStatus(error.code());
}
response.resetBuffer();
if (error == request_not_authorized_error){
response.setHeader("WWW-Authenticate", "Basic realm=\"Smartgears\"");
log.info("setting WWW-Authenticate to response header");
}
response.setStatus(error.code());
response.getWriter().write("Error ("+error.code()+") : "+error.message()+"\nStacktrace:\n");
t.printStackTrace(response.getWriter());
response.flushBuffer();
}
} }
} }

View File

@ -217,5 +217,14 @@ public class Utils {
new ServiceInfo(new ServiceIdentifier(application.configuration().serviceClass(), application.configuration().name(), hostedin)); new ServiceInfo(new ServiceIdentifier(application.configuration().serviceClass(), application.configuration().name(), hostedin));
} }
public static Throwable getCause(Throwable e) {
Throwable cause = null;
Throwable result = e;
while(null != (cause = result.getCause()) && (result != cause) ) {
result = cause;
}
return result;
}
} }

View File

@ -4,14 +4,12 @@ import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertNotNull; import static junit.framework.Assert.assertNotNull;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.util.Arrays;
import org.gcube.smartgears.configuration.Mode; import org.gcube.smartgears.configuration.Mode;
import org.gcube.smartgears.configuration.application.ApplicationConfiguration; import org.gcube.smartgears.configuration.application.ApplicationConfiguration;
import org.gcube.smartgears.configuration.application.ApplicationConfigurationBinder; import org.gcube.smartgears.configuration.application.ApplicationConfigurationBinder;
import org.gcube.smartgears.configuration.application.ApplicationExtensions; import org.gcube.smartgears.configuration.application.ApplicationExtensions;
import org.gcube.smartgears.configuration.application.DefaultApplicationConfiguration; import org.gcube.smartgears.configuration.application.DefaultApplicationConfiguration;
import org.gcube.smartgears.configuration.application.Exclude;
import org.gcube.smartgears.configuration.application.Include; import org.gcube.smartgears.configuration.application.Include;
import org.gcube.smartgears.extensions.ApplicationExtension; import org.gcube.smartgears.extensions.ApplicationExtension;
import org.gcube.smartgears.persistence.DefaultPersistence; import org.gcube.smartgears.persistence.DefaultPersistence;
@ -101,7 +99,6 @@ public class ConfigurationTest {
return new DefaultApplicationConfiguration() return new DefaultApplicationConfiguration()
.secure(true)
.mode(Mode.offline) .mode(Mode.offline)
.context("ctx") .context("ctx")
.name("name") .name("name")

View File

@ -28,7 +28,6 @@ public class ConfigurationTest {
String xml = "<container mode='offline'>" String xml = "<container mode='offline'>"
+ "<hostname>localhost</hostname>" + "<hostname>localhost</hostname>"
+ "<port>8080</port>" + "<port>8080</port>"
+ "<secure-port>8484</secure-port>"
+ "<infrastructure>gcube</infrastructure>" + "<infrastructure>gcube</infrastructure>"
+ "<authorizeChildrenContext>true</authorizeChildrenContext> " + "<authorizeChildrenContext>true</authorizeChildrenContext> "
+"<token>token1</token>" + "<token>token2</token>" + "<persistence location='target'/>" + appXml + "<site>" +"<token>token1</token>" + "<token>token2</token>" + "<persistence location='target'/>" + appXml + "<site>"
@ -54,7 +53,7 @@ public class ConfigurationTest {
private ContainerConfiguration sampleContainerConfiguration() { private ContainerConfiguration sampleContainerConfiguration() {
return new ContainerConfiguration().mode(Mode.offline).hostname("localhost").port(8080).securePort(8484).infrastructure("gcube") return new ContainerConfiguration().mode(Mode.offline).hostname("localhost").port(8080).infrastructure("gcube")
.startTokens(Arrays.asList("token1", "token2")) .startTokens(Arrays.asList("token1", "token2"))
.site(new Site().country("it").location("rome").latitude("41.9000").longitude("12.5000")) .site(new Site().country("it").location("rome").latitude("41.9000").longitude("12.5000"))
.property("prop1", "val1").property("prop2", "val2").publicationFrequency(30) .property("prop1", "val1").property("prop2", "val2").publicationFrequency(30)