Commit 9b6501d2 authored by julienmege's avatar julienmege

fix(Preview) Add proxy servlet to redirect to portal

- system property bonita.portal.origin should be set by the studio
- setting 8081 as default value in server.sh

Covers [BS-18138](https://bonitasoft.atlassian.net/browse/BS-18138)
parent 7622acaa
......@@ -126,6 +126,11 @@
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</dependency>
<dependency>
<groupId>org.mitre.dsmiley.httpproxy</groupId>
<artifactId>smiley-http-proxy-servlet</artifactId>
<version>1.10</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-artifact</artifactId>
......
......@@ -22,12 +22,13 @@ import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;
import com.google.common.base.Strings;
import org.apache.commons.lang3.StringUtils;
import org.mitre.dsmiley.httpproxy.ProxyServlet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.context.support.HttpRequestHandlerServlet;
import org.springframework.web.servlet.DispatcherServlet;
/**
......@@ -36,6 +37,12 @@ import org.springframework.web.servlet.DispatcherServlet;
*/
public class SpringWebApplicationInitializer implements WebApplicationInitializer {
/**
* System property set by the studio to target bonita portal
*/
public static final String BONITA_PORTAL_ORIGIN = "bonita.portal.origin";
private static final String[] BANNER = { "",
"d8888b. .d88b. d8b db d888888b d888888b .d8b. .d8888. .d88b. d88888b d888888b",
"88 `8D .8P Y8. 888o 88 `88' `~~88~~' d8' `8b 88' YP .8P Y8. 88' `~~88~~'",
......@@ -77,10 +84,29 @@ public class SpringWebApplicationInitializer implements WebApplicationInitialize
servletContext.addListener(new ContextLoaderListener(rootContext));
// Register and map the dispatcher servlet
ServletRegistration.Dynamic proxyRegistration = servletContext.addServlet("proxyAPI", ProxyServlet.class);
proxyRegistration.setLoadOnStartup(1);
proxyRegistration.setInitParameter("targetUri", getPortalOrigin() + "/bonita");
proxyRegistration.setInitParameter(ProxyServlet.P_LOG, "true");
proxyRegistration.setInitParameter(ProxyServlet.P_PRESERVECOOKIES, "true");
proxyRegistration.setInitParameter(ProxyServlet.P_PRESERVEHOST, "true");
proxyRegistration.addMapping("/bonita/*");
ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcher", new DispatcherServlet(rootContext));
dispatcher.setLoadOnStartup(1);
dispatcher.setMultipartConfig(new MultipartConfigElement(System.getProperty("java.io.tmpdir")));
dispatcher.setAsyncSupported(true);
dispatcher.addMapping("/");
}
private String getPortalOrigin() {
String portalOrigin = System.getProperty(BONITA_PORTAL_ORIGIN);
if(StringUtils.isNotBlank(portalOrigin)){
return portalOrigin;
}
logger.warn("System property " + BONITA_PORTAL_ORIGIN + " is not set. Same origin as UI Designer will be used for portal calls.");
return "";
}
}
......@@ -27,6 +27,7 @@ import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringUtils;
import org.bonitasoft.web.designer.controller.preview.Previewer;
import org.bonitasoft.web.designer.controller.utils.HttpFile;
import org.bonitasoft.web.designer.model.asset.AssetType;
......@@ -74,7 +75,8 @@ public class PreviewController {
try {
String queryString = isEmpty(request.getQueryString()) ? "" : "?" + request.getQueryString();
response.setStatus(HttpServletResponse.SC_TEMPORARY_REDIRECT);
response.addHeader("Location", "/bonita/API/" + RequestMappingUtils.extractPathWithinPattern(request) + queryString);
response.addHeader("Location", request.getContextPath() + "/bonita/API/" + RequestMappingUtils.extractPathWithinPattern(request) + queryString);
response.flushBuffer();
} catch (IOException e) {
String message = "Error while redirecting API call";
......@@ -126,5 +128,4 @@ public class PreviewController {
// fail silently
}
}
}
......@@ -45,8 +45,6 @@ module.exports = function (gulp, config) {
if (req.url === '/' || matchStaticFile(req) && !matchStaticProxyfied(req)) {
next();
} else if (/^\/bonita\/.*/.test(req.url)) {
proxy.web(req, res, { target: 'http://127.0.0.1:8081' });
} else {
proxy.web(req, res, { target: 'http://127.0.0.1:8080' });
}
......
#!/bin/sh
export PORTAL_ORIGIN=http://localhost:8081
export JAVA_TOOL_OPTIONS="$JAVA_TOOL_OPTIONS -Dbonita.portal.origin=$PORTAL_ORIGIN"
# CURL is needed
url=http://localhost:8080/rest/pages
......@@ -11,6 +14,7 @@ waitJetty() {
echo "Jetty started"
}
echo Using $bonita.portal.origin as portal origin. You can edit it in community/server.sh
(cd backend/webapp/ && yarn start &)
waitJetty
(cd frontend/ && yarn start)
......
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