2
2
3
3
import com .reedelk .file .internal .attribute .FileAttribute ;
4
4
import com .reedelk .file .internal .exception .FileDeleteException ;
5
+ import com .reedelk .file .internal .exception .NotValidFileException ;
5
6
import com .reedelk .runtime .api .annotation .*;
6
7
import com .reedelk .runtime .api .component .ProcessorSync ;
7
8
import com .reedelk .runtime .api .flow .FlowContext ;
8
9
import com .reedelk .runtime .api .message .Message ;
9
- import com .reedelk .runtime .api .message .MessageAttributes ;
10
10
import com .reedelk .runtime .api .message .MessageBuilder ;
11
+ import com .reedelk .runtime .api .message .content .MimeType ;
11
12
import com .reedelk .runtime .api .script .ScriptEngineService ;
12
13
import com .reedelk .runtime .api .script .dynamicvalue .DynamicString ;
13
14
import org .osgi .service .component .annotations .Component ;
14
15
import org .osgi .service .component .annotations .Reference ;
15
16
import org .osgi .service .component .annotations .ServiceScope ;
16
17
17
18
import java .nio .file .Files ;
19
+ import java .nio .file .Path ;
18
20
import java .nio .file .Paths ;
19
21
import java .util .Optional ;
20
22
21
23
import static com .reedelk .file .internal .commons .Messages .FileDelete .ERROR_FILE_DELETE ;
24
+ import static com .reedelk .file .internal .commons .Messages .FileDelete .FILE_NAME_ERROR ;
22
25
import static com .reedelk .runtime .api .commons .ComponentPrecondition .Configuration .requireNotNull ;
23
26
24
27
@ ModuleComponent ("File Delete" )
28
+ @ ComponentOutput (
29
+ attributes = FileAttribute .class ,
30
+ payload = String .class ,
31
+ description = "The path and name of the deleted file." )
25
32
@ Description ("Deletes a file from the file system with the given File name. " +
26
33
"An error is raised if the given file could not be found. " +
27
34
"The file name can be a dynamic expression." )
@@ -45,23 +52,25 @@ public void initialize() {
45
52
@ Override
46
53
public Message apply (FlowContext flowContext , Message message ) {
47
54
return service .evaluate (fileName , flowContext , message ).flatMap (evaluatedFileNameToRemove -> {
55
+ Path filePathToDelete ;
48
56
try {
49
- Files .delete (Paths .get (evaluatedFileNameToRemove ));
57
+ filePathToDelete = Paths .get (evaluatedFileNameToRemove );
58
+ Files .delete (filePathToDelete );
50
59
} catch (Exception exception ) {
51
60
String errorMessage = ERROR_FILE_DELETE .format (exception .getMessage ());
52
61
throw new FileDeleteException (errorMessage , exception );
53
62
}
54
63
55
- MessageAttributes attributes = new FileAttribute (evaluatedFileNameToRemove );
64
+ FileAttribute attributes = new FileAttribute (evaluatedFileNameToRemove );
56
65
57
66
Message outMessage = MessageBuilder .get (FileDelete .class )
58
67
.attributes (attributes )
59
- .empty ( )
68
+ .withString ( filePathToDelete . toString (), MimeType . TEXT_PLAIN )
60
69
.build ();
61
70
62
71
return Optional .of (outMessage );
63
72
64
- }).orElse ( MessageBuilder . get ( FileDelete . class ). empty (). build ( ));
73
+ }).orElseThrow (() -> new NotValidFileException ( FILE_NAME_ERROR . format ( fileName . toString ()) ));
65
74
}
66
75
67
76
public void setFileName (DynamicString fileName ) {
0 commit comments