📄 reverseproxyproxiedmethod.java
字号:
package com.sslexplorer.reverseproxy;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Vector;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.maverick.http.AsyncHttpMethod;
import com.maverick.http.HttpConnection;
import com.maverick.http.HttpRequest;
import com.maverick.http.HttpResponse;
import com.sslexplorer.boot.Util;
import com.sslexplorer.security.SessionInfo;
public class ReverseProxyProxiedMethod extends AsyncHttpMethod {
private byte[] content;
private HttpRequest proxiedHeaders = new HttpRequest();
private ByteArrayOutputStream out = new ByteArrayOutputStream();
private String charsetEncoding = null;
static Log log = LogFactory.getLog(ReverseProxyProxiedMethod.class);
/**
* @param name
* @param uri
* @param parameters
* @param session
* @param i
* @param string
*/
public ReverseProxyProxiedMethod(String name, String encodedActualURI, Map parameters, SessionInfo session) { // ,
// Use the request URI's toASCIIString because its encodes correctly. If there
// are GET parameters they will already be present, if its a post then
// the execute method will deal with them
super(name, encodedActualURI);
String key;
Object val;
for(Iterator it = parameters.keySet().iterator();it.hasNext();) {
key = (String) it.next();
val = parameters.get(key);
if(val instanceof String)
setParameter(key, (String)val);
else if(val instanceof List) {
// Multiple parameter values.
List l = (List)val;
for(Iterator it2 = l.iterator(); it2.hasNext();) {
setParameter(key, (String) it2.next());
}
}
else if(val instanceof String[]) {
String[] tmp = (String[]) val;
for(int i=0;i<tmp.length;i++) {
setParameter(key, tmp[i]);
}
}
}
}
public void setCharsetEncoding(String charsetEncoding) {
this.charsetEncoding = charsetEncoding;
}
public OutputStream getOutputStream() {
return out;
}
public void setContent(byte[] content, String contentType) {
this.content = content;
proxiedHeaders.removeFields("Content-Type");
proxiedHeaders.removeFields("Content-Length");
proxiedHeaders.setHeaderField("Content-Type", contentType);
proxiedHeaders.setHeaderField("Content-Length", String.valueOf(content.length));
}
public void setContent(byte[] content) {
this.content = content;
proxiedHeaders.removeFields("Content-Length");
proxiedHeaders.setHeaderField("Content-Length", String.valueOf(content.length));
}
public HttpRequest getProxiedRequest() {
return proxiedHeaders;
}
public void executeAsync(HttpRequest request, HttpConnection connection) throws IOException {
String encodedContent = "";
/**
* Encode parameters into content if application/x-www-form-urlencoded
*/
if ((proxiedHeaders.getHeaderField("Content-type") != null && proxiedHeaders.getHeaderField("Content-type")
.equalsIgnoreCase("application/x-www-form-urlencoded"))
|| name.endsWith("POST")) {
String key;
String value;
Vector v;
for (Iterator it = parameters.keySet().iterator(); it.hasNext();) {
key = (String) it.next();
v = (Vector) parameters.get(key);
for (Iterator it2 = v.iterator(); it2.hasNext();) {
value = (String) it2.next();
encodedContent += (encodedContent.length() > 0 ? "&" : "") + Util.urlEncode(key) + "="
+ Util.urlEncode(value);
}
}
if(charsetEncoding==null)
setContent(encodedContent.getBytes(), "application/x-www-form-urlencoded");
else
setContent(encodedContent.getBytes(charsetEncoding), "application/x-www-form-urlencoded");
}
// Setup all the proxied headers
for (Enumeration e = proxiedHeaders.getHeaderFieldNames(); e.hasMoreElements();) {
String header = (String) e.nextElement();
String[] values = proxiedHeaders.getHeaderFields(header);
for (int i = 0; i < values.length; i++) {
request.setHeaderField(header, values[i]);
}
}
request.performRequest(this, connection);
if(!encodedContent.equals("")) {
connection.getOutputStream().write(encodedContent.getBytes());
}
}
public HttpResponse execute(HttpRequest request, HttpConnection connection) throws IOException {
String encodedContent = "";
/**
* Encode parameters into content if application/x-www-form-urlencoded
*/
if ((proxiedHeaders.getHeaderField("Content-type") != null
&& proxiedHeaders.getHeaderField("Content-type").equalsIgnoreCase("application/x-www-form-urlencoded"))
|| (name.endsWith("POST") && content==null)) {
String key;
String value;
Vector v;
for (Iterator it = parameters.keySet().iterator(); it.hasNext();) {
key = (String) it.next();
v = (Vector) parameters.get(key);
for (Iterator it2 = v.iterator(); it2.hasNext();) {
value = (String) it2.next();
encodedContent += (encodedContent.length() > 0 ? "&" : "") + key + "="
+ value;
}
}
if(charsetEncoding==null)
setContent(encodedContent.getBytes(), "application/x-www-form-urlencoded");
else
setContent(encodedContent.getBytes(charsetEncoding), "application/x-www-form-urlencoded");
}
// Setup all the proxied headers
for (Enumeration e = proxiedHeaders.getHeaderFieldNames(); e.hasMoreElements();) {
String header = (String) e.nextElement();
if (header.equalsIgnoreCase("Authorization"))
if (request.getHeaderField("Authorization") != null)
continue;
String[] values = proxiedHeaders.getHeaderFields(header);
for (int i = 0; i < values.length; i++) {
request.setHeaderField(header, values[i]);
}
}
request.performRequest(this, connection);
if (content != null) {
connection.getOutputStream().write(content);
}
return new HttpResponse(connection);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -