Skip to content

Commit da0f93f

Browse files
christian-raedelslavisa-baeldung
authored andcommitted
BAEL-86:
- Split controller into interfaces and implementations. - Added ability to up/download files.
1 parent a1a8c29 commit da0f93f

17 files changed

Lines changed: 391 additions & 61 deletions

File tree

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.baeldung.spring.dispatcher.servlet.models;
2+
3+
import java.util.UUID;
4+
5+
public class Attachment {
6+
private String id;
7+
8+
private String name;
9+
10+
private String description;
11+
12+
public Attachment() {
13+
this.id = UUID.randomUUID().toString();
14+
}
15+
16+
public Attachment(String name, String description) {
17+
this();
18+
this.name = name;
19+
this.description = description;
20+
}
21+
22+
public String getId() {
23+
return id;
24+
}
25+
26+
public void setId(String id) {
27+
this.id = id;
28+
}
29+
30+
public String getName() {
31+
return name;
32+
}
33+
34+
public void setName(String name) {
35+
this.name = name;
36+
}
37+
38+
public String getDescription() {
39+
return description;
40+
}
41+
42+
public void setDescription(String description) {
43+
this.description = description;
44+
}
45+
46+
@Override
47+
public boolean equals(Object o) {
48+
if (this == o) return true;
49+
if (o == null || getClass() != o.getClass()) return false;
50+
Attachment that = (Attachment) o;
51+
return id.equals(that.id);
52+
}
53+
54+
@Override
55+
public int hashCode() {
56+
return id.hashCode();
57+
}
58+
}

‎spring-dispatcher-servlet/src/main/java/com/baeldung/spring/dispatcher/servlet/models/Task.java‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,17 @@
33
import org.springframework.format.annotation.DateTimeFormat;
44

55
import java.util.Date;
6+
import java.util.HashSet;
7+
import java.util.Set;
68

79
public class Task {
810
private String description;
911

1012
@DateTimeFormat(pattern = "yyyy-MM-dd'T'hh:mm")
1113
private Date due;
1214

15+
private Set<Attachment> attachments = new HashSet<>();
16+
1317
public Task() {
1418
}
1519

@@ -37,4 +41,12 @@ public Date getDue() {
3741
public void setDue(Date due) {
3842
this.due = due;
3943
}
44+
45+
public Set<Attachment> getAttachments() {
46+
return attachments;
47+
}
48+
49+
public void setAttachments(Set<Attachment> attachments) {
50+
this.attachments = attachments;
51+
}
4052
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.baeldung.spring.dispatcher.servlet.web;
2+
3+
import org.springframework.core.io.FileSystemResource;
4+
import org.springframework.http.ResponseEntity;
5+
import org.springframework.web.bind.annotation.GetMapping;
6+
import org.springframework.web.bind.annotation.PathVariable;
7+
import org.springframework.web.bind.annotation.RequestMapping;
8+
import org.springframework.web.bind.annotation.RequestParam;
9+
10+
@RequestMapping("/attachments")
11+
public interface AttachmentController {
12+
@GetMapping("/{attachmentId}")
13+
ResponseEntity<FileSystemResource> getAttachment(
14+
@PathVariable("attachmentId") String attachmentId,
15+
@RequestParam(name = "download", required = false, defaultValue = "false") boolean forcedDownload
16+
);
17+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.baeldung.spring.dispatcher.servlet.web;
2+
3+
import com.baeldung.spring.dispatcher.servlet.models.Task;
4+
import org.springframework.beans.factory.annotation.Autowired;
5+
import org.springframework.core.io.FileSystemResource;
6+
import org.springframework.http.HttpHeaders;
7+
import org.springframework.http.HttpStatus;
8+
import org.springframework.http.MediaType;
9+
import org.springframework.http.ResponseEntity;
10+
import org.springframework.stereotype.Controller;
11+
import org.springframework.web.bind.annotation.PathVariable;
12+
import org.springframework.web.bind.annotation.RequestParam;
13+
14+
import java.net.URLConnection;
15+
import java.util.Collection;
16+
import java.util.List;
17+
import java.util.Map;
18+
19+
@Controller
20+
public class AttachmentControllerImpl implements AttachmentController {
21+
@Autowired
22+
private Map<String, List<Task>> taskMap;
23+
24+
@Override
25+
public ResponseEntity<FileSystemResource> getAttachment(
26+
@PathVariable("attachmentId") String attachmentId,
27+
@RequestParam(name = "download", required = false, defaultValue = "false") boolean forcedDownload
28+
) {
29+
FileSystemResource resource = new FileSystemResource("/tmp/" + attachmentId);
30+
HttpHeaders headers = new HttpHeaders();
31+
taskMap.values().stream()
32+
.flatMap(Collection::stream)
33+
.flatMap(t -> t.getAttachments().stream())
34+
.filter(a -> a.getId().equals(attachmentId))
35+
.findFirst()
36+
.ifPresent(a -> {
37+
headers.add("Content-Disposition",
38+
"attachment; filename=" + a.getName());
39+
headers.add("Content-Type", forcedDownload ?
40+
MediaType.APPLICATION_OCTET_STREAM_VALUE :
41+
URLConnection.guessContentTypeFromName(a.getName()));
42+
});
43+
return new ResponseEntity<>(resource, headers, HttpStatus.OK);
44+
}
45+
}
Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,13 @@
11
package com.baeldung.spring.dispatcher.servlet.web;
22

3-
import com.baeldung.spring.dispatcher.servlet.models.Task;
4-
import org.springframework.beans.factory.annotation.Autowired;
5-
import org.springframework.stereotype.Controller;
63
import org.springframework.ui.Model;
74
import org.springframework.web.bind.annotation.GetMapping;
85
import org.springframework.web.bind.annotation.RequestMapping;
96

10-
import java.util.List;
11-
import java.util.Map;
12-
import java.util.stream.Collectors;
13-
14-
@Controller
157
@RequestMapping("/")
16-
public class HomeController {
17-
@Autowired
18-
private Map<String, List<Task>> taskMap;
19-
8+
public interface HomeController {
209
@GetMapping("/*")
21-
public String home(Model model) {
22-
List<String> users = taskMap.keySet().stream()
23-
.sorted()
24-
.collect(Collectors.toList());
25-
model.addAttribute("users", users);
26-
return "home";
27-
}
10+
String home(
11+
Model model
12+
);
2813
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.baeldung.spring.dispatcher.servlet.web;
2+
3+
import com.baeldung.spring.dispatcher.servlet.models.Task;
4+
import org.springframework.beans.factory.annotation.Autowired;
5+
import org.springframework.stereotype.Controller;
6+
import org.springframework.ui.Model;
7+
8+
import java.util.List;
9+
import java.util.Map;
10+
import java.util.stream.Collectors;
11+
12+
@Controller
13+
public class HomeControllerImpl implements HomeController {
14+
@Autowired
15+
private Map<String, List<Task>> taskMap;
16+
17+
@Override
18+
public String home(Model model) {
19+
List<String> users = taskMap.keySet().stream()
20+
.sorted()
21+
.collect(Collectors.toList());
22+
model.addAttribute("users", users);
23+
return "home";
24+
}
25+
}
Lines changed: 34 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,49 @@
11
package com.baeldung.spring.dispatcher.servlet.web;
22

33
import com.baeldung.spring.dispatcher.servlet.models.Task;
4-
import org.springframework.beans.factory.annotation.Autowired;
5-
import org.springframework.stereotype.Controller;
64
import org.springframework.ui.Model;
75
import org.springframework.web.bind.annotation.*;
6+
import org.springframework.web.multipart.MultipartFile;
87

9-
import java.util.*;
10-
import java.util.stream.Collectors;
11-
12-
@Controller
138
@RequestMapping("/tasks")
14-
public class TaskController {
15-
@Autowired
16-
private Map<String, List<Task>> taskMap;
17-
9+
public interface TaskController {
1810
@GetMapping("/{username}/list")
19-
public String listForm(
20-
Model model,
21-
@PathVariable("username") String username
22-
) {
23-
List<Task> tasks = taskMap.get(username).stream()
24-
.sorted(Comparator.comparing(Task::getDue))
25-
.collect(Collectors.toList());
26-
model.addAttribute("username", username);
27-
model.addAttribute("tasks", tasks);
28-
return "task-list";
29-
}
11+
String listTasks(
12+
@PathVariable("username") String username,
13+
Model model
14+
);
3015

3116
@GetMapping("/{username}/add")
32-
public String addForm(
33-
Model model,
34-
@PathVariable("username") String username
35-
) {
36-
model.addAttribute("username", username);
37-
model.addAttribute("task", new Task(new Date()));
38-
return "task-add";
39-
}
17+
String addTask(
18+
@PathVariable("username") String username,
19+
Model model
20+
);
4021

4122
@PostMapping("/{username}/add")
42-
public String addSubmit(
23+
String addTask(
4324
@PathVariable("username") String username,
4425
@ModelAttribute Task task
45-
) {
46-
List<Task> taskList = taskMap.get(username);
47-
if (taskList == null) {
48-
taskList = new ArrayList<>();
49-
}
50-
taskList.add(task);
51-
taskMap.put(username, taskList);
52-
return "redirect:list";
53-
}
26+
);
27+
28+
@GetMapping("/{username}/get/{id}")
29+
String getTask(
30+
@PathVariable("username") String username,
31+
@PathVariable("id") int id,
32+
Model model
33+
);
34+
35+
@GetMapping("/{username}/get/{id}/attach")
36+
String attachToTask(
37+
@PathVariable("username") String username,
38+
@PathVariable("id") int id,
39+
Model model
40+
);
41+
42+
@PostMapping("/{username}/get/{id}/attach")
43+
String attachToTask(
44+
@PathVariable("username") String username,
45+
@PathVariable("id") int id,
46+
@RequestParam("attachment") MultipartFile attachment,
47+
@RequestParam("description") String description
48+
);
5449
}

0 commit comments

Comments
 (0)