From 55d31996152e0808d7d6af3de20e03bbb7291bf1 Mon Sep 17 00:00:00 2001 From: Lucio Lelii Date: Wed, 16 Jan 2019 14:37:56 +0000 Subject: [PATCH] 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 --- distro/changelog.xml | 5 + pom.xml | 2 +- .../application/ApplicationConfiguration.java | 16 +-- .../BridgedApplicationConfiguration.java | 25 ++-- .../DefaultApplicationConfiguration.java | 128 +++--------------- .../application/ProxyAddress.java | 97 +++++++++++++ .../container/ContainerConfiguration.java | 85 ++++-------- .../configuration/container/ProxyAddress.java | 52 ------- .../application/lifecycle/ProfileBuilder.java | 18 ++- .../request/RequestAccounting.java | 14 +- .../managers/ApplicationManager.java | 4 +- .../smartgears/managers/RequestManager.java | 58 ++++---- .../org/gcube/smartgears/utils/Utils.java | 9 ++ .../test/application/ConfigurationTest.java | 3 - .../test/container/ConfigurationTest.java | 3 +- 15 files changed, 228 insertions(+), 291 deletions(-) create mode 100644 src/main/java/org/gcube/smartgears/configuration/application/ProxyAddress.java delete mode 100644 src/main/java/org/gcube/smartgears/configuration/container/ProxyAddress.java diff --git a/distro/changelog.xml b/distro/changelog.xml index cf5b236..21b4dbd 100644 --- a/distro/changelog.xml +++ b/distro/changelog.xml @@ -1,4 +1,9 @@ + + Added Proxy Address to Application Configuration + Added protocol to Container Configuration (http by default) + Changed the logs in accounting handler to log error or success and eventually error code + Added ThreadLocal InnerMethodName to set method name from application diff --git a/pom.xml b/pom.xml index 44ea0ba..a86d480 100644 --- a/pom.xml +++ b/pom.xml @@ -10,7 +10,7 @@ org.gcube.core common-smartgears - 2.1.6-SNAPSHOT + 2.1.7-SNAPSHOT SmartGears diff --git a/src/main/java/org/gcube/smartgears/configuration/application/ApplicationConfiguration.java b/src/main/java/org/gcube/smartgears/configuration/application/ApplicationConfiguration.java index 05d7e75..c7e1257 100644 --- a/src/main/java/org/gcube/smartgears/configuration/application/ApplicationConfiguration.java +++ b/src/main/java/org/gcube/smartgears/configuration/application/ApplicationConfiguration.java @@ -20,17 +20,7 @@ public interface ApplicationConfiguration { */ 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 @@ -53,9 +43,7 @@ public interface ApplicationConfiguration { * @return this configuration */ ApplicationConfiguration mode(Mode mode); - - ApplicationConfiguration secure(boolean value); /** * Returns the name of the application. @@ -110,6 +98,9 @@ public interface ApplicationConfiguration { */ ApplicationConfiguration description(String description); + ProxyAddress proxyAddress(); + + ApplicationConfiguration proxyAddress(ProxyAddress proxyaddress); /** * Returns the tokens in which the application operates when it first starts. @@ -173,5 +164,4 @@ public interface ApplicationConfiguration { - } \ No newline at end of file diff --git a/src/main/java/org/gcube/smartgears/configuration/application/BridgedApplicationConfiguration.java b/src/main/java/org/gcube/smartgears/configuration/application/BridgedApplicationConfiguration.java index 6140b29..89e99b3 100644 --- a/src/main/java/org/gcube/smartgears/configuration/application/BridgedApplicationConfiguration.java +++ b/src/main/java/org/gcube/smartgears/configuration/application/BridgedApplicationConfiguration.java @@ -42,9 +42,6 @@ public class BridgedApplicationConfiguration implements ApplicationConfiguration log.trace("setting persistence location for {} @ {}",application.name(), dir.getAbsolutePath()); } - if (container.proxyAddress()==null) - application.proxied(false); - } public ApplicationConfiguration inner() { @@ -92,6 +89,10 @@ public class BridgedApplicationConfiguration implements ApplicationConfiguration public String description() { return application.description(); } + + public ProxyAddress proxyAddress() { + return application.proxyAddress(); + } public ApplicationConfiguration description(String description) { return application.description(description); @@ -130,15 +131,6 @@ public class BridgedApplicationConfiguration implements ApplicationConfiguration application.merge(config); } - @Override - public boolean secure() { - return application.secure(); - } - - @Override - public ApplicationConfiguration secure(boolean value) { - return application.secure(value); - } @Override public Set startTokens() { @@ -150,10 +142,6 @@ public class BridgedApplicationConfiguration implements ApplicationConfiguration return application.startTokens(tokens); } - @Override - public ApplicationConfiguration proxied(boolean proxied) { - return application.proxied(proxied); - } @Override public boolean proxied() { @@ -170,5 +158,10 @@ public class BridgedApplicationConfiguration implements ApplicationConfiguration return application.includes(includes); } + @Override + public ApplicationConfiguration proxyAddress(ProxyAddress proxyaddress) { + return application.proxyAddress(proxyaddress); + } + } diff --git a/src/main/java/org/gcube/smartgears/configuration/application/DefaultApplicationConfiguration.java b/src/main/java/org/gcube/smartgears/configuration/application/DefaultApplicationConfiguration.java index fb89df4..89e93ff 100644 --- a/src/main/java/org/gcube/smartgears/configuration/application/DefaultApplicationConfiguration.java +++ b/src/main/java/org/gcube/smartgears/configuration/application/DefaultApplicationConfiguration.java @@ -37,16 +37,10 @@ public class DefaultApplicationConfiguration implements ApplicationConfiguration @XmlAttribute private Mode mode = Mode.online; - - @XmlAttribute(name="isSecure") - private boolean secure = false; @XmlAttribute(name="context") String context; - - @XmlAttribute(name="proxied") - private boolean proxied = true; - + @XmlElement(name="name" , required=true) @NotNull String name; @@ -65,6 +59,10 @@ public class DefaultApplicationConfiguration implements ApplicationConfiguration @XmlElement(name="description") String description=""; + @XmlElementRef + @IsValid + ProxyAddress proxyAddress; + @XmlElementRef Set excludes= new LinkedHashSet(); @@ -79,6 +77,7 @@ public class DefaultApplicationConfiguration implements ApplicationConfiguration public Set excludes() { return excludes; } + @Override public Set includes() { @@ -91,11 +90,6 @@ public class DefaultApplicationConfiguration implements ApplicationConfiguration public Mode mode() { return mode; } - - @Override - public boolean secure() { - return secure; - } @Override public String name() { @@ -113,6 +107,10 @@ public class DefaultApplicationConfiguration implements ApplicationConfiguration return this; } + public ProxyAddress proxyAddress() { + return proxyAddress; + } + @Override public ApplicationConfiguration excludes(Exclude ... excludes) { this.excludes=new HashSet(Arrays.asList(excludes)); @@ -135,17 +133,7 @@ public class DefaultApplicationConfiguration implements ApplicationConfiguration public String serviceClass() { return group; } - - @Override - public boolean proxied() { - return proxied; - } - @Override - public ApplicationConfiguration proxied(boolean proxied) { - this.proxied = proxied; - return this; - } @Override public ApplicationConfiguration serviceClass(String group) { @@ -186,7 +174,10 @@ public class DefaultApplicationConfiguration implements ApplicationConfiguration return this; } - + @Override + public boolean proxied() { + return proxyAddress!=null; + } @Override public Persistence persistence() { @@ -198,10 +189,10 @@ public class DefaultApplicationConfiguration implements ApplicationConfiguration this.persistenceManager=manager; return this; } - + @Override - public ApplicationConfiguration secure(boolean value) { - this.secure=value; + public ApplicationConfiguration proxyAddress(ProxyAddress proxyaddress) { + this.proxyAddress = proxyaddress; 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; - } - - - } \ No newline at end of file diff --git a/src/main/java/org/gcube/smartgears/configuration/application/ProxyAddress.java b/src/main/java/org/gcube/smartgears/configuration/application/ProxyAddress.java new file mode 100644 index 0000000..ae9bb97 --- /dev/null +++ b/src/main/java/org/gcube/smartgears/configuration/application/ProxyAddress.java @@ -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; + } + + + + + +} diff --git a/src/main/java/org/gcube/smartgears/configuration/container/ContainerConfiguration.java b/src/main/java/org/gcube/smartgears/configuration/container/ContainerConfiguration.java index 4ea5052..a12fca5 100644 --- a/src/main/java/org/gcube/smartgears/configuration/container/ContainerConfiguration.java +++ b/src/main/java/org/gcube/smartgears/configuration/container/ContainerConfiguration.java @@ -42,28 +42,25 @@ public class ContainerConfiguration { private Mode mode = Mode.online; @XmlElement - @NotNull + @NotNull @IsValid String hostname; @XmlElement @NotNull Integer port; - @XmlElementRef - @IsValid - ProxyAddress proxyAddress; - @XmlElement(name ="authentication-endpoint") String authenticationEnpoint = null; - @XmlElement(name ="secure-port") - Integer securePort; + @XmlElement(name ="protocol") + @NotNull @IsValid + String protocol="http"; @XmlElement boolean authorizeChildrenContext = false; @XmlElement - @NotNull + @NotNull@IsValid String infrastructure; @XmlElement @@ -186,6 +183,8 @@ public class ContainerConfiguration { return this; } + + /** * Returns the host name of the container. * @return the host name; @@ -217,8 +216,8 @@ public class ContainerConfiguration { * Returns the port at which the container is listening for requests. * @return the port */ - public Integer securePort() { - return securePort; + public String protocol() { + return protocol; } @@ -241,8 +240,8 @@ public class ContainerConfiguration { return this; } - public ContainerConfiguration securePort(int port) { - this.securePort=port; + public ContainerConfiguration protocol(String protocol) { + this.protocol=protocol; return this; } @@ -276,15 +275,6 @@ public class ContainerConfiguration { return this; } - - public ProxyAddress proxyAddress() { - return proxyAddress; - } - - public ContainerConfiguration setProxyaddress(ProxyAddress proxyaddress) { - this.proxyAddress = proxyaddress; - return this; - } /** * Returns the persistence manager of the container. @@ -420,8 +410,6 @@ public class ContainerConfiguration { return true; } - - } @@ -430,32 +418,18 @@ public class ContainerConfiguration { public int hashCode() { final int prime = 31; int result = 1; - result = prime * result - + ((allowedContext == null) ? 0 : allowedContext.hashCode()); + result = prime * result + ((allowedContext == null) ? 0 : allowedContext.hashCode()); result = prime * result + ((apps == null) ? 0 : apps.hashCode()); - result = prime - * result - + ((authenticationEnpoint == null) ? 0 : authenticationEnpoint - .hashCode()); + result = prime * result + ((authenticationEnpoint == null) ? 0 : authenticationEnpoint.hashCode()); result = prime * result + (authorizeChildrenContext ? 1231 : 1237); - result = prime * result - + ((hostname == null) ? 0 : hostname.hashCode()); - result = prime * result - + ((infrastructure == null) ? 0 : infrastructure.hashCode()); + result = prime * result + ((hostname == null) ? 0 : hostname.hashCode()); + result = prime * result + ((infrastructure == null) ? 0 : infrastructure.hashCode()); result = prime * result + ((mode == null) ? 0 : mode.hashCode()); - result = prime - * result - + ((persistenceManager == null) ? 0 : persistenceManager - .hashCode()); + result = prime * result + ((persistenceManager == null) ? 0 : persistenceManager.hashCode()); result = prime * result + ((port == null) ? 0 : port.hashCode()); - result = prime * result - + ((properties == null) ? 0 : properties.hashCode()); - result = prime * result - + ((proxyAddress == null) ? 0 : proxyAddress.hashCode()); - result = prime * result - + (int) (publicationFrequency ^ (publicationFrequency >>> 32)); - result = prime * result - + ((securePort == null) ? 0 : securePort.hashCode()); + result = prime * result + ((properties == null) ? 0 : properties.hashCode()); + result = prime * result + ((protocol == null) ? 0 : protocol.hashCode()); + result = prime * result + (int) (publicationFrequency ^ (publicationFrequency >>> 32)); result = prime * result + ((site == null) ? 0 : site.hashCode()); result = prime * result + ((tokens == null) ? 0 : tokens.hashCode()); return result; @@ -514,18 +488,13 @@ public class ContainerConfiguration { return false; } else if (!properties.equals(other.properties)) return false; - if (proxyAddress == null) { - if (other.proxyAddress != null) + if (protocol == null) { + if (other.protocol != null) return false; - } else if (!proxyAddress.equals(other.proxyAddress)) + } else if (!protocol.equals(other.protocol)) return false; if (publicationFrequency != other.publicationFrequency) return false; - if (securePort == null) { - if (other.securePort != null) - return false; - } else if (!securePort.equals(other.securePort)) - return false; if (site == null) { if (other.site != null) return false; @@ -539,7 +508,13 @@ public class ContainerConfiguration { 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 + "]"; + } } \ No newline at end of file diff --git a/src/main/java/org/gcube/smartgears/configuration/container/ProxyAddress.java b/src/main/java/org/gcube/smartgears/configuration/container/ProxyAddress.java deleted file mode 100644 index 8cfde55..0000000 --- a/src/main/java/org/gcube/smartgears/configuration/container/ProxyAddress.java +++ /dev/null @@ -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; - } - - - -} diff --git a/src/main/java/org/gcube/smartgears/handlers/application/lifecycle/ProfileBuilder.java b/src/main/java/org/gcube/smartgears/handlers/application/lifecycle/ProfileBuilder.java index d22b93c..522b9ba 100644 --- a/src/main/java/org/gcube/smartgears/handlers/application/lifecycle/ProfileBuilder.java +++ b/src/main/java/org/gcube/smartgears/handlers/application/lifecycle/ProfileBuilder.java @@ -12,11 +12,15 @@ import org.gcube.common.resources.gcore.HostingNode; import org.gcube.smartgears.configuration.application.ApplicationConfiguration; import org.gcube.smartgears.configuration.container.ContainerConfiguration; import org.gcube.smartgears.context.application.ApplicationContext; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class ProfileBuilder { private static List servletExcludes = Arrays.asList("default","jsp"); + private static final Logger log = LoggerFactory.getLogger(ProfileBuilder.class); + private ApplicationContext context; public ProfileBuilder(ApplicationContext context) { @@ -43,18 +47,18 @@ public class ProfileBuilder { .status((context.lifecycle().state().remoteForm())); endpoint.profile().endpoints().clear(); - + String baseAddress; if (configuration.proxied()){ - String protocol = container.proxyAddress().secure()? "https://": "http://"; - int port = container.proxyAddress().port(); + String protocol = configuration.proxyAddress().protocol(); + 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 { - String protocol = configuration.secure()? "https://": "http://"; - int port = configuration.secure()?container.securePort(): container.port(); + String protocol = container.protocol(); + 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()) diff --git a/src/main/java/org/gcube/smartgears/handlers/application/request/RequestAccounting.java b/src/main/java/org/gcube/smartgears/handlers/application/request/RequestAccounting.java index b9910a5..69f6cc0 100644 --- a/src/main/java/org/gcube/smartgears/handlers/application/request/RequestAccounting.java +++ b/src/main/java/org/gcube/smartgears/handlers/application/request/RequestAccounting.java @@ -69,19 +69,21 @@ public class RequestAccounting extends RequestHandler { String callerIp = e.request().getHeader("x-forwarded-for"); if(callerIp==null) 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(), - 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(); InnerMethodName.instance.reset(); if (resetScope) 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()); AccountingPersistence persistence = AccountingPersistenceFactory.getPersistence(); ServiceUsageRecord serviceUsageRecord = new ServiceUsageRecord(); @@ -92,11 +94,11 @@ public class RequestAccounting extends RequestHandler { serviceUsageRecord.setScope(ScopeProvider.instance.get()); serviceUsageRecord.setServiceClass(context.configuration().serviceClass()); serviceUsageRecord.setServiceName(context.configuration().name()); - + serviceUsageRecord.setHost(context.container().configuration().hostname()+":"+context.container().configuration().port()); serviceUsageRecord.setCalledMethod(InnerMethodName.instance.get()); serviceUsageRecord.setCallerHost(remoteHost); - serviceUsageRecord.setOperationResult(OperationResult.SUCCESS); + serviceUsageRecord.setOperationResult(success?OperationResult.SUCCESS:OperationResult.FAILED); serviceUsageRecord.setDuration(System.currentTimeMillis()-startCallThreadLocal.get()); persistence.account(serviceUsageRecord); diff --git a/src/main/java/org/gcube/smartgears/managers/ApplicationManager.java b/src/main/java/org/gcube/smartgears/managers/ApplicationManager.java index b2e1aa0..fd3cc03 100644 --- a/src/main/java/org/gcube/smartgears/managers/ApplicationManager.java +++ b/src/main/java/org/gcube/smartgears/managers/ApplicationManager.java @@ -78,11 +78,11 @@ public class ApplicationManager { context.configuration().validate(); - if (context.configuration().secure() && +/* if (context.configuration().secure() && container.configuration().securePort()==null) 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())); - +*/ context.configuration().startTokens(generateTokensForApplication(container)); diff --git a/src/main/java/org/gcube/smartgears/managers/RequestManager.java b/src/main/java/org/gcube/smartgears/managers/RequestManager.java index ec14e99..4138e27 100644 --- a/src/main/java/org/gcube/smartgears/managers/RequestManager.java +++ b/src/main/java/org/gcube/smartgears/managers/RequestManager.java @@ -17,6 +17,7 @@ import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; 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.Include; 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.request.RequestError; import org.gcube.smartgears.handlers.application.request.RequestException; +import org.gcube.smartgears.utils.Utils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -80,25 +82,27 @@ public class RequestManager implements Filter { // create a per-request context with temporary properties ApplicationContext ctx = new DefaultApplicationContext(context); - + RequestEvent event = new RequestEvent(servlet, ctx, httprequest,httpresponse); try { pipeline.forward(event); } catch(Throwable t) { + log.error("error in doFilter, forward",t); handleError(httprequest,httpresponse,t); return; } - + try{ // dispatch to other filters for this servlet chain.doFilter(request, response); - }catch(Throwable t){ - t.printStackTrace(); - handleError(httprequest,httpresponse,t); + }catch(ServletException t){ + log.error("error in doFilter",t.getRootCause()); + handleError(httprequest,httpresponse,t.getRootCause()); } - + + ResponseEvent responseEvent = new ResponseEvent(servlet, ctx, httprequest, httpresponse); try { @@ -106,10 +110,11 @@ public class RequestManager implements Filter { pipeline.reverse().forward(responseEvent); } catch(Throwable t) { - t.printStackTrace(); + log.error("error in doFilter, reverse",t); handleError(httprequest,httpresponse,t); return; } + } } @@ -172,7 +177,7 @@ public class RequestManager implements Filter { } return new ArrayList<>(); } - + return handlersToFilter; } @@ -241,22 +246,29 @@ public class RequestManager implements Filter { }*/ 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? - RequestException.class.cast(t).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()); - } + RequestError error = t instanceof RequestException? + RequestException.class.cast(t).error(): + application_error; + 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(); + } } } diff --git a/src/main/java/org/gcube/smartgears/utils/Utils.java b/src/main/java/org/gcube/smartgears/utils/Utils.java index bdf06aa..43caaa2 100644 --- a/src/main/java/org/gcube/smartgears/utils/Utils.java +++ b/src/main/java/org/gcube/smartgears/utils/Utils.java @@ -217,5 +217,14 @@ public class Utils { 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; + } } diff --git a/src/test/java/test/application/ConfigurationTest.java b/src/test/java/test/application/ConfigurationTest.java index a444ccf..c9d6d81 100644 --- a/src/test/java/test/application/ConfigurationTest.java +++ b/src/test/java/test/application/ConfigurationTest.java @@ -4,14 +4,12 @@ import static junit.framework.Assert.assertEquals; import static junit.framework.Assert.assertNotNull; import java.io.ByteArrayInputStream; -import java.util.Arrays; import org.gcube.smartgears.configuration.Mode; import org.gcube.smartgears.configuration.application.ApplicationConfiguration; import org.gcube.smartgears.configuration.application.ApplicationConfigurationBinder; import org.gcube.smartgears.configuration.application.ApplicationExtensions; 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.extensions.ApplicationExtension; import org.gcube.smartgears.persistence.DefaultPersistence; @@ -101,7 +99,6 @@ public class ConfigurationTest { return new DefaultApplicationConfiguration() - .secure(true) .mode(Mode.offline) .context("ctx") .name("name") diff --git a/src/test/java/test/container/ConfigurationTest.java b/src/test/java/test/container/ConfigurationTest.java index 551dce8..0f29ef6 100644 --- a/src/test/java/test/container/ConfigurationTest.java +++ b/src/test/java/test/container/ConfigurationTest.java @@ -28,7 +28,6 @@ public class ConfigurationTest { String xml = "" + "localhost" + "8080" - + "8484" + "gcube" + "true " +"token1" + "token2" + "" + appXml + "" @@ -54,7 +53,7 @@ public class ConfigurationTest { 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")) .site(new Site().country("it").location("rome").latitude("41.9000").longitude("12.5000")) .property("prop1", "val1").property("prop2", "val2").publicationFrequency(30)