List of usage examples for jdk.nashorn.api.scripting ScriptObjectMirror keySet
@Override
public Set<String> keySet()
From source file:com.bytelightning.opensource.pokerface.ScriptResponseProducer.java
License:Open Source License
/** * {@inheritDoc}//from w w w. j a v a 2 s . co m * This method actually does all the work of this class by invoking the endpoint, and processing it's result. */ @SuppressWarnings("unchecked") @Override public HttpResponse generateResponse() { Object result; ScriptObjectMirror som; // First call the endpoint to obtain a result which will be either an HttpResponse (in which case our work is done), or a ScriptObjectMirror with properties to create an HttpResponse ourselves. try { result = endpoint.callMember("generateResponse", request, context); if (result instanceof HttpResponse) { this.setResponse((HttpResponse) result); return response; } som = (ScriptObjectMirror) result; } catch (Exception ex) { setException(ex); return response; } Object obj; // Interpret the http statusCode int statusCode; obj = som.getMember("statusCode"); if ((obj == null) || ScriptObjectMirror.isUndefined(obj)) statusCode = HttpStatus.SC_OK; else if (obj instanceof Number) statusCode = ((Number) obj).intValue(); else if (obj instanceof ScriptObjectMirror) statusCode = (int) (((ScriptObjectMirror) obj).toNumber() + Double.MIN_VALUE); else statusCode = Integer.parseInt(obj.toString()); // Interpret the http reasonPhrase String reasonPhrase; obj = som.getMember("reasonPhrase"); if ((obj == null) || ScriptObjectMirror.isUndefined(obj)) reasonPhrase = EnglishReasonPhraseCatalog.INSTANCE.getReason(statusCode, Locale.US); else reasonPhrase = obj.toString(); // Create a basic response BasicHttpResponse response = new BasicHttpResponse(request.getProtocolVersion(), statusCode, reasonPhrase); // Interpret the headers supplied by the endpoint. obj = som.getMember("headers"); if ((obj != null) && (!ScriptObjectMirror.isUndefined(obj))) { List<ScriptObjectMirror> headers; if (obj instanceof List<?>) headers = (List<ScriptObjectMirror>) obj; else { headers = new ArrayList<ScriptObjectMirror>(); if ((obj instanceof ScriptObjectMirror) && ((ScriptObjectMirror) obj).isArray()) { for (Object sobj : ((ScriptObjectMirror) obj).values()) headers.add((ScriptObjectMirror) sobj); } else Logger.error("The endpoint at " + request.getRequestLine().getUri() + " returned an illegal headers list [class=" + obj.getClass().getName() + "]"); } for (ScriptObjectMirror hdr : headers) { for (String key : hdr.keySet()) { Object value = hdr.getMember(key); response.addHeader(key, ConvertHeaderToString(value)); } } } // Interpret the content type of the response data. ContentType ct = ContentType.DEFAULT_TEXT; if (response.getFirstHeader("Content-Type") != null) ct = ContentType.parse(response.getFirstHeader("content-type").getValue()); obj = som.getMember("mimeType"); if ((obj != null) && (!ScriptObjectMirror.isUndefined(obj))) ct = ContentType.create(obj.toString(), ct.getCharset()); obj = som.getMember("charset"); if ((obj != null) && (!ScriptObjectMirror.isUndefined(obj))) ct = ContentType.create(ct.getMimeType(), obj.toString()); obj = som.getMember("completion"); if ((obj != null) && (!ScriptObjectMirror.isUndefined(obj))) if (((ScriptObjectMirror) obj).isFunction()) completionCallback = (ScriptObjectMirror) obj; // Create an HttpEntity to represent the content. obj = som.getMember("content"); if ((obj != null) && (!ScriptObjectMirror.isUndefined(obj))) { HttpEntity entity; if ((obj instanceof ScriptObjectMirror) && ((ScriptObjectMirror) obj).isFunction()) entity = new NJavascriptFunctionEntity(request.getRequestLine().getUri(), endpoint, (ScriptObjectMirror) obj, ct, response, context); else { entity = Utils.WrapObjWithHttpEntity(obj, ct); if (entity == null) Logger.error("The endpoint at " + request.getRequestLine().getUri() + " returned an unknown content type [class=" + obj.getClass().getName() + "]"); } if (entity != null) response.setEntity(entity); } else // Normally setting the HttpEntity into the response would set the content-type, but we will have to specify it ourselves in this case. response.setHeader("Content-Type", ct.toString()); // Let our superclass know what the endpoint has provided. this.setResponse(response); String id = (String) context.getAttribute("pokerface.txId"); Logger.info("[client<-endpoint] " + id + " " + response.getStatusLine()); return response; }
From source file:IDE.SyntaxTree.NewEmptyJUnitTest.java
public void Parse(ScriptObjectMirror node, int deep) { ScriptObjectMirror child_scriptObjectMirror = null; for (String key : node.keySet()) { Object scriptObjectMirror = node.get(key); Object type = node.get("type"); String tree = ""; for (int i = 1; i < deep; i++) tree += "\t"; // if("Identifier".equals(type)||"FunctionExpression".equals(type)||"ThisExpression".equals(type)) //ThisExpression System.out.println(tree + " " + key + " " + scriptObjectMirror); if ((scriptObjectMirror != null) && ((scriptObjectMirror.getClass() == Object.class) || (scriptObjectMirror.getClass() == ScriptObjectMirror.class) || (scriptObjectMirror.getClass() == Array.class))) { if (scriptObjectMirror.getClass() == ScriptObjectMirror.class) { Parse((ScriptObjectMirror) ((ScriptObjectMirror) scriptObjectMirror), deep++); } else { Parse((ScriptObjectMirror) scriptObjectMirror, deep++); }//from w w w .ja v a2s . com } } }
From source file:org.nuxeo.automation.scripting.internals.MarshalingHelper.java
License:Apache License
public static Object unwrap(ScriptObjectMirror jso) { if (jso.isArray()) { List<Object> l = new ArrayList<>(); for (Object o : jso.values()) { if (o instanceof ScriptObjectMirror) { l.add(unwrap((ScriptObjectMirror) o)); } else { l.add(o);// w ww . j a v a 2 s.c o m } } return l; } else { Map<String, Object> result = new HashMap<>(); for (String k : jso.keySet()) { Object o = jso.get(k); if (o instanceof ScriptObjectMirror) { result.put(k, unwrap((ScriptObjectMirror) o)); } else { result.put(k, o); } } return result; } }
From source file:org.nuxeo.automation.scripting.internals.ScriptObjectMirrors.java
License:Apache License
public static Map<String, Object> unwrapMap(ScriptObjectMirror jso) { if (!JAVASCRIPT_MAP_CLASS_TYPE.equals(jso.getClassName())) { throw new IllegalArgumentException("JavaScript input is not an Object!"); }//w w w . j ava 2 s . c o m Map<String, Object> result = new HashMap<>(); for (String k : jso.keySet()) { Object o = jso.get(k); if (o instanceof ScriptObjectMirror) { result.put(k, unwrap((ScriptObjectMirror) o)); } else { result.put(k, DocumentScriptingWrapper.unwrap(o)); } } return result; }
From source file:org.siphon.jssql.SqlExecutor.java
License:Open Source License
/*** * //from w w w . j a v a2s .c o m * @param connection * @param callProcStmt pkg.proc(?, ?, ?) * @param args [arg1, arg2, {OUTCURSOR, arg3}] * @return success or not * @throws SqlExecutorException */ public boolean call(Connection connection, String callProcStmt, NativeArray args) throws SqlExecutorException { if (connection == null) { return this.call(callProcStmt, args); } long start = System.currentTimeMillis(); CallableStatement proc = null; boolean errorOccu = false; try { // if(logger.isDebugEnabled()) logger.debug("execute procedure " + // callProcStmt + " with args: " + JSON.tryStringify(args)); proc = connection.prepareCall(String.format("{call %s}", callProcStmt)); NativeArray nargs = args; setArgs(proc, nargs); boolean result = proc.execute(); for (int i = 0; i < JsTypeUtil.getArrayLength(nargs); i++) { Object oarg = args.get(i); if (oarg instanceof ScriptObjectMirror) { ScriptObjectMirror arg = (ScriptObjectMirror) oarg; if (arg.containsKey("OUTCURSOR")) { Object oarr = arg.get("OUTCURSOR"); if (oarr instanceof ScriptObjectMirror) { ScriptObjectMirror outArr = (ScriptObjectMirror) oarr; ResultSet rs = (ResultSet) proc.getObject(i + 1); ScriptObjectMirror rsArry = rsToDataTable(rs); rs.close(); Object[] ids = rsArry.keySet().toArray(); for (int j = 0; j < ids.length; j++) { outArr.put((String) ids[j], rsArry.get(ids[j])); } } } else if (arg.containsKey("OUT")) { int type = (int) arg.get("JDBC_TYPE"); Object output = translateOutputParameterValue(type, proc, i + 1); arg.put("result", output); } } } return result; } catch (SQLException | UnsupportedDataTypeException | ScriptException | NoSuchMethodException e) { errorOccu = true; throw new SqlExecutorException( "error occurs when execute " + callProcStmt + " with args : " + JSON.tryStringify(args), e); } finally { long exhaust = System.currentTimeMillis() - start; if (exhaust > 30000) { logger.warn(String.format("%s with args:%s exhaust %s", callProcStmt, args, exhaust)); } DbConnectionUtil.close(proc); } }
From source file:org.siphon.jssql.SqlExecutor.java
License:Open Source License
void setArg(PreparedStatement ps, int index, Object arg) throws SQLException, SqlExecutorException, UnsupportedDataTypeException, NoSuchMethodException, ScriptException { boolean output = false; int outputParameterType = 0; CallableStatement cs = null;//from w w w . j a va 2s.co m if (ps instanceof CallableStatement) { cs = (CallableStatement) ps; if (arg instanceof ScriptObjectMirror && ((ScriptObjectMirror) arg).containsKey("OUT")) { ScriptObjectMirror jsarg = ((ScriptObjectMirror) arg); outputParameterType = (int) jsarg.get("JDBC_TYPE"); arg = jsarg.get("VALUE"); output = true; } } if (output) { cs.registerOutParameter(index + 1, outputParameterType); if (JsTypeUtil.isNull(arg) || (arg instanceof Double && Double.isNaN((Double) arg))) { return; } } if (JsTypeUtil.isNull(arg)) { ps.setObject(index + 1, null); } else if (arg instanceof CharSequence) { ps.setString(index + 1, arg.toString()); } else if (arg instanceof NativeString) { ps.setString(index + 1, arg.toString()); } else if (arg instanceof Double) { // js number always be // Doublebut if its came from // JSON.parse since JSON is jdk // given global object, it will // make Integer and ... double d = ((Double) arg).doubleValue(); if (d == (int) d) { ps.setInt(index + 1, (int) d); } else if (d == (long) d) { ps.setLong(index + 1, (long) d); } else { ps.setBigDecimal(index + 1, new BigDecimal(d)); } } else if (arg instanceof Integer) { ps.setInt(index + 1, (Integer) arg); } else if (arg instanceof Long) { ps.setLong(index + 1, (Long) arg); } else if (arg instanceof Float) { ps.setFloat(index + 1, (Float) arg); } else if (jsTypeUtil.isNativeDate(arg)) { ps.setTimestamp(index + 1, parseDate(arg)); } else if (arg instanceof ZonedDateTime) { ZonedDateTime zdt = (ZonedDateTime) arg; ps.setTimestamp(index + 1, new Timestamp(zdt.toInstant().toEpochMilli())); } else if (arg instanceof Boolean) { ps.setBoolean(index + 1, JsTypeUtil.isTrue(arg)); } else if (arg instanceof ScriptObjectMirror || arg instanceof ScriptObject) { String attr = null; Object value = null; if (arg instanceof ScriptObjectMirror) { ScriptObjectMirror atm = (ScriptObjectMirror) arg; if (atm.keySet().contains("toJavaObject")) { Object obj = atm.callMember("toJavaObject"); setArg(ps, index, obj); return; } attr = atm.keySet().iterator().next(); value = atm.get(attr); } else { ScriptObject obj = (ScriptObject) arg; if (obj.containsKey("toJavaObject")) { ScriptObjectMirror atm = (ScriptObjectMirror) jsTypeUtil.toScriptObjectMirror(obj); Object result = atm.callMember("toJavaObject"); setArg(ps, index, result); return; } String[] arr = obj.getOwnKeys(false); if (arr.length == 0) { throw new SqlExecutorException("js argument " + arg + " (" + arg.getClass() + ") at " + index + " is an empty js object"); } attr = arr[0]; value = obj.get(attr); } if ("STRING".equals(attr)) { ps.setString(index + 1, String.valueOf(value)); } else if ("DECIMAL".equals(attr)) { if (value instanceof Double) { ps.setBigDecimal(index + 1, new BigDecimal((Double) value)); } else { ps.setBigDecimal(index + 1, new BigDecimal(value + "")); } } else if ("INT".equals(attr)) { if (value instanceof Double) { if (((Double) value).isNaN()) { ps.setObject(index + 1, null); } else { ps.setInt(index + 1, ((Double) value).intValue()); } } else { ps.setInt(index + 1, new Integer(value + "")); } } else if ("BOOLEAN".equals(attr)) { ps.setBoolean(index + 1, JsTypeUtil.isTrue(arg)); } else if ("DOUBLE".equals(attr)) { if (value instanceof Double) { if (((Double) value).isNaN()) { ps.setObject(index + 1, null); } else { ps.setDouble(index + 1, (double) value); } } else { ps.setDouble(index + 1, new Double(value + "")); } } else if ("FLOAT".equals(attr)) { if (value instanceof Double) { if (((Double) value).isNaN()) { ps.setObject(index + 1, null); } else { ps.setFloat(index + 1, (float) (double) value); } } else { ps.setFloat(index + 1, new Float(value + "")); } } else if ("DATE".equals(attr)) { ps.setTimestamp(index + 1, parseDate(value)); } else if ("TIME".equals(attr)) { ps.setTimestamp(index + 1, parseTime(value)); } else if ("BINARY".equals(attr)) { ps.setBytes(index + 1, parseBinary(value)); } else if ("CLOB".equals(attr)) { Clob clob = ps.getConnection().createClob(); clob.setString(1, String.valueOf(value)); ps.setClob(index + 1, clob); } else if ("LONG".equals(attr)) { if (value instanceof Double) { if (((Double) value).isNaN()) { ps.setObject(index + 1, null); } else { ps.setLong(index + 1, ((Double) value).longValue()); } } else { ps.setLong(index + 1, new Long(value + "")); } } else if ("OUTCURSOR".equals(attr)) { // cs.registerOutParameter(i+1, OracleTypes.CURSOR); cs.registerOutParameter(index + 1, -10); } else if ("ARRAY".equals(attr)) { if (value instanceof NativeArray) { ps.setArray(index + 1, createSqlArray(ps.getConnection(), (NativeArray) value)); } else { setArg(ps, index, value); // value is {ARRAY : ['int', e1, e2, ...]} } // ps.setObject(i+1, createSqlArray(ps.getConnection(), // (NativeArray) value)); } else if ("JSON".equals(attr) || "JSONB".equals(attr)) { PGobject obj = new PGobject(); obj.setType(attr.toLowerCase()); obj.setValue(this.JSON.tryStringify(value)); ps.setObject(index + 1, obj); } else if ("UUID".equals(attr)) { if (value != null) { ps.setObject(index + 1, UUID.fromString(value.toString())); } else { ps.setObject(index + 1, null); } } else { if (this.defaultJsonDbType != null) { PGobject obj = new PGobject(); obj.setType(this.defaultJsonDbType); obj.setValue(this.JSON.tryStringify(arg)); ps.setObject(index + 1, obj); } else { throw new SqlExecutorException("js argument " + arg + " (" + arg.getClass() + ") not support"); } } } else { throw new SqlExecutorException( "js argument " + arg + " (" + arg.getClass() + ") at " + index + " not support"); } }
From source file:org.siphon.jssql.SqlExecutor.java
License:Open Source License
private Object convertJsObjToJavaType(Object arg) throws UnsupportedDataTypeException, SqlExecutorException { if (JsTypeUtil.isNull(arg)) { return null; } else if (arg instanceof ScriptObjectMirror) { ScriptObjectMirror atm = (ScriptObjectMirror) arg; String attr = (String) atm.keySet().iterator().next(); Object value = atm.get(attr); if ("STRING".equals(attr)) { return String.valueOf(value); } else if ("DECIMAL".equals(attr)) { if (value instanceof Double) { return new BigDecimal((Double) value); } else { return new BigDecimal(value + ""); }/*from www .j a v a 2 s . c om*/ } else if ("INT".equals(attr)) { if (value instanceof Double) { if (((Double) value).isNaN()) { return null; } else { return ((Double) value).intValue(); } } else { return new Integer(value + ""); } } else if ("BOOLEAN".equals(attr)) { return JsTypeUtil.isTrue(arg); } else if ("DOUBLE".equals(attr)) { if (value instanceof Double) { if (((Double) value).isNaN()) { return null; } else { return (double) value; } } else { return new Double(value + ""); } } else if ("FLOAT".equals(attr)) { if (value instanceof Double) { if (((Double) value).isNaN()) { return null; } else { return (float) (double) value; } } else { return new Float(value + ""); } } else if ("DATE".equals(attr)) { return parseDate(value); } else if ("TIME".equals(attr)) { return parseTime(value); } else if ("BINARY".equals(attr)) { return parseBinary(value); } else if ("CLOB".equals(attr)) { return String.valueOf(value); } else if ("LONG".equals(attr)) { if (value instanceof Double) { if (((Double) value).isNaN()) { return null; } else { return ((Double) value).longValue(); } } else { return new Long(value + ""); } // } else if ("ARRAY".equals(attr)){ // TODO ?? // NativeArray arr = (NativeArray) value; } else { throw new UnsupportedDataTypeException( "js object " + arg + " (" + arg.getClass() + ") not support"); } } else if (arg instanceof String) { return (String) arg; } else if (arg instanceof ConsString) { return arg.toString(); } else if (arg instanceof Double) { // js number always be Doublebut if // its came from JSON.parse since // JSON is jdk given global object, // it will make Integer and ... double d = ((Double) arg).doubleValue(); if (d == (int) d) { return (int) d; } else if (d == (long) d) { return (long) d; } else { return new BigDecimal(d); } } else if (arg instanceof Integer) { return arg; } else if (arg instanceof Long) { return arg; } else if (arg instanceof Float) { return arg; } else if (jsTypeUtil.isNativeDate(arg)) { return parseDate(arg); } else if (arg instanceof Boolean) { return parseBinary(arg); } else { throw new UnsupportedDataTypeException("js object " + arg + " (" + arg.getClass() + ") not support"); } }
From source file:Runner.Contact.java
public void replaceVariables(ScriptObjectMirror template, ScriptObjectMirror... sources) throws Exception { MessageTemplateVariables variables = loadVariables(sources); Map<String, String> fields = new HashMap(); for (String fieldName : template.keySet()) { fields.put(fieldName, (String) template.get(fieldName)); }// w w w . ja v a 2 s .c o m Map<String, String> updatedFields = MessageTemplateEngine.replaceVariables(fields, variables); for (String key : updatedFields.keySet()) { template.put(key, updatedFields.get(key)); } }
From source file:Runner.Contact.java
private MessageTemplateVariables loadVariables(ScriptObjectMirror... sources) { MessageTemplateVariables variables = new MessageTemplateVariables(); if (sources == null) { return variables; }/*from www . ja va2 s . co m*/ for (ScriptObjectMirror sourceItem : sources) { sourceItem.keySet().stream().forEach((sourceName) -> { HashMap<String, String> sourceMirror = (HashMap) sourceItem.get(sourceName); Map<String, String> sourceFields = new HashMap<>(); for (String sourceFieldName : sourceMirror.keySet()) { sourceFields.put(sourceFieldName, (String) sourceMirror.get(sourceFieldName)); variables.addSource(sourceName, sourceFields); } }); } return variables; }