Adds url encode/decode commands
Adds a URL Endoding helper Adds charset option Adds decode operations Fixes gh-11
This commit is contained in:
committed by
Spencer Gibb
parent
c66f05dd3a
commit
02c13fec63
@@ -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<Command> getCommands() {
|
||||
return Arrays.<Command>asList(new EncryptCommand(), new DecryptCommand(), new LauncherCommand());
|
||||
return Arrays.<Command>asList(new EncryptCommand(), new DecryptCommand(), new UrlEncodeCommand(), new UrlDecodeCommand(), new LauncherCommand());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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<String> charsetOption;
|
||||
|
||||
@Override
|
||||
protected final void options() {
|
||||
this.charsetOption = option(asList("charset", "c"),
|
||||
"Character set (defaults to UTF-8)").withRequiredArg();
|
||||
doOptions();
|
||||
}
|
||||
|
||||
protected void doOptions(){
|
||||
|
||||
}
|
||||
}
|
||||
@@ -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 "<text>";
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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] <text>";
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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+"));
|
||||
}
|
||||
}
|
||||
@@ -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+"));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user