• 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 
21 import java.io.BufferedReader;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.io.InputStreamReader;
25 import java.nio.charset.StandardCharsets;
26 import java.util.ArrayList;
27 import java.util.HashMap;
28 import java.util.List;
29 import java.util.Optional;
30 
31 /**
32  * 入参白名单获取类
33  *
34  * @since 2022/06/01
35  */
36 public final class ParamsTrustlist {
37     /**
38      * Define generic string
39      */
40     public static final String OPTIONS = " [options]:";
41 
42     /**
43      * Define commond list
44      */
45     private static final List<String> COMMONDS = new ArrayList<String>();
46 
47     /**
48      * Define trust map
49      */
50     private static HashMap<String, List<String>> trustMap = new HashMap<>();
51 
52     static {
53         COMMONDS.add(CmdUtil.Method.GENERATE_KEYPAIR + OPTIONS);
54         COMMONDS.add(CmdUtil.Method.GENERATE_CSR + OPTIONS);
55         COMMONDS.add(CmdUtil.Method.GENERATE_CERT + OPTIONS);
56         COMMONDS.add(CmdUtil.Method.GENERATE_CA + OPTIONS);
57         COMMONDS.add(CmdUtil.Method.GENERATE_APP_CERT + OPTIONS);
58         COMMONDS.add(CmdUtil.Method.GENERATE_PROFILE_CERT + OPTIONS);
59         COMMONDS.add(CmdUtil.Method.SIGN_PROFILE + OPTIONS);
60         COMMONDS.add(CmdUtil.Method.VERIFY_PROFILE + OPTIONS);
61         COMMONDS.add(CmdUtil.Method.SIGN_APP + OPTIONS);
62         COMMONDS.add(CmdUtil.Method.VERIFY_APP + OPTIONS);
63     }
64 
ParamsTrustlist()65     private ParamsTrustlist() {
66     }
67 
68     /**
69      * Generate Trustlist
70      */
generateTrustlist()71     public static void generateTrustlist() {
72         ClassLoader classLoader = ParamsTrustlist.class.getClassLoader();
73         if (classLoader == null) {
74             return ;
75         }
76         String page = "help.txt";
77         try (InputStream inputStream = classLoader.getResourceAsStream(page)) {
78             if (inputStream == null) {
79                 return;
80             }
81             try (InputStreamReader isr = new InputStreamReader(inputStream, StandardCharsets.UTF_8);
82                 BufferedReader br = new BufferedReader(isr)) {
83                 readHelpParam(br);
84             }
85         } catch (IOException e) {
86             CustomException.throwException(ERROR.READ_FILE_ERROR, "Failed to read " + page + " resource");
87         }
88     }
89 
readHelpParam(BufferedReader br)90     private static void readHelpParam(BufferedReader br) throws IOException {
91         String str;
92         String cmdStandBy = null;
93         while ((str = br.readLine()) != null) {
94             String param = str.trim();
95             if (COMMONDS.contains(param)) {
96                 cmdStandBy = param;
97             } else {
98                 putTrustMap(cmdStandBy, param);
99             }
100         }
101     }
102 
103     /**
104      * Put trustlist map
105      *
106      * @param cmdStandBy command as key
107      * @param param commond as value
108      */
putTrustMap(String cmdStandBy, String param)109     private static void putTrustMap(String cmdStandBy, String param) {
110         if (param.startsWith("-")) {
111             String subParam = param.substring(0, param.indexOf(":")).trim();
112             List<String> trustLists = Optional.ofNullable(
113                     trustMap.get(cmdStandBy)).orElse(new ArrayList<>());
114             trustLists.add(subParam);
115             trustMap.put(cmdStandBy, trustLists);
116         }
117     }
118 
119     /**
120      * Get Trustlist
121      *
122      * @param commond commond
123      * @return  TrustList
124      */
getTrustList(String commond)125     public static List<String> getTrustList(String commond) {
126         generateTrustlist();
127         String keyParam = commond + OPTIONS;
128         return Optional.ofNullable(trustMap.get(keyParam)).orElse(new ArrayList<>());
129     }
130 
131 }
132