📄 net.java
字号:
os.writeUTF(command);
os.flush();
os.close();
return connection.getInputStream();
}
public InputStream sendCommand(String module, String command, String argument, URL host, String passwd)
throws IOException, UnknownServiceException {
URL url = new URL(host, module + (sessionId != null ? ";jsessionid=" + sessionId : ""));
URLConnection connection = url.openConnection();
if (!passwd.equals("")) {
connection.setRequestProperty("Authorization", "Basic " + passwd);
}
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setUseCaches(false);
connection.setRequestProperty("Content-Type", "application/octet-stream");
ObjectOutputStream os = new ObjectOutputStream(connection.getOutputStream());
os.writeUTF(command);
os.writeUTF(argument);
os.flush();
os.close();
return connection.getInputStream();
}
public InputStream sendCommand(String module, String command, Object argument, URL host, String passwd)
throws IOException, UnknownServiceException {
URL url = new URL(host, module + (sessionId != null ? ";jsessionid=" + sessionId : ""));
URLConnection connection = url.openConnection();
if (!passwd.equals("")) {
connection.setRequestProperty("Authorization", "Basic " + passwd);
}
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setUseCaches(false);
connection.setRequestProperty("Content-Type", "application/octet-stream");
ObjectOutputStream os = new ObjectOutputStream(connection.getOutputStream());
os.writeUTF(command);
os.writeObject(argument);
os.flush();
os.close();
return connection.getInputStream();
}
public synchronized boolean isConnected() {
if (GlobalContext.getInstance().isConnection()) {
if (GlobalContext.getInstance().getPreferenceInfo().isDebug()) {
System.out.println("Connection skip");
}
return true;
}
/* int cnt = 0;
while (true) {
if (!GlobalContext.getInstance().isConnection()) {
break;
}
cnt++;
if (cnt == CONNCTION_MAX_RETRY_COUNT) {
if (GlobalContext.getInstance().getPreferenceInfo().isDebug()) {
System.out.println("Connection retry over");
}
return true;
}
if (GlobalContext.getInstance().getPreferenceInfo().isDebug()) {
System.out.println("Connection skip " + cnt);
}
try {
wait(CONNECTION_CHECK_PERIOD);
/// Thread.sleep(CONNECTION_CHECK_PERIOD);
} catch (InterruptedException e) {
}
}*/
return false;
}
private URL getURL(String module) throws MalformedURLException {
URL url = null;
if (!isApplication()) {
url = new URL(new URL("http://127.0.0.1:8080"), "SPS/servlet/"+module + (sessionId != null ? ";jsessionid=" + sessionId : ""));
// url = new URL(applet.getDocumentBase(), module + (sessionId != null ? ";jsessionid=" + sessionId : ""));// + "?cmd=" + (argument == null ? command : command + "&" + argument));// + "?" + "counter=" + counter++);
} else {
url = new URL(new URL(docBase), module);
// System.out.println("applet.getDocumentBase() = " + applet.getDocumentBase());
// url = new URL(new URL("http://127.0.0.1:8080/SPS/servlet/terminal.mst") + (sessionId != null ? ";jsessionid=" + sessionId : ""));
url = new URL(new URL("http://127.0.0.1:8080"), "SPS/servlet/"+module + (sessionId != null ? ";jsessionid=" + sessionId : ""));
}
return url;
}
private SSLSocketFactory getSSLSocketFactory() throws NoSuchAlgorithmException, KeyManagementException {
SSLSocketFactory sslf = null;
SSLContext sslContext = null;
KeyManager[] km = { new RelaxedX509KeyManager() };
TrustManager[] tm = { new RelaxedX509TrustManager() };
sslContext = SSLContext.getInstance("SSL");
sslContext.init(km, tm, new SecureRandom());
if (sslContext != null) {
sslf = sslContext.getSocketFactory();
}
return sslf;
}
private InputStream connectHttps(URL url, String command, Object argument) throws IOException, NoSuchAlgorithmException, KeyManagementException {
HttpsURLConnection connection = (HttpsURLConnection)url.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setUseCaches(false);
if (argument != null && !argument.equals(String.class)) {
connection.setRequestProperty("Content-Type", "application/octet-stream");
}
SSLSocketFactory sslf = getSSLSocketFactory();
if (sslf != null) {
connection.setSSLSocketFactory(sslf);
}
connection.connect();
/* Certificate[] cer = connection.getServerCertificates();
for (int i = 0; i < cer.length; i++) {
System.out.println("certificate = " + cer[i]);
}*/
/* if (argument != null) {
System.out.println("argument = " + argument);
}*/
ObjectOutputStream os = new ObjectOutputStream(connection.getOutputStream());
os.writeUTF(command);
os.writeInt(isApplication() ? 0 : 1);
if (argument != null && argument.getClass().equals(String.class)) {
os.writeUTF((String)argument);
} else {
os.writeObject(argument);
}
os.flush();
os.close();
return connection.getInputStream();
}
private InputStream connectHttp(URL url, String command, Object argument) throws IOException {
URLConnection connection = url.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setUseCaches(false);
if (argument != null && !argument.equals(String.class)) {
connection.setRequestProperty("Content-Type", "application/octet-stream");
}
ObjectOutputStream os = new ObjectOutputStream(connection.getOutputStream());
os.writeUTF(command);
// TEST os.writeInt(0) TODO
//os.writeInt(isApplication() ? 0 : 1);
os.writeInt(0);
///System.out.println("Command = " + command + " argument = " + argument);
if (argument != null && argument.getClass().equals(String.class)) {
/*if (command.equals("GetJobList")) {
System.out.println("argument is String");
}*/
os.writeUTF((String)argument);
} else {
/*if (command.equals("GetJobList")) {
System.out.println("argument is Object");
}*/
os.writeObject(argument);
}
os.flush();
os.close();
return connection.getInputStream();
}
private boolean isApplication() {
if (applet == null) {
return true;
}
return false;
}
private boolean isHttps(URL url) {
if (url.getProtocol().equals("https")) {
return true;
}
return false;
}
class RelaxedX509KeyManager implements X509KeyManager {
public String chooseClientAlias(String[] keyType, Principal[] issuers, Socket socket) {
return null;
}
public String chooseServerAlias(String keyType, Principal[] issuers, Socket socket) {
return null;
}
public X509Certificate[] getCertificateChain(String alias) {
return null;
}
public String[] getClientAliases(String keyType, Principal[] issuers) {
return null;
}
public String[] getServerAliases(String keyType, Principal[] issuers) {
return null;
}
public PrivateKey getPrivateKey(String keyType) {
return null;
}
}
class RelaxedX509TrustManager implements X509TrustManager {
/**
* 怣棅偝傟側偄徹柧彂偱傕嫮惂揑偵擣徹偡傞乮僋儔僀傾儞僩擣徹乯
*/
public boolean isClientTrusted(X509Certificate[] chain){
return true;
}
/**
* 怣棅偝傟側偄徹柧彂偱傕嫮惂揑偵擣徹偡傞
*/
public boolean isServerTrusted(X509Certificate[] chain){
return true;
}
/**
* 徹柧彂傪曉偡
*/
public X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkClientTrusted(X509Certificate[] chain, String authType) {}
public void checkServerTrusted(X509Certificate[] chain, String authType) {}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -