Java Code Examples for org.mozilla.javascript.NativeObject#put()

The following examples show how to use org.mozilla.javascript.NativeObject#put() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ScriptedIncomingMapping.java    From ditto with Eclipse Public License 2.0 6 votes vote down vote up
static NativeObject mapExternalMessageToNativeObject(final ExternalMessage message) {
    final NativeObject headersObj = new NativeObject();
    message.getHeaders().forEach((key, value) -> headersObj.put(key, headersObj, value));

    final NativeArrayBuffer bytePayload =
            message.getBytePayload()
                    .map(bb -> {
                        final NativeArrayBuffer nativeArrayBuffer = new NativeArrayBuffer(bb.remaining());
                        bb.get(nativeArrayBuffer.getBuffer());
                        return nativeArrayBuffer;
                    })
                    .orElse(null);

    final String contentType = message.getHeaders().get(ExternalMessage.CONTENT_TYPE_HEADER);
    final String textPayload = message.getTextPayload().orElse(null);

    final NativeObject externalMessage = new NativeObject();
    externalMessage.put(EXTERNAL_MESSAGE_HEADERS, externalMessage, headersObj);
    externalMessage.put(EXTERNAL_MESSAGE_TEXT_PAYLOAD, externalMessage, textPayload);
    externalMessage.put(EXTERNAL_MESSAGE_BYTE_PAYLOAD, externalMessage, bytePayload);
    externalMessage.put(EXTERNAL_MESSAGE_CONTENT_TYPE, externalMessage, contentType);
    return externalMessage;
}
 
Example 2
Source File: JSGIRequest.java    From io with Apache License 2.0 5 votes vote down vote up
/**
 * サーブレットのリクエストからJavaScriptのJSGIオブジェクトを生成する.
 * @return JavaScript用JSGIオブジェクト
 */
public NativeObject getRequestObject() {
    NativeObject request = new NativeObject();
    request.put("method", request, this.req.getMethod());
    request.put("host", request, this.req.getAttribute("host").toString());
    request.put("port", request, this.req.getAttribute("port").toString());
    request.put("scriptName", request, this.req.getAttribute("scriptName").toString());
    request.put("pathInfo", request, this.req.getPathInfo());
    // サーバ動作時のPOSTでservlet.getQueryStringでクエリが取れないために以下の取り方で実現。
    String[] urlItems = this.req.getAttribute("env.requestUri").toString().split("\\?");
    String queryString = "";
    if (urlItems.length > 1) {
        queryString = urlItems[1];
    }
    request.put("queryString", request, queryString);
    request.put("scheme", request, this.req.getAttribute("scheme").toString());

    request.put("headers", request, this.headers());

    request.put("env", request, this.env());

    request.put("jsgi", request, this.makeJSGI());

    request.put("input", request, this.input);

    dump(request);
    return request;
}
 
Example 3
Source File: JSGIRequest.java    From io with Apache License 2.0 5 votes vote down vote up
private NativeObject makeJSGI() {
    NativeObject jsgi = new NativeObject();
    NativeArray version = new NativeArray(new Object[]{JSGI_VERSION_MAJOR, JSGI_VERSION_MINOR});
    jsgi.put("version", jsgi, version);
    jsgi.put("errors", jsgi, "");
    jsgi.put("multithread", jsgi, "");
    jsgi.put("multiprocess", jsgi, "");
    jsgi.put("run_once", jsgi, "");
    jsgi.put("cgi", jsgi, "");
    jsgi.put("ext", jsgi, "");
    return jsgi;
}
 
