Repository: tipsy/javalin
Pull request #1299 (2021-07-01 14:54:55)

[micrometer] Fix context path


Patch:
diff --git a/README.md b/README.md
index 9939d571..e329ae7a 100644
--- a/README.md
+++ b/README.md
@@ -40,7 +40,7 @@ General information:
 #### Gradle
 
 ```groovy
-compile "io.javalin:javalin:3.13.8"
+implementation "io.javalin:javalin:3.13.8"
 ```
 
 ### Start programming (Java)
diff --git a/javalin-bundle/pom.xml b/javalin-bundle/pom.xml
index 54d327f3..74eb1a99 100644
--- a/javalin-bundle/pom.xml
+++ b/javalin-bundle/pom.xml
@@ -45,4 +45,30 @@
             <artifactId>logback-classic</artifactId>
         </dependency>
     </dependencies>
+
+    <profiles>
+        <profile>
+            <id>javadoc</id>
+            <build>
+                <plugins>
+                    <plugin>
+                        <artifactId>maven-jar-plugin</artifactId>
+                        <executions>
+                            <execution>
+                                <id>empty-javadoc-jar</id>
+                                <phase>package</phase>
+                                <goals>
+                                    <goal>jar</goal>
+                                </goals>
+                                <configuration>
+                                    <classifier>javadoc</classifier>
+                                    <classesDirectory>${project.basedir}/javadoc</classesDirectory>
+                                </configuration>
+                            </execution>
+                        </executions>
+                    </plugin>
+                </plugins>
+            </build>
+        </profile>
+    </profiles>
 </project>
diff --git a/javalin-graphql/pom.xml b/javalin-graphql/pom.xml
index 2c93e790..0b7150fb 100644
--- a/javalin-graphql/pom.xml
+++ b/javalin-graphql/pom.xml
@@ -9,6 +9,10 @@
 
     <artifactId>javalin-graphql</artifactId>
 
+    <properties>
+        <dokka.skip>false</dokka.skip>
+    </properties>
+
     <dependencies>
         <!-- BEGIN Project dependencies -->
         <dependency>
diff --git a/javalin-openapi/pom.xml b/javalin-openapi/pom.xml
index 9a473dce..12ae4223 100644
--- a/javalin-openapi/pom.xml
+++ b/javalin-openapi/pom.xml
@@ -9,6 +9,10 @@
 
     <artifactId>javalin-openapi</artifactId>
 
+    <properties>
+        <dokka.skip>false</dokka.skip>
+    </properties>
+
     <dependencies>
         <!-- BEGIN Project dependencies -->
         <dependency>
diff --git a/javalin-without-jetty/pom.xml b/javalin-without-jetty/pom.xml
index 5250e468..7fd60e6b 100644
--- a/javalin-without-jetty/pom.xml
+++ b/javalin-without-jetty/pom.xml
@@ -18,6 +18,8 @@
     </dependencies>
 
     <properties>
+        <javadoc.skip>false</javadoc.skip>
+
         <original.root>../javalin/src/main/java/io/javalin</original.root>
         <hackity.hack.root>../javalin-without-jetty/target/generated-sources/src/main/java/io/javalin</hackity.hack.root>
         <javalin.original.path>${original.root}/Javalin.java</javalin.original.path>
diff --git a/javalin/pom.xml b/javalin/pom.xml
index 3ff58da1..85b38e8c 100644
--- a/javalin/pom.xml
+++ b/javalin/pom.xml
@@ -9,6 +9,10 @@
 
     <artifactId>javalin</artifactId>
 
+    <properties>
+        <dokka.skip>false</dokka.skip>
+    </properties>
+
     <dependencies>
         <!-- Logging -->
         <dependency>
diff --git a/javalin/src/main/java/io/javalin/core/JavalinServer.kt b/javalin/src/main/java/io/javalin/core/JavalinServer.kt
index 3bcfb3fd..de1ef7d8 100644
--- a/javalin/src/main/java/io/javalin/core/JavalinServer.kt
+++ b/javalin/src/main/java/io/javalin/core/JavalinServer.kt
@@ -7,6 +7,7 @@
 package io.javalin.core
 
 import io.javalin.core.util.JavalinLogger
