Skip to content

Commit 8bae55a

Browse files
committed
feat: add FilesController and FilesService
1 parent ee782fe commit 8bae55a

File tree

8 files changed

+154
-0
lines changed

8 files changed

+154
-0
lines changed

quartz-manager-parent/quartz-manager-web-showcase/pom.xml

+5
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,11 @@
112112
<groupId>org.apache.commons</groupId>
113113
<artifactId>commons-lang3</artifactId>
114114
</dependency>
115+
<dependency>
116+
<groupId>org.python</groupId>
117+
<artifactId>jython-slim</artifactId>
118+
<version>2.7.3b1</version>
119+
</dependency>
115120

116121
<!-- TEST -->
117122
<dependency>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package it.fabioformosa.quartzmanager.controllers;
2+
3+
import it.fabioformosa.quartzmanager.services.FileService;
4+
import lombok.extern.slf4j.Slf4j;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.http.MediaType;
7+
import org.springframework.http.ResponseEntity;
8+
import org.springframework.web.bind.annotation.PostMapping;
9+
import org.springframework.web.bind.annotation.RequestMapping;
10+
import org.springframework.web.bind.annotation.RequestParam;
11+
import org.springframework.web.bind.annotation.RestController;
12+
import org.springframework.web.multipart.MultipartFile;
13+
14+
@Slf4j
15+
@RestController
16+
@RequestMapping("/files")
17+
public class FilesController {
18+
19+
@Autowired
20+
private FileService fileService;
21+
22+
public FilesController() {
23+
}
24+
25+
@PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
26+
public ResponseEntity<String> handleFileUpload(@RequestParam("file") MultipartFile file) {
27+
28+
try {
29+
fileService.uploadFile(file);
30+
// Return a success message
31+
return ResponseEntity.ok("File uploaded successfully!");
32+
} catch (Exception e) {
33+
// Handle file IO exceptions
34+
System.out.println(e.getMessage());
35+
return ResponseEntity.status(500).body("Failed to upload the file");
36+
}
37+
}
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package it.fabioformosa.quartzmanager.jobs.myjobs;
2+
3+
import it.fabioformosa.quartzmanager.api.jobs.AbstractQuartzManagerJob;
4+
import it.fabioformosa.quartzmanager.api.jobs.entities.LogRecord;
5+
import org.quartz.JobExecutionContext;
6+
7+
import javax.script.ScriptContext;
8+
import javax.script.ScriptEngine;
9+
import javax.script.ScriptEngineManager;
10+
import javax.script.SimpleScriptContext;
11+
import java.io.File;
12+
import java.io.FileReader;
13+
import java.io.StringWriter;
14+
15+
public class PythonJob extends AbstractQuartzManagerJob {
16+
@Override
17+
public LogRecord doIt(JobExecutionContext jobExecutionContext) {
18+
StringWriter writer = new StringWriter();
19+
ScriptContext context = new SimpleScriptContext();
20+
context.setWriter(writer);
21+
22+
ScriptEngineManager manager = new ScriptEngineManager();
23+
ScriptEngine engine = manager.getEngineByName("python");
24+
// 1. Load as File
25+
File file = new File(jobExecutionContext.getTrigger().getJobDataMap().getString("file"));
26+
try {
27+
engine.eval(new FileReader(file), context);
28+
return new LogRecord(LogRecord.LogType.INFO, writer.toString().trim());
29+
} catch (Exception e) {
30+
throw new RuntimeException(e);
31+
}
32+
}
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package it.fabioformosa.quartzmanager.services;
2+
3+
import lombok.extern.slf4j.Slf4j;
4+
import org.springframework.beans.factory.annotation.Value;
5+
import org.springframework.stereotype.Service;
6+
import org.springframework.web.multipart.MultipartFile;
7+
8+
import java.io.File;
9+
import java.nio.file.Path;
10+
import java.nio.file.Paths;
11+
12+
@Slf4j
13+
@Service
14+
public class FileService {
15+
String uploadsDirectory;
16+
public FileService(@Value("${chista-scheduler.uploadFolder}") String uploadsDirectory) {
17+
this.uploadsDirectory = uploadsDirectory;
18+
createUploadsFolderIfNotExists();
19+
}
20+
21+
public String getUploadsDirectory() {
22+
return this.uploadsDirectory;
23+
}
24+
25+
public void uploadFile(MultipartFile file) throws Exception {
26+
if (file.isEmpty()) {
27+
throw new Exception("Please select a file to upload");
28+
}
29+
// Get the file's original name
30+
String originalFileName = file.getOriginalFilename();
31+
32+
// Construct the path where you want to save the file
33+
Path filePath = Paths.get(uploadsDirectory, originalFileName);
34+
35+
// Save the file to the specified location
36+
file.transferTo(filePath.toFile());
37+
}
38+
39+
private void createUploadsFolderIfNotExists() {
40+
File uploadDir = new File(uploadsDirectory);
41+
if (!uploadDir.exists()) {
42+
uploadDir.mkdir();
43+
}
44+
}
45+
}

quartz-manager-parent/quartz-manager-web-showcase/src/main/resources/application.yml

+7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
chista-scheduler:
2+
uploadFolder: /Users/midhundarvin/workplace/uploads
13
quartz-manager:
24
jobClassPackages: it.fabioformosa.quartzmanager.jobs
35
oas:
@@ -29,6 +31,11 @@ spring:
2931
cache: false
3032
mode: LEGACYHTML5
3133
jpa.open-in-view: false
34+
servlet:
35+
# application.properties
36+
multipart:
37+
max-file-size: 10MB
38+
max-request-size: 10MB
3239

3340
logging:
3441
level:
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
org.quartz.scheduler.instanceName=example
2+
org.quartz.scheduler.isAutoStartup=true
23
org.quartz.threadPool.threadCount=1

quartz-manager-parent/quartz-manager-web-showcase/src/test/java/it/fabioformosa/QuartManagerApplicationTests.java

+24
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,17 @@
44
import org.springframework.boot.test.context.SpringBootTest;
55
import org.springframework.test.context.web.WebAppConfiguration;
66

7+
import javax.script.ScriptContext;
8+
import javax.script.ScriptEngine;
9+
import javax.script.ScriptEngineManager;
10+
import javax.script.SimpleScriptContext;
11+
import java.io.FileReader;
12+
import java.io.StringWriter;
13+
import java.net.URL;
14+
import java.nio.file.Paths;
15+
16+
import static org.springframework.test.util.AssertionErrors.assertEquals;
17+
718
@SpringBootTest(classes = QuartzManagerDemoApplication.class)
819
@WebAppConfiguration
920
class QuartManagerApplicationTests {
@@ -12,4 +23,17 @@ class QuartManagerApplicationTests {
1223
void contextLoads() {
1324
}
1425

26+
@Test
27+
public void givenPythonScriptEngineIsAvailable_whenScriptInvoked_thenOutputDisplayed() throws Exception {
28+
StringWriter writer = new StringWriter();
29+
ScriptContext context = new SimpleScriptContext();
30+
context.setWriter(writer);
31+
32+
ScriptEngineManager manager = new ScriptEngineManager();
33+
ScriptEngine engine = manager.getEngineByName("python");
34+
URL resource = getClass().getClassLoader().getResource("hello.py");
35+
engine.eval(new FileReader(Paths.get(resource.toURI()).toFile()), context);
36+
assertEquals("Should contain script output: ", "Hello Baeldung Readers!!", writer.toString().trim());
37+
}
38+
1539
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
print("Hello Baeldung Readers!!")

0 commit comments

Comments
 (0)