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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user