Roberto Sánchez
2014-02-24 3de8fa2128c740e131676683dec649bb44c8ea73
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package net.curisit.securis;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import net.curisit.securis.beans.RequestBean;
import net.curisit.securis.utils.JsonUtils;
import net.curisit.securis.utils.LicUtils;
import org.apache.commons.io.IOUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class ReqGenerator {
   private static final Logger log = LogManager.getLogger(ReqGenerator.class);
   private static ReqGenerator singleton = new ReqGenerator();
   private byte[] LOGO_SECRET;
   private ReqGenerator() {
       try {
           LOGO_SECRET = "Logo ipsum s3cr3t test áíóú".getBytes("utf-8");
       } catch (UnsupportedEncodingException e) {
           e.printStackTrace();
       }
   }
   public static ReqGenerator getInstance() {
       return singleton;
   }
   public RequestBean createRequest(String appCode, String customerCode) throws SeCurisException {
       RequestBean req = new RequestBean();
       req.setAppCode(appCode);
       req.setCustomerCode(customerCode);
       req.setArch(HWInfo.getArch());
       req.setCrcLogo(getCrcLogo());
       req.setMacAddresses(HWInfo.getMACAddress());
       req.setOsName(HWInfo.getOsName());
       return req;
   }
   public RequestBean loadRequest(File requestFile) throws SeCurisException {
       try {
           String json = new String(Files.readAllBytes(Paths.get(requestFile.toURI())), "utf-8");
           RequestBean req = JsonUtils.json2object(json, RequestBean.class);
           return req;
       } catch (IOException e) {
           log.error("Request file {} was not found or is not accesible");
           throw new SeCurisException("ERROR accesing request file: " + requestFile.getAbsolutePath(), e);
       }
   }
   /**
    * Generate a request file using a {@link RequestBean}
    * 
    * @param req
    * @param file
    * @throws SeCurisException
    */
   public void save(RequestBean req, File file) throws SeCurisException {
       byte[] json;
       try {
           json = JsonUtils.toJSON(req, true).getBytes("utf-8");
           Files.write(Paths.get(file.toURI()), json, StandardOpenOption.CREATE);
       } catch (UnsupportedEncodingException e) {
           log.error("Error creating json doc from request: " + req, e);
           throw new SeCurisException("Error creating json doc from request: " + req, e);
       } catch (IOException e) {
           log.error("Error creating request file: " + file, e);
           throw new SeCurisException("Error creating request file: " + file, e);
       }
       log.info("License saved in {}", file);
   }
   private String getCrcLogo() {
       String logResource = "images/logo_customer.png";
       InputStream is = getClass().getClassLoader().getResourceAsStream(logResource);
       try {
           String shaLogo = LicUtils.sha256(IOUtils.toByteArray(is), LOGO_SECRET);
           return shaLogo;
       } catch (IOException e) {
           log.error("Customer logo was not found in classpath in " + logResource, e);
           return null;
       }
   }
}