From 02c13fec639f25be8f68c17e6032a58405960ccc Mon Sep 17 00:00:00 2001 From: William Witt Date: Thu, 3 Nov 2016 14:27:15 -0500 Subject: [PATCH] Adds url encode/decode commands Adds a URL Endoding helper Adds charset option Adds decode operations Fixes gh-11 --- .../cli/command/CloudCommandFactory.java | 4 +- .../command/url/BaseEncodeOptionHandler.java | 22 ++++++ .../cli/command/url/UrlDecodeCommand.java | 67 ++++++++++++++++++ .../cli/command/url/UrlEncodeCommand.java | 65 ++++++++++++++++++ .../cli/command/url/UrlDecodeCommandTest.java | 68 +++++++++++++++++++ .../cli/command/url/UrlEncodeCommandTest.java | 68 +++++++++++++++++++ 6 files changed, 293 insertions(+), 1 deletion(-) create mode 100644 spring-cloud-cli/src/main/java/org/springframework/cloud/cli/command/url/BaseEncodeOptionHandler.java create mode 100644 spring-cloud-cli/src/main/java/org/springframework/cloud/cli/command/url/UrlDecodeCommand.java create mode 100644 spring-cloud-cli/src/main/java/org/springframework/cloud/cli/command/url/UrlEncodeCommand.java create mode 100644 spring-cloud-cli/src/test/java/org/springframework/cloud/cli/command/url/UrlDecodeCommandTest.java create mode 100644 spring-cloud-cli/src/test/java/org/springframework/cloud/cli/command/url/UrlEncodeCommandTest.java diff --git a/spring-cloud-cli/src/main/java/org/springframework/cloud/cli/command/CloudCommandFactory.java b/spring-cloud-cli/src/main/java/org/springframework/cloud/cli/command/CloudCommandFactory.java index 1230862..d01e6a7 100644 --- a/spring-cloud-cli/src/main/java/org/springframework/cloud/cli/command/CloudCommandFactory.java +++ b/spring-cloud-cli/src/main/java/org/springframework/cloud/cli/command/CloudCommandFactory.java @@ -22,6 +22,8 @@ import org.springframework.boot.cli.command.Command; import org.springframework.boot.cli.command.CommandFactory; import org.springframework.cloud.cli.command.encrypt.DecryptCommand; import org.springframework.cloud.cli.command.encrypt.EncryptCommand; +import org.springframework.cloud.cli.command.url.UrlDecodeCommand; +import org.springframework.cloud.cli.command.url.UrlEncodeCommand; import org.springframework.cloud.launcher.cli.LauncherCommand; /** @@ -32,7 +34,7 @@ public class CloudCommandFactory implements CommandFactory { @Override public Collection getCommands() { - return Arrays.asList(new EncryptCommand(), new DecryptCommand(), new LauncherCommand()); + return Arrays.asList(new EncryptCommand(), new DecryptCommand(), new UrlEncodeCommand(), new UrlDecodeCommand(), new LauncherCommand()); } } diff --git a/spring-cloud-cli/src/main/java/org/springframework/cloud/cli/command/url/BaseEncodeOptionHandler.java b/spring-cloud-cli/src/main/java/org/springframework/cloud/cli/command/url/BaseEncodeOptionHandler.java new file mode 100644 index 0000000..32baf06 --- /dev/null +++ b/spring-cloud-cli/src/main/java/org/springframework/cloud/cli/command/url/BaseEncodeOptionHandler.java @@ -0,0 +1,22 @@ +package org.springframework.cloud.cli.command.url; + +import static java.util.Arrays.asList; + +import joptsimple.OptionSpec; +import org.springframework.boot.cli.command.options.OptionHandler; + + +public class BaseEncodeOptionHandler extends OptionHandler { + OptionSpec charsetOption; + + @Override + protected final void options() { + this.charsetOption = option(asList("charset", "c"), + "Character set (defaults to UTF-8)").withRequiredArg(); + doOptions(); + } + + protected void doOptions(){ + + } +} diff --git a/spring-cloud-cli/src/main/java/org/springframework/cloud/cli/command/url/UrlDecodeCommand.java b/spring-cloud-cli/src/main/java/org/springframework/cloud/cli/command/url/UrlDecodeCommand.java new file mode 100644 index 0000000..271afda --- /dev/null +++ b/spring-cloud-cli/src/main/java/org/springframework/cloud/cli/command/url/UrlDecodeCommand.java @@ -0,0 +1,67 @@ +/* + * Copyright 2013-2014 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.cloud.cli.command.url; + +import java.io.UnsupportedEncodingException; +import java.net.URLDecoder; +import java.net.URLEncoder; +import java.nio.charset.Charset; +import java.nio.charset.UnsupportedCharsetException; + +import org.springframework.boot.cli.command.OptionParsingCommand; +import org.springframework.boot.cli.command.options.OptionHandler; +import org.springframework.boot.cli.command.status.ExitStatus; +import org.springframework.util.StringUtils; + +import joptsimple.OptionSet; + +/** + * @author William Witt + */ +public class UrlDecodeCommand extends OptionParsingCommand { + + public UrlDecodeCommand() { + super("urlDecode", "URL decodes the subsequent string", + new UrlDecodeOptionHandler()); + } + + @Override + public String getUsageHelp() { + return ""; + } + + private static class UrlDecodeOptionHandler extends BaseEncodeOptionHandler { + + @Override + protected synchronized ExitStatus run(OptionSet options) throws Exception { + String charset = "UTF-8"; + if(options.has(charsetOption)){ + charset = options.valueOf(charsetOption); + } + String text = StringUtils + .collectionToDelimitedString(options.nonOptionArguments(), " "); + try { + Charset.forName(charset); + String outText = URLDecoder.decode(text, charset); + System.out.println(outText); + return ExitStatus.OK; + } catch (UnsupportedCharsetException e){ + System.out.println("Unsupported Character Set"); + return ExitStatus.ERROR; + } + } + } +} diff --git a/spring-cloud-cli/src/main/java/org/springframework/cloud/cli/command/url/UrlEncodeCommand.java b/spring-cloud-cli/src/main/java/org/springframework/cloud/cli/command/url/UrlEncodeCommand.java new file mode 100644 index 0000000..39896aa --- /dev/null +++ b/spring-cloud-cli/src/main/java/org/springframework/cloud/cli/command/url/UrlEncodeCommand.java @@ -0,0 +1,65 @@ +/* + * Copyright 2013-2014 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.cloud.cli.command.url; + +import joptsimple.OptionSet; +import org.springframework.boot.cli.command.OptionParsingCommand; +import org.springframework.boot.cli.command.status.ExitStatus; +import org.springframework.util.StringUtils; + +import java.io.UnsupportedEncodingException; +import java.net.URLEncoder; +import java.nio.charset.Charset; +import java.nio.charset.UnsupportedCharsetException; + +/** + * @author William Witt + */ +public class UrlEncodeCommand extends OptionParsingCommand { + + public UrlEncodeCommand() { + super("urlEncode", "URL encodes the subsequent string", + new UrlEncodeOptionHandler()); + } + + @Override + public String getUsageHelp() { + return "[options] "; + } + + private static class UrlEncodeOptionHandler extends BaseEncodeOptionHandler{ + + @Override + protected synchronized ExitStatus run(OptionSet options) throws Exception { + String charset = "UTF-8"; + if(options.has(charsetOption)){ + charset = options.valueOf(charsetOption); + } + String text = StringUtils + .collectionToDelimitedString(options.nonOptionArguments(), " "); + try { + Charset.forName(charset); + String outText = URLEncoder.encode(text, charset); + System.out.println(outText); + return ExitStatus.OK; + } catch (UnsupportedCharsetException e){ + System.out.println("Unsupported Character Set"); + return ExitStatus.ERROR; + } + } + } +} + diff --git a/spring-cloud-cli/src/test/java/org/springframework/cloud/cli/command/url/UrlDecodeCommandTest.java b/spring-cloud-cli/src/test/java/org/springframework/cloud/cli/command/url/UrlDecodeCommandTest.java new file mode 100644 index 0000000..bc66b14 --- /dev/null +++ b/spring-cloud-cli/src/test/java/org/springframework/cloud/cli/command/url/UrlDecodeCommandTest.java @@ -0,0 +1,68 @@ +/* + * Copyright 2013-2014 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.cloud.cli.command.url; + +import org.junit.Rule; +import org.junit.Test; +import org.springframework.boot.cli.command.Command; +import org.springframework.boot.cli.command.status.ExitStatus; +import org.springframework.boot.test.rule.OutputCapture; + +import static org.junit.Assert.*; + +/** + * @author William Witt + */ +public class UrlDecodeCommandTest { + Command command = new UrlDecodeCommand(); + + @Rule + public OutputCapture capture = new OutputCapture(); + + @Test + public void urlDecodeNoSpecialChars() throws Exception { + command.run("abcdefg"); + assertEquals("abcdefg\n", capture.toString()); + } + + @Test + public void urlDecodeSpecialChars() throws Exception { + command.run("a+b+c%26d%25efg%2B"); + assertEquals("a b c&d%efg+\n", capture.toString()); + } + + @Test + public void urlDecodeNoSpecialCharsWithCharset() throws Exception { + command.run("-c", "UTF-8", "abcdefg"); + assertEquals("abcdefg\n", capture.toString()); + } + + @Test + public void urlDecodeSpecialCharsWithCharset() throws Exception { + command.run("-c", "UTF-8", "a+b+c%26d%25efg%2B"); + assertEquals("a b c&d%efg+\n", capture.toString()); + } + + @Test + public void urlDecodeNoSpecialCharsWithUnsupportedCharset() throws Exception { + assertEquals(ExitStatus.ERROR, command.run("-c", "UTF-9", "abcdefg")); + } + + @Test + public void urlDecodeSpecialCharsWithUnsupportedCharset() throws Exception { + assertEquals(ExitStatus.ERROR, command.run("-c", "UTF-9", "a b c&d%efg+")); + } +} \ No newline at end of file diff --git a/spring-cloud-cli/src/test/java/org/springframework/cloud/cli/command/url/UrlEncodeCommandTest.java b/spring-cloud-cli/src/test/java/org/springframework/cloud/cli/command/url/UrlEncodeCommandTest.java new file mode 100644 index 0000000..55b51be --- /dev/null +++ b/spring-cloud-cli/src/test/java/org/springframework/cloud/cli/command/url/UrlEncodeCommandTest.java @@ -0,0 +1,68 @@ +/* + * Copyright 2013-2014 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.cloud.cli.command.url; + +import org.junit.Rule; +import org.junit.Test; +import org.springframework.boot.cli.command.Command; +import org.springframework.boot.cli.command.status.ExitStatus; +import org.springframework.boot.test.rule.OutputCapture; + +import static org.junit.Assert.*; + +/** + * @author William Witt + */ +public class UrlEncodeCommandTest { + Command command = new UrlEncodeCommand(); + + @Rule + public OutputCapture capture = new OutputCapture(); + + @Test + public void urlEncodeNoSpecialChars() throws Exception { + command.run("abcdefg"); + assertEquals("abcdefg\n", capture.toString()); + } + + @Test + public void urlEncodeSpecialChars() throws Exception { + command.run("a b c&d%efg+"); + assertEquals("a+b+c%26d%25efg%2B\n", capture.toString()); + } + + @Test + public void urlEncodeNoSpecialCharsWithCharset() throws Exception { + command.run("-c", "UTF-8", "abcdefg"); + assertEquals("abcdefg\n", capture.toString()); + } + + @Test + public void urlEncodeSpecialCharsWithCharset() throws Exception { + command.run("-c", "UTF-8", "a b c&d%efg+"); + assertEquals("a+b+c%26d%25efg%2B\n", capture.toString()); + } + + @Test + public void urlEncodeNoSpecialCharsWithUnsupportedCharset() throws Exception { + assertEquals(ExitStatus.ERROR, command.run("-c", "UTF-9", "abcdefg")); + } + + @Test + public void urlEncodeSpecialCharsWithUnsupportedCharset() throws Exception { + assertEquals(ExitStatus.ERROR, command.run("-c", "UTF-9", "a b c&d%efg+")); + } +} \ No newline at end of file