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.activemq.console.command;
018    
019    import java.util.List;
020    
021    import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;
022    
023    public class EncryptCommand extends AbstractCommand {
024    
025        protected String[] helpFile = new String[] {
026                "Task Usage: Main encrypt --password <password> --input <input>",
027                "Description: Encrypts given text.",
028                "", 
029                "Encrypt Options:",
030                "    --password <password>      Password to be used by the encryptor.",
031                "    --input <input>            Text to be encrypted.",
032                "    --version                  Display the version information.",
033                "    -h,-?,--help               Display the stop broker help information.",
034                ""
035            };
036        
037        StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
038        String input;
039        String password;
040        
041        @Override
042        public String getName() {
043            return "encrypt";
044        }
045    
046        @Override
047        public String getOneLineDescription() {
048            return "Encrypts given text";
049        }
050    
051        @Override
052        protected void printHelp() {
053            context.printHelp(helpFile);
054        }
055    
056        @Override
057        protected void runTask(List<String> tokens) throws Exception {
058            if (password == null || input == null) {
059                context.printException(new IllegalArgumentException("input and password parameters are mandatory"));
060                return;
061            }
062            encryptor.setPassword(password);
063            context.print("Encrypted text: " + encryptor.encrypt(input));
064        }
065    
066        @Override
067        protected void handleOption(String token, List<String> tokens) throws Exception {
068            if (token.startsWith("--input")) {
069                if (tokens.isEmpty() || ((String)tokens.get(0)).startsWith("-")) {
070                    context.printException(new IllegalArgumentException("input not specified"));
071                    return;
072                }
073    
074                input=(String)tokens.remove(0);
075            } else if (token.startsWith("--password")) {
076                if (tokens.isEmpty() || ((String)tokens.get(0)).startsWith("-")) {
077                    context.printException(new IllegalArgumentException("password not specified"));
078                    return;
079                }
080    
081                password=(String)tokens.remove(0);            
082            } else {
083                super.handleOption(token, tokens);
084            }
085        }
086        
087        
088    
089    }