Refine search
@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); }
/** * Unescape and decode a given string regarded as an escaped string with the * default protocol charset. * * @param escaped a string * @return the unescaped string * * @throws URIException if the string cannot be decoded (invalid) * * @see URI#getDefaultProtocolCharset */ public static String decode(String escaped) throws URIException { try { byte[] rawdata = URLCodec.decodeUrl(EncodingUtil.getAsciiBytes(escaped)); return EncodingUtil.getString(rawdata, URI.getDefaultProtocolCharset()); } catch (DecoderException e) { throw new URIException(e.getMessage()); } }
/** * Encodes an array of bytes into an array of URL safe 7-bit characters. Unsafe characters are escaped. * * @param bytes * array of bytes to convert to URL safe characters * @return array of bytes containing URL safe characters */ @Override public byte[] encode(final byte[] bytes) { return encodeUrl(WWW_FORM_URL_SAFE, bytes); }
/** * 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); } }
/** * Encodes a string into its URL safe form using the default string charset. Unsafe characters are escaped. * * @param str * string to convert to a URL safe form * @return URL safe string * @throws EncoderException * Thrown if URL encoding is unsuccessful * * @see #getDefaultCharset() */ @Override public String encode(final String str) throws EncoderException { if (str == null) { return null; } try { return encode(str, getDefaultCharset()); } catch (final UnsupportedEncodingException e) { throw new EncoderException(e.getMessage(), e); } }
private static void changeProjectDescription(String sessionId, String azkabanServerUrl, String azkabanProjectName, String projectDescription) throws IOException { String encodedProjectDescription; try { encodedProjectDescription = new URLCodec().encode(projectDescription); } catch (EncoderException e) { throw new IOException("Could not encode Azkaban project description", e); } Map<String, String> params = Maps.newHashMap(); params.put("ajax", "changeDescription"); params.put("project", azkabanProjectName); params.put("description", encodedProjectDescription); executeGetRequest(prepareGetRequest(azkabanServerUrl + "/manager", sessionId, params)); }
/** * Decodes an array of URL safe 7-bit characters into an array of original bytes. Escaped characters are converted * back to their original representation. * * @param bytes * array of URL safe characters * @return array of original bytes * @throws DecoderException * Thrown if URL decoding is unsuccessful */ @Override public byte[] decode(final byte[] bytes) throws DecoderException { return decodeUrl(bytes); }
@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); }
/** * Encodes an object into its URL safe form. Unsafe characters are escaped. * * @param obj * string to convert to a URL safe form * @return URL safe object * @throws EncoderException * Thrown if URL encoding is not applicable to objects of this type or if encoding is unsuccessful */ @Override public Object encode(final Object obj) throws EncoderException { if (obj == null) { return null; } else if (obj instanceof byte[]) { return encode((byte[])obj); } else if (obj instanceof String) { return encode((String)obj); } else { throw new EncoderException("Objects of type " + obj.getClass().getName() + " cannot be URL encoded"); } }
/** * 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); }
@Test public void testDefaultEncoding() throws Exception { final String plain = "Hello there!"; final URLCodec urlCodec = new URLCodec("UnicodeBig"); urlCodec.encode(plain); // To work around a weird quirk in Java 1.2.2 final String encoded1 = urlCodec.encode(plain, "UnicodeBig"); final String encoded2 = urlCodec.encode(plain); assertEquals(encoded1, encoded2); this.validateState(urlCodec); } }
/** * 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()); } }
/** * Encodes a string into its URL safe form using the default string * charset. Unsafe characters are escaped. * * @param pString string to convert to a URL safe form * @return URL safe string * @throws EncoderException Thrown if URL encoding is unsuccessful * * @see #getDefaultCharset() */ public String encode(String pString) throws EncoderException { if (pString == null) { return null; } try { return encode(pString, getDefaultCharset()); } catch(UnsupportedEncodingException e) { throw new EncoderException(e.getMessage()); } }
/** * Decodes an array of URL safe 7-bit characters into an array of * original bytes. Escaped characters are converted back to their * original representation. * * @param bytes array of URL safe characters * @return array of original bytes * @throws DecoderException Thrown if URL decoding is unsuccessful */ public byte[] decode(byte[] bytes) throws DecoderException { return decodeUrl(bytes); }
@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); }
/** * Encodes a string into its URL safe form using the specified * string charset. Unsafe characters are escaped. * * @param pString string to convert to a URL safe form * @param charset the charset for pString * @return URL safe string * @throws UnsupportedEncodingException Thrown if charset is not * supported */ public String encode(String pString, String charset) throws UnsupportedEncodingException { if (pString == null) { return null; } return new String(encode(pString.getBytes(charset)), StringEncodings.US_ASCII); }
/** * 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 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); }