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        protected void printHelp() {
043            context.printHelp(helpFile);
044        }
045    
046        @Override
047        protected void runTask(List<String> tokens) throws Exception {
048            if (password == null || input == null) {
049                context.printException(new IllegalArgumentException("input and password parameters are mandatory"));
050                return;
051            }
052            encryptor.setPassword(password);
053            context.print("Encrypted text: " + encryptor.encrypt(input));
054        }
055    
056        @Override
057        protected void handleOption(String token, List<String> tokens) throws Exception {
058            if (token.startsWith("--input")) {
059                if (tokens.isEmpty() || ((String)tokens.get(0)).startsWith("-")) {
060                    context.printException(new IllegalArgumentException("input not specified"));
061                    return;
062                }
063    
064                input=(String)tokens.remove(0);
065            } else if (token.startsWith("--password")) {
066                if (tokens.isEmpty() || ((String)tokens.get(0)).startsWith("-")) {
067                    context.printException(new IllegalArgumentException("password not specified"));
068                    return;
069                }
070    
071                password=(String)tokens.remove(0);            
072            } else {
073                super.handleOption(token, tokens);
074            }
075        }
076        
077        
078    
079    }