Commit 9b485529 authored by Matija Obreza's avatar Matija Obreza

NewGUIFilter handles /1/ URLs

parent 0a0297b6
/**
* Copyright 2014 Global Crop Diversity Trust
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
package org.genesys2.server.servlet.filter;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
public class NewGUIFilter implements Filter {
private static final Logger LOG = Logger.getLogger(NewGUIFilter.class);
public static final String REQUEST_GUIVERSION_ATTR = LocaleURLFilter.class.getName() + ".GUIVERSION";
private static final Pattern newGuiPattern = Pattern.compile("/(\\d+)(/.+)");
private String[] excludedPaths;
@Override
public void init(FilterConfig filterConfig) throws ServletException {
String excludePaths = filterConfig.getInitParameter("exclude-paths");
if (StringUtils.isNotBlank(excludePaths)) {
String[] ex = excludePaths.split("\\s+");
for (String e : ex) {
LOG.info("Excluding path: " + e);
}
this.excludedPaths = ex;
}
}
@Override
public void destroy() {
if (LOG.isDebugEnabled()) {
LOG.debug("Destroying NewGUIFilter");
}
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
final HttpServletRequest httpRequest = (HttpServletRequest) servletRequest;
HttpServletResponse httpResponse = (HttpServletResponse) servletResponse;
final String originalUrl = httpRequest.getRequestURI().substring(httpRequest.getContextPath().length());
if (LOG.isTraceEnabled()) {
LOG.trace("Incoming URL: " + originalUrl);
}
final Matcher matcher = newGuiPattern.matcher(originalUrl);
if (matcher.matches()) {
final String guiPrefix = "/" + matcher.group(1);
final String remainingUrl = matcher.group(2);
try {
final int guiVersion = Integer.parseInt(matcher.group(1));
httpRequest.setAttribute(REQUEST_GUIVERSION_ATTR, guiVersion);
} catch (NumberFormatException e) {
LOG.info("Invalid GUI version in " + matcher.group(1) + ": " + e.getMessage());
}
if (LOG.isDebugEnabled()) {
LOG.trace("URL matches! prefix=" + guiPrefix + " remaining=" + remainingUrl);
LOG.debug("Proxying request to remaining URL " + remainingUrl);
}
HttpServletRequestWrapper guiRequest = new HttpServletRequestWrapper(httpRequest) {
@Override
public String getServletPath() {
String servletPath = super.getServletPath();
if (originalUrl.equals(servletPath)) {
if (LOG.isDebugEnabled())
LOG.debug("servletPath=" + servletPath + " remaining=" + remainingUrl);
return remainingUrl;
}
return servletPath;
}
@Override
public String getRequestURI() {
String requestURI = super.getRequestURI();
if (originalUrl.equals(requestURI)) {
if (LOG.isDebugEnabled())
LOG.debug("requestURI=" + requestURI + " remaining=" + remainingUrl);
return remainingUrl;
}
return requestURI;
}
};
HttpServletResponseWrapper guiResponse = new HttpServletResponseWrapper(httpResponse) {
private boolean isExcluded(String url) {
for (String excludedPath : excludedPaths) {
if (url.startsWith(excludedPath)) {
if (LOG.isTraceEnabled()) {
LOG.trace("Excluded=" + excludedPath + " matches " + url);
}
return true;
}
}
return url.startsWith("?");
}
@Override
public String encodeURL(String url) {
if (isExcluded(url)) {
return super.encodeURL(url);
} else {
String encodedURL = super.encodeURL(guiPrefix + url);
if (LOG.isDebugEnabled()) {
LOG.debug("encodeURL " + url + " to " + encodedURL);
}
return encodedURL;
}
}
@Override
@Deprecated
public String encodeUrl(String url) {
if (isExcluded(url)) {
return super.encodeUrl(url);
} else {
String encodedURL = super.encodeUrl(guiPrefix + url);
if (LOG.isDebugEnabled()) {
LOG.debug("encodeUrl " + url + " to " + encodedURL);
}
return encodedURL;
}
}
@Override
public String encodeRedirectURL(String url) {
if (isExcluded(url)) {
return super.encodeRedirectURL(url);
} else {
String encodedURL = super.encodeRedirectURL(guiPrefix + url);
if (LOG.isDebugEnabled()) {
LOG.debug("encodeRedirectURL " + url + " to " + encodedURL);
}
return encodedURL;
}
}
};
filterChain.doFilter(guiRequest, guiResponse);
} else {
if (LOG.isTraceEnabled()) {
LOG.trace("No match on url " + originalUrl);
}
filterChain.doFilter(servletRequest, servletResponse);
}
}
}
......@@ -41,3 +41,4 @@ log4j.category.com.hazelcast=info
#log4j.category.org.eclipse.jetty.servlets=trace
#LocaleURLFilter
#log4j.category.org.genesys2.server.servlet.filter=debug
log4j.category.org.genesys2.server.servlet.filter.NewGUIFilter=debug
......@@ -10,12 +10,12 @@
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="<c:url value="/" />">Genesys 2</a>
<a class="navbar-brand" href="<c:url value="/1/welcome" />">Genesys 2</a>
</div>
<div id="navbar" class="collapse navbar-collapse">
<ul class="nav navbar-nav">
<li class="active">
<a href="<c:url value="/welcome" />" title="<spring:message code="menu.home" />">
<a href="<c:url value="/1/welcome" />" title="<spring:message code="menu.home" />">
<spring:message code="menu.home" />
</a>
</li>
......@@ -26,33 +26,33 @@
</a>
<ul class="dropdown-menu">
<li>
<a href="<c:url value="/content/about/about" />">
<a href="<c:url value="/1/content/about/about" />">
<spring:message code="menu.about" />
</a>
</li>
<li>
<a href="<c:url value="/content/about/contact" />">
<a href="<c:url value="/1/content/about/contact" />">
<spring:message code="menu.contact" />
</a>
</li>
<li>
<a href="<c:url value="/content/about/what-is-genesys" />">
<a href="<c:url value="/1/content/about/what-is-genesys" />">
<spring:message code="menu.what-is-genesys" />
</a>
</li>
<%-- <li><a href="<c:url value="/content/about/about" />"><i class="fa fa-caret-right"></i> <spring:message code="menu.who-uses-genesys" /></a></li> --%>
<%-- <li><a href="<c:url value="/1/content/about/about" />"><i class="fa fa-caret-right"></i> <spring:message code="menu.who-uses-genesys" /></a></li> --%>
<li>
<a href="<c:url value="/content/about/history-of-genesys" />">
<a href="<c:url value="/1/content/about/history-of-genesys" />">
<spring:message code="menu.history-of-genesys" />
</a>
</li>
<li>
<a href="<c:url value="/content/about/newsletter" />">
<a href="<c:url value="/1/content/about/newsletter" />">
<spring:message code="menu.newsletter" />
</a>
</li>
<li>
<a href="<c:url value="/content/about/frequently-asked-questions" />">
<a href="<c:url value="/1/content/about/frequently-asked-questions" />">
<spring:message code="menu.faq" />
</a>
</li>
......@@ -67,19 +67,19 @@
</ul>
</li>
<li>
<a href="<c:url value="/explore" />">
<a href="<c:url value="/1/explore" />">
<spring:message code="menu.browse" />
</a>
</li>
<%-- <li><a href="<c:url value="/data/" />"><spring:message code="menu.datasets" /></a></li> --%>
<%-- <li><a href="<c:url value="/descriptors/" />"><spring:message code="menu.descriptors" /></a></li> --%>
<%-- <li><a href="<c:url value="/1/data/" />"><spring:message code="menu.datasets" /></a></li> --%>
<%-- <li><a href="<c:url value="/1/descriptors/" />"><spring:message code="menu.descriptors" /></a></li> --%>
<li>
<a href="<c:url value="/geo/" />">
<a href="<c:url value="/1/geo/" />">
<spring:message code="menu.countries" />
</a>
</li>
<li>
<a href="<c:url value="/wiews/active" />">
<a href="<c:url value="/1/wiews/active" />">
<spring:message code="menu.institutes" />
</a>
</li>
......@@ -146,7 +146,7 @@
<a href="<c:url value="/google/login" />" class="btn btn-default google-signin">
<spring:message code="login.with-google-plus" />
</a>
<a href="<c:url value="/registration" />" class="btn btn-default">
<a href="<c:url value="/1/registration" />" class="btn btn-default">
<spring:message code="login.register-now" />
</a>
<!-- CSRF protection -->
......@@ -164,7 +164,7 @@
</a>
<ul class="dropdown-menu pull-left">
<li>
<a href="<c:url value="/profile/${user.username}" />">
<a href="<c:url value="/1/profile/${user.username}" />">
<spring:message code="user.pulldown.profile" />
</a>
</li>
......@@ -178,32 +178,32 @@
<security:authorize access="hasRole('ADMINISTRATOR')">
<li role="separator" class="divider"></li>
<li>
<a href="<c:url value="/admin/" />">
<a href="<c:url value="/1/admin/" />">
<spring:message code="user.pulldown.administration" />
</a>
</li>
<li>
<a href="<c:url value="/profile/list" />">
<a href="<c:url value="/1/profile/list" />">
<spring:message code="user.pulldown.users" />
</a>
</li>
<li>
<a href="<c:url value="/team" />">
<a href="<c:url value="/1/team" />">
<spring:message code="user.pulldown.teams" />
</a>
</li>
<li>
<a href="<c:url value="/admin/oauth-clients/" />">
<a href="<c:url value="/1/admin/oauth-clients/" />">
<spring:message code="user.pulldown.oauth-clients" />
</a>
</li>
<li>
<a href="<c:url value="/content" />">
<a href="<c:url value="/1/content" />">
<spring:message code="user.pulldown.manage-content" />
</a>
</li>
<li>
<a href="<c:url value="/project" />">
<a href="<c:url value="/1/project" />">
<spring:message code="project.page.list.title" />
</a>
</li>
......
......@@ -190,15 +190,29 @@
<filter-name>webApiFilter</filter-name>
<url-pattern>/webapi/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>sitemesh</filter-name>
<filter-class>org.sitemesh.config.ConfigurableSiteMeshFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>sitemesh</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>newGUIFilter</filter-name>
<filter-class>org.genesys2.server.servlet.filter.NewGUIFilter</filter-class>
<init-param>
<param-name>exclude-paths</param-name>
<param-value>/html /login-attempt</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>newGUIFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>sitemesh</filter-name>
<filter-class>org.sitemesh.config.ConfigurableSiteMeshFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>sitemesh</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>mvc</servlet-name>
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment