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