ParseQS and Global made into packages
This commit is contained in:
@@ -6,6 +6,7 @@ import com.github.nkzawa.engineio.client.transports.PollingXHR;
|
||||
import com.github.nkzawa.engineio.client.transports.WebSocket;
|
||||
import com.github.nkzawa.engineio.parser.Packet;
|
||||
import com.github.nkzawa.engineio.parser.Parser;
|
||||
import com.github.nkzawa.parseqs.ParseQS;
|
||||
import com.github.nkzawa.thread.EventThread;
|
||||
import org.json.JSONObject;
|
||||
|
||||
@@ -167,7 +168,7 @@ public abstract class Socket extends Emitter {
|
||||
this.hostname = opts.hostname != null ? opts.hostname : "localhost";
|
||||
this.port = opts.port != 0 ? opts.port : (this.secure ? 443 : 80);
|
||||
this.query = opts.query != null ?
|
||||
Util.qsParse(opts.query) : new HashMap<String, String>();
|
||||
ParseQS.decode(opts.query) : new HashMap<String, String>();
|
||||
this.upgrade = opts.upgrade;
|
||||
this.path = (opts.path != null ? opts.path : "/engine.io").replaceAll("/$", "") + "/";
|
||||
this.timestampParam = opts.timestampParam != null ? opts.timestampParam : "t";
|
||||
|
||||
@@ -1,56 +0,0 @@
|
||||
package com.github.nkzawa.engineio.client;
|
||||
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.URLDecoder;
|
||||
import java.net.URLEncoder;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class Util {
|
||||
|
||||
private Util() {}
|
||||
|
||||
public static String qs(Map<String, String> obj) {
|
||||
StringBuilder str = new StringBuilder();
|
||||
for (Map.Entry<String, String> entry : obj.entrySet()) {
|
||||
if (str.length() > 0) str.append("&");
|
||||
str.append(encodeURIComponent(entry.getKey())).append("=")
|
||||
.append(encodeURIComponent(entry.getValue()));
|
||||
}
|
||||
return str.toString();
|
||||
}
|
||||
|
||||
public static Map<String, String> qsParse(String qs) {
|
||||
Map<String, String> qry = new HashMap<String, String>();
|
||||
String[] pairs = qs.split("&");
|
||||
for (String _pair : pairs) {
|
||||
String[] pair = _pair.split("=");
|
||||
qry.put(decodeURIComponent(pair[0]),
|
||||
pair.length > 0 ? decodeURIComponent(pair[1]) : "");
|
||||
}
|
||||
return qry;
|
||||
}
|
||||
|
||||
public static String encodeURIComponent(String str) {
|
||||
try {
|
||||
return URLEncoder.encode(str, "UTF-8")
|
||||
.replace("+", "%20")
|
||||
.replace("%21", "!")
|
||||
.replace("%27", "'")
|
||||
.replace("%28", "(")
|
||||
.replace("%29", ")")
|
||||
.replace("%7E", "~");
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static String decodeURIComponent(String str) {
|
||||
try {
|
||||
return URLDecoder.decode(str, "UTF-8");
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,9 +2,9 @@ package com.github.nkzawa.engineio.client.transports;
|
||||
|
||||
|
||||
import com.github.nkzawa.engineio.client.Transport;
|
||||
import com.github.nkzawa.engineio.client.Util;
|
||||
import com.github.nkzawa.engineio.parser.Packet;
|
||||
import com.github.nkzawa.engineio.parser.Parser;
|
||||
import com.github.nkzawa.parseqs.ParseQS;
|
||||
import com.github.nkzawa.thread.EventThread;
|
||||
|
||||
import java.util.Date;
|
||||
@@ -197,7 +197,7 @@ abstract public class Polling extends Transport {
|
||||
query.put(this.timestampParam, String.valueOf(new Date().getTime()));
|
||||
}
|
||||
|
||||
String _query = Util.qs(query);
|
||||
String _query = ParseQS.encode(query);
|
||||
|
||||
if (this.port > 0 && (("https".equals(schema) && this.port != 443)
|
||||
|| ("http".equals(schema) && this.port != 80))) {
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
package com.github.nkzawa.engineio.client.transports;
|
||||
|
||||
|
||||
import com.github.nkzawa.thread.EventThread;
|
||||
import com.github.nkzawa.engineio.client.Transport;
|
||||
import com.github.nkzawa.engineio.client.Util;
|
||||
import com.github.nkzawa.engineio.parser.Packet;
|
||||
import com.github.nkzawa.engineio.parser.Parser;
|
||||
import com.github.nkzawa.parseqs.ParseQS;
|
||||
import com.github.nkzawa.thread.EventThread;
|
||||
import org.java_websocket.client.WebSocketClient;
|
||||
import org.java_websocket.drafts.Draft_17;
|
||||
import org.java_websocket.handshake.ServerHandshake;
|
||||
@@ -13,11 +13,7 @@ import org.java_websocket.handshake.ServerHandshake;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.TreeMap;
|
||||
import java.util.*;
|
||||
|
||||
public class WebSocket extends Transport {
|
||||
|
||||
@@ -160,7 +156,7 @@ public class WebSocket extends Transport {
|
||||
query.put(this.timestampParam, String.valueOf(new Date().getTime()));
|
||||
}
|
||||
|
||||
String _query = Util.qs(query);
|
||||
String _query = ParseQS.encode(query);
|
||||
if (_query.length() > 0) {
|
||||
_query = "?" + _query;
|
||||
}
|
||||
|
||||
33
src/main/java/com/github/nkzawa/global/Global.java
Normal file
33
src/main/java/com/github/nkzawa/global/Global.java
Normal file
@@ -0,0 +1,33 @@
|
||||
package com.github.nkzawa.global;
|
||||
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.URLDecoder;
|
||||
import java.net.URLEncoder;
|
||||
|
||||
public class Global {
|
||||
|
||||
private Global() {}
|
||||
|
||||
public static String encodeURIComponent(String str) {
|
||||
try {
|
||||
return URLEncoder.encode(str, "UTF-8")
|
||||
.replace("+", "%20")
|
||||
.replace("%21", "!")
|
||||
.replace("%27", "'")
|
||||
.replace("%28", "(")
|
||||
.replace("%29", ")")
|
||||
.replace("%7E", "~");
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static String decodeURIComponent(String str) {
|
||||
try {
|
||||
return URLDecoder.decode(str, "UTF-8");
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
33
src/main/java/com/github/nkzawa/parseqs/ParseQS.java
Normal file
33
src/main/java/com/github/nkzawa/parseqs/ParseQS.java
Normal file
@@ -0,0 +1,33 @@
|
||||
package com.github.nkzawa.parseqs;
|
||||
|
||||
|
||||
import com.github.nkzawa.global.Global;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class ParseQS {
|
||||
|
||||
private ParseQS() {}
|
||||
|
||||
public static String encode(Map<String, String> obj) {
|
||||
StringBuilder str = new StringBuilder();
|
||||
for (Map.Entry<String, String> entry : obj.entrySet()) {
|
||||
if (str.length() > 0) str.append("&");
|
||||
str.append(Global.encodeURIComponent(entry.getKey())).append("=")
|
||||
.append(Global.encodeURIComponent(entry.getValue()));
|
||||
}
|
||||
return str.toString();
|
||||
}
|
||||
|
||||
public static Map<String, String> decode(String qs) {
|
||||
Map<String, String> qry = new HashMap<String, String>();
|
||||
String[] pairs = qs.split("&");
|
||||
for (String _pair : pairs) {
|
||||
String[] pair = _pair.split("=");
|
||||
qry.put(Global.decodeURIComponent(pair[0]),
|
||||
pair.length > 0 ? Global.decodeURIComponent(pair[1]) : "");
|
||||
}
|
||||
return qry;
|
||||
}
|
||||
}
|
||||
@@ -1,50 +0,0 @@
|
||||
package com.github.nkzawa.engineio.client;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.JUnit4;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
@RunWith(JUnit4.class)
|
||||
public class UtilTest {
|
||||
|
||||
@Test
|
||||
public void qs() {
|
||||
Map<String, String> obj;
|
||||
|
||||
obj = new HashMap<String, String>() {{
|
||||
put("a", "b");
|
||||
}};
|
||||
assertThat(Util.qs(obj), is("a=b"));
|
||||
|
||||
obj = new LinkedHashMap<String, String>() {{
|
||||
put("a", "b");
|
||||
put("c", "d");
|
||||
}};
|
||||
assertThat(Util.qs(obj), is("a=b&c=d"));
|
||||
|
||||
obj = new LinkedHashMap<String, String>() {{
|
||||
put("a", "b");
|
||||
put("c", "tobi rocks");
|
||||
}};
|
||||
assertThat(Util.qs(obj), is("a=b&c=tobi%20rocks"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void encodeURIComponent() {
|
||||
assertThat(Util.encodeURIComponent(" ~'()! "), is("%20~'()!%20"));
|
||||
assertThat(Util.encodeURIComponent("+:;"), is("%2B%3A%3B"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void decodeURIComponent() {
|
||||
assertThat(Util.decodeURIComponent("%20%7E%27%28%29%21%20"), is(" ~'()! "));
|
||||
assertThat(Util.decodeURIComponent("%2B%3A%3B"), is("+:;"));
|
||||
}
|
||||
}
|
||||
24
src/test/java/com/github/nkzawa/global/GlobalTest.java
Normal file
24
src/test/java/com/github/nkzawa/global/GlobalTest.java
Normal file
@@ -0,0 +1,24 @@
|
||||
package com.github.nkzawa.global;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.JUnit4;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
@RunWith(JUnit4.class)
|
||||
public class GlobalTest {
|
||||
|
||||
@Test
|
||||
public void encodeURIComponent() {
|
||||
assertThat(Global.encodeURIComponent(" ~'()! "), is("%20~'()!%20"));
|
||||
assertThat(Global.encodeURIComponent("+:;"), is("%2B%3A%3B"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void decodeURIComponent() {
|
||||
assertThat(Global.decodeURIComponent("%20%7E%27%28%29%21%20"), is(" ~'()! "));
|
||||
assertThat(Global.decodeURIComponent("%2B%3A%3B"), is("+:;"));
|
||||
}
|
||||
}
|
||||
51
src/test/java/com/github/nkzawa/parseqs/ParseQSTest.java
Normal file
51
src/test/java/com/github/nkzawa/parseqs/ParseQSTest.java
Normal file
@@ -0,0 +1,51 @@
|
||||
package com.github.nkzawa.parseqs;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.JUnit4;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
@RunWith(JUnit4.class)
|
||||
public class ParseQSTest {
|
||||
|
||||
@Test
|
||||
public void decode() {
|
||||
Map<String, String> queryObject = ParseQS.decode("foo=bar");
|
||||
assertThat(queryObject.get("foo"), is("bar"));
|
||||
|
||||
queryObject = ParseQS.decode("france=paris&germany=berlin");
|
||||
assertThat(queryObject.get("france"), is("paris"));
|
||||
assertThat(queryObject.get("germany"), is("berlin"));
|
||||
|
||||
queryObject = ParseQS.decode("india=new%20delhi");
|
||||
assertThat(queryObject.get("india"), is("new delhi"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void encode() {
|
||||
Map<String, String> obj;
|
||||
|
||||
obj = new HashMap<String, String>() {{
|
||||
put("a", "b");
|
||||
}};
|
||||
assertThat(ParseQS.encode(obj), is("a=b"));
|
||||
|
||||
obj = new LinkedHashMap<String, String>() {{
|
||||
put("a", "b");
|
||||
put("c", "d");
|
||||
}};
|
||||
assertThat(ParseQS.encode(obj), is("a=b&c=d"));
|
||||
|
||||
obj = new LinkedHashMap<String, String>() {{
|
||||
put("a", "b");
|
||||
put("c", "tobi rocks");
|
||||
}};
|
||||
assertThat(ParseQS.encode(obj), is("a=b&c=tobi%20rocks"));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user