generated from leanix/repository-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Feature/cid 2731/establish ws connection on startup #5
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
Merged
geoandri
merged 5 commits into
main
from
feature/CID-2731/establish-ws-connection-on-startup
Jul 12, 2024
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
03b7882
CID-2731 Establish Websocket connection on start up
geoandri 4d7b07a
CID-2731 Establish Websocket connection on start up
geoandri e7cfa26
CID-2731 Addressing review comments
geoandri 71a860d
CID-2731 Addressing review comments
geoandri da7f9ae
CID-2731 Addressing review comments
geoandri File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
src/main/kotlin/net/leanix/githubagent/client/AuthClient.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package net.leanix.githubagent.client | ||
|
||
import net.leanix.githubagent.dto.JwtDto | ||
import org.springframework.cloud.openfeign.FeignClient | ||
import org.springframework.http.HttpHeaders.AUTHORIZATION | ||
import org.springframework.http.MediaType.APPLICATION_FORM_URLENCODED_VALUE | ||
import org.springframework.web.bind.annotation.PostMapping | ||
import org.springframework.web.bind.annotation.RequestBody | ||
import org.springframework.web.bind.annotation.RequestHeader | ||
|
||
@FeignClient( | ||
name = "authentication", | ||
url = "\${leanix.auth.access-token-uri}", | ||
) | ||
fun interface AuthClient { | ||
|
||
@PostMapping(value = ["/oauth2/token"], consumes = [APPLICATION_FORM_URLENCODED_VALUE]) | ||
fun getToken( | ||
@RequestHeader(name = AUTHORIZATION) authorization: String, | ||
@RequestBody body: String, | ||
): JwtDto | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
src/main/kotlin/net/leanix/githubagent/config/LeanIXProperties.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package net.leanix.githubagent.config | ||
|
||
import org.springframework.boot.context.properties.ConfigurationProperties | ||
|
||
@ConfigurationProperties(prefix = "leanix") | ||
data class LeanIXProperties( | ||
val wsBaseUrl: String, | ||
val auth: Auth | ||
) { | ||
data class Auth( | ||
val accessTokenUri: String, | ||
val apiUserToken: String | ||
) | ||
} |
58 changes: 58 additions & 0 deletions
58
src/main/kotlin/net/leanix/githubagent/config/WebSocketClientConfig.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package net.leanix.githubagent.config | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper | ||
import io.github.resilience4j.retry.annotation.Retry | ||
import net.leanix.githubagent.handler.BrokerStompSessionHandler | ||
import net.leanix.githubagent.services.AuthService | ||
import org.springframework.context.annotation.Bean | ||
import org.springframework.context.annotation.Configuration | ||
import org.springframework.messaging.converter.MappingJackson2MessageConverter | ||
import org.springframework.messaging.simp.stomp.StompHeaders | ||
import org.springframework.messaging.simp.stomp.StompSession | ||
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler | ||
import org.springframework.web.socket.WebSocketHttpHeaders | ||
import org.springframework.web.socket.client.standard.StandardWebSocketClient | ||
import org.springframework.web.socket.messaging.WebSocketStompClient | ||
import org.springframework.web.socket.sockjs.client.SockJsClient | ||
import org.springframework.web.socket.sockjs.client.WebSocketTransport | ||
|
||
@Configuration | ||
class WebSocketClientConfig( | ||
private val brokerStompSessionHandler: BrokerStompSessionHandler, | ||
private val objectMapper: ObjectMapper, | ||
private val authService: AuthService, | ||
private val leanIXProperties: LeanIXProperties, | ||
private val gitHubEnterpriseProperties: GitHubEnterpriseProperties | ||
) { | ||
|
||
@Retry(name = "ws-init-session") | ||
fun initSession(): StompSession { | ||
val headers = WebSocketHttpHeaders() | ||
val stompHeaders = StompHeaders() | ||
stompHeaders["Authorization"] = "Bearer ${authService.getBearerToken()}" | ||
stompHeaders["GitHub-Enterprise-URL"] = gitHubEnterpriseProperties.baseUrl | ||
stompHeaders["GiHub-Enterprise-App-Id"] = gitHubEnterpriseProperties.gitHubAppId | ||
geoandri marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return stompClient().connectAsync( | ||
leanIXProperties.wsBaseUrl, | ||
headers, | ||
stompHeaders, | ||
brokerStompSessionHandler, | ||
).get() | ||
} | ||
|
||
@Bean | ||
fun stompClient(): WebSocketStompClient { | ||
val jsonConverter = MappingJackson2MessageConverter() | ||
jsonConverter.objectMapper = objectMapper | ||
|
||
val simpleWebSocketClient = StandardWebSocketClient() | ||
val transports = listOf(WebSocketTransport(simpleWebSocketClient)) | ||
val sockJsClient = SockJsClient(transports) | ||
val stompClient = WebSocketStompClient(sockJsClient) | ||
stompClient.messageConverter = jsonConverter | ||
val scheduler = ThreadPoolTaskScheduler() | ||
scheduler.initialize() | ||
stompClient.taskScheduler = scheduler | ||
return stompClient | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package net.leanix.githubagent.dto | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty | ||
|
||
data class JwtDto( | ||
val scope: String, | ||
val expired: Boolean, | ||
@JsonProperty("access_token") | ||
val accessToken: String, | ||
@JsonProperty("token_type") | ||
val tokenType: String, | ||
@JsonProperty("expired_in") | ||
val expiredIn: Int, | ||
) |
35 changes: 35 additions & 0 deletions
35
src/main/kotlin/net/leanix/githubagent/handler/BrokerStompSessionHandler.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package net.leanix.githubagent.handler | ||
|
||
import org.slf4j.LoggerFactory | ||
import org.springframework.messaging.simp.stomp.StompCommand | ||
import org.springframework.messaging.simp.stomp.StompHeaders | ||
import org.springframework.messaging.simp.stomp.StompSession | ||
import org.springframework.messaging.simp.stomp.StompSessionHandlerAdapter | ||
import org.springframework.stereotype.Component | ||
|
||
@Component | ||
class BrokerStompSessionHandler : StompSessionHandlerAdapter() { | ||
|
||
private val logger = LoggerFactory.getLogger(BrokerStompSessionHandler::class.java) | ||
|
||
override fun afterConnected(session: StompSession, connectedHeaders: StompHeaders) { | ||
logger.info("connected to the server: ${session.sessionId}") | ||
session.subscribe("/user/queue/repositories-string", this) | ||
geoandri marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
override fun handleException( | ||
session: StompSession, | ||
command: StompCommand?, | ||
headers: StompHeaders, | ||
payload: ByteArray, | ||
exception: Throwable, | ||
) { | ||
logger.error("handleException", exception) | ||
logger.error(exception.message) | ||
} | ||
|
||
override fun handleTransportError(session: StompSession, exception: Throwable) { | ||
logger.error("handleTransportError", exception) | ||
logger.error(exception.message) | ||
mohamedlajmileanix marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} |
7 changes: 6 additions & 1 deletion
7
src/main/kotlin/net/leanix/githubagent/runners/PostStartupRunner.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,21 @@ | ||
package net.leanix.githubagent.runners | ||
|
||
import net.leanix.githubagent.services.GitHubAuthenticationService | ||
import net.leanix.githubagent.services.WebSocketService | ||
import org.springframework.boot.ApplicationArguments | ||
import org.springframework.boot.ApplicationRunner | ||
import org.springframework.context.annotation.Profile | ||
import org.springframework.stereotype.Component | ||
|
||
@Component | ||
@Profile("!test") | ||
class PostStartupRunner(private val githubAuthenticationService: GitHubAuthenticationService) : ApplicationRunner { | ||
class PostStartupRunner( | ||
private val githubAuthenticationService: GitHubAuthenticationService, | ||
private val webSocketService: WebSocketService | ||
) : ApplicationRunner { | ||
|
||
override fun run(args: ApplicationArguments?) { | ||
githubAuthenticationService.generateJwtToken() | ||
webSocketService.initSession() | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/main/kotlin/net/leanix/githubagent/services/AuthService.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package net.leanix.githubagent.services | ||
|
||
import net.leanix.githubagent.client.AuthClient | ||
import net.leanix.githubagent.config.LeanIXProperties | ||
import org.springframework.stereotype.Service | ||
import java.util.* | ||
|
||
@Service | ||
class AuthService( | ||
mohamedlajmileanix marked this conversation as resolved.
Show resolved
Hide resolved
|
||
private val authClient: AuthClient, | ||
private val leanIXProperties: LeanIXProperties | ||
) { | ||
|
||
fun getBearerToken(): String { | ||
return authClient.getToken( | ||
authorization = getBasicAuthHeader(), | ||
body = "grant_type=client_credentials", | ||
).accessToken | ||
} | ||
|
||
private fun getBasicAuthHeader(): String = | ||
"Basic " + Base64.getEncoder().encodeToString( | ||
"apitoken:${leanIXProperties.auth.apiUserToken}".toByteArray(), | ||
) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
src/main/kotlin/net/leanix/githubagent/services/WebSocketService.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package net.leanix.githubagent.services | ||
|
||
import net.leanix.githubagent.config.WebSocketClientConfig | ||
import org.slf4j.LoggerFactory | ||
import org.springframework.messaging.simp.stomp.StompSession | ||
import org.springframework.stereotype.Service | ||
|
||
@Service | ||
class WebSocketService( | ||
private val webSocketClientConfig: WebSocketClientConfig | ||
) { | ||
|
||
private val logger = LoggerFactory.getLogger(WebSocketService::class.java) | ||
var stompSession: StompSession? = null | ||
|
||
fun initSession() { | ||
logger.info("init session") | ||
stompSession = webSocketClientConfig.initSession() | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.