+import io.javalin.core.util.Util
 import io.javalin.websocket.JavalinWsServlet
 import org.eclipse.jetty.server.Handler
 import org.eclipse.jetty.server.HttpConnectionFactory
@@ -43,7 +44,7 @@ class JavalinServer(val config: JavalinConfig) {
         config.inner.sessionHandler = config.inner.sessionHandler ?: defaultSessionHandler()
         val nullParent = null // javalin handlers are orphans
 
-        val wsAndHttpHandler = object : ServletContextHandler(nullParent, config.contextPath, SESSIONS) {
+        val wsAndHttpHandler = object : ServletContextHandler(nullParent, Util.normalizeContextPath(config.contextPath), SESSIONS) {
             override fun doHandle(target: String, jettyRequest: Request, request: HttpServletRequest, response: HttpServletResponse) {
                 request.setAttribute("jetty-target", target) // used in JettyResourceHandler
                 request.setAttribute("jetty-request", jettyRequest)
diff --git a/javalin/src/main/java/io/javalin/plugin/metrics/MicrometerPlugin.kt b/javalin/src/main/java/io/javalin/plugin/metrics/MicrometerPlugin.kt
index d56e1222..75dcedaf 100644
--- a/javalin/src/main/java/io/javalin/plugin/metrics/MicrometerPlugin.kt
+++ b/javalin/src/main/java/io/javalin/plugin/metrics/MicrometerPlugin.kt
@@ -10,7 +10,6 @@ import io.javalin.Javalin
 import io.javalin.core.plugin.Plugin
 import io.javalin.http.Context
 import io.javalin.http.ExceptionHandler
-import io.javalin.http.HandlerEntry
 import io.javalin.http.HandlerType
 import io.micrometer.core.instrument.MeterRegistry
 import io.micrometer.core.instrument.Metrics
@@ -21,6 +20,7 @@ import io.micrometer.core.instrument.binder.jetty.JettyConnectionMetrics
 import io.micrometer.core.instrument.binder.jetty.JettyServerThreadPoolMetrics
 import io.micrometer.core.instrument.binder.jetty.TimedHandler
 import org.apache.commons.lang3.StringUtils
+import org.jetbrains.kotlin.util.prefixIfNot
 import javax.servlet.http.HttpServletRequest
 import javax.servlet.http.HttpServletResponse
 
@@ -44,22 +44,15 @@ class MicrometerPlugin @JvmOverloads constructor(
                     } else {
                         "Unknown"
                     }
-
+                    val pathInfo = request.pathInfo.removePrefix(app._conf.contextPath).prefixIfNot("/")
                     response.setHeader(EXCEPTION_HEADER, null)
-                    val uri = app.servlet()
-                        .matcher
-                        .findEntries(HandlerType.valueOf(request.method), request.pathInfo)
-                        .stream()
-                        .findAny()
-                        .map(HandlerEntry::path)
-                        .map { path: String -> if (path == "/" || StringUtils.isBlank(path)) "root" else path }
-                        .map { path: String ->
-                            if (!tagRedirectPaths && response.status in 300..399) "REDIRECTION" else path
-                        }
-                        .map { path: String ->
-                            if (!tagNotFoundMappedPaths && response.status == 404) "NOT_FOUND" else path
-                        }
-                        .orElse("NOT_FOUND")
+                    val handlerType = HandlerType.valueOf(request.method)
+                    val uri = app.servlet().matcher.findEntries(handlerType, pathInfo).asSequence()
+                        .map { it.path }
+                        .map { if (it == "/" || it.isBlank()) "root" else it }
+                        .map { if (!tagRedirectPaths && response.status in 300..399) "REDIRECTION" else it }
+                        .map { if (!tagNotFoundMappedPaths && response.status == 404) "NOT_FOUND" else it }
+                        .firstOrNull() ?: "NOT_FOUND"
                     return Tags.concat(
                         super.getTags(request, response),
                         "uri", uri,
diff --git a/pom.xml b/pom.xml
index f9f1e608..239ca451 100644
--- a/pom.xml
+++ b/pom.xml
@@ -50,6 +50,10 @@
     <properties>
         <jdk.version>1.8</jdk.version>
 
+        <dokka.jdkVersion>8</dokka.jdkVersion>
+        <dokka.skip>true</dokka.skip>
+        <javadoc.skip>true</javadoc.skip>
+
         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
         <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
         <kotlin.version>1.5.0</kotlin.version>
@@ -64,9 +68,12 @@
         <surefire.plugin.version>3.0.0-M5</surefire.plugin.version>
         <antrun.plugin.version>3.0.0</antrun.plugin.version>
         <shade.plugin.version>3.2.4</shade.plugin.version>
-        <javadoc.plugin.version>2.7</javadoc.plugin.version>
+        <javadoc.plugin.version>3.3.0</javadoc.plugin.version>
         <build-helper.plugin.version>3.2.0</build-helper.plugin.version>
         <enforcer.plugin.version>3.0.0-M3</enforcer.plugin.version>
+        <clean.plugin.version>3.1.0</clean.plugin.version>
+        <install.plugin.version>3.0.0-M1</install.plugin.version>
+        <resources.plugin.version>3.2.0</resources.plugin.version>
 
         <!-- DEPENDENCIES -->
         <classgraph.version>4.8.102</classgraph.version>
@@ -114,12 +121,33 @@
         <dependency>
             <groupId>org.jetbrains.kotlin</groupId>
             <artifactId>kotlin-stdlib-jdk8</artifactId>
-            <version>${kotlin.version}</version>
         </dependency>
     </dependencies>
 
     <dependencyManagement>
         <dependencies>
+            <!-- Kotlin -->
+            <dependency>
+                <groupId>org.jetbrains.kotlin</groupId>
+                <artifactId>kotlin-stdlib-jdk8</artifactId>
+                <version>${kotlin.version}</version>
+            </dependency>
+            <dependency>
+                <groupId>org.jetbrains.kotlin</groupId>
+                <artifactId>kotlin-reflect</artifactId>
+                <version>${kotlin.version}</version>
+            </dependency>
+            <dependency>
+                <groupId>org.jetbrains.kotlin</groupId>
+                <artifactId>kotlin-stdlib-common</artifactId>
+                <version>${kotlin.version}</version>
+            </dependency>
+            <dependency>
+                <groupId>org.jetbrains.kotlin</groupId>
+                <artifactId>kotlin-stdlib</artifactId>
+                <version>${kotlin.version}</version>
+            </dependency>
+
             <dependency>
                 <groupId>com.google.guava</groupId>
                 <artifactId>guava</artifactId>
@@ -488,6 +516,18 @@
 
         <pluginManagement>
             <plugins>
+                <plugin>
+                    <artifactId>maven-clean-plugin</artifactId>
+                    <version>${clean.plugin.version}</version>
+                </plugin>
+                <plugin>
+                    <artifactId>maven-install-plugin</artifactId>
+                    <version>${install.plugin.version}</version>
+                </plugin>
+                <plugin>
+                    <artifactId>maven-resources-plugin</artifactId>
+                    <version>${resources.plugin.version}</version>
+                </plugin>
                 <plugin>
                     <artifactId>maven-compiler-plugin</artifactId>
                     <version>${compiler.plugin.version}</version>
@@ -496,6 +536,13 @@
                     <groupId>org.jetbrains.dokka</groupId>
                     <artifactId>dokka-maven-plugin</artifactId>
                     <version>${dokka.plugin.version}</version>
+                    <configuration>
+                        <skip>${dokka.skip}</skip>
+                        <jdkVersion>${dokka.jdkVersion}</jdkVersion>
+                        <sourceDirectories>
+                            <dir>${project.basedir}/src/main/java</dir>
+                        </sourceDirectories>
+                    </configuration>
                 </plugin>
                 <plugin>
                     <artifactId>maven-surefire-plugin</artifactId>
@@ -516,6 +563,9 @@
                 <plugin>
                     <artifactId>maven-javadoc-plugin</artifactId>
                     <version>${javadoc.plugin.version}</version>
+                    <configuration>
+                        <skip>${javadoc.skip}</skip>
+                    </configuration>
                 </plugin>
                 <plugin>
                     <groupId>org.codehaus.mojo</groupId>
@@ -536,6 +586,38 @@
     </build>
 
     <profiles>
+        <profile>
+            <id>javadoc</id>
+            <build>
+                <plugins>
+                    <plugin>
+                        <groupId>org.jetbrains.dokka</groupId>
+                        <artifactId>dokka-maven-plugin</artifactId>
+                        <executions>
+                            <execution>
+                                <phase>package</phase>
+                                <id>attach-javadocs-dokka</id>
+                                <goals>
+                                    <goal>javadocJar</goal>
+                                </goals>
+                            </execution>
+                        </executions>
+                    </plugin>
+                    <plugin>
+                        <artifactId>maven-javadoc-plugin</artifactId>
+                        <executions>
+                            <execution>
+                                <phase>package</phase>
+                                <id>attach-javadocs-javadoc</id>
+                                <goals>
+                                    <goal>jar</goal>
+                                </goals>
+                            </execution>
+                        </executions>
+                    </plugin>
+                </plugins>
+            </build>
+        </profile>
         <profile>
             <id>sign-artifacts</id>
             <activation>


Test patch:
diff --git a/javalin-testtools/pom.xml b/javalin-testtools/pom.xml
index 4221f03c..2141da17 100644
--- a/javalin-testtools/pom.xml
+++ b/javalin-testtools/pom.xml
@@ -9,6 +9,10 @@
 
     <artifactId>javalin-testtools</artifactId>
 
+    <properties>
+        <dokka.skip>false</dokka.skip>
+    </properties>
+
     <dependencies>
         <dependency>
             <groupId>io.javalin</groupId>
diff --git a/javalin/src/test/java/io/javalin/TestMicrometerPlugin.kt b/javalin/src/test/java/io/javalin/TestMicrometerPlugin.kt
index 628f1623..a07aa9ac 100644
--- a/javalin/src/test/java/io/javalin/TestMicrometerPlugin.kt
+++ b/javalin/src/test/java/io/javalin/TestMicrometerPlugin.kt
@@ -35,6 +35,22 @@ class TestMicrometerPlugin {
         assertThat(timerCount).isEqualTo(requestCount.toLong())
     }
 
+    @Test
+    fun `successful request with context path`() = TestUtil.test(setupApp(contextPath = "/api")) { app, http ->
+        val requestCount = (2..9).random()
+        app.get("/hello") { it.status(200) }
+        repeat(requestCount) { http.get("/api/hello") }
+        val timerCount = meterRegistry.get("jetty.server.requests")
+            .tag("uri", "/hello")
+            .tag("method", "GET")
+            .tag("exception", "None")
+            .tag("status", "200")
+            .tag("outcome", "SUCCESS")
+            .timer()
+            .count()
+        assertThat(timerCount).isEqualTo(requestCount.toLong())
+    }
+
     @Test
     fun `request throwing exception`() = TestUtil.test(setupApp()) { app, http ->
         val requestCount = (2..9).random()
@@ -189,8 +205,10 @@ class TestMicrometerPlugin {
     private fun setupApp(
         tagRedirectPaths: Boolean = false,
         tagNotFoundMappedPaths: Boolean = false,
-        autoGenerateEtags: Boolean? = null
+        autoGenerateEtags: Boolean? = null,
+        contextPath: String = "/"
     ) = Javalin.create { config ->
+        config.contextPath = contextPath
         config.registerPlugin(
             MicrometerPlugin(
                 registry = meterRegistry,