From 1a79ce47f9a81425b4a91d0f04df627c73ede3cb Mon Sep 17 00:00:00 2001 From: Kacper Magdziarz Date: Tue, 17 Dec 2024 09:12:54 +0100 Subject: [PATCH] [ACS-9045] Pre-commit --- .../repo/web/util/AbstractJettyComponent.java | 366 +++++++++--------- 1 file changed, 180 insertions(+), 186 deletions(-) diff --git a/remote-api/src/test/java/org/alfresco/repo/web/util/AbstractJettyComponent.java b/remote-api/src/test/java/org/alfresco/repo/web/util/AbstractJettyComponent.java index 38888064efc..6a2c83eb1f1 100644 --- a/remote-api/src/test/java/org/alfresco/repo/web/util/AbstractJettyComponent.java +++ b/remote-api/src/test/java/org/alfresco/repo/web/util/AbstractJettyComponent.java @@ -4,41 +4,30 @@ * %% * Copyright (C) 2005 - 2023 Alfresco Software Limited * %% - * This file is part of the Alfresco software. - * If the software was purchased under a paid Alfresco license, the terms of - * the paid license agreement will prevail. Otherwise, the software is + * This file is part of the Alfresco software. + * If the software was purchased under a paid Alfresco license, the terms of + * the paid license agreement will prevail. Otherwise, the software is * provided under the following open source license terms: - * + * * Alfresco is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. - * + * * Alfresco is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. - * + * * You should have received a copy of the GNU Lesser General Public License * along with Alfresco. If not, see . * #L% */ package org.alfresco.repo.web.util; -import java.io.BufferedReader; -import java.io.File; -import java.io.IOException; -import java.io.InputStreamReader; -import java.net.InetAddress; -import java.net.ServerSocket; -import java.net.Socket; -import java.util.Arrays; -import java.util.Date; - import jakarta.servlet.ServletContext; import jakarta.servlet.ServletContextEvent; import jakarta.servlet.ServletContextListener; - import org.alfresco.error.AlfrescoRuntimeException; import org.alfresco.util.BaseApplicationContextHelper; import org.alfresco.util.TempFileProvider; @@ -56,213 +45,218 @@ import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context.support.GenericWebApplicationContext; +import java.io.BufferedReader; +import java.io.File; +import java.io.IOException; +import java.io.InputStreamReader; +import java.net.InetAddress; +import java.net.ServerSocket; +import java.net.Socket; +import java.util.Arrays; +import java.util.Date; + /** * Manages an embedded jetty server, hooking it up to the repository spring context. - * - * @author steveglover * + * @author steveglover */ public abstract class AbstractJettyComponent implements JettyComponent { - protected static final Log logger = LogFactory.getLog(AbstractJettyComponent.class); - public static final int JETTY_STOP_PORT = 8079; public static final String JETTY_LOCAL_IP = "127.0.0.1"; - + protected static final Log logger = LogFactory.getLog(AbstractJettyComponent.class); + protected static Server server; protected int port = 8081; protected String contextPath = "/alfresco"; protected String publicApiServletName = "api"; protected String[] configLocations; protected String[] classLocations; - protected static Server server; - private WebAppContext webAppContext; - public AbstractJettyComponent(int port, String contextPath, String[] configLocations, String[] classLocations) - { - this.configLocations = configLocations; - this.classLocations = classLocations; - this.port = port; - this.contextPath = contextPath; - server = new Server(port); - } - - public int getPort() - { - return port; - } - - /* - * Creates a web application context wrapping a Spring application context (adapted from core spring code in - * org.springframework.web.context.ContextLoader) - */ - protected WebApplicationContext createWebApplicationContext(ServletContext sc, ApplicationContext parent) - { - GenericWebApplicationContext wac = (GenericWebApplicationContext) BeanUtils.instantiateClass(GenericWebApplicationContext.class); - - // Assign the best possible id value. - wac.setId(ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX + contextPath); - - wac.setParent(parent); - wac.setServletContext(sc); - wac.refresh(); - - return wac; - } - - public ConfigurableApplicationContext getApplicationContext() - { - return (ConfigurableApplicationContext)webAppContext.getContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE); - } - - protected abstract void configureWebAppContext(WebAppContext webAppContext); - - public void start() - { - if(logger.isDebugEnabled()) - { - logger.debug("["+new Date()+"] startJetty: starting embedded Jetty server ..."); - } - - try - { - if(logger.isDebugEnabled()) - { - logger.debug("["+new Date()+"] startJetty"); - } - - this.webAppContext = new WebAppContext(); - webAppContext.setContextPath(contextPath); - - configure(webAppContext); - - server.setHandler(webAppContext); - - // for clean shutdown, add monitor thread - - // from: http://ptrthomas.wordpress.com/2009/01/24/how-to-start-and-stop-jetty-revisited/ - // adapted from: http://jetty.codehaus.org/jetty/jetty-6/xref/org/mortbay/start/Monitor.html - Thread monitor = new MonitorThread(); - monitor.start(); - - configureWebAppContext(webAppContext); - - ignoreAmbiguousLinks(server); - - server.start(); - - if(logger.isDebugEnabled()) - { - logger.debug("["+new Date()+"] startJetty: ... embedded Jetty server started on port " + port); - } + public AbstractJettyComponent(int port, String contextPath, String[] configLocations, String[] classLocations) + { + this.configLocations = configLocations; + this.classLocations = classLocations; + this.port = port; + this.contextPath = contextPath; + server = new Server(port); + } + + public int getPort() + { + return port; + } + + /* + * Creates a web application context wrapping a Spring application context (adapted from core spring code in + * org.springframework.web.context.ContextLoader) + */ + protected WebApplicationContext createWebApplicationContext(ServletContext sc, ApplicationContext parent) + { + GenericWebApplicationContext wac = BeanUtils.instantiateClass(GenericWebApplicationContext.class); + + // Assign the best possible id value. + wac.setId(ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX + contextPath); + + wac.setParent(parent); + wac.setServletContext(sc); + wac.refresh(); + + return wac; + } + + public ConfigurableApplicationContext getApplicationContext() + { + return (ConfigurableApplicationContext) webAppContext.getContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE); + } + + protected abstract void configureWebAppContext(WebAppContext webAppContext); + + public void start() + { + if (logger.isDebugEnabled()) + { + logger.debug("[" + new Date() + "] startJetty: starting embedded Jetty server ..."); + } + + try + { + if (logger.isDebugEnabled()) + { + logger.debug("[" + new Date() + "] startJetty"); + } + + this.webAppContext = new WebAppContext(); + webAppContext.setContextPath(contextPath); + + configure(webAppContext); + + server.setHandler(webAppContext); + + // for clean shutdown, add monitor thread + + // from: http://ptrthomas.wordpress.com/2009/01/24/how-to-start-and-stop-jetty-revisited/ + // adapted from: http://jetty.codehaus.org/jetty/jetty-6/xref/org/mortbay/start/Monitor.html + Thread monitor = new MonitorThread(); + monitor.start(); + + configureWebAppContext(webAppContext); + + ignoreAmbiguousLinks(server); + + server.start(); + + if (logger.isDebugEnabled()) + { + logger.debug("[" + new Date() + "] startJetty: ... embedded Jetty server started on port " + port); + } } catch (Exception e) { - logger.error("["+new Date()+"] startJetty: ... failed to start embedded Jetty server on port " + port + ", " + e); + logger.error("[" + new Date() + "] startJetty: ... failed to start embedded Jetty server on port " + port + ", " + e); } - } - - protected void configure(WebAppContext webAppContext) - { + } + + protected void configure(WebAppContext webAppContext) + { try { - ClassLoader classLoader = BaseApplicationContextHelper.buildClassLoader(classLocations); + ClassLoader classLoader = BaseApplicationContextHelper.buildClassLoader(classLocations); webAppContext.setClassLoader(classLoader); } catch (IOException e) { throw new ExceptionInInitializerError(e); } - - webAppContext.addEventListener(new ServletContextListener() - { - public void contextInitialized(ServletContextEvent sce) - { - // create a Spring web application context, wrapping and providing access to - // the application context - try - { - ServletContext servletContext = sce.getServletContext(); - - // initialise web application context - WebApplicationContextLoader.getApplicationContext(servletContext, configLocations, classLocations); - - if(logger.isDebugEnabled()) - { - logger.debug("contextInitialized "+sce); - } - } - catch(Throwable t) - { - logger.error("Failed to start Jetty server: " + t); - throw new AlfrescoRuntimeException("Failed to start Jetty server", t); - } - } - - public void contextDestroyed(ServletContextEvent sce) - { - if(logger.isDebugEnabled()) - { - logger.debug("contextDestroyed "+sce); - } - } - }); - - // arbitrary temporary file location - File tmp = new File(TempFileProvider.getSystemTempDir(), String.valueOf(System.currentTimeMillis())); - webAppContext.setContextPath(tmp.getAbsolutePath()); - } - - /** - * In newer jetty versions there is a stricter check for links e.g. "//" is not allowed, which clashes - * with some of our tests, because even a NodeRef triggers it - "workspace://..." - * Since Jetty is only used in tests it's alright to block this behaviour. - * - * @param server - */ - private void ignoreAmbiguousLinks(Server server) { - Arrays.stream(server.getConnectors()) - .flatMap(c -> c.getConnectionFactories().stream()) - .filter(cf -> cf instanceof HttpConnectionFactory) - .map(cf -> (HttpConnectionFactory) cf) - .forEach(hcf -> hcf.getHttpConfiguration().setUriCompliance(UriCompliance.RFC3986)); - } - - public void shutdown() - { - try - { - server.stop(); - } - catch(Exception e) - { - throw new AlfrescoRuntimeException("", e); - } - } - - private static class MonitorThread extends Thread + + webAppContext.addEventListener(new ServletContextListener() + { + public void contextInitialized(ServletContextEvent sce) + { + // create a Spring web application context, wrapping and providing access to + // the application context + try + { + ServletContext servletContext = sce.getServletContext(); + + // initialise web application context + WebApplicationContextLoader.getApplicationContext(servletContext, configLocations, classLocations); + + if (logger.isDebugEnabled()) + { + logger.debug("contextInitialized " + sce); + } + } + catch (Throwable t) + { + logger.error("Failed to start Jetty server: " + t); + throw new AlfrescoRuntimeException("Failed to start Jetty server", t); + } + } + + public void contextDestroyed(ServletContextEvent sce) + { + if (logger.isDebugEnabled()) + { + logger.debug("contextDestroyed " + sce); + } + } + }); + + // arbitrary temporary file location + File tmp = new File(TempFileProvider.getSystemTempDir(), String.valueOf(System.currentTimeMillis())); + webAppContext.setContextPath(tmp.getAbsolutePath()); + } + + /** + * In newer jetty versions there is a stricter check for links e.g. "//" is not allowed, which clashes with some of our tests, because even a NodeRef triggers it - "workspace://..." Since Jetty is only used in tests it's alright to block this behaviour. + * + * @param server + */ + private void ignoreAmbiguousLinks(Server server) + { + Arrays.stream(server.getConnectors()) + .flatMap(c -> c.getConnectionFactories().stream()) + .filter(cf -> cf instanceof HttpConnectionFactory) + .map(cf -> (HttpConnectionFactory) cf) + .forEach(hcf -> hcf.getHttpConfiguration().setUriCompliance(UriCompliance.RFC3986)); + } + + public void shutdown() + { + try + { + server.stop(); + } + catch (Exception e) + { + throw new AlfrescoRuntimeException("", e); + } + } + + private static class MonitorThread extends Thread { - private ServerSocket socket; - - public MonitorThread() + private final ServerSocket socket; + + public MonitorThread() { setDaemon(true); setName("StopMonitor"); - try + try { socket = new ServerSocket(JETTY_STOP_PORT, 1, InetAddress.getByName(JETTY_LOCAL_IP)); - } - catch(Exception e) + } + catch (Exception e) { throw new RuntimeException(e); } } - + @Override - public void run() + public void run() { Socket accept; - try + try { accept = socket.accept(); BufferedReader reader = new BufferedReader(new InputStreamReader(accept.getInputStream())); @@ -270,8 +264,8 @@ public void run() server.stop(); accept.close(); socket.close(); - } - catch(Exception e) + } + catch (Exception e) { throw new RuntimeException(e); }