@@ -21,6 +21,10 @@ package com.linkedplanet.kotlininsightclient.sdk
2121
2222import arrow.core.Either
2323import arrow.core.raise.either
24+ import com.atlassian.sal.api.net.Request
25+ import com.atlassian.sal.api.net.Response
26+ import com.atlassian.sal.api.net.TrustedRequest
27+ import com.atlassian.sal.api.net.TrustedRequestFactory
2428import com.linkedplanet.kotlininsightclient.api.error.InsightClientError
2529import com.linkedplanet.kotlininsightclient.api.error.OtherNotFoundError
2630import com.linkedplanet.kotlininsightclient.api.interfaces.InsightAttachmentOperator
@@ -33,7 +37,6 @@ import com.linkedplanet.kotlininsightclient.sdk.util.getOSGiComponent
3337import com.linkedplanet.kotlininsightclient.sdk.util.toISOString
3438import com.riadalabs.jira.plugins.insight.channel.external.api.facade.ObjectFacade
3539import com.riadalabs.jira.plugins.insight.services.model.AttachmentBean
36- import java.io.ByteArrayInputStream
3740import java.io.InputStream
3841import java.io.PipedInputStream
3942import java.io.PipedOutputStream
@@ -51,6 +54,7 @@ object SdkInsightAttachmentOperator : InsightAttachmentOperator {
5154
5255 private val objectFacade: ObjectFacade by getOSGiComponent()
5356 private val attachmentUrlResolver = ReverseEngineeredAttachmentUrlResolver ()
57+ private val trustedRequestFactory: TrustedRequestFactory <* > by getOSGiComponent()
5458
5559 override suspend fun getAttachments (objectId : InsightObjectId ): Either <InsightClientError , List <InsightAttachment >> =
5660 catchAsInsightClientError {
@@ -61,27 +65,32 @@ object SdkInsightAttachmentOperator : InsightAttachmentOperator {
6165
6266 override suspend fun downloadAttachment (url : String ): Either <InsightClientError , InputStream > =
6367 catchAsInsightClientError {
64- val attachmentId = attachmentUrlResolver.parseAttachmentIdFromPathInformation(url)
65- val attachmentBean = objectFacade.loadAttachmentBeanById(attachmentId)
66- // TODO: Replace with REST-based retrieval
67- // val url = "/rest/insight/1.0/object/${objectId}/attachment/${attachmentId}/download"
68- ByteArrayInputStream (ByteArray (0 ))
69- }.mapLeft { OtherNotFoundError (" Attachment download failed for url:$url " ) }
68+ val request: TrustedRequest = trustedRequestFactory.createTrustedRequest(Request .MethodType .GET , url)
69+ request.setConnectionTimeout(15000 )
70+ request.setSoTimeout(30000 )
7071
71- override suspend fun downloadAttachmentZip (objectId : InsightObjectId ): Either <InsightClientError , InputStream > =
72- either {
73- val fileMap = allAttachmentStreamsForInsightObject(objectId).bind()
74- zipInputStreamForMultipleInputStreams(fileMap).bind()
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+ }
79+ }
80+
81+ inputStream ? : throw RuntimeException (" No data returned while accessing attachment at $url " )
82+ }.mapLeft {
83+ OtherNotFoundError (" Attachment download failed for URL: $url — ${it.message} " )
7584 }
7685
77- private fun allAttachmentStreamsForInsightObject (objectId : InsightObjectId ) =
78- catchAsInsightClientError {
79- val attachmentBeans = objectFacade.findAttachmentBeans(objectId.raw)
80- attachmentBeans.map { bean ->
81- // TODO: Replace with REST-based retrieval
82- val attachmentContent = ByteArrayInputStream (ByteArray (0 ))
83- bean.filename to attachmentContent
86+ override suspend fun downloadAttachmentZip (objectId : InsightObjectId ): Either <InsightClientError , InputStream > =
87+ either {
88+ val attachments = getAttachments(objectId).bind()
89+ val fileMap: Map <String , InputStream > = attachments.map { attachment ->
90+ val attachmentContent = downloadAttachment(attachment.url).bind()
91+ attachment.filename to attachmentContent
8492 }.toMap()
93+ zipInputStreamForMultipleInputStreams(fileMap).bind()
8594 }
8695
8796 private fun zipInputStreamForMultipleInputStreams (
@@ -112,8 +121,7 @@ object SdkInsightAttachmentOperator : InsightAttachmentOperator {
112121 Files .copy(inputStream, tempFilePath, StandardCopyOption .REPLACE_EXISTING )
113122 val mimeType = URLConnection .guessContentTypeFromName(filename)
114123 val bean = objectFacade.addAttachmentBean(objectId.raw, tempFilePath.toFile(), filename, mimeType, null )
115- val insightAttachment = beanToInsightAttachment(bean)
116- insightAttachment
124+ beanToInsightAttachment(bean)
117125 }
118126
119127 override suspend fun deleteAttachment (attachmentId : AttachmentId ): Either <InsightClientError , Unit > =
0 commit comments