/** * Decodes a URL safe object into its original form. Escaped characters are converted back to their original * representation. * * @param obj * URL safe object to convert into its original form * @return original object * @throws DecoderException * Thrown if the argument is not a <code>String</code> or <code>byte[]</code>. Thrown if a failure * condition is encountered during the decode process. */ @Override public Object decode(final Object obj) throws DecoderException { if (obj == null) { return null; } else if (obj instanceof byte[]) { return decode((byte[]) obj); } else if (obj instanceof String) { return decode((String) obj); } else { throw new DecoderException("Objects of type " + obj.getClass().getName() + " cannot be URL decoded"); } }
/** * Decodes a URL safe string into its original form using the * specified encoding. Escaped characters are converted back * to their original representation. * * @param pString URL safe string to convert into its original form * @param charset the original string charset * @return original string * @throws DecoderException Thrown if URL decoding is unsuccessful * @throws UnsupportedEncodingException Thrown if charset is not * supported */ public String decode(String pString, String charset) throws DecoderException, UnsupportedEncodingException { if (pString == null) { return null; } return new String(decode(pString.getBytes(StringEncodings.US_ASCII)), charset); }
/** * Decodes a URL safe string into its original form using the specified encoding. Escaped characters are converted * back to their original representation. * * @param str * URL safe string to convert into its original form * @param charset * the original string charset * @return original string * @throws DecoderException * Thrown if URL decoding is unsuccessful * @throws UnsupportedEncodingException * Thrown if charset is not supported */ public String decode(final String str, final String charset) throws DecoderException, UnsupportedEncodingException { if (str == null) { return null; } return new String(decode(StringUtils.getBytesUsAscii(str)), charset); }
/** * Decodes a URL safe object into its original form. Escaped * characters are converted back to their original representation. * * @param pObject URL safe object to convert into its original form * @return original object * @throws DecoderException Thrown if URL decoding is not * applicable to objects of this type * if decoding is unsuccessful */ public Object decode(Object pObject) throws DecoderException { if (pObject == null) { return null; } else if (pObject instanceof byte[]) { return decode((byte[])pObject); } else if (pObject instanceof String) { return decode((String)pObject); } else { throw new DecoderException("Objects of type " + pObject.getClass().getName() + " cannot be URL decoded"); } }
/** * Decodes a URL safe string into its original form using the default string charset. Escaped characters are * converted back to their original representation. * * @param str * URL safe string to convert into its original form * @return original string * @throws DecoderException * Thrown if URL decoding is unsuccessful * @see #getDefaultCharset() */ @Override public String decode(final String str) throws DecoderException { if (str == null) { return null; } try { return decode(str, getDefaultCharset()); } catch (final UnsupportedEncodingException e) { throw new DecoderException(e.getMessage(), e); } }
/** * Decodes a URL safe string into its original form using the default * string charset. Escaped characters are converted back to their * original representation. * * @param pString URL safe string to convert into its original form * @return original string * @throws DecoderException Thrown if URL decoding is unsuccessful * * @see #getDefaultCharset() */ public String decode(String pString) throws DecoderException { if (pString == null) { return null; } try { return decode(pString, getDefaultCharset()); } catch(UnsupportedEncodingException e) { throw new DecoderException(e.getMessage()); } }
@Test public void testDecodeObjects() throws Exception { final URLCodec urlCodec = new URLCodec(); final String plain = "Hello+there%21"; String decoded = (String) urlCodec.decode((Object) plain); assertEquals("Basic URL decoding test", "Hello there!", decoded); final byte[] plainBA = plain.getBytes(Charsets.UTF_8); final byte[] decodedBA = (byte[]) urlCodec.decode((Object) plainBA); decoded = new String(decodedBA); assertEquals("Basic URL decoding test", "Hello there!", decoded); final Object result = urlCodec.decode((Object) null); assertEquals( "Decoding a null Object should return null", null, result); try { final Object dObj = new Double(3.0); urlCodec.decode( dObj ); fail( "Trying to url encode a Double object should cause an exception."); } catch (final DecoderException ee) { // Exception expected, test segment passes. } this.validateState(urlCodec); }
final URLCodec urlCodec = new URLCodec(); try { urlCodec.decode("%"); fail("DecoderException should have been thrown"); } catch (final DecoderException e) { urlCodec.decode("%A"); fail("DecoderException should have been thrown"); } catch (final DecoderException e) { urlCodec.decode("%WW"); fail("DecoderException should have been thrown"); } catch (final DecoderException e) { urlCodec.decode("%0W"); fail("DecoderException should have been thrown"); } catch (final DecoderException e) {
@Test public void testDecodeStringWithNull() throws Exception { final URLCodec urlCodec = new URLCodec(); final String test = null; final String result = urlCodec.decode( test, "charset" ); assertEquals("Result should be null", null, result); }
@Test public void testEncodeUrlWithNullBitSet() throws Exception { final URLCodec urlCodec = new URLCodec(); final String plain = "Hello there!"; final String encoded = new String( URLCodec.encodeUrl(null, plain.getBytes(Charsets.UTF_8))); assertEquals("Basic URL encoding test", "Hello+there%21", encoded); assertEquals("Basic URL decoding test", plain, urlCodec.decode(encoded)); this.validateState(urlCodec); }
@Test public void testDecodeInvalidContent() throws UnsupportedEncodingException, DecoderException { final String ch_msg = constructString(SWISS_GERMAN_STUFF_UNICODE); final URLCodec urlCodec = new URLCodec(); final byte[] input = ch_msg.getBytes("ISO-8859-1"); final byte[] output = urlCodec.decode(input); assertEquals(input.length, output.length); for (int i = 0; i < input.length; i++) { assertEquals(input[i], output[i]); } this.validateState(urlCodec); }
@Test public void testBasicEncodeDecode() throws Exception { final URLCodec urlCodec = new URLCodec(); final String plain = "Hello there!"; final String encoded = urlCodec.encode(plain); assertEquals("Basic URL encoding test", "Hello+there%21", encoded); assertEquals("Basic URL decoding test", plain, urlCodec.decode(encoded)); this.validateState(urlCodec); }
@Test public void testSafeCharEncodeDecode() throws Exception { final URLCodec urlCodec = new URLCodec(); final String plain = "abc123_-.*"; final String encoded = urlCodec.encode(plain); assertEquals("Safe chars URL encoding test", plain, encoded); assertEquals("Safe chars URL decoding test", plain, urlCodec.decode(encoded)); this.validateState(urlCodec); }
@Test public void testEncodeDecodeNull() throws Exception { final URLCodec urlCodec = new URLCodec(); assertNull("Null string URL encoding test", urlCodec.encode((String)null)); assertNull("Null string URL decoding test", urlCodec.decode((String)null)); this.validateState(urlCodec); }
@Test public void testUnsafeEncodeDecode() throws Exception { final URLCodec urlCodec = new URLCodec(); final String plain = "~!@#$%^&()+{}\"\\;:`,/[]"; final String encoded = urlCodec.encode(plain); assertEquals("Unsafe chars URL encoding test", "%7E%21%40%23%24%25%5E%26%28%29%2B%7B%7D%22%5C%3B%3A%60%2C%2F%5B%5D", encoded); assertEquals("Unsafe chars URL decoding test", plain, urlCodec.decode(encoded)); this.validateState(urlCodec); }
@Test public void testInvalidEncoding() { final URLCodec urlCodec = new URLCodec("NONSENSE"); final String plain = "Hello there!"; try { urlCodec.encode(plain); fail("We set the encoding to a bogus NONSENSE vlaue, this shouldn't have worked."); } catch (final EncoderException ee) { // Exception expected, test segment passes. } try { urlCodec.decode(plain); fail("We set the encoding to a bogus NONSENSE vlaue, this shouldn't have worked."); } catch (final DecoderException ee) { // Exception expected, test segment passes. } this.validateState(urlCodec); }
@Test public void testUTF8RoundTrip() throws Exception { final String ru_msg = constructString(RUSSIAN_STUFF_UNICODE); final String ch_msg = constructString(SWISS_GERMAN_STUFF_UNICODE); final URLCodec urlCodec = new URLCodec(); this.validateState(urlCodec); assertEquals( "%D0%92%D1%81%D0%B5%D0%BC_%D0%BF%D1%80%D0%B8%D0%B2%D0%B5%D1%82", urlCodec.encode(ru_msg, CharEncoding.UTF_8) ); assertEquals("Gr%C3%BCezi_z%C3%A4m%C3%A4", urlCodec.encode(ch_msg, CharEncoding.UTF_8)); assertEquals(ru_msg, urlCodec.decode(urlCodec.encode(ru_msg, CharEncoding.UTF_8), CharEncoding.UTF_8)); assertEquals(ch_msg, urlCodec.decode(urlCodec.encode(ch_msg, CharEncoding.UTF_8), CharEncoding.UTF_8)); this.validateState(urlCodec); }
@SneakyThrows public static String urlDecode(final String text) { return text != null ? URL_CODEC.decode(text) : null; } }
private static String decodeUrlCodec(final String decodeText) { final URLCodec codec = new URLCodec(); try { return codec.decode(decodeText, "UTF-8"); } catch (final DecoderException e) { return "Error: www-form-urlencoded value cannot be decoded..."; } catch (final UnsupportedEncodingException e) { return "Error: www-form-urlencoded value cannot be decoded..."; } }
String s1 = "test"; System.out.println(s1); Base64 base64 = new Base64(); String s2 = base64.encodeAsString(s1.getBytes()); System.out.println(s2); URLCodec url = new URLCodec(); String s3 = url.encode(s2); System.out.println(s3); s2 = url.decode(s3); System.out.println(s2); s1 = new String(base64.decode(s2)); System.out.println(s1);