/** * 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"); } }
/** * 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); }
/** * Encodes a string into its URL safe form using the specified string charset. Unsafe characters are escaped. * * @param str * string to convert to a URL safe form * @param charset * the charset for str * @return URL safe string * @throws UnsupportedEncodingException * Thrown if charset is not supported */ public String encode(final String str, final String charset) throws UnsupportedEncodingException { if (str == null) { return null; } return StringUtils.newStringUsAscii(encode(str.getBytes(charset))); }
/** * Encodes an object into its URL safe form. Unsafe characters are * escaped. * * @param pObject 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 */ public Object encode(Object pObject) throws EncoderException { if (pObject == null) { return null; } else if (pObject instanceof byte[]) { return encode((byte[])pObject); } else if (pObject instanceof String) { return encode((String)pObject); } else { throw new EncoderException("Objects of type " + pObject.getClass().getName() + " cannot be URL encoded"); } }
/** * 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)); }
/*** * Authenticate a user and obtain a session.id from response. Once a session.id has been obtained, * until the session expires, this id can be used to do any API requests with a proper permission granted. * A session expires if user log's out, changes machine, browser or location, if Azkaban is restarted, * or if the session expires. The default session timeout is 24 hours (one day). User can re-login irrespective * of wheter the session has expired or not. For the same user, a new session will always override the old one. * @param username Username. * @param password Password. * @param azkabanServerUrl Azkaban Server Url. * @return Session Id. * @throws IOException * @throws EncoderException */ public static String authenticateAndGetSessionId(String username, String password, String azkabanServerUrl) throws IOException, EncoderException { // Create post request Map<String, String> params = Maps.newHashMap(); params.put("action", "login"); params.put("username", username); params.put("password", codec.encode(password)); return executePostRequest(preparePostRequest(azkabanServerUrl, null, params)).get("session.id"); }
/** * 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()); } }
@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); } }
@Test public void testEncodeObjects() throws Exception { final URLCodec urlCodec = new URLCodec(); final String plain = "Hello there!"; String encoded = (String) urlCodec.encode((Object) plain); assertEquals("Basic URL encoding test", "Hello+there%21", encoded); final byte[] plainBA = plain.getBytes(Charsets.UTF_8); final byte[] encodedBA = (byte[]) urlCodec.encode((Object) plainBA); encoded = new String(encodedBA); assertEquals("Basic URL encoding test", "Hello+there%21", encoded); final Object result = urlCodec.encode((Object) null); assertEquals( "Encoding a null Object should return null", null, result); try { final Object dObj = new Double(3.0); urlCodec.encode( dObj ); fail( "Trying to url encode a Double object should cause an exception."); } catch (final EncoderException ee) { // Exception expected, test segment passes. } this.validateState(urlCodec); }
@Test public void testEncodeStringWithNull() throws Exception { final URLCodec urlCodec = new URLCodec(); final String test = null; final String result = urlCodec.encode( test, "charset" ); assertEquals("Result should be null", null, result); }
@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); }
@Test public void testEncodeNull() throws Exception { final URLCodec urlCodec = new URLCodec(); final byte[] plain = null; final byte[] encoded = urlCodec.encode(plain); assertEquals("Encoding a null string should return null", null, 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 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 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 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 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); }