Skip to content

Commit

Permalink
feat: Update to Wiremock 3.5.2
Browse files Browse the repository at this point in the history
resolve jetty 12 madness
  • Loading branch information
holomekc committed Apr 1, 2024
1 parent 781fcff commit c03781e
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 4 deletions.
5 changes: 1 addition & 4 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ dependencies {
api "org.eclipse.jetty:jetty-alpn-java-client"
api "org.eclipse.jetty:jetty-alpn-client"

api "org.eclipse.jetty.websocket:websocket-jakarta-server"
api "org.eclipse.jetty.websocket:websocket-jakarta-server:$versions.jetty"

api "io.jsonwebtoken:jjwt-api:0.11.5"
api "io.jsonwebtoken:jjwt-impl:0.11.5"
Expand Down Expand Up @@ -219,9 +219,6 @@ allprojects {
version = '1.0.0-Snapshot'
}

sourceCompatibility = 11
targetCompatibility = 11

compileJava {
options.encoding = 'UTF-8'

Expand Down
3 changes: 3 additions & 0 deletions wiremock-jetty12/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ dependencies {
api "org.eclipse.jetty.http2:jetty-http2-server:$versions.jetty"
api "org.eclipse.jetty:jetty-alpn-java-server:$versions.jetty"

api "org.eclipse.jetty:jetty-rewrite:$versions.jetty"
api "org.eclipse.jetty.ee10.websocket:jetty-ee10-websocket-jakarta-server:$versions.jetty"

testImplementation "org.eclipse.jetty.http2:jetty-http2-client:$versions.jetty"
testImplementation "org.eclipse.jetty.http2:jetty-http2-client-transport:$versions.jetty"
testImplementation "org.eclipse.jetty:jetty-alpn-java-client:$versions.jetty"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import com.github.tomakehurst.wiremock.jetty.JettyFaultInjectorFactory;
import com.github.tomakehurst.wiremock.jetty.JettyHttpServer;
import com.github.tomakehurst.wiremock.jetty.JettyHttpUtils;
import com.github.tomakehurst.wiremock.jetty.websockets.WebSocketEndpoint;
import com.github.tomakehurst.wiremock.jetty11.Jetty11Utils;
import com.github.tomakehurst.wiremock.jetty11.SslContexts;
import com.github.tomakehurst.wiremock.servlet.ContentTypeSettingFilter;
Expand All @@ -53,11 +54,14 @@
import org.eclipse.jetty.ee10.servlet.ServletContextRequest;
import org.eclipse.jetty.ee10.servlet.ServletHolder;
import org.eclipse.jetty.ee10.servlets.CrossOriginFilter;
import org.eclipse.jetty.ee10.websocket.jakarta.server.config.JakartaWebSocketServletContainerInitializer;
import org.eclipse.jetty.http.HttpVersion;
import org.eclipse.jetty.http.MimeTypes;
import org.eclipse.jetty.http2.server.HTTP2CServerConnectionFactory;
import org.eclipse.jetty.http2.server.HTTP2ServerConnectionFactory;
import org.eclipse.jetty.io.NetworkTrafficListener;
import org.eclipse.jetty.rewrite.handler.RewriteHandler;
import org.eclipse.jetty.rewrite.handler.RewriteRegexRule;
import org.eclipse.jetty.server.*;
import org.eclipse.jetty.server.handler.gzip.GzipHandler;
import org.eclipse.jetty.util.Callback;
Expand Down Expand Up @@ -211,6 +215,12 @@ protected Handler createHandler(
notifier);

final List<Handler> handlers = new ArrayList<>();
// wiremock-gui
// We prepend the rewrite handle for the wiremock gui web app to make sure that we route the
// requests properly
final RewriteHandler rewriteHandler = webAppRewriteContext(adminContext);
handlers.add(rewriteHandler);

Handler.Abstract asyncTimeoutSettingHandler =
new Handler.Abstract() {
@Override
Expand Down Expand Up @@ -247,6 +257,29 @@ protected void decorateAdminServiceContextBeforeConfig(
protected void decorateAdminServiceContextAfterConfig(
ServletContextHandler adminServiceContext) {}

// wiremock-gui
/**
* Rewrite web app. We use Angular. We must rewrite every /__admin/webapp path to the index.html
* of our single page application.
*
* @param adminContextHandler admin context handler is used to provide the static file content
* @return the rewrite handler
*/
private RewriteHandler webAppRewriteContext(ServletContextHandler adminContextHandler) {
final RewriteHandler rewrite = new RewriteHandler();
// rewrite.setRewriteRequestURI(true);
// rewrite.setRewritePathInfo(true);

RewriteRegexRule rewriteRule = new RewriteRegexRule();
rewriteRule.setRegex("/__admin/webapp/(mappings|matched|unmatched|state|files).*");
rewriteRule.setReplacement("/__admin/webapp/index.html");
rewrite.addRule(rewriteRule);

rewrite.setHandler(adminContextHandler);

return rewrite;
}

private void addCorsFilter(ServletContextHandler context) {
context.addFilter(buildCorsFilter(), "/*", EnumSet.of(DispatcherType.REQUEST));
}
Expand Down Expand Up @@ -301,6 +334,17 @@ private ServletContextHandler addAdminContext(

adminContext.addServlet(NotMatchedServlet.class, "/not-matched");

// wiremock-gui Include into admin context
ServletHolder webapp = adminContext.addServlet(DefaultServlet.class, "/webapp/*");
webapp.setAsyncSupported(false);

// wiremock-gui Include websocket into admin context
JakartaWebSocketServletContainerInitializer.configure(
adminContext,
(servletContext, serverContainer) -> {
serverContainer.addEndpoint(WebSocketEndpoint.class);
});

addCorsFilter(adminContext);

decorateAdminServiceContextAfterConfig(adminContext);
Expand Down

0 comments on commit c03781e

Please sign in to comment.