Skip to content

Commit 197afff

Browse files
p7novphilwebb
authored andcommitted
Add Kotlin alternatives to Java documentation samples
See gh-29499
1 parent 0e906dc commit 197afff

File tree

318 files changed

+9619
-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.

318 files changed

+9619
-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,60 @@
1+
/*
2+
* Copyright 2012-2021 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 javax.servlet.*
28+
import kotlin.jvm.Throws
29+
30+
@Configuration(proxyBeanMethods = false)
31+
class MyCloudFoundryConfiguration {
32+
@Bean
33+
fun servletWebServerFactory(): TomcatServletWebServerFactory {
34+
return object : TomcatServletWebServerFactory() {
35+
override fun prepareContext(host: Host, initializers: Array<ServletContextInitializer>) {
36+
super.prepareContext(host, initializers)
37+
val child = StandardContext()
38+
child.addLifecycleListener(FixContextListener())
39+
child.path = "/cloudfoundryapplication"
40+
val initializer = getServletContextInitializer(contextPath)
41+
child.addServletContainerInitializer(initializer, emptySet())
42+
child.crossContext = true
43+
host.addChild(child)
44+
}
45+
}
46+
}
47+
48+
private fun getServletContextInitializer(contextPath: String): ServletContainerInitializer {
49+
return ServletContainerInitializer { classes: Set<Class<*>?>?, context: ServletContext ->
50+
val servlet: Servlet = object : GenericServlet() {
51+
@Throws(ServletException::class, IOException::class)
52+
override fun service(req: ServletRequest, res: ServletResponse) {
53+
val context = req.servletContext.getContext(contextPath)
54+
context.getRequestDispatcher("/cloudfoundryapplication").forward(req, res)
55+
}
56+
}
57+
context.addServlet("cloudfoundry", servlet).addMapping("/*")
58+
}
59+
}
60+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright 2012-2021 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+
override fun health(): Mono<Health> {
27+
// @formatter:off
28+
return doHealthCheck()!!.onErrorResume { exception: Throwable? ->
29+
Mono.just(
30+
Health.Builder().down(exception).build()
31+
)
32+
}
33+
// @formatter:on
34+
}
35+
36+
private fun doHealthCheck(): Mono<Health>? {
37+
// perform some specific health check
38+
return /**/null
39+
}
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright 2012-2021 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+
override fun health(): Health {
26+
val errorCode = check()
27+
return if (errorCode != 0) {
28+
Health.down().withDetail("Error Code", errorCode).build()
29+
} else Health.up().build()
30+
}
31+
32+
private fun check(): Int {
33+
// perform some specific health check
34+
return /**/0
35+
}
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* Copyright 2012-2021 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,36 @@
1+
/*
2+
* Copyright 2012-2021 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+
class MyEndpoint {
25+
// tag::read[]
26+
@get:ReadOperation
27+
val data: CustomData
28+
get() = CustomData("test", 5)
29+
30+
// end::read[]
31+
// tag::write[]
32+
@WriteOperation
33+
fun updateData(name: String?, counter: Int) {
34+
// injects "test" and 42
35+
} // end::write[]
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright 2012-2021 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.*
23+
24+
@Component
25+
class MyInfoContributor : InfoContributor {
26+
override fun contribute(builder: Info.Builder) {
27+
builder.withDetail("example", Collections.singletonMap("key", "value"))
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright 2012-2021 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.config.annotation.web.configurers.ExpressionUrlAuthorizationConfigurer
24+
import org.springframework.security.config.annotation.web.configurers.ExpressionUrlAuthorizationConfigurer.ExpressionInterceptUrlRegistry
25+
import org.springframework.security.web.SecurityFilterChain
26+
27+
@Configuration(proxyBeanMethods = false)
28+
class MySecurityConfiguration {
29+
@Bean
30+
fun securityFilterChain(http: HttpSecurity): SecurityFilterChain {
31+
http.requestMatcher(EndpointRequest.toAnyEndpoint())
32+
.authorizeRequests { requests: ExpressionUrlAuthorizationConfigurer<HttpSecurity>.ExpressionInterceptUrlRegistry ->
33+
requests.anyRequest().permitAll()
34+
}
35+
return http.build()
36+
}
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2012-2021 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.config.annotation.web.configurers.ExpressionUrlAuthorizationConfigurer
24+
import org.springframework.security.config.annotation.web.configurers.ExpressionUrlAuthorizationConfigurer.ExpressionInterceptUrlRegistry
25+
import org.springframework.security.web.SecurityFilterChain
26+
27+
@Configuration(proxyBeanMethods = false)
28+
class MySecurityConfiguration {
29+
@Bean
30+
fun securityFilterChain(http: HttpSecurity): SecurityFilterChain {
31+
http.requestMatcher(EndpointRequest.toAnyEndpoint())
32+
.authorizeRequests { requests: ExpressionUrlAuthorizationConfigurer<HttpSecurity>.ExpressionInterceptUrlRegistry ->
33+
requests.anyRequest().hasRole("ENDPOINT_ADMIN")
34+
}
35+
http.httpBasic()
36+
return http.build()
37+
}
38+
}

0 commit comments

Comments
 (0)