2020-05-02 16:14:47 +00:00
|
|
|
import '@ccms/nashorn'
|
2019-09-27 10:39:03 +00:00
|
|
|
|
2020-09-17 09:44:48 +00:00
|
|
|
const URL = Java.type("java.net.URL")
|
|
|
|
const Files = Java.type("java.nio.file.Files")
|
|
|
|
const StandardCopyOption = Java.type("java.nio.file.StandardCopyOption")
|
|
|
|
const JavaString = Java.type("java.lang.String")
|
|
|
|
const SecureRandom = Java.type("java.security.SecureRandom")
|
|
|
|
const SSLContext = Java.type("javax.net.ssl.SSLContext")
|
|
|
|
const HttpsURLConnection = Java.type("javax.net.ssl.HttpsURLConnection")
|
|
|
|
const HostnameVerifier = Java.type("javax.net.ssl.HostnameVerifier")
|
|
|
|
const X509TrustManager = Java.type("javax.net.ssl.X509TrustManager")
|
|
|
|
|
|
|
|
const SocketTimeoutException = Java.type('java.net.SocketTimeoutException')
|
|
|
|
|
|
|
|
const Callable = Java.type('java.util.concurrent.Callable')
|
2019-09-27 10:39:03 +00:00
|
|
|
const Executors = Java.type('java.util.concurrent.Executors')
|
|
|
|
|
|
|
|
const UTF_8 = "UTF-8"
|
|
|
|
|
2020-09-17 09:44:48 +00:00
|
|
|
const TrustAnyHostnameVerifier = new HostnameVerifier({ verify: () => true })
|
2019-09-27 10:39:03 +00:00
|
|
|
|
|
|
|
const SSLSocketFactory = function initSSLSocketFactory() {
|
2020-09-17 09:44:48 +00:00
|
|
|
let sslContext = SSLContext.getInstance("TLS")
|
2019-09-27 10:39:03 +00:00
|
|
|
sslContext.init(null, [new X509TrustManager({
|
|
|
|
getAcceptedIssuers: () => null,
|
|
|
|
checkClientTrusted: () => { },
|
|
|
|
checkServerTrusted: () => { }
|
2020-09-17 09:44:48 +00:00
|
|
|
})], new SecureRandom())
|
|
|
|
return sslContext.getSocketFactory()
|
|
|
|
}()
|
2019-09-27 10:39:03 +00:00
|
|
|
|
|
|
|
interface Future<T> {
|
2020-09-17 09:44:48 +00:00
|
|
|
cancel(): boolean
|
|
|
|
isCancelled(): boolean
|
|
|
|
isDone(): boolean
|
|
|
|
get(): T
|
2019-09-27 10:39:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
enum ReadyState {
|
|
|
|
UNSENT,//Client has been created. open() not called yet.
|
|
|
|
OPENED,//open() has been called.
|
|
|
|
HEADERS_RECEIVED,//send() has been called, and headers and status are available.
|
|
|
|
LOADING,//Downloading; responseText holds partial data.
|
|
|
|
DONE,//The operation is complete.
|
|
|
|
}
|
2019-11-04 12:20:08 +00:00
|
|
|
type RequestMethod =
|
|
|
|
| 'get' | 'GET'
|
|
|
|
| 'delete' | 'DELETE'
|
|
|
|
| 'head' | 'HEAD'
|
|
|
|
| 'options' | 'OPTIONS'
|
|
|
|
| 'post' | 'POST'
|
|
|
|
| 'put' | 'PUT'
|
2020-09-17 09:44:48 +00:00
|
|
|
| 'patch' | 'PATCH'
|
2019-11-04 12:20:08 +00:00
|
|
|
type ResponseType =
|
|
|
|
| 'arraybuffer'
|
|
|
|
| 'blob'
|
|
|
|
| 'document'
|
|
|
|
| 'json'
|
|
|
|
| 'text'
|
2020-09-17 09:44:48 +00:00
|
|
|
| 'stream'
|
2019-11-04 12:20:08 +00:00
|
|
|
type EventType =
|
|
|
|
| 'load'
|
|
|
|
| 'error'
|
|
|
|
| 'abort'
|
|
|
|
| 'progress'
|
|
|
|
| 'timeout'
|
|
|
|
| 'loadend'
|
2020-09-17 09:44:48 +00:00
|
|
|
| 'loadstart'
|
|
|
|
type HttpHeader = { [key: string]: string }
|
2019-11-04 12:20:08 +00:00
|
|
|
|
2019-09-27 10:39:03 +00:00
|
|
|
|
2020-09-17 09:44:48 +00:00
|
|
|
const executor = Executors.newCachedThreadPool()
|
2019-09-27 10:39:03 +00:00
|
|
|
|
|
|
|
export class XMLHttpRequest {
|
2020-09-17 09:44:48 +00:00
|
|
|
private _timeout: number
|
2019-11-04 12:20:08 +00:00
|
|
|
private _responseType: ResponseType = 'text';
|
2020-09-17 09:44:48 +00:00
|
|
|
private _withCredentials: boolean
|
2019-09-27 10:39:03 +00:00
|
|
|
|
|
|
|
private _readyState: ReadyState = ReadyState.UNSENT;
|
|
|
|
|
2020-09-17 09:44:48 +00:00
|
|
|
private _method: RequestMethod
|
|
|
|
private _url: string
|
|
|
|
private _async: boolean
|
|
|
|
private _mimeType: string
|
2020-05-11 14:13:03 +00:00
|
|
|
private _requestHeaders: HttpHeader = {};
|
2019-09-27 10:39:03 +00:00
|
|
|
|
|
|
|
private _status: number = 0;
|
|
|
|
private _statusText: string = null;
|
2020-09-17 09:44:48 +00:00
|
|
|
private _response: any
|
|
|
|
private _responseText: any
|
|
|
|
private _responseURL: string
|
2020-05-07 09:12:15 +00:00
|
|
|
private _responseHeaders: HttpHeader = {};
|
2019-09-27 10:39:03 +00:00
|
|
|
|
|
|
|
private _connection = null;
|
|
|
|
|
|
|
|
get timeout() {
|
2020-09-17 09:44:48 +00:00
|
|
|
return this._timeout
|
2019-09-27 10:39:03 +00:00
|
|
|
}
|
|
|
|
set timeout(timeout: number) {
|
|
|
|
this._timeout = timeout
|
|
|
|
}
|
|
|
|
get readyState() {
|
2020-09-17 09:44:48 +00:00
|
|
|
return this._readyState
|
2019-09-27 10:39:03 +00:00
|
|
|
}
|
|
|
|
set responseType(type: ResponseType) {
|
2020-09-17 09:44:48 +00:00
|
|
|
this._responseType = type
|
2019-09-27 10:39:03 +00:00
|
|
|
}
|
|
|
|
get responseType() {
|
2020-09-17 09:44:48 +00:00
|
|
|
return this._responseType
|
2019-09-27 10:39:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
get status() {
|
2020-09-17 09:44:48 +00:00
|
|
|
return this._status
|
2019-09-27 10:39:03 +00:00
|
|
|
}
|
|
|
|
get statusText() {
|
2020-09-17 09:44:48 +00:00
|
|
|
return this._statusText
|
2019-09-27 10:39:03 +00:00
|
|
|
}
|
|
|
|
get response() {
|
2020-09-17 09:44:48 +00:00
|
|
|
return this._response || this.get()
|
2019-09-27 10:39:03 +00:00
|
|
|
}
|
|
|
|
get responseText() {
|
2020-09-17 09:44:48 +00:00
|
|
|
return this._responseText
|
2019-09-27 10:39:03 +00:00
|
|
|
}
|
|
|
|
get responseXML() {
|
2020-09-17 09:44:48 +00:00
|
|
|
return this._response
|
2019-09-27 10:39:03 +00:00
|
|
|
}
|
|
|
|
get responseURL() {
|
2020-09-17 09:44:48 +00:00
|
|
|
return this._responseURL
|
2019-09-27 10:39:03 +00:00
|
|
|
}
|
|
|
|
|
2020-09-17 09:44:48 +00:00
|
|
|
public onload: () => void
|
|
|
|
public onerror: (ex: Error) => void
|
|
|
|
public onabort: () => void
|
|
|
|
public onprogress: () => void
|
|
|
|
public ontimeout: (ex: Error) => void
|
|
|
|
public onloadend: () => void
|
|
|
|
public onloadstart: () => void
|
|
|
|
public onreadystatechange: () => void
|
2019-09-27 10:39:03 +00:00
|
|
|
|
|
|
|
setRequestHeader(key: string, val: string) {
|
2020-09-17 09:44:48 +00:00
|
|
|
this._requestHeaders[key] = val
|
2019-09-27 10:39:03 +00:00
|
|
|
}
|
|
|
|
getResponseHeader(key: string): string {
|
2020-09-17 09:44:48 +00:00
|
|
|
return this._responseHeaders[key]
|
2019-09-27 10:39:03 +00:00
|
|
|
}
|
|
|
|
getAllResponseHeaders(): any {
|
2020-09-17 09:44:48 +00:00
|
|
|
return this._responseHeaders
|
2019-09-27 10:39:03 +00:00
|
|
|
}
|
|
|
|
addEventListener(event: EventType, callback: Function) {
|
2020-09-17 09:44:48 +00:00
|
|
|
this[`on${event.toLowerCase()}`] = callback
|
2019-09-27 10:39:03 +00:00
|
|
|
}
|
|
|
|
overrideMimeType(mimeType: string) {
|
2020-09-17 09:44:48 +00:00
|
|
|
this._mimeType = mimeType
|
2019-09-27 10:39:03 +00:00
|
|
|
}
|
|
|
|
open(method: RequestMethod, url: string, async: boolean = true, user?: string, password?: string) {
|
2020-05-07 16:50:30 +00:00
|
|
|
if (this._readyState !== ReadyState.UNSENT) { throw new Error(`Error Status ${this._readyState}!`) }
|
|
|
|
|
2020-09-17 09:44:48 +00:00
|
|
|
this._method = method
|
|
|
|
this._url = url
|
|
|
|
this._async = async
|
2019-09-27 10:39:03 +00:00
|
|
|
|
|
|
|
this._connection = new URL(this._url).openConnection()
|
|
|
|
if (this._connection instanceof HttpsURLConnection) {
|
2020-09-17 09:44:48 +00:00
|
|
|
this._connection.setHostnameVerifier(TrustAnyHostnameVerifier)
|
|
|
|
this._connection.setSSLSocketFactory(SSLSocketFactory)
|
2019-09-27 10:39:03 +00:00
|
|
|
}
|
2020-09-17 09:44:48 +00:00
|
|
|
this._connection.setRequestMethod(this._method)
|
|
|
|
this._connection.setDoOutput(true)
|
|
|
|
this._connection.setDoInput(true)
|
|
|
|
this._connection.setConnectTimeout(this._timeout)
|
|
|
|
this._connection.setReadTimeout(this._timeout)
|
2019-09-27 10:39:03 +00:00
|
|
|
|
2020-09-17 09:44:48 +00:00
|
|
|
this.setReadyState(ReadyState.OPENED)
|
2019-09-27 10:39:03 +00:00
|
|
|
}
|
|
|
|
send(body?: string | object): Future<string> {
|
2020-05-11 14:13:03 +00:00
|
|
|
for (const header in this._requestHeaders) {
|
2020-09-17 09:44:48 +00:00
|
|
|
this._connection.setRequestProperty(header, this._requestHeaders[header])
|
2020-05-11 14:13:03 +00:00
|
|
|
}
|
2019-09-27 10:39:03 +00:00
|
|
|
if (this._readyState !== ReadyState.OPENED) { throw new Error(`Error Status ${this._readyState}!`) }
|
2020-09-17 09:44:48 +00:00
|
|
|
let future = executor.submit(new Callable({ call: () => this._send(body) }))
|
2019-09-27 10:39:03 +00:00
|
|
|
if (!this._async) { future.get() }
|
2020-09-17 09:44:48 +00:00
|
|
|
return future
|
2019-09-27 10:39:03 +00:00
|
|
|
}
|
2020-02-23 14:33:51 +00:00
|
|
|
get() {
|
2020-05-12 06:22:31 +00:00
|
|
|
if (this._response === undefined) {
|
|
|
|
switch (this._responseType) {
|
|
|
|
case "json":
|
2020-09-17 09:44:48 +00:00
|
|
|
return this._response = JSON.parse(this._responseText)
|
2020-05-12 06:22:31 +00:00
|
|
|
case "text":
|
2020-09-17 09:44:48 +00:00
|
|
|
return this._response = this._responseText
|
2020-05-12 06:22:31 +00:00
|
|
|
default:
|
|
|
|
throw Error(`Unsupport ResponseType: ${this._responseType} !`)
|
|
|
|
}
|
2020-02-23 14:33:51 +00:00
|
|
|
}
|
2020-09-17 09:44:48 +00:00
|
|
|
return this._response
|
2020-02-23 14:33:51 +00:00
|
|
|
}
|
2019-09-27 10:39:03 +00:00
|
|
|
abort() {
|
2020-09-17 09:44:48 +00:00
|
|
|
this._connection.disconnect()
|
|
|
|
this.onabort && this.onabort()
|
2019-09-27 10:39:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private _send(body?: string | object) {
|
|
|
|
try {
|
2020-09-17 09:44:48 +00:00
|
|
|
this._connection.connect()
|
|
|
|
this.onloadstart && this.onloadstart()
|
2019-09-27 10:39:03 +00:00
|
|
|
if (body) {
|
2020-09-17 09:44:48 +00:00
|
|
|
let bodyType = Object.prototype.toString.call(body)
|
2020-05-07 16:50:30 +00:00
|
|
|
if (typeof body !== "string") { throw new Error(`body(${bodyType}) must be string!`) }
|
2020-09-17 09:44:48 +00:00
|
|
|
var out = this._connection.getOutputStream()
|
|
|
|
out.write(new JavaString(body).getBytes(UTF_8))
|
|
|
|
out.flush()
|
|
|
|
out.close()
|
2019-09-27 10:39:03 +00:00
|
|
|
}
|
2020-09-17 09:44:48 +00:00
|
|
|
this.setReadyState(ReadyState.LOADING)
|
|
|
|
this._status = this._connection.getResponseCode()
|
|
|
|
this._statusText = this._connection.getResponseMessage()
|
2019-09-27 10:39:03 +00:00
|
|
|
if (this._status >= 0 && this._status < 300) {
|
2020-09-17 09:44:48 +00:00
|
|
|
this._responseText = this.readOutput(this._connection.getInputStream())
|
2019-09-27 10:39:03 +00:00
|
|
|
} else if (this._status >= 300 && this._status < 400) {
|
2020-09-17 09:44:48 +00:00
|
|
|
this._responseURL = this.getResponseHeader('Location')
|
2019-09-27 10:39:03 +00:00
|
|
|
} else {
|
2020-09-17 09:44:48 +00:00
|
|
|
this._responseText = this.readOutput(this._connection.getErrorStream())
|
2019-09-27 10:39:03 +00:00
|
|
|
}
|
2020-09-17 09:44:48 +00:00
|
|
|
this.setResponseHeaders(this._connection.getHeaderFields())
|
|
|
|
this.onloadend && this.onloadend()
|
2019-09-27 10:39:03 +00:00
|
|
|
} catch (ex) {
|
2019-11-04 12:20:08 +00:00
|
|
|
if (ex instanceof SocketTimeoutException && this.ontimeout) {
|
|
|
|
return this.ontimeout(ex)
|
|
|
|
} else if (this.onerror) {
|
2020-09-17 09:44:48 +00:00
|
|
|
return this.onerror(ex)
|
2019-09-27 10:39:03 +00:00
|
|
|
}
|
2020-09-17 09:44:48 +00:00
|
|
|
throw ex
|
2019-09-27 10:39:03 +00:00
|
|
|
} finally {
|
2020-09-17 09:44:48 +00:00
|
|
|
this._connection.disconnect()
|
|
|
|
this.setReadyState(ReadyState.DONE)
|
2019-09-27 10:39:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-04 12:20:08 +00:00
|
|
|
private setResponseHeaders(header: any) {
|
2020-05-07 09:12:15 +00:00
|
|
|
header.forEach((key: string | number, value: string | any[]) => {
|
2020-09-17 09:44:48 +00:00
|
|
|
this._responseHeaders[key + ''] = value[value.length - 1] + ''
|
|
|
|
})
|
2019-11-04 12:20:08 +00:00
|
|
|
}
|
|
|
|
|
2019-09-27 10:39:03 +00:00
|
|
|
private setReadyState(state: ReadyState) {
|
2020-09-17 09:44:48 +00:00
|
|
|
this._readyState = state
|
|
|
|
this.onreadystatechange && this.onreadystatechange()
|
2019-09-27 10:39:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private readOutput(input: any) {
|
2020-09-17 09:44:48 +00:00
|
|
|
var tempFile = Files.createTempFile('xhr', '.response')
|
|
|
|
Files.copy(input, tempFile, StandardCopyOption['REPLACE_EXISTING']); tempFile.toFile().deleteOnExit()
|
|
|
|
return new JavaString(Files.readAllBytes(tempFile), 'UTF-8')
|
2019-09-27 10:39:03 +00:00
|
|
|
}
|
|
|
|
}
|