mirror of https://github.com/usbharu/Hideout.git
Merge pull request #97 from usbharu/feature/logging-2
Feature/logging 2
This commit is contained in:
commit
1118ca30fb
|
@ -13,6 +13,7 @@ import dev.usbharu.hideout.service.job.JobQueueParentService
|
||||||
import dev.usbharu.hideout.service.user.UserService
|
import dev.usbharu.hideout.service.user.UserService
|
||||||
import io.ktor.http.*
|
import io.ktor.http.*
|
||||||
import kjob.core.job.JobProps
|
import kjob.core.job.JobProps
|
||||||
|
import org.slf4j.LoggerFactory
|
||||||
import org.springframework.beans.factory.annotation.Qualifier
|
import org.springframework.beans.factory.annotation.Qualifier
|
||||||
import org.springframework.stereotype.Service
|
import org.springframework.stereotype.Service
|
||||||
|
|
||||||
|
@ -32,7 +33,7 @@ class APReceiveFollowServiceImpl(
|
||||||
private val apRequestService: APRequestService
|
private val apRequestService: APRequestService
|
||||||
) : APReceiveFollowService {
|
) : APReceiveFollowService {
|
||||||
override suspend fun receiveFollow(follow: Follow): ActivityPubResponse {
|
override suspend fun receiveFollow(follow: Follow): ActivityPubResponse {
|
||||||
// TODO: Verify HTTP Signature
|
logger.info("FOLLOW from: {} to: {}", follow.actor, follow.`object`)
|
||||||
jobQueueParentService.schedule(ReceiveFollowJob) {
|
jobQueueParentService.schedule(ReceiveFollowJob) {
|
||||||
props[ReceiveFollowJob.actor] = follow.actor
|
props[ReceiveFollowJob.actor] = follow.actor
|
||||||
props[ReceiveFollowJob.follow] = objectMapper.writeValueAsString(follow)
|
props[ReceiveFollowJob.follow] = objectMapper.writeValueAsString(follow)
|
||||||
|
@ -42,12 +43,12 @@ class APReceiveFollowServiceImpl(
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun receiveFollowJob(props: JobProps<ReceiveFollowJob>) {
|
override suspend fun receiveFollowJob(props: JobProps<ReceiveFollowJob>) {
|
||||||
// throw Exception()
|
|
||||||
transaction.transaction {
|
transaction.transaction {
|
||||||
val actor = props[ReceiveFollowJob.actor]
|
val actor = props[ReceiveFollowJob.actor]
|
||||||
val targetActor = props[ReceiveFollowJob.targetActor]
|
val targetActor = props[ReceiveFollowJob.targetActor]
|
||||||
val person = apUserService.fetchPerson(actor, targetActor)
|
val person = apUserService.fetchPerson(actor, targetActor)
|
||||||
val follow = objectMapper.readValue<Follow>(props[ReceiveFollowJob.follow])
|
val follow = objectMapper.readValue<Follow>(props[ReceiveFollowJob.follow])
|
||||||
|
logger.info("START Follow from: {} to: {}", targetActor, actor)
|
||||||
|
|
||||||
val signer = userQueryService.findByUrl(targetActor)
|
val signer = userQueryService.findByUrl(targetActor)
|
||||||
|
|
||||||
|
@ -68,6 +69,11 @@ class APReceiveFollowServiceImpl(
|
||||||
userQueryService.findByUrl(follow.actor ?: throw java.lang.IllegalArgumentException("Actor is null"))
|
userQueryService.findByUrl(follow.actor ?: throw java.lang.IllegalArgumentException("Actor is null"))
|
||||||
|
|
||||||
userService.followRequest(targetEntity.id, followActorEntity.id)
|
userService.followRequest(targetEntity.id, followActorEntity.id)
|
||||||
|
logger.info("SUCCESS Follow from: {} to: {}", targetActor, actor)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
private val logger = LoggerFactory.getLogger(APReceiveFollowServiceImpl::class.java)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -34,6 +34,7 @@ class APRequestServiceImpl(
|
||||||
) : APRequestService {
|
) : APRequestService {
|
||||||
|
|
||||||
override suspend fun <R : Object> apGet(url: String, signer: User?, responseClass: Class<R>): R {
|
override suspend fun <R : Object> apGet(url: String, signer: User?, responseClass: Class<R>): R {
|
||||||
|
logger.debug("START ActivityPub Request GET url: {}, signer: {}", url, signer?.url)
|
||||||
val date = dateTimeFormatter.format(ZonedDateTime.now(ZoneId.of("GMT")))
|
val date = dateTimeFormatter.format(ZonedDateTime.now(ZoneId.of("GMT")))
|
||||||
val u = URL(url)
|
val u = URL(url)
|
||||||
if (signer?.privateKey == null) {
|
if (signer?.privateKey == null) {
|
||||||
|
@ -41,6 +42,7 @@ class APRequestServiceImpl(
|
||||||
header("Accept", ContentType.Application.Activity)
|
header("Accept", ContentType.Application.Activity)
|
||||||
header("Date", date)
|
header("Date", date)
|
||||||
}.bodyAsText()
|
}.bodyAsText()
|
||||||
|
logBody(bodyAsText, url)
|
||||||
return objectMapper.readValue(bodyAsText, responseClass)
|
return objectMapper.readValue(bodyAsText, responseClass)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -63,7 +65,7 @@ class APRequestServiceImpl(
|
||||||
signHeaders = listOf("(request-target)", "date", "host", "accept")
|
signHeaders = listOf("(request-target)", "date", "host", "accept")
|
||||||
)
|
)
|
||||||
|
|
||||||
val bodyAsText = httpClient.get(url) {
|
val httpResponse = httpClient.get(url) {
|
||||||
headers {
|
headers {
|
||||||
headers {
|
headers {
|
||||||
appendAll(headers)
|
appendAll(headers)
|
||||||
|
@ -72,8 +74,16 @@ class APRequestServiceImpl(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
contentType(ContentType.Application.Activity)
|
contentType(ContentType.Application.Activity)
|
||||||
}.bodyAsText()
|
}
|
||||||
return objectMapper.readValue(bodyAsText, responseClass)
|
val bodyAsText = httpResponse.bodyAsText()
|
||||||
|
val readValue = objectMapper.readValue(bodyAsText, responseClass)
|
||||||
|
logger.debug(
|
||||||
|
"SUCCESS ActivityPub Request GET status: {} url: {}",
|
||||||
|
httpResponse.status,
|
||||||
|
httpResponse.request.url
|
||||||
|
)
|
||||||
|
logBody(bodyAsText, url)
|
||||||
|
return readValue
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun <T : Object, R : Object> apPost(
|
override suspend fun <T : Object, R : Object> apPost(
|
||||||
|
@ -87,6 +97,7 @@ class APRequestServiceImpl(
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun <T : Object> apPost(url: String, body: T?, signer: User?): String {
|
override suspend fun <T : Object> apPost(url: String, body: T?, signer: User?): String {
|
||||||
|
logger.debug("START ActivityPub Request POST url: {}, signer: {}", url, signer?.url)
|
||||||
if (body != null) {
|
if (body != null) {
|
||||||
val mutableListOf = mutableListOf<String>()
|
val mutableListOf = mutableListOf<String>()
|
||||||
mutableListOf.add("https://www.w3.org/ns/activitystreams")
|
mutableListOf.add("https://www.w3.org/ns/activitystreams")
|
||||||
|
@ -96,6 +107,20 @@ class APRequestServiceImpl(
|
||||||
|
|
||||||
val requestBody = objectMapper.writeValueAsString(body)
|
val requestBody = objectMapper.writeValueAsString(body)
|
||||||
|
|
||||||
|
logger.trace(
|
||||||
|
"""
|
||||||
|
|
|
||||||
|
|***** BEGIN HTTP Request Trace url: {} *****
|
||||||
|
|
|
||||||
|
|$requestBody
|
||||||
|
|
|
||||||
|
|***** END HTTP Request Trace url: {} *****
|
||||||
|
|
|
||||||
|
""".trimMargin(),
|
||||||
|
url,
|
||||||
|
url
|
||||||
|
)
|
||||||
|
|
||||||
val sha256 = MessageDigest.getInstance("SHA-256")
|
val sha256 = MessageDigest.getInstance("SHA-256")
|
||||||
|
|
||||||
val digest = Base64Util.encode(sha256.digest(requestBody.toByteArray()))
|
val digest = Base64Util.encode(sha256.digest(requestBody.toByteArray()))
|
||||||
|
@ -103,19 +128,17 @@ class APRequestServiceImpl(
|
||||||
val date = dateTimeFormatter.format(ZonedDateTime.now(ZoneId.of("GMT")))
|
val date = dateTimeFormatter.format(ZonedDateTime.now(ZoneId.of("GMT")))
|
||||||
val u = URL(url)
|
val u = URL(url)
|
||||||
if (signer?.privateKey == null) {
|
if (signer?.privateKey == null) {
|
||||||
logger.debug("NOT SIGN Request: {}", url)
|
val bodyAsText = httpClient.post(url) {
|
||||||
logger.trace("{}", signer)
|
|
||||||
return httpClient.post(url) {
|
|
||||||
header("Accept", ContentType.Application.Activity)
|
header("Accept", ContentType.Application.Activity)
|
||||||
header("Date", date)
|
header("Date", date)
|
||||||
header("Digest", "sha-256=$digest")
|
header("Digest", "sha-256=$digest")
|
||||||
setBody(requestBody)
|
setBody(requestBody)
|
||||||
contentType(ContentType.Application.Activity)
|
contentType(ContentType.Application.Activity)
|
||||||
}.bodyAsText()
|
}.bodyAsText()
|
||||||
|
logBody(bodyAsText, url)
|
||||||
|
return bodyAsText
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.debug("SIGN Request: {}", url)
|
|
||||||
|
|
||||||
val headers = headers {
|
val headers = headers {
|
||||||
append("Accept", ContentType.Application.Activity)
|
append("Accept", ContentType.Application.Activity)
|
||||||
append("Date", date)
|
append("Date", date)
|
||||||
|
@ -136,7 +159,7 @@ class APRequestServiceImpl(
|
||||||
signHeaders = listOf("(request-target)", "date", "host", "digest")
|
signHeaders = listOf("(request-target)", "date", "host", "digest")
|
||||||
)
|
)
|
||||||
|
|
||||||
return httpClient.post(url) {
|
val httpResponse = httpClient.post(url) {
|
||||||
headers {
|
headers {
|
||||||
headers {
|
headers {
|
||||||
appendAll(headers)
|
appendAll(headers)
|
||||||
|
@ -146,7 +169,31 @@ class APRequestServiceImpl(
|
||||||
}
|
}
|
||||||
setBody(requestBody)
|
setBody(requestBody)
|
||||||
contentType(ContentType.Application.Activity)
|
contentType(ContentType.Application.Activity)
|
||||||
}.bodyAsText()
|
}
|
||||||
|
val bodyAsText = httpResponse.bodyAsText()
|
||||||
|
logger.debug(
|
||||||
|
"SUCCESS ActivityPub Request POST status: {} url: {}",
|
||||||
|
httpResponse.status,
|
||||||
|
httpResponse.request.url
|
||||||
|
)
|
||||||
|
logBody(bodyAsText, url)
|
||||||
|
return bodyAsText
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun logBody(bodyAsText: String, url: String) {
|
||||||
|
logger.trace(
|
||||||
|
"""
|
||||||
|
|
|
||||||
|
|***** BEGIN HTTP Response Trace url: {} *****
|
||||||
|
|
|
||||||
|
|$bodyAsText
|
||||||
|
|
|
||||||
|
|***** END HTTP Response TRACE url: {} *****
|
||||||
|
|
|
||||||
|
""".trimMargin(),
|
||||||
|
url,
|
||||||
|
url
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
|
|
Loading…
Reference in New Issue