Map<String, String> headers = new HashMap<>(); for (Param h : headerParams) headers.put(h.k, Objects.toString(h.v)); for (Map.Entry<String, String> h : headers.entrySet()) xhr.setRequestHeader(h.getKey(), h.getValue()); xhr.setRequestHeader(CONTENT_TYPE, MULTIPART_FORM_DATA); FormData form = new FormData(); formParams.forEach(p -> form.append(p.k, Objects.toString(p.v))); xhr.send(form); } else { if (!headers.containsKey(CONTENT_TYPE)) xhr.setRequestHeader(CONTENT_TYPE, APPLICATION_JSON); if (!headers.containsKey(ACCEPT)) xhr.setRequestHeader(ACCEPT, APPLICATION_JSON); if (data != null) xhr.send(Global.JSON.stringify(data)); else xhr.send();
@JsIgnore public void download(Operation operation, Consumer<String> success) { Operation downloadOperation = runAs(operation); String url = downloadUrl(downloadOperation); XMLHttpRequest request = newXhr(url, GET, downloadOperation, exceptionCallback, xhr -> { int status = (int) xhr.status; String responseText = xhr.responseText; if (status == 200) { success.accept(responseText); } else { handleErrorCodes(url, status, downloadOperation, exceptionCallback); } }); request.setRequestHeader(ACCEPT.header(), APPLICATION_DMR_ENCODED); request.setRequestHeader(CONTENT_TYPE.header(), APPLICATION_DMR_ENCODED); request.send(); // Downloads are not supported in macros! }
private XMLHttpRequest newXhr(String url, HttpMethod method, Operation operation, OnError error, OnLoad onLoad) { XMLHttpRequest xhr = new XMLHttpRequest(); // The order of the XHR methods is important! Do not rearrange the code unless you know what you're doing! xhr.onload = event -> onLoad.onLoad(xhr); xhr.addEventListener("error", //NON-NLS event -> handleErrorCodes(url, (int) xhr.status, operation, error), false); xhr.open(method.name(), url, true); xhr.setRequestHeader(X_MANAGEMENT_CLIENT_NAME.header(), HEADER_MANAGEMENT_CLIENT_VALUE); String bearerToken = getBearerToken(); if (bearerToken != null) { xhr.setRequestHeader("Authorization", "Bearer " + bearerToken); } xhr.withCredentials = true; return xhr; }
private Single<ModelNode> dmr(Operation operation) { Operation dmrOperation = runAs(operation); // runAs might mutate the operation, so do it synchronously boolean get = GetOperation.isSupported(dmrOperation.getName()); String url = get ? operationUrl(dmrOperation) : endpoints.dmr(); HttpMethod method = get ? GET : POST; // ^-- those eager fields are useful if we don't want to evaluate it on each Single subscription return Single.fromEmitter(emitter -> { // in general, code inside the RX type should be able to be executed multiple times and always returns // the same result, so we need to be careful to not mutate anything (like the operation). This is useful // for example if we want to use the retry operator which will try again (subscribe again) if it fails. XMLHttpRequest xhr = newDmrXhr(url, method, dmrOperation, new DmrPayloadProcessor(), emitter::onSuccess, (op, fail) -> emitter.onError(new DispatchFailure(fail, operation)), (op, error) -> emitter.onError(error)); xhr.setRequestHeader(ACCEPT.header(), APPLICATION_DMR_ENCODED); xhr.setRequestHeader(CONTENT_TYPE.header(), APPLICATION_DMR_ENCODED); if (get) { xhr.send(); } else { xhr.send(dmrOperation.toBase64String()); } recordOperation(operation); }); }