• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 package com.ohos.hapsigntoolcmd;
17 
18 import com.ohos.hapsigntool.error.CustomException;
19 import com.ohos.hapsigntool.error.ERROR;
20 import com.ohos.hapsigntool.error.SignToolErrMsg;
21 
22 import java.io.BufferedReader;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.io.InputStreamReader;
26 import java.nio.charset.StandardCharsets;
27 import java.util.ArrayList;
28 import java.util.HashMap;
29 import java.util.List;
30 import java.util.Optional;
31 
32 /**
33  * 入参白名单获取类
34  *
35  * @since 2022/06/01
36  */
37 public final class ParamsTrustlist {
38     /**
39      * Define generic string
40      */
41     public static final String OPTIONS = " [options]:";
42 
43     /**
44      * Define commond list
45      */
46     private static final List<String> COMMONDS = new ArrayList<String>();
47 
48     /**
49      * Define trust map
50      */
51     private static HashMap<String, List<String>> trustMap = new HashMap<>();
52 
53     static {
54         COMMONDS.add(CmdUtil.Method.GENERATE_KEYPAIR + OPTIONS);
55         COMMONDS.add(CmdUtil.Method.GENERATE_CSR + OPTIONS);
56         COMMONDS.add(CmdUtil.Method.GENERATE_CERT + OPTIONS);
57         COMMONDS.add(CmdUtil.Method.GENERATE_CA + OPTIONS);
58         COMMONDS.add(CmdUtil.Method.GENERATE_APP_CERT + OPTIONS);
59         COMMONDS.add(CmdUtil.Method.GENERATE_PROFILE_CERT + OPTIONS);
60         COMMONDS.add(CmdUtil.Method.SIGN_PROFILE + OPTIONS);
61         COMMONDS.add(CmdUtil.Method.VERIFY_PROFILE + OPTIONS);
62         COMMONDS.add(CmdUtil.Method.SIGN_APP + OPTIONS);
63         COMMONDS.add(CmdUtil.Method.VERIFY_APP + OPTIONS);
64     }
65 
ParamsTrustlist()66     private ParamsTrustlist() {
67     }
68 
69     /**
70      * Generate Trustlist
71      */
generateTrustlist()72     public static void generateTrustlist() {
73         ClassLoader classLoader = ParamsTrustlist.class.getClassLoader();
74         if (classLoader == null) {
75             return ;
76         }
77         String page = "help.txt";
78         try (InputStream inputStream = classLoader.getResourceAsStream(page)) {
79             if (inputStream == null) {
80                 return;
81             }
82             try (InputStreamReader isr = new InputStreamReader(inputStream, StandardCharsets.UTF_8);
83                 BufferedReader br = new BufferedReader(isr)) {
84                 readHelpParam(br);
85             }
86         } catch (IOException e) {
87             CustomException.throwException(ERROR.READ_FILE_ERROR, SignToolErrMsg.FILE_READ_FAILED.toString(page));
88         }
89     }
90 
readHelpParam(BufferedReader br)91     private static void readHelpParam(BufferedReader br) throws IOException {
92         String str;
93         String cmdStandBy = null;
94         while ((str = br.readLine()) != null) {
95             String param = str.trim();
96             if (COMMONDS.contains(param)) {
97                 cmdStandBy = param;
98             } else {
99                 putTrustMap(cmdStandBy, param);
100             }
101         }
102     }
103 
104     /**
105      * Put trustlist map
106      *
107      * @param cmdStandBy command as key
108      * @param param commond as value
109      */
putTrustMap(String cmdStandBy, String param)110     private static void putTrustMap(String cmdStandBy, String param) {
111         if (param.startsWith("-")) {
112             String subParam = param.substring(0, param.indexOf(":")).trim();
113             List<String> trustLists = Optional.ofNullable(
114                     trustMap.get(cmdStandBy)).orElse(new ArrayList<>());
115             trustLists.add(subParam);
116             trustMap.put(cmdStandBy, trustLists);
117         }
118     }
119 
120     /**
121      * Get Trustlist
122      *
123      * @param commond commond
124      * @return  TrustList
125      */
getTrustList(String commond)126     public static List<String> getTrustList(String commond) {
127         generateTrustlist();
128         String keyParam = commond + OPTIONS;
129         return Optional.ofNullable(trustMap.get(keyParam)).orElse(new ArrayList<>());
130     }
131 
132 }
133