AdminUserProgramFileResource(AdminUserProgramFileService)   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 3
c 1
b 0
f 0
rs 10
cc 1
1
package com.osomapps.pt.admin.user;
2
3
import java.io.IOException;
4
import javax.servlet.http.HttpServletResponse;
5
import org.springframework.beans.factory.annotation.Autowired;
6
import org.springframework.util.FastByteArrayOutputStream;
7
import org.springframework.web.bind.annotation.GetMapping;
8
import org.springframework.web.bind.annotation.PathVariable;
9
import org.springframework.web.bind.annotation.RequestMapping;
10
import org.springframework.web.bind.annotation.ResponseBody;
11
import org.springframework.web.bind.annotation.RestController;
12
13
@RestController
14
@RequestMapping("api/v1/admin/user-program-file")
15
class AdminUserProgramFileResource {
16
17
    private final AdminUserProgramFileService userProgramFileService;
18
19
    @Autowired
20
    AdminUserProgramFileResource(AdminUserProgramFileService userProgramFileService) {
21
        this.userProgramFileService = userProgramFileService;
22
    }
23
24
    @GetMapping(value = "{id}/{fileName}")
25
    @ResponseBody
26
    Object findOne(
27
            @PathVariable Long id, @PathVariable String fileName, HttpServletResponse response)
28
            throws IOException {
29
        try (FastByteArrayOutputStream outputStream = new FastByteArrayOutputStream()) {
30
            final ProgramResponseDTO programResponseDTO =
31
                    userProgramFileService.createXlsx(id, outputStream);
32
            response.setContentType(programResponseDTO.getFileType());
33
            response.setHeader(
34
                    "Content-disposition",
35
                    "attachment; filename=" + programResponseDTO.getFileName());
36
            outputStream.writeTo(response.getOutputStream());
37
            response.getOutputStream().close();
38
            outputStream.reset();
39
        }
40
        return null;
41
    }
42
}
43