001 /**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.camel.component.jasypt;
018
019 import java.util.ArrayList;
020 import java.util.Arrays;
021 import java.util.LinkedList;
022 import java.util.List;
023
024 import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;
025
026 /**
027 * @version $Revision: 21219 $
028 */
029 public class Main {
030
031 private final StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
032 private final List<Option> options = new ArrayList<Option>();
033 private String command;
034 private String password;
035 private String input;
036 private String algorithm;
037
038 private abstract class Option {
039 private String abbreviation;
040 private String fullName;
041 private String description;
042
043 protected Option(String abbreviation, String fullName, String description) {
044 this.abbreviation = "-" + abbreviation;
045 this.fullName = "-" + fullName;
046 this.description = description;
047 }
048
049 public boolean processOption(String arg, LinkedList<String> remainingArgs) {
050 if (arg.equalsIgnoreCase(abbreviation) || fullName.startsWith(arg)) {
051 doProcess(arg, remainingArgs);
052 return true;
053 }
054 return false;
055 }
056
057 public String getAbbreviation() {
058 return abbreviation;
059 }
060
061 public String getDescription() {
062 return description;
063 }
064
065 public String getFullName() {
066 return fullName;
067 }
068
069 public String getInformation() {
070 return " " + getAbbreviation() + " or " + getFullName() + " = " + getDescription();
071 }
072
073 protected abstract void doProcess(String arg, LinkedList<String> remainingArgs);
074 }
075
076 private abstract class ParameterOption extends Option {
077 private String parameterName;
078
079 protected ParameterOption(String abbreviation, String fullName, String description, String parameterName) {
080 super(abbreviation, fullName, description);
081 this.parameterName = parameterName;
082 }
083
084 protected void doProcess(String arg, LinkedList<String> remainingArgs) {
085 if (remainingArgs.isEmpty()) {
086 System.err.println("Expected fileName for ");
087 showOptions();
088 } else {
089 String parameter = remainingArgs.removeFirst();
090 doProcess(arg, parameter, remainingArgs);
091 }
092 }
093
094 public String getInformation() {
095 return " " + getAbbreviation() + " or " + getFullName()
096 + " <" + parameterName + "> = " + getDescription();
097 }
098
099 protected abstract void doProcess(String arg, String parameter, LinkedList<String> remainingArgs);
100 }
101
102 public Main() {
103 addOption(new Option("h", "help", "Displays the help screen") {
104 protected void doProcess(String arg, LinkedList<String> remainingArgs) {
105 showOptions();
106 }
107 });
108
109 addOption(new ParameterOption("c", "command", "Command either encrypt or decrypt", "command") {
110 protected void doProcess(String arg, String parameter, LinkedList<String> remainingArgs) {
111 if ("encrypt".equals(parameter) || "decrypt".equals(parameter)) {
112 command = parameter;
113 } else {
114 throw new IllegalArgumentException("Unknown command, was: " + parameter);
115 }
116 }
117 });
118
119 addOption(new ParameterOption("p", "password", "Password to use", "password") {
120 protected void doProcess(String arg, String parameter, LinkedList<String> remainingArgs) {
121 password = parameter;
122 }
123 });
124
125 addOption(new ParameterOption("i", "input", "Text to encrypt or decrypt", "input") {
126 protected void doProcess(String arg, String parameter, LinkedList<String> remainingArgs) {
127 input = parameter;
128 }
129 });
130
131 addOption(new ParameterOption("a", "algorithm", "Optional algorithm to use", "algorithm") {
132 protected void doProcess(String arg, String parameter, LinkedList<String> remainingArgs) {
133 algorithm = parameter;
134 }
135 });
136 }
137
138 private void addOption(Option option) {
139 options.add(option);
140 }
141
142 private void showOptions() {
143 System.out.println("Apache Camel Jasypt takes the following options");
144 System.out.println();
145 for (Option option : options) {
146 System.out.println(option.getInformation());
147 }
148 System.out.println();
149 System.out.println();
150 }
151
152 private boolean parseArguments(String[] arguments) {
153 LinkedList<String> args = new LinkedList<String>(Arrays.asList(arguments));
154
155 boolean valid = true;
156 while (!args.isEmpty()) {
157 String arg = args.removeFirst();
158
159 boolean handled = false;
160 for (Option option : options) {
161 if (option.processOption(arg, args)) {
162 handled = true;
163 break;
164 }
165 }
166 if (!handled) {
167 System.out.println("Error: Unknown option: " + arg);
168 System.out.println();
169 valid = false;
170 break;
171 }
172 }
173
174 return valid;
175 }
176
177 public void run(String[] args) throws Exception {
178 if (!parseArguments(args)) {
179 showOptions();
180 return;
181 }
182
183 if (command == null) {
184 System.out.println("Error: Command is empty");
185 System.out.println();
186 showOptions();
187 return;
188 }
189 if (password == null) {
190 System.out.println("Error: Password is empty");
191 System.out.println();
192 showOptions();
193 return;
194 }
195 if (input == null) {
196 System.out.println("Error: Input is empty");
197 System.out.println();
198 showOptions();
199 return;
200 }
201
202 encryptor.setPassword(password);
203 if (algorithm != null) {
204 encryptor.setAlgorithm(algorithm);
205 }
206 if ("encrypt".equals(command)) {
207 System.out.println("Encrypted text: " + encryptor.encrypt(input));
208 } else {
209 System.out.println("Decrypted text: " + encryptor.decrypt(input));
210 }
211 }
212
213 public static void main(String[] args) throws Exception {
214 Main main = new Main();
215 if (args.length == 0) {
216 main.showOptions();
217 return;
218 } else {
219 main.run(args);
220 }
221 }
222
223 }