12
12
*/
13
13
package com .netflix .conductor .core .storage ;
14
14
15
- import java .io .ByteArrayInputStream ;
15
+ import java .io .File ;
16
+ import java .io .FileInputStream ;
17
+ import java .io .FileOutputStream ;
18
+ import java .io .IOException ;
16
19
import java .io .InputStream ;
17
- import java .util . Map ;
18
- import java .util .concurrent . ConcurrentHashMap ;
20
+ import java .nio . file . Files ;
21
+ import java .util .UUID ;
19
22
23
+ import org .apache .commons .io .IOUtils ;
20
24
import org .slf4j .Logger ;
21
25
import org .slf4j .LoggerFactory ;
22
26
23
27
import com .netflix .conductor .common .run .ExternalStorageLocation ;
24
28
import com .netflix .conductor .common .utils .ExternalPayloadStorage ;
25
29
30
+ import com .fasterxml .jackson .databind .ObjectMapper ;
31
+
26
32
/**
27
33
* A dummy implementation of {@link ExternalPayloadStorage} used when no external payload is
28
34
* configured
@@ -31,40 +37,64 @@ public class DummyPayloadStorage implements ExternalPayloadStorage {
31
37
32
38
private static final Logger LOGGER = LoggerFactory .getLogger (DummyPayloadStorage .class );
33
39
34
- private final Map <String , byte []> dummyDataStore = new ConcurrentHashMap <>();
40
+ private ObjectMapper objectMapper ;
41
+ private File payloadDir ;
35
42
36
- private static final String DUMMY_DATA_STORE_KEY = "DUMMY_PAYLOAD_STORE_KEY" ;
43
+ public DummyPayloadStorage () {
44
+ try {
45
+ this .objectMapper = new ObjectMapper ();
46
+ this .payloadDir = Files .createTempDirectory ("payloads" ).toFile ();
47
+ LOGGER .info (
48
+ "{} initialized in directory: {}" ,
49
+ this .getClass ().getSimpleName (),
50
+ payloadDir .getAbsolutePath ());
51
+ } catch (IOException ioException ) {
52
+ LOGGER .error (
53
+ "Exception encountered while creating payloads directory : {}" ,
54
+ ioException .getMessage ());
55
+ }
56
+ }
37
57
38
58
@ Override
39
59
public ExternalStorageLocation getLocation (
40
60
Operation operation , PayloadType payloadType , String path ) {
41
- ExternalStorageLocation externalStorageLocation = new ExternalStorageLocation ();
42
- externalStorageLocation .setPath (path != null ? path : " " );
43
- return externalStorageLocation ;
61
+ ExternalStorageLocation location = new ExternalStorageLocation ();
62
+ location .setPath (path + UUID . randomUUID () + ".json " );
63
+ return location ;
44
64
}
45
65
46
66
@ Override
47
67
public void upload (String path , InputStream payload , long payloadSize ) {
68
+ File file = new File (payloadDir , path );
69
+ String filePath = file .getAbsolutePath ();
48
70
try {
49
- final byte [] payloadBytes = new byte [(int ) payloadSize ];
50
- final int bytesRead = payload .read (new byte [(int ) payloadSize ]);
51
-
52
- if (bytesRead > 0 ) {
53
- dummyDataStore .put (
54
- path == null || path .isEmpty () ? DUMMY_DATA_STORE_KEY : path , payloadBytes );
71
+ if (!file .exists () && file .createNewFile ()) {
72
+ LOGGER .debug ("Created file: {}" , filePath );
73
+ }
74
+ IOUtils .copy (payload , new FileOutputStream (file ));
75
+ LOGGER .debug ("Written to {}" , filePath );
76
+ } catch (IOException e ) {
77
+ // just handle this exception here and return empty map so that test will fail in case
78
+ // this exception is thrown
79
+ LOGGER .error ("Error writing to {}" , filePath );
80
+ } finally {
81
+ try {
82
+ if (payload != null ) {
83
+ payload .close ();
84
+ }
85
+ } catch (IOException e ) {
86
+ LOGGER .warn ("Unable to close input stream when writing to file" );
55
87
}
56
- } catch (Exception e ) {
57
- LOGGER .error ("Error encountered while uploading payload {}" , e .getMessage ());
58
88
}
59
89
}
60
90
61
91
@ Override
62
92
public InputStream download (String path ) {
63
- final byte [] data =
64
- dummyDataStore . get ( path == null || path . isEmpty () ? DUMMY_DATA_STORE_KEY : path );
65
- if ( data != null ) {
66
- return new ByteArrayInputStream ( data );
67
- } else {
93
+ try {
94
+ LOGGER . debug ( "Reading from {}" , path );
95
+ return new FileInputStream ( new File ( payloadDir , path ));
96
+ } catch ( IOException e ) {
97
+ LOGGER . error ( "Error reading {}" , path , e );
68
98
return null ;
69
99
}
70
100
}
0 commit comments