diff --git a/src/main/kotlin/dev/usbharu/hideout/service/ap/APRequestServiceImpl.kt b/src/main/kotlin/dev/usbharu/hideout/service/ap/APRequestServiceImpl.kt index 3106f131..b8074037 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/ap/APRequestServiceImpl.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/ap/APRequestServiceImpl.kt @@ -34,6 +34,7 @@ class APRequestServiceImpl( ) : APRequestService { override suspend fun apGet(url: String, signer: User?, responseClass: Class): R { + logger.debug("START ActivityPub Request GET url: {}, signer: {}", url, signer?.url) val date = dateTimeFormatter.format(ZonedDateTime.now(ZoneId.of("GMT"))) val u = URL(url) if (signer?.privateKey == null) { @@ -63,7 +64,7 @@ class APRequestServiceImpl( signHeaders = listOf("(request-target)", "date", "host", "accept") ) - val bodyAsText = httpClient.get(url) { + val httpResponse = httpClient.get(url) { headers { headers { appendAll(headers) @@ -72,8 +73,24 @@ class APRequestServiceImpl( } } 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 + ) + logger.trace( + """ + |***** BEGIN HTTP Response Trace url: {} ***** + | + |$bodyAsText + | + |***** END HTTP Response TRACE url: {} ***** + """.trimMargin(), url, url + ) + return readValue } override suspend fun apPost( @@ -87,6 +104,7 @@ class APRequestServiceImpl( } override suspend fun apPost(url: String, body: T?, signer: User?): String { + logger.debug("START ActivityPub Request POST url: {}, signer: {}", url, signer?.url) if (body != null) { val mutableListOf = mutableListOf() mutableListOf.add("https://www.w3.org/ns/activitystreams") @@ -96,6 +114,16 @@ class APRequestServiceImpl( 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 digest = Base64Util.encode(sha256.digest(requestBody.toByteArray())) @@ -103,8 +131,6 @@ class APRequestServiceImpl( val date = dateTimeFormatter.format(ZonedDateTime.now(ZoneId.of("GMT"))) val u = URL(url) if (signer?.privateKey == null) { - logger.debug("NOT SIGN Request: {}", url) - logger.trace("{}", signer) return httpClient.post(url) { header("Accept", ContentType.Application.Activity) header("Date", date) @@ -114,8 +140,6 @@ class APRequestServiceImpl( }.bodyAsText() } - logger.debug("SIGN Request: {}", url) - val headers = headers { append("Accept", ContentType.Application.Activity) append("Date", date) @@ -136,7 +160,7 @@ class APRequestServiceImpl( signHeaders = listOf("(request-target)", "date", "host", "digest") ) - return httpClient.post(url) { + val httpResponse = httpClient.post(url) { headers { headers { appendAll(headers) @@ -146,7 +170,23 @@ class APRequestServiceImpl( } setBody(requestBody) contentType(ContentType.Application.Activity) - }.bodyAsText() + } + val bodyAsText = httpResponse.bodyAsText() + logger.debug( + "SUCCESS ActivityPub Request POST status: {} url: {}", + httpResponse.status, + httpResponse.request.url + ) + logger.trace( + """ + |***** BEGIN HTTP Response Trace url: {} ***** + | + |$bodyAsText + | + |***** END HTTP Response TRACE url: {} ***** + """.trimMargin(), url, url + ) + return bodyAsText } companion object {