📄 httpproxyhandler.java
字号:
/**
* @作者: 胡俊
* @创建时间: 2008-03-05
* @更新时间: 2008-03-05
* @描述:
*
*/
package com.nari.pmos.proxy.client;
import java.io.*;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLConnection;
/**
* @author hujun
*
* TODO To change the template for this generated type comment go to Window -
* Preferences - Java - Code Style - Code Templates
*/
public class HttpProxyHandler implements InvocationHandler {
private String sessionId = "";
private String proxyMode = "";
private String httpURL = "";
private String usrAccount = "";
private String usrPwd = "";
private Class classInterface;
public HttpProxyHandler(Class classInterface, String sessionId,
String proxyMode, String httpURL, String account, String pwd) {
this.classInterface = classInterface;
this.sessionId = sessionId;
this.proxyMode = proxyMode;
this.httpURL = httpURL;
this.usrAccount = account;
this.usrPwd = pwd;
}
/**
* 在序列中发送的数据 接口名称 方法名称 参数的个数 参数数组 String String Integer
*/
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
Object r = null;
Class[] classes = method.getParameterTypes();
String[] classes_type = new String[classes.length];
Integer N = new Integer(classes.length);
int i = 0;
int n = N.intValue();
// === 因java无法正确转换 int double,所以作如下处理(原因没有弄清楚) ===
for (i = 0; i < n; i++) {
classes_type[i] = "";
if (classes[i].getName().equals("int")) {
classes[i] = Integer.class;
classes_type[i] = "int";
} else if (classes[i].getName().equals("double")) {
classes[i] = Double.class;
classes_type[i] = "double";
}
if (classes[i].getName().equals("boolean")) {
classes[i] = Boolean.class;
classes_type[i] = "boolean";
}
}
// ===========================================================
// === 通过http协议访问 servlet ===============================
try {
String urlStr = null;
if (sessionId.length() > 0) {
urlStr = httpURL + ";jseeeionid=" + sessionId + "?proxymode="
+ proxyMode;
} else {
urlStr = httpURL + "?proxymode=" + proxyMode;
}
// System.out.println("httpURL: " + httpURL);
URL url = new URL(urlStr);
URLConnection urlconn = url.openConnection();
// 初始化连接
urlconn.setUseCaches(false);
urlconn.setDoOutput(true);
urlconn.setDoInput(true);
// 准备,然后向http服务器发送数据(串行化对象),http服务器由相应的servlet接收并处理
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(byteOut);
// System.out.println("开始写调用参数对象......");
out.writeObject(usrAccount); // user account
out.writeObject(usrPwd); // user password
out.writeObject(classInterface.getName()); // 被调用的EJB对应的JNDI名称
out.writeObject(method.getName()); // 被调用的EJB对应的方法名
out.writeObject(N); // 增加一个参数表示被调用的EJB方法的参数个数
// System.out.println("PmosHttpProxyHandler...1");
// 写入被调用的EJB方法的参数数据
for (i = 0; i < n; i++) {
out.writeObject(classes[i]);
}
;
for (i = 0; i < n; i++) {
out.writeObject(args[i]);
}
for (i = 0; i < n; i++) {
out.writeObject(classes_type[i]);
}
// Flush 数据缓冲区
out.flush();
byte buf[] = byteOut.toByteArray();
// System.out.println("PmosHttpProxyHandler...2");
// 设置属性
urlconn.setRequestProperty("Content-type",
"application/octet-stream");
urlconn.setRequestProperty("Content-length", "" + buf.length);
// 发送数据
DataOutputStream dataOut = new DataOutputStream(urlconn
.getOutputStream());
dataOut.write(buf);
dataOut.flush();
dataOut.close();
// 等待并读取服务器响应数据
ObjectInputStream in = new ObjectInputStream(urlconn
.getInputStream());
// 读取数据
// System.out.println("PmosHttpProxyHandler...3");
N = (Integer) in.readObject();
if (N.intValue() == 1)
r = in.readObject();
in.close();
} catch (Exception e) {
System.out
.println("--- PmosHttpProxyHandler Problem accessing the response text. --- ");
e.printStackTrace();
return "--- PmosHttpProxyHandler Problem accessing the response text. --- ";
}
// ===========================================================
return r;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -