Skip to content

Commit ff22722

Browse files
committed
fix attachment downloads
1 parent d915384 commit ff22722

File tree

2 files changed

+30
-17
lines changed

2 files changed

+30
-17
lines changed

kotlin-insight-client/kotlin-insight-client-sdk/src/main/kotlin/com/linkedplanet/kotlininsightclient/sdk/SdkInsightAttachmentOperator.kt

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,25 @@ package com.linkedplanet.kotlininsightclient.sdk
2121

2222
import arrow.core.Either
2323
import arrow.core.raise.either
24+
import com.atlassian.jira.security.JiraAuthenticationContext
2425
import com.atlassian.sal.api.net.Request
2526
import com.atlassian.sal.api.net.Response
2627
import com.atlassian.sal.api.net.TrustedRequest
2728
import com.atlassian.sal.api.net.TrustedRequestFactory
29+
import com.linkedplanet.kotlininsightclient.api.error.HttpInsightClientError
2830
import com.linkedplanet.kotlininsightclient.api.error.InsightClientError
29-
import com.linkedplanet.kotlininsightclient.api.error.OtherNotFoundError
3031
import com.linkedplanet.kotlininsightclient.api.interfaces.InsightAttachmentOperator
3132
import com.linkedplanet.kotlininsightclient.api.model.AttachmentId
3233
import com.linkedplanet.kotlininsightclient.api.model.InsightAttachment
3334
import com.linkedplanet.kotlininsightclient.api.model.InsightObjectId
3435
import com.linkedplanet.kotlininsightclient.sdk.services.ReverseEngineeredAttachmentUrlResolver
3536
import com.linkedplanet.kotlininsightclient.sdk.util.catchAsInsightClientError
37+
import com.linkedplanet.kotlininsightclient.sdk.util.getComponent
3638
import com.linkedplanet.kotlininsightclient.sdk.util.getOSGiComponent
3739
import com.linkedplanet.kotlininsightclient.sdk.util.toISOString
3840
import com.riadalabs.jira.plugins.insight.channel.external.api.facade.ObjectFacade
3941
import com.riadalabs.jira.plugins.insight.services.model.AttachmentBean
42+
import org.apache.http.client.utils.URIBuilder
4043
import java.io.InputStream
4144
import java.io.PipedInputStream
4245
import java.io.PipedOutputStream
@@ -53,6 +56,7 @@ import kotlin.io.path.createTempFile
5356
object SdkInsightAttachmentOperator : InsightAttachmentOperator {
5457

5558
private val objectFacade: ObjectFacade by getOSGiComponent()
59+
private val jiraAuthenticationContext: JiraAuthenticationContext by getComponent()
5660
private val attachmentUrlResolver = ReverseEngineeredAttachmentUrlResolver()
5761
private val trustedRequestFactory: TrustedRequestFactory<*> by getOSGiComponent()
5862

@@ -64,23 +68,32 @@ object SdkInsightAttachmentOperator : InsightAttachmentOperator {
6468
}
6569

6670
override suspend fun downloadAttachment(url: String): Either<InsightClientError, InputStream> =
67-
catchAsInsightClientError {
68-
val request: TrustedRequest = trustedRequestFactory.createTrustedRequest(Request.MethodType.GET, url)
69-
request.setConnectionTimeout(15000)
70-
request.setSoTimeout(30000)
71-
72-
var inputStream: InputStream? = null
73-
request.execute { response: Response ->
74-
if (response.statusCode == 200) {
75-
inputStream = response.responseBodyAsStream
76-
} else {
77-
throw RuntimeException("HTTP ${response.statusCode} while accessing attachment at $url")
78-
}
71+
either {
72+
val request = trustedGetRequestForCurrentUser(url).bind()
73+
val response = Either.catch { request.executeAndReturn<Response> { it } as Response }
74+
.mapLeft { downloadFailed(500, url) }.bind()
75+
if (response.isSuccessful) {
76+
response.responseBodyAsStream
7977
}
78+
raise(downloadFailed(response.statusCode, url))
79+
}
80+
81+
private fun downloadFailed(statusCode: Int, url: String, ) = HttpInsightClientError(
82+
statusCode = statusCode,
83+
message = "Anhang Download fehlgeschlagen für URL: $url"
84+
)
8085

81-
inputStream ?: throw RuntimeException("No data returned while accessing attachment at $url")
86+
private fun trustedGetRequestForCurrentUser(url: String): Either<InsightClientError, TrustedRequest> =
87+
Either.catch {
88+
trustedRequestFactory.createTrustedRequest(Request.MethodType.GET, url).apply {
89+
addTrustedTokenAuthentication(URIBuilder(url).host, jiraAuthenticationContext.loggedInUser.username)
90+
setHeader("Content-Type", "application/json")
91+
}
8292
}.mapLeft {
83-
OtherNotFoundError("Attachment download failed for URL: $url${it.message}")
93+
HttpInsightClientError(
94+
statusCode = 500,
95+
message = "Es konnte keine Verbindung zu Assets erzeugt werden."
96+
)
8497
}
8598

8699
override suspend fun downloadAttachmentZip(objectId: InsightObjectId): Either<InsightClientError, InputStream> =

kotlin-insight-client/kotlin-insight-client-test-base/src/main/kotlin/com/linkedplanet/kotlininsightclient/InsightAttachmentOperatorTest.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ interface InsightAttachmentOperatorTest {
103103
@Test
104104
fun attachmentTestGetAttachmentsForNotExistingObject() = runBlocking {
105105
val responseError = insightAttachmentOperator.getAttachments(InsightObjectId.notPersistedObjectId).asError()
106-
assertThat(responseError.message, containsString("-1"))
106+
assertThat(responseError.error, containsString("Insight-Fehler"))
107107
}
108108

109109
@Test
@@ -167,7 +167,7 @@ interface InsightAttachmentOperatorTest {
167167
@Test
168168
fun attachmentTestDownloadZipForNotExistingObject() = runBlocking {
169169
val responseError = insightAttachmentOperator.downloadAttachmentZip(InsightObjectId.notPersistedObjectId).asError()
170-
assertThat(responseError.message, containsString("-1"))
170+
assertThat(responseError.error, containsString("Insight-Fehler"))
171171
}
172172

173173
}

0 commit comments

Comments
 (0)