SECARSP-306 *Implement AuditTrail APIs for Dashboard: *Get device audit reports:...
[platform/core/security/suspicious-activity-monitor.git] / server / samserver / src / main / java / com / samsung / samserver / web / rest / service / ui / GetDeviceReportsRestService.java
1 /*
2  * In Samsung Ukraine R&D Center (SRK under a contract between)
3  * LLC "Samsung Electronics Co", Ltd (Seoul, Republic of Korea)
4  * Copyright (C) 2018 Samsung Electronics Co., Ltd. All rights reserved.
5  */
6 package com.samsung.samserver.web.rest.service.ui;
7
8 import com.fasterxml.jackson.databind.ObjectMapper;
9 import com.samsung.samserver.domain.report.*;
10 import com.samsung.samserver.service.*;
11 import com.samsung.samserver.web.rest.errors.DeviceServiceError;
12 import org.springframework.beans.factory.annotation.Autowired;
13 import org.springframework.stereotype.Service;
14 import org.springframework.web.bind.annotation.PathVariable;
15 import org.springframework.web.bind.annotation.RequestParam;
16 import org.springframework.http.*;
17 import org.slf4j.*;
18
19 import javax.servlet.http.HttpServletResponse;
20 import java.io.*;
21 import java.text.SimpleDateFormat;
22 import java.util.*;
23 import java.util.zip.*;
24
25 import static com.samsung.samserver.web.rest.service.vm.DReport.DReportUserEmb.convert;
26 import static com.samsung.samserver.web.rest.service.vm.DReport.DReportSyscallEmb.convert;
27
28 /**
29  * REST service implementation for get device reports.
30  *
31  * @author <A HREF="mailto:m.dalakov@samsung.com">Mykhailo Dalakov</A>
32  * @version 1.0
33  */
34 @Service
35 public class GetDeviceReportsRestService {
36
37     private final Logger log = LoggerFactory.getLogger(GetDeviceReportsRestService.class);
38
39     @Autowired
40     private DeviceReportsDataService deviceReportsDataService;
41
42     @Autowired
43     private ReportService reportService;
44
45     private byte[] getZipCompressed(String[] filename, byte[][] in) throws IOException {
46         int bufSize = 2048;
47         byte[] data = new byte[bufSize];
48         try (ByteArrayOutputStream bos = new ByteArrayOutputStream(); ZipOutputStream zos = new ZipOutputStream(bos)) {
49             for (int i=0; i<filename.length; i++) {
50                 try (BufferedInputStream bis = new BufferedInputStream(new ByteArrayInputStream(in[i]), bufSize)) {
51                     ZipEntry entry = new ZipEntry(filename[i]);
52                     zos.putNextEntry(entry);
53                     int count;
54                     while ((count = bis.read(data, 0, bufSize)) != -1) {
55                         zos.write(data, 0, count);
56                     }
57                     zos.closeEntry();
58                 }
59             }
60             zos.flush();
61             zos.finish();
62             return bos.toByteArray();
63         }
64     }
65
66     public ResponseEntity<String> getDeviceReports(@RequestParam(value = "id") Long id,
67                                                    @RequestParam(value = "from") Integer from,
68                                                    @RequestParam(value = "to") Integer to) throws IOException {
69         int currTime = (int) (System.currentTimeMillis()/1000);
70         to = (to < currTime) ? to : currTime;
71         log.debug("UI request get device reports: {}, {}, {}", id, from, to);
72         Optional<DeviceReportsData> dr =  deviceReportsDataService.findOne(id, from, to);
73         if (!dr.isPresent()) {
74             List<ReportSyscall> reportSyscallList = reportService.findReportSyscall(id, from, to);
75             List<ReportUser> reportUserList = reportService.findReportUser(id, from, to);
76             ObjectMapper mapper = new ObjectMapper();
77             byte[] reportSyscall = mapper.writeValueAsBytes(convert(reportSyscallList));
78             byte[] reportUser = mapper.writeValueAsBytes(convert(reportUserList));
79             byte [] out = getZipCompressed(new String[]{"syscall","user"}, new byte[][]{reportSyscall,reportUser});
80             dr = Optional.of(deviceReportsDataService.save(new DeviceReportsData().data(out), id, from, to));
81         }
82         SimpleDateFormat formatter = new SimpleDateFormat("MMM-dd-yyyy");
83         String uri = "/audit/reports/"
84                 + Base64.getEncoder().encodeToString(("Reports["
85                 + formatter.format(new Date((long)from*1000))
86                 +" - "
87                 + formatter.format(new Date((long)to*1000))
88                 +"].zip").getBytes())
89                 + "?ruid="+dr.get().getRuid();
90         String out = "{\n" +
91                 "\"uri\": \""+uri+"\"\n" +
92                 "}";
93         return new ResponseEntity<>(out, new HttpHeaders(), HttpStatus.OK);
94     }
95
96     public HttpEntity<byte[]> loadReportsData(HttpServletResponse response, @PathVariable("name") String name,
97                                               @RequestParam(value = "ruid") String ruid){
98         log.debug("request to load reports Data: {}", ruid);
99         Optional<DeviceReportsData> dr = deviceReportsDataService.findOne(ruid);
100         if (!dr.isPresent()) {
101             throw new DeviceServiceError.ReportsDataNotFoundException();
102         }
103         HttpHeaders headers = new HttpHeaders();
104         headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
105         response.setHeader("Content-Disposition", "attachment; filename=" + new String(Base64.getDecoder().decode(name)));
106         return new HttpEntity<>(dr.get().getData(), headers);
107     }
108
109 }