@@ -154,70 +154,34 @@ void restoreAttachment(AttachmentRestoreEventContext context) {
154154
155155 @ On
156156 void readAttachment (AttachmentReadEventContext context ) {
157+ String contentId = context .getContentId ();
157158 logger .info (
158159 "OS Attachment Service handler called for reading attachment with document id: {}" ,
159- context .getContentId ());
160- try {
161- Future <InputStream > future = osClient .readContent (context .getContentId ());
162- InputStream inputStream = future .get (); // Wait for the content to be read
163- if (inputStream != null ) {
164- // Wrap to convert any checked IOExceptions during reads into UncheckedIOException,
165- // so we can map them to ServiceException consistently without closing the stream here.
166- InputStream safeInputStream =
167- new java .io .FilterInputStream (inputStream ) {
168- @ Override
169- public int read () {
170- try {
171- return super .read ();
172- } catch (java .io .IOException e ) {
173- throw new java .io .UncheckedIOException (e );
174- }
175- }
160+ contentId );
176161
177- @ Override
178- public int read (byte [] b , int off , int len ) {
179- try {
180- return super .read (b , off , len );
181- } catch (java .io .IOException e ) {
182- throw new java .io .UncheckedIOException (e );
183- }
184- }
185-
186- @ Override
187- public int read (byte [] b ) {
188- try {
189- return super .read (b );
190- } catch (java .io .IOException e ) {
191- throw new java .io .UncheckedIOException (e );
192- }
193- }
194- };
162+ try {
163+ // Get the stream from the client (blocking wait)
164+ InputStream inputStream = osClient .readContent (contentId ).get ();
195165
196- try {
197- // Do not close the stream here; CAP runtime will stream and close it
198- context .getData ().setContent (safeInputStream );
199- } catch (RuntimeException rte ) {
200- if (rte instanceof java .io .UncheckedIOException ) {
201- throw new ServiceException (
202- "Failed to read file with document id {}" , context .getContentId (), rte .getCause ());
203- }
204- throw rte ;
205- }
166+ if (inputStream != null ) {
167+ // Pass the open stream directly.
168+ // CAP runtime will stream it to the client and close it automatically.
169+ context .getData ().setContent (inputStream );
206170 } else {
207- logger .error ("Document not found for id {}" , context . getContentId () );
171+ logger .error ("Document not found for id {}" , contentId );
208172 throw new AttachmentNotFoundException (
209- "Document not found for id " + context . getContentId (), context . getContentId () );
173+ "Document not found for id " + contentId , contentId );
210174 }
211175 } catch (InterruptedException ex ) {
212176 Thread .currentThread ().interrupt ();
213177 throw new ServiceException (
214- "Failed to read file with document id {}" , context . getContentId () , ex );
178+ "Failed to read file with document id {}" , contentId , ex );
215179 } catch (AttachmentNotFoundException ex ) {
216180 throw new ServiceException (
217- "Attachment not found with document id {}" , context . getContentId () , ex );
181+ "Attachment not found with document id {}" , contentId , ex );
218182 } catch (ObjectStoreServiceException | ExecutionException ex ) {
219183 throw new ServiceException (
220- "Failed to read file with document id {}" , context . getContentId () , ex );
184+ "Failed to read file with document id {}" , contentId , ex );
221185 } finally {
222186 context .setCompleted ();
223187 }
0 commit comments