This commit is contained in:
地平线
2015-05-20 18:16:51 +08:00
parent 2d850d4607
commit 5a4a7006f1
3 changed files with 107 additions and 23 deletions

View File

@@ -21,6 +21,8 @@ import java.nio.charset.*;
*/
public final class HttpRequest extends Request {
protected static final Charset UTF8 = Charset.forName("UTF-8");
protected static final String SESSIONID_NAME = "JSESSIONID";
private static final byte[] flashRequestContent1 = "<policy-file-request/>\0".getBytes();
@@ -55,6 +57,8 @@ public final class HttpRequest extends Request {
private final ByteArray array = new ByteArray();
private boolean bodyparsed = false;
protected boolean flashPolicy = false;
protected boolean boundary = false;
@@ -164,9 +168,9 @@ public final class HttpRequest extends Request {
}
private void parseBody() {
if (this.boundary || array.isEmpty()) return;
if (this.boundary || bodyparsed) return;
addParameter(array, 0, array.count());
array.clear();
bodyparsed = true;
}
private void addParameter(final ByteArray array, final int offset, final int len) {
@@ -236,6 +240,18 @@ public final class HttpRequest extends Request {
return String.valueOf(addr);
}
public String getBody(final Charset charset) {
return array.toString(charset);
}
public String getBody() {
return array.toString();
}
public String getBodyUTF8() {
return array.toString(UTF8);
}
public SocketAddress getRemoteAddress() {
return this.channel.getRemoteAddress();
}
@@ -246,7 +262,7 @@ public final class HttpRequest extends Request {
return this.getClass().getSimpleName() + "{method:" + this.method + ", requestURI:" + this.requestURI
+ ", contentType:" + this.contentType + ", connection:" + this.connection + ", protocol:" + this.protocol
+ ", contentLength:" + this.contentLength + ", cookiestr:" + this.cookiestr
+ ", host:" + this.host + ", params:" + this.params + ", header:" + this.header + "body:" + (array == null ? "null" : array.toString()) + "}";
+ ", host:" + this.host + ", params:" + this.params + ", header:" + this.header + "body:" + getBody() + "}";
}
public final MultiContext getMultiContext() {
@@ -272,6 +288,7 @@ public final class HttpRequest extends Request {
this.connection = null;
this.contentLength = -1;
this.boundary = false;
this.bodyparsed = false;
this.flashPolicy = false;
this.header.clear();

View File

@@ -1527,35 +1527,44 @@ public final class DataJDBCSource implements DataSource {
}
private String genSQL(String queryColumn, EntityXInfo info, String column, FilterExpress express, Serializable key) {
String sql = "SELECT " + queryColumn + " FROM " + info.getTable() + " WHERE " + info.getSQLColumn(column) + " " + express.value();
String sql = "SELECT " + queryColumn + " FROM " + info.getTable();
if (key instanceof Number) {
sql += " " + key;
sql += " WHERE " + info.getSQLColumn(column) + " " + express.value() + " " + key;
} else if (key instanceof Collection) {
StringBuilder sb = new StringBuilder();
for (Object o : (Collection) key) {
if (sb.length() > 0) sb.append(',');
if (o instanceof Number) {
sb.append('"').append(o).append('"');
} else {
sb.append('"').append(o.toString().replace("\"", "\\\"")).append('"');
Collection list = (Collection) key;
if (list.isEmpty()) {
sql += " WHERE 1 " + (express == FilterExpress.NOTIN ? "=" : "!=") + " 1";
} else {
StringBuilder sb = new StringBuilder();
for (Object o : list) {
if (sb.length() > 0) sb.append(',');
if (o instanceof Number) {
sb.append('"').append(o).append('"');
} else {
sb.append('"').append(o.toString().replace("\"", "\\\"")).append('"');
}
}
sql += " WHERE " + info.getSQLColumn(column) + " " + express.value() + " (" + sb + ")";
}
sql += " (" + sb + ")";
} else if (key.getClass().isArray()) {
StringBuilder sb = new StringBuilder();
int len = Array.getLength(key);
for (int i = 0; i < len; i++) {
Object o = Array.get(key, i);
if (sb.length() > 0) sb.append(',');
if (o instanceof Number) {
sb.append('"').append(o).append('"');
} else {
sb.append('"').append(o.toString().replace("\"", "\\\"")).append('"');
if (len == 0) {
sql += " WHERE 1 " + (express == FilterExpress.NOTIN ? "=" : "!=") + " 1";
} else {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < len; i++) {
Object o = Array.get(key, i);
if (sb.length() > 0) sb.append(',');
if (o instanceof Number) {
sb.append('"').append(o).append('"');
} else {
sb.append('"').append(o.toString().replace("\"", "\\\"")).append('"');
}
}
sql += " WHERE " + info.getSQLColumn(column) + " " + express.value() + " (" + sb + ")";
}
sql += " (" + sb + ")";
} else {
sql += " \"" + key.toString().replace("\"", "\\\"") + "\"";
sql += " WHERE " + info.getSQLColumn(column) + " " + express.value() + " \"" + key.toString().replace("\"", "\\\"") + "\"";
}
return sql;
}

View File

@@ -8,7 +8,9 @@ import java.io.*;
import java.lang.reflect.Field;
import java.net.*;
import java.nio.ByteBuffer;
import java.nio.charset.*;
import java.util.*;
import javax.net.ssl.*;
/**
*
@@ -16,6 +18,8 @@ import java.util.*;
*/
public final class Utility {
private static final Charset UTF_8 = Charset.forName("UTF-8");
private static final char hex[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
private static final sun.misc.Unsafe UNSAFE;
@@ -295,6 +299,60 @@ public final class Utility {
}
//-----------------------------------------------------------------------------
public static String postHttpContent(String url) throws IOException {
return remoteHttpContent(null, "POST", url, null).toString("UTF-8");
}
public static String postHttpContent(String url, String body) throws IOException {
return remoteHttpContent(null, "POST", url, body).toString("UTF-8");
}
public static String getHttpContent(String url) throws IOException {
return remoteHttpContent(null, "GET", url, null).toString("UTF-8");
}
public static byte[] getHttpBytesContent(String url) throws IOException {
return remoteHttpContent(null, "GET", url, null).toByteArray();
}
public static String postHttpContent(SSLContext ctx, String url) throws IOException {
return remoteHttpContent(ctx, "POST", url, null).toString("UTF-8");
}
public static String postHttpContent(SSLContext ctx, String url, String body) throws IOException {
return remoteHttpContent(ctx, "POST", url, body).toString("UTF-8");
}
public static String getHttpContent(SSLContext ctx, String url) throws IOException {
return remoteHttpContent(ctx, "GET", url, null).toString("UTF-8");
}
public static byte[] getHttpBytesContent(SSLContext ctx, String url) throws IOException {
return remoteHttpContent(ctx, "GET", url, null).toByteArray();
}
protected static ByteArrayOutputStream remoteHttpContent(SSLContext ctx, String method, String url, String body) throws IOException {
HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
conn.setConnectTimeout(3000);
conn.setReadTimeout(3000);
if (ctx != null && conn instanceof HttpsURLConnection) ((HttpsURLConnection) conn).setSSLSocketFactory(ctx.getSocketFactory());
conn.setRequestMethod(method);
if (body != null) {
conn.setDoOutput(true);
conn.getOutputStream().write(body.getBytes(UTF_8));
}
conn.connect();
InputStream in = conn.getInputStream();
ByteArrayOutputStream out = new ByteArrayOutputStream(1024);
byte[] bytes = new byte[1024];
int pos;
while ((pos = in.read(bytes)) != -1) {
out.write(bytes, 0, pos);
}
conn.disconnect();
return out;
}
public static String read(InputStream in) throws IOException {
return read(in, "UTF-8");
}