Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #12279 ensure handlers added correctly for maven plugin #12282

Merged
merged 2 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
import java.util.List;
import java.util.Map;

import org.eclipse.jetty.maven.MavenServerConnector;
import org.eclipse.jetty.maven.PluginLog;
import org.eclipse.jetty.security.LoginService;
import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.Handler;
Expand Down Expand Up @@ -65,7 +63,15 @@ public static void configureHandlers(Server server, List<ContextHandler> context
if (contexts == null)
{
contexts = new ContextHandlerCollection();
server.setHandler(contexts);
if (server.getHandler() != null)
{
Handler.Sequence handlers = new Handler.Sequence();
handlers.addHandler(server.getHandler());
handlers.addHandler(contexts);
server.setHandler(handlers);
}
else
server.setHandler(contexts);
}

if (contextHandlers != null)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
//
// ========================================================================
// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//

package org.eclipse.jetty.maven;

import java.util.Arrays;
import java.util.List;
import java.util.Set;

import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.Response;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.ContextHandler;
import org.eclipse.jetty.server.handler.ContextHandlerCollection;
import org.eclipse.jetty.util.Callback;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class ServerSupportTest
{
@Test
public void testNoServerHandlers() throws Exception
{
//Test that a server will always create a ContextHandlerCollection and DefaultHandler
Server server = new Server();
assertNull(server.getHandler());
ServerSupport.configureHandlers(server, null, null);
assertNotNull(server.getDefaultHandler());
assertNotNull(server.getHandler());
}

@Test
public void testExistingServerHandler() throws Exception
{
//Test that if a Server already has a handler, we replace it with a
//sequence containing the original handler plus a ContextHandlerCollection
Server server = new Server();
Handler.Abstract testHandler = new Handler.Abstract()
{
@Override
public boolean handle(Request request, Response response, Callback callback) throws Exception
{
return false;
}
};

server.setHandler(testHandler);
ServerSupport.configureHandlers(server, null, null);
assertNotNull(server.getDefaultHandler());
assertInstanceOf(Handler.Sequence.class, server.getHandler());
Handler.Sequence handlers = (Handler.Sequence)server.getHandler();
assertTrue(handlers.contains(testHandler));
assertNotNull(handlers.getDescendant(ContextHandlerCollection.class));
}

@Test
public void testExistingServerHandlerWithContextHandlers() throws Exception
{
//Test that if a Server already has a handler, we replace it with
//a sequence containing the original handler plus a ContextHandlerCollection
//into which we add any supplied ContextHandlers
Server server = new Server();
Handler.Abstract testHandlerA = new Handler.Abstract()
{
@Override
public boolean handle(Request request, Response response, Callback callback) throws Exception
{
return false;
}
};

ContextHandler contextHandlerA = new ContextHandler();
contextHandlerA.setContextPath("/A");
ContextHandler contextHandlerB = new ContextHandler();
contextHandlerB.setContextPath("/B");
List<ContextHandler> contextHandlerList = Arrays.asList(contextHandlerA, contextHandlerB);

server.setHandler(testHandlerA);
ServerSupport.configureHandlers(server, contextHandlerList, null);
assertNotNull(server.getDefaultHandler());
assertInstanceOf(Handler.Sequence.class, server.getHandler());
Handler.Sequence handlers = (Handler.Sequence)server.getHandler();
List<Handler> handlerList = handlers.getHandlers();
assertEquals(testHandlerA, handlerList.get(0));
Handler second = handlerList.get(1);
assertInstanceOf(ContextHandlerCollection.class, second);
ContextHandlerCollection contextHandlers = (ContextHandlerCollection)second;
Set<String> contextPaths = contextHandlers.getContextPaths();
assertNotNull(contextPaths);
assertTrue(contextPaths.contains("/A"));
assertTrue(contextPaths.contains("/B"));
}
}
Loading