Skip to content

Commit

Permalink
Merge pull request #158 from bobi/dev
Browse files Browse the repository at this point in the history
- updated some code styles
  • Loading branch information
bobi committed Aug 20, 2023
2 parents ec6d57e + 18576ac commit 0254708
Show file tree
Hide file tree
Showing 11 changed files with 49 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package com.github.bobi.aemgroovyconsoleplugin.config

import com.intellij.util.messages.Topic

interface SettingsChangedNotifier {
fun interface SettingsChangedNotifier {
companion object {
val TOPIC: Topic<SettingsChangedNotifier> = Topic.create("AEM Servers configuration changed", SettingsChangedNotifier::class.java)
val TOPIC: Topic<SettingsChangedNotifier> =
Topic.create("AEM Servers configuration changed", SettingsChangedNotifier::class.java)
}

fun settingsChanged()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ import javax.swing.*

private const val TOKEN_DESCRIPTION = "Paste JSON from Adobe AEM Developer Console"

private const val INVALID_JSON_MESSAGE = "Invalid json"

/**
* User: Andrey Bardashevsky
* Date/Time: 29.07.2022 15:45
Expand Down Expand Up @@ -72,6 +74,7 @@ class AemServerEditDialog(private val project: Project, private val tableItem: A
init()
}

@Suppress("kotlin:S3776")
override fun createCenterPanel(): JComponent = panel {
row("Server Name: ") {
textField()
Expand Down Expand Up @@ -305,10 +308,10 @@ class AemServerEditDialog(private val project: Project, private val tableItem: A
val (ok, statusCode, accessToken) = gson.fromJson(value, AemDevToken::class.java)

if (!ok || statusCode != 200 || accessToken.isBlank()) {
return error("Invalid json")
return error(INVALID_JSON_MESSAGE)
}
} catch (e: Throwable) {
return error("Invalid json")
return error(INVALID_JSON_MESSAGE)
}
}

Expand Down Expand Up @@ -336,10 +339,10 @@ class AemServerEditDialog(private val project: Project, private val tableItem: A
|| integration.privateKey.isBlank()
|| integration.publicKey.isBlank()
) {
return error("Invalid json")
return error(INVALID_JSON_MESSAGE)
}
} catch (e: Throwable) {
return error("Invalid json")
return error(INVALID_JSON_MESSAGE)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,18 +87,15 @@ class GenericMethodHolder(descriptor: GenericMethodDescriptor, psiFile: PsiFile)
place: PsiElement,
genericTypes: Map<String, PsiClassType>
): PsiClassType {
return if (genericTypes.contains(typeDescriptor.fqn)) {
genericTypes[typeDescriptor.fqn]!!
} else {
if (typeDescriptor.genericType != null) {
return genericTypes[typeDescriptor.fqn]
?: if (typeDescriptor.genericType != null) {
TypesUtil.createGenericType(typeDescriptor.fqn, place, genericTypes[typeDescriptor.genericType])
} else {
TypesUtil.createType(typeDescriptor.fqn, place)
}
}
}

companion object {
private val ORIGIN_INFO = "via ${this::class.java.name}"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,10 @@ class AemConsoleEditorDecorator(project: Project) : EditorNotificationProvider {
init {
val notifications = project.getService(EditorNotifications::class.java)

project.messageBus.connect().subscribe(SettingsChangedNotifier.TOPIC, object : SettingsChangedNotifier {
override fun settingsChanged() {
notifications.updateAllNotifications()
}
})
project.messageBus.connect().subscribe(
SettingsChangedNotifier.TOPIC,
SettingsChangedNotifier { notifications.updateAllNotifications() }
)
}

override fun collectNotificationData(
Expand All @@ -44,4 +43,4 @@ class AemConsoleEditorDecorator(project: Project) : EditorNotificationProvider {

return Function<FileEditor, JComponent?> { AemConsoleEditorToolbar(project, it) }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ class AemGroovyConsoleScriptExecutor(private val project: Project) {
}
}

@Suppress("kotlin:S3776")
override fun actionPerformed(e: AnActionEvent) {
val project = e.project
val component = e.getData(PlatformDataKeys.CONTEXT_COMPONENT)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,25 @@ object PasswordsService {

fun removeCredentials(id: Long) {
val passwordSafe = PasswordSafe.instance
passwordSafe.set(credentialAttributes(id), null)
passwordSafe.set(credentialAttributes(id, AEM_ACCESS_TOKEN_SUFFIX), null)
passwordSafe[credentialAttributes(id)] = null
passwordSafe[credentialAttributes(id, AEM_ACCESS_TOKEN_SUFFIX)] = null
}

fun setCredentials(id: Long, user: String, password: String) {
val passwordSafe = PasswordSafe.instance

passwordSafe.set(credentialAttributes(id), Credentials(user, password))
passwordSafe.set(credentialAttributes(id, AEM_ACCESS_TOKEN_SUFFIX), null)
passwordSafe[credentialAttributes(id)] = Credentials(user, password)
passwordSafe[credentialAttributes(id, AEM_ACCESS_TOKEN_SUFFIX)] = null
}

fun getCredentials(id: Long): Credentials? = PasswordSafe.instance.get(credentialAttributes(id))
fun getCredentials(id: Long): Credentials? = PasswordSafe.instance[credentialAttributes(id)]

fun setAccessToken(id: Long, token: String) {
PasswordSafe.instance.set(credentialAttributes(id, AEM_ACCESS_TOKEN_SUFFIX), Credentials("token", token))
PasswordSafe.instance[credentialAttributes(id, AEM_ACCESS_TOKEN_SUFFIX)] = Credentials("token", token)
}

fun getAccessToken(id: Long): String? =
PasswordSafe.instance.get(credentialAttributes(id, AEM_ACCESS_TOKEN_SUFFIX))?.getPasswordAsString()
PasswordSafe.instance[credentialAttributes(id, AEM_ACCESS_TOKEN_SUFFIX)]?.getPasswordAsString()

private fun credentialAttributes(id: Long, suffix: String = "") = CredentialAttributes(
generateServiceName(AEM_GROOVY_CONSOLE, id.toString() + suffix),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,14 @@ class AdobeIMSTokenProvider : Disposable {

private fun getImsToken(config: AemServerHttpConfig): String {
if (config.id == null) {
return fetchAccessToken(config).access_token
return fetchAccessToken(config).accessToken
} else {
try {
val token = PasswordsService.getAccessToken(config.id)
if (token !== null) {
val accessToken = verifyAccessToken(token)

return accessToken.access_token
return accessToken.accessToken
} else {
throw Exception("Token not found")
}
Expand All @@ -74,21 +74,21 @@ class AdobeIMSTokenProvider : Disposable {

PasswordsService.setAccessToken(config.id, gson.toJson(accessToken))

return accessToken.access_token
return accessToken.accessToken
}
}
}

private fun verifyAccessToken(token: String): AccessToken {
val accessToken = gson.fromJson(token, AccessToken::class.java)

val splitToken = accessToken.access_token.split(Regex("\\."))
val splitToken = accessToken.accessToken.split(Regex("\\."))
val unsignedToken = splitToken[0] + "." + splitToken[1] + "."

val jwt = Jwts.parserBuilder().build().parseClaimsJwt(unsignedToken)

val createdAt = jwt.body.get("created_at", String::class.java).toLong()
val expiresIn = jwt.body.get("expires_in", String::class.java).toLong()
val createdAt = jwt.body["created_at", String::class.java].toLong()
val expiresIn = jwt.body["expires_in", String::class.java].toLong()

val expiresAt = Instant.ofEpochMilli(createdAt + expiresIn).minus(1.hours.toJavaDuration())

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,6 @@ import com.intellij.openapi.components.Service
import com.intellij.openapi.components.service
import com.intellij.openapi.diagnostic.thisLogger
import com.intellij.openapi.project.Project
import io.ktor.client.*
import io.ktor.client.call.*
import io.ktor.client.engine.cio.*
import io.ktor.client.plugins.auth.*
import io.ktor.client.plugins.auth.providers.*
import io.ktor.client.plugins.contentnegotiation.*
import io.ktor.client.request.*
import io.ktor.client.request.forms.*
import io.ktor.client.statement.*
import io.ktor.http.*
import io.ktor.serialization.kotlinx.json.*
import io.ktor.utils.io.core.*
import org.apache.hc.client5.http.classic.methods.HttpPost
import org.apache.hc.client5.http.config.ConnectionConfig
Expand Down Expand Up @@ -170,9 +159,7 @@ class GroovyConsoleHttpService(project: Project) : Disposable {
val user = credentials?.userName.orEmpty()
val password = credentials?.getPasswordAsString().orEmpty()

if (user.isBlank() || password.isBlank()) {
throw IllegalArgumentException("Credentials is not found for server: ${config.id}")
}
require(user.isNotBlank() && password.isNotBlank()) { "Credentials is not found for server: ${config.id}" }

return AemServerCredentials(user, password)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.github.bobi.aemgroovyconsoleplugin.services.model

import com.google.gson.annotations.SerializedName

/**
* User: Andrey Bardashevsky
* Date/Time: 08.07.2023 18:32
Expand Down Expand Up @@ -30,7 +32,10 @@ data class AemCertificateToken(
)

data class AccessToken(
val token_type: String,
val access_token: String,
val expires_in: Long,
@SerializedName("token_type")
val tokenType: String,
@SerializedName("access_token")
val accessToken: String,
@SerializedName("expires_in")
val expiresIn: Long,
)
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ object Notifications {
notify(title, content, NotificationType.INFORMATION)
}

@Suppress("unused")
fun notifyWarn(title: String = "", content: String) {
notify(title, content, NotificationType.WARNING)
}

private fun notify(title: String, content: String, type: NotificationType) {
Notifications.Bus.notify(Notification(NOTOFICATION_GROUP_ID, title, content, type))
}
}
}
7 changes: 5 additions & 2 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@
fieldName="INSTANCE"
/>

<projectConfigurable groupId="tools"
instance="com.github.bobi.aemgroovyconsoleplugin.config.ui.AemServersConfigurable"/>
<projectConfigurable id="com.github.bobi.aemgroovyconsoleplugin.config.ui.AemServersConfigurable"
instance="com.github.bobi.aemgroovyconsoleplugin.config.ui.AemServersConfigurable"
groupId="tools"
displayName="AEM Groovy Console"
nonDefaultProject="true"/>

<editorNotificationProvider
implementation="com.github.bobi.aemgroovyconsoleplugin.editor.AemConsoleEditorDecorator"/>
Expand Down

0 comments on commit 0254708

Please sign in to comment.