Skip to content

Commit 4ab858a

Browse files
committed
Merge pull request #29499 from p7nov
* gh-29499: Polish 'Add Kotlin alternatives to Java documentation samples' Add Kotlin alternatives to Java documentation samples Closes gh-29499
2 parents 0e906dc + d212243 commit 4ab858a

File tree

323 files changed

+10164
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

323 files changed

+10164
-0
lines changed

spring-boot-project/spring-boot-docs/build.gradle

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ plugins {
44
id "org.asciidoctor.jvm.convert"
55
id "org.springframework.boot.conventions"
66
id "org.springframework.boot.deployed"
7+
id 'org.jetbrains.kotlin.jvm'
78
}
89

910
description = "Spring Boot Docs"
@@ -102,6 +103,7 @@ dependencies {
102103
exclude group: "javax.xml.bind", module: "jaxb-api"
103104
exclude group: "org.jboss.spec.javax.transaction", module: "jboss-transaction-api_1.2_spec"
104105
}
106+
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
105107
implementation("org.jooq:jooq") {
106108
exclude group: "javax.xml.bind", module: "jaxb-api"
107109
}
@@ -321,6 +323,9 @@ syncDocumentationSourceForAsciidoctor {
321323
from("src/test/java") {
322324
into "test/java"
323325
}
326+
from("src/main/kotlin") {
327+
into "main/kotlin"
328+
}
324329
from("src/main/groovy") {
325330
into "main/groovy"
326331
}
@@ -345,6 +350,9 @@ syncDocumentationSourceForAsciidoctorMultipage {
345350
from("src/test/java") {
346351
into "test/java"
347352
}
353+
from("src/main/kotlin") {
354+
into "main/kotlin"
355+
}
348356
from("src/main/groovy") {
349357
into "main/groovy"
350358
}
@@ -369,6 +377,9 @@ syncDocumentationSourceForAsciidoctorPdf {
369377
from("src/test/java") {
370378
into "test/java"
371379
}
380+
from("src/main/kotlin") {
381+
into "main/kotlin"
382+
}
372383
from("src/main/groovy") {
373384
into "main/groovy"
374385
}

spring-boot-project/spring-boot-docs/src/docs/asciidoc/attributes.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
:github-issues: https://github.com/{github-repo}/issues/
2020
:github-wiki: https://github.com/{github-repo}/wiki
2121
:docs-java: {docdir}/../main/java/org/springframework/boot/docs
22+
:docs-kotlin: {docdir}/../main/kotlin/org/springframework/boot/docs
2223
:docs-groovy: {docdir}/../main/groovy/org/springframework/boot/docs
2324
:docs-resources: ../../main/resources
2425
:spring-boot-code: https://github.com/{github-repo}/tree/{github-tag}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Copyright 2012-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.docs.actuator.cloudfoundry.customcontextpath
18+
19+
import org.apache.catalina.Host
20+
import org.apache.catalina.core.StandardContext
21+
import org.apache.catalina.startup.Tomcat.FixContextListener
22+
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory
23+
import org.springframework.boot.web.servlet.ServletContextInitializer
24+
import org.springframework.context.annotation.Bean
25+
import org.springframework.context.annotation.Configuration
26+
import java.io.IOException
27+
import java.util.Collections.emptySet
28+
import javax.servlet.GenericServlet
29+
import javax.servlet.Servlet
30+
import javax.servlet.ServletContainerInitializer
31+
import javax.servlet.ServletContext
32+
import javax.servlet.ServletException
33+
import javax.servlet.ServletRequest
34+
import javax.servlet.ServletResponse
35+
import kotlin.jvm.Throws
36+
37+
@Suppress("UNUSED_ANONYMOUS_PARAMETER")
38+
@Configuration(proxyBeanMethods = false)
39+
class MyCloudFoundryConfiguration {
40+
41+
@Bean
42+
fun servletWebServerFactory(): TomcatServletWebServerFactory {
43+
return object : TomcatServletWebServerFactory() {
44+
45+
override fun prepareContext(host: Host, initializers: Array<ServletContextInitializer>) {
46+
super.prepareContext(host, initializers)
47+
val child = StandardContext()
48+
child.addLifecycleListener(FixContextListener())
49+
child.path = "/cloudfoundryapplication"
50+
val initializer = getServletContextInitializer(contextPath)
51+
child.addServletContainerInitializer(initializer, emptySet())
52+
child.crossContext = true
53+
host.addChild(child)
54+
}
55+
56+
}
57+
}
58+
59+
private fun getServletContextInitializer(contextPath: String): ServletContainerInitializer {
60+
return ServletContainerInitializer { classes: Set<Class<*>?>?, context: ServletContext ->
61+
val servlet: Servlet = object : GenericServlet() {
62+
63+
@Throws(ServletException::class, IOException::class)
64+
override fun service(req: ServletRequest, res: ServletResponse) {
65+
val servletContext = req.servletContext.getContext(contextPath)
66+
servletContext.getRequestDispatcher("/cloudfoundryapplication").forward(req, res)
67+
}
68+
69+
}
70+
context.addServlet("cloudfoundry", servlet).addMapping("/*")
71+
}
72+
}
73+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright 2012-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.docs.actuator.endpoints.health.reactivehealthindicators
18+
19+
import org.springframework.boot.actuate.health.Health
20+
import org.springframework.boot.actuate.health.ReactiveHealthIndicator
21+
import org.springframework.stereotype.Component
22+
import reactor.core.publisher.Mono
23+
24+
@Component
25+
class MyReactiveHealthIndicator : ReactiveHealthIndicator {
26+
27+
override fun health(): Mono<Health> {
28+
// @formatter:off
29+
return doHealthCheck()!!.onErrorResume { exception: Throwable? ->
30+
Mono.just(Health.Builder().down(exception).build())
31+
}
32+
// @formatter:on
33+
}
34+
35+
private fun doHealthCheck(): Mono<Health>? {
36+
// perform some specific health check
37+
return /**/ null
38+
}
39+
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright 2012-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.docs.actuator.endpoints.health.writingcustomhealthindicators
18+
19+
import org.springframework.boot.actuate.health.Health
20+
import org.springframework.boot.actuate.health.HealthIndicator
21+
import org.springframework.stereotype.Component
22+
23+
@Component
24+
class MyHealthIndicator : HealthIndicator {
25+
26+
override fun health(): Health {
27+
val errorCode = check()
28+
if (errorCode != 0) {
29+
return Health.down().withDetail("Error Code", errorCode).build()
30+
}
31+
return Health.up().build()
32+
}
33+
34+
private fun check(): Int {
35+
// perform some specific health check
36+
return /**/ 0
37+
}
38+
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* Copyright 2012-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.docs.actuator.endpoints.implementingcustom
18+
19+
class CustomData(val name: String, val counter: Int)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright 2012-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.docs.actuator.endpoints.implementingcustom
18+
19+
import org.springframework.boot.actuate.endpoint.annotation.Endpoint
20+
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation
21+
import org.springframework.boot.actuate.endpoint.annotation.WriteOperation
22+
23+
@Endpoint(id = "custom")
24+
@Suppress("UNUSED_PARAMETER")
25+
class MyEndpoint {
26+
27+
// tag::read[]
28+
@ReadOperation
29+
fun getData(): CustomData {
30+
return CustomData("test", 5)
31+
}
32+
// end::read[]
33+
34+
// tag::write[]
35+
@WriteOperation
36+
fun updateData(name: String?, counter: Int) {
37+
// injects "test" and 42
38+
}
39+
// end::write[]
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright 2012-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.docs.actuator.endpoints.info.writingcustominfocontributors
18+
19+
import org.springframework.boot.actuate.info.Info
20+
import org.springframework.boot.actuate.info.InfoContributor
21+
import org.springframework.stereotype.Component
22+
import java.util.Collections
23+
24+
@Component
25+
class MyInfoContributor : InfoContributor {
26+
27+
override fun contribute(builder: Info.Builder) {
28+
builder.withDetail("example", Collections.singletonMap("key", "value"))
29+
}
30+
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright 2012-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.docs.actuator.endpoints.security.exposeall
18+
19+
import org.springframework.boot.actuate.autoconfigure.security.servlet.EndpointRequest
20+
import org.springframework.context.annotation.Bean
21+
import org.springframework.context.annotation.Configuration
22+
import org.springframework.security.config.annotation.web.builders.HttpSecurity
23+
import org.springframework.security.web.SecurityFilterChain
24+
25+
@Configuration(proxyBeanMethods = false)
26+
class MySecurityConfiguration {
27+
28+
@Bean
29+
fun securityFilterChain(http: HttpSecurity): SecurityFilterChain {
30+
http.requestMatcher(EndpointRequest.toAnyEndpoint()).authorizeRequests {
31+
requests -> requests.anyRequest().permitAll() }
32+
return http.build()
33+
}
34+
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright 2012-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.docs.actuator.endpoints.security.typical
18+
19+
import org.springframework.boot.actuate.autoconfigure.security.servlet.EndpointRequest
20+
import org.springframework.context.annotation.Bean
21+
import org.springframework.context.annotation.Configuration
22+
import org.springframework.security.config.annotation.web.builders.HttpSecurity
23+
import org.springframework.security.web.SecurityFilterChain
24+
25+
@Configuration(proxyBeanMethods = false)
26+
class MySecurityConfiguration {
27+
28+
@Bean
29+
fun securityFilterChain(http: HttpSecurity): SecurityFilterChain {
30+
http.requestMatcher(EndpointRequest.toAnyEndpoint()).authorizeRequests { requests ->
31+
requests.anyRequest().hasRole("ENDPOINT_ADMIN")
32+
}
33+
http.httpBasic()
34+
return http.build()
35+
}
36+
37+
}

0 commit comments

Comments
 (0)