Example 4
Source File: PostmanJsVariables.java    From postman-runner with Apache License 2.0 5 votes vote down vote up
private void prepareJsVariables(PostmanHttpResponse httpResponse) {

		this.responseCode = new NativeObject();
		if (httpResponse != null) {
			List<Object> headerList = new ArrayList<Object>(httpResponse.headers.size());
			for (Map.Entry<String, String> h : httpResponse.headers.entrySet()) {
				NativeObject hobj = new NativeObject();
				hobj.put("key", hobj, h.getKey());
				hobj.put("value", hobj, h.getValue());
				headerList.add(hobj);
			}
			this.responseHeaders = new NativeArray(headerList.toArray());
			this.responseBody = Context.javaToJS(httpResponse.body, scope);

			this.responseCode.put("code", responseCode, httpResponse.code);
		} else {
			this.responseHeaders = new NativeArray(new Object[] {});
			this.responseBody = Context.javaToJS("", scope);

			this.responseCode.put("code", responseCode, 0);
		}

		// TODO: fix me
		this.responseTime = Context.javaToJS(0.0, scope);

		// TODO: fix me
		this.iteration = Context.javaToJS(0, scope);

		// The postman js var is only used to setEnvironmentVariables()
		this.postman = Context.javaToJS(this.env, scope);

		this.environment = new NativeObject();
		Set<Map.Entry<String, PostmanEnvValue>> map = this.env.lookup.entrySet();
		for (Map.Entry<String, PostmanEnvValue> e : map) {
			this.environment.put(e.getKey(), environment, e.getValue().value);
		}

		this.tests = new NativeObject();
		this.preRequestScript = new NativeObject();
	}
 
Example 5
Source File: HostObjectUtils.java    From carbon-apimgt with Apache License 2.0 4 votes vote down vote up
public static NativeObject sendHttpHEADRequest(String urlVal, String invalidStatusCodesRegex) {
    boolean isConnectionError = true;
    String response = null;
    NativeObject data = new NativeObject();
    //HttpClient client = new DefaultHttpClient();
    HttpHead head = new HttpHead(urlVal);
    //Change implementation to use http client as default http client do not work properly with mutual SSL.
    org.apache.commons.httpclient.HttpClient clientnew = new org.apache.commons.httpclient.HttpClient();
    // extract the host name and add the Host http header for sanity
    head.addHeader("Host", urlVal.replaceAll("https?://", "").replaceAll("(/.*)?", ""));
    clientnew.getParams().setParameter("http.socket.timeout", 4000);
    clientnew.getParams().setParameter("http.connection.timeout", 4000);
    HttpMethod method = new HeadMethod(urlVal);

    if (System.getProperty(APIConstants.HTTP_PROXY_HOST) != null &&
            System.getProperty(APIConstants.HTTP_PROXY_PORT) != null) {
        if (log.isDebugEnabled()) {
            log.debug("Proxy configured, hence routing through configured proxy");
        }
        String proxyHost = System.getProperty(APIConstants.HTTP_PROXY_HOST);
        String proxyPort = System.getProperty(APIConstants.HTTP_PROXY_PORT);
        clientnew.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY,
                new HttpHost(proxyHost, Integer.parseInt(proxyPort)));
    }

    try {
        int statusCodeNew = clientnew.executeMethod(method);
        //Previous implementation
        // HttpResponse httpResponse = client.execute(head);
        String statusCode = String.valueOf(statusCodeNew);//String.valueOf(httpResponse.getStatusLine().getStatusCode());
        String reasonPhrase = String.valueOf(statusCodeNew);//String.valueOf(httpResponse.getStatusLine().getReasonPhrase());
        //If the endpoint doesn't match the regex which specify the invalid status code, it will return success.
        if (!statusCode.matches(invalidStatusCodesRegex)) {
            if (log.isDebugEnabled() && statusCode.equals(String.valueOf(HttpStatus.SC_METHOD_NOT_ALLOWED))) {
                log.debug("Endpoint doesn't support HTTP HEAD");
            }
            response = "success";
            isConnectionError = false;

        } else {
            //This forms the real backend response to be sent to the client
            data.put("statusCode", data, statusCode);
            data.put("reasonPhrase", data, reasonPhrase);
            response = "";
            isConnectionError = false;
        }
    } catch (IOException e) {
        // sending a default error message.
        log.error("Error occurred while connecting to backend : " + urlVal + ", reason : " + e.getMessage(), e);
        String[] errorMsg = e.getMessage().split(": ");
        if (errorMsg.length > 1) {
            response = errorMsg[errorMsg.length - 1]; //This is to get final readable part of the error message in the exception and send to the client
            isConnectionError = false;
        }
    } finally {
        method.releaseConnection();
    }
    data.put("response", data, response);
    data.put("isConnectionError", data, isConnectionError);
    return data;
}