Merge from ECOMP's repository
[vid.git] / vid-app-common / src / main / java / org / onap / vid / aai / util / JettyObfuscationConversionCommandLineUtil.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.vid.aai.util;
22
23
24 import org.apache.commons.cli.*;
25 import org.eclipse.jetty.util.security.Password;
26
27
28 public class JettyObfuscationConversionCommandLineUtil {
29         /**
30          * The main method.
31          *
32          * @param args the arguments
33          */
34         public static void main(String[] args){
35                 Options options = new Options();
36                 options.addOption("e", true, "obfuscate the given string");
37                 options.addOption("d", true, "deobfuscate the given string");
38                 
39                 CommandLineParser parser = new BasicParser();
40                 
41                 try {
42                         CommandLine cmd = parser.parse(options, args);
43                         String toProcess = null;
44                         
45                         if (cmd.hasOption("e")){
46                                 toProcess = cmd.getOptionValue("e");
47                                 String encoded = Password.obfuscate(toProcess);
48                                 System.out.println(encoded);
49                         } else if (cmd.hasOption("d")) {
50                                 toProcess = cmd.getOptionValue("d");
51                                 String decodedStr = Password.deobfuscate(toProcess);
52                                 System.out.println(decodedStr);
53                         } else {
54                                 usage();
55                         }
56                 } catch (ParseException e) {
57                         System.out.println("failed to parse input");
58                         System.out.println(e.toString());
59                         usage();
60                 } catch (Exception e) {
61                         System.out.println("exception:" + e.toString());
62                 }
63         }
64         
65         /**
66          * Usage.
67          */
68         private static void usage(){
69                 System.out.println("usage:");
70                 System.out.println("-e [string] to obfuscate");
71                 System.out.println("-d [string] to deobfuscate");
72                 System.out.println("-h help");
73         }
74 }