private String resolveTargetTypeName(Class<?> targetType, BMap<String, BValue> jBMap) { if (jBMap.hasKey(TYPE_TAG)) { BValue jType = jBMap.get(TYPE_TAG); return jType.stringValue(); } else if (targetType != null) { return ObjectHelper.getTrimmedClassName(targetType); } throw new IllegalStateException("Target type should either come from jBMap or via targetType param"); }
private static void methodInvocationCheck(BMap<String, BValue> bStruct, HttpCarbonMessage reqMsg, int statusCode) { if (bStruct.getNativeData(METHOD_ACCESSED) != null || reqMsg == null) { throw new IllegalStateException("illegal function invocation"); } if (!is100ContinueRequest(reqMsg, statusCode)) { bStruct.addNativeData(METHOD_ACCESSED, true); } }
public static BMap<String, BValue> extractEntity(BMap<String, BValue> httpMessageStruct) { Object isEntityBodyAvailable = httpMessageStruct.getNativeData(IS_BODY_BYTE_CHANNEL_ALREADY_SET); if (isEntityBodyAvailable == null || !((Boolean) isEntityBodyAvailable)) { return null; } return (BMap<String, BValue>) httpMessageStruct .get(isRequestStruct(httpMessageStruct) ? REQUEST_ENTITY_FIELD : RESPONSE_ENTITY_FIELD); }
/** * Populates the HTTP caller with native data. * * @param caller Represents the HTTP caller * @param inboundMsg Represents carbon message * @param config Represents service endpoint configuration */ public static void enrichHttpCallerWithNativeData(BMap<String, BValue> caller, HttpCarbonMessage inboundMsg, Struct config) { caller.addNativeData(HttpConstants.TRANSPORT_MESSAGE, inboundMsg); caller.put(HttpConstants.HTTP_CONNECTOR_CONFIG_FIELD, (BMap<String, BValue>) config.getVMValue()); }
BMap<String, BValue> bStruct = new BMap<>(structType); BMap<String, BRefType<?>> jsonObject = (BMap<String, BRefType<?>>) json; StructureTypeInfo structInfo = (StructureTypeInfo) structType.getTypeInfo(); try { if (!jsonObject.hasKey(fieldName)) { bStruct.put(fieldName, fieldType.getZeroValue()); continue; BRefType<?> jsonValue = jsonObject.get(fieldName); bStruct.put(fieldName, convertJSON(jsonValue, fieldType)); } catch (Exception e) { handleError(e, fieldName);
private BMap<String, BRefType<?>> setDataSourcePropertiesMap(BMap<String, BRefType<?>> dataSourceConfigMap, String jdbcurl, String username, String password) { if (dataSourceConfigMap != null) { if (!dataSourceConfigMap.hasKey(Constants.URL)) { dataSourceConfigMap.put(Constants.URL, new BString(jdbcurl)); } } else { dataSourceConfigMap = new BMap<>(); dataSourceConfigMap.put(Constants.URL, new BString(jdbcurl)); } dataSourceConfigMap.put(Constants.USER, new BString(username)); dataSourceConfigMap.put(Constants.PASSWORD, new BString(password)); return dataSourceConfigMap; }
private static void convertStructToMap(Strand strand, int[] operands, StackFrame sf) { int i = operands[0]; int j = operands[1]; // TODO: do validation for type? BMap newMap = new BMap(BTypes.typeMap); ((BMap) sf.refRegs[i]).getMap().forEach((key, value) -> newMap.put(key, value == null ? null : ((BValue) value).copy(new HashMap<>()))); sf.refRegs[j] = newMap; }
@Override public BMap getServiceField(String fieldName) { return (BMap) value.get(fieldName); }
@Override public void invalidate() { sessionManager.invalidateSession(this); attributeMap = new BMap<>(); isValid = false; }
public RequestCacheControlStruct(BMap<String, BValue> requestCacheControl) { this.requestCacheControl = requestCacheControl; // Initialize the struct fields to default values we use requestCacheControl.put(REQ_CACHE_CONTROL_NO_TRANSFORM_FIELD, new BBoolean(TRUE)); requestCacheControl.put(REQ_CACHE_CONTROL_MAX_AGE_FIELD, new BInteger(-1)); requestCacheControl.put(REQ_CACHE_CONTROL_MAX_STALE_FIELD, new BInteger(-1)); requestCacheControl.put(REQ_CACHE_CONTROL_MIN_FRESH_FIELD, new BInteger(-1)); }
@Override public void execute(Context context, CallableUnitCallback callback) { BMap<String, BValue> httpConnection = (BMap<String, BValue>) context.getRefArgument(0); WebSocketHandshaker webSocketHandshaker = (WebSocketHandshaker) httpConnection.getNativeData(WebSocketConstants.WEBSOCKET_MESSAGE); WebSocketConnectionManager connectionManager = (WebSocketConnectionManager) httpConnection .getNativeData(HttpConstants.NATIVE_DATA_WEBSOCKET_CONNECTION_MANAGER); if (webSocketHandshaker == null) { throw new BallerinaConnectorException("Not a WebSocket upgrade request. Cannot upgrade from HTTP to WS"); } if (connectionManager == null) { throw new BallerinaConnectorException("Cannot accept a WebSocket upgrade without WebSocket " + "connection manager"); } WebSocketService webSocketService = (WebSocketService) httpConnection.getNativeData( WebSocketConstants.WEBSOCKET_SERVICE); BMap<String, BString> headers = (BMap<String, BString>) context.getRefArgument(1); DefaultHttpHeaders httpHeaders = new DefaultHttpHeaders(); String[] keys = headers.keys(); for (String key : keys) { httpHeaders.add(key, headers.get(key)); } WebSocketUtil.handleHandshake(webSocketService, connectionManager, httpHeaders, webSocketHandshaker, context, callback); }
@SuppressWarnings("unchecked") private Object deserializeMap(BMap<String, BValue> payload, Map target) { BValue complexKeyMap = payload.get(COMPLEX_KEY_MAP_TAG); if (complexKeyMap instanceof BMap) { return deserializeComplexKeyMap((BMap<String, BValue>) complexKeyMap, payload, target); } for (String key : payload.keys()) { if (key.equals(COMPLEX_KEY_MAP_TAG)) { // don't process this entry here, as this is the complex key-map entry continue; } BValue value = payload.get(key); Class<?> fieldType = Object.class; if (value instanceof BMap) { BMap<String, BValue> item = (BMap<String, BValue>) value; if (item.hasKey(TYPE_TAG)) { BValue val = item.get(TYPE_TAG); String typeName = val.stringValue(); fieldType = findClass(typeName); } } else if (value instanceof BBoolean) { fieldType = BBoolean.class; } target.put(key, deserialize(value, fieldType)); } return target; }
@Override public Object getNativeData(String key) { return value.getNativeData(key); }
@Override public void execute(Context context) { BMap<String, BValue> bStruct = (BMap<String, BValue>) context.getRefArgument(0); Counter counter = Counter.builder(bStruct.get(ObserveNativeImplConstants.NAME_FIELD).stringValue()) .description(bStruct.get(ObserveNativeImplConstants.DESCRIPTION_FIELD).stringValue()) .tags(Utils.toStringMap((BMap) bStruct.get(ObserveNativeImplConstants.TAGS_FIELD))) .build(); bStruct.addNativeData(ObserveNativeImplConstants.METRIC_NATIVE_INSTANCE_KEY, counter); } }
public static void addNativeToRecode(Object module, BMap<String, BValue> bStruct) { bStruct.addNativeData(NATIVE_KEY, module); }
public SerializableBMap(BMap<K, V> bMap, SerializableState state) { structName = bMap.getType().getName(); pkgPath = bMap.getType().getPackagePath(); bMap.getNativeData().forEach((k, o) -> nativeData.put(k, state.serialize(o))); bMap.getMap().forEach((k, v) -> map.put(k, state.serialize(v))); }
@Override @SuppressWarnings("unchecked") public BMap toObject(BPacket packet, BValueDeserializer bValueDeserializer) { BMap payload = (BMap<String, BValue>) packet.getValue(); HashMap deserializedMap = (HashMap) bValueDeserializer.deserialize(payload, HashMap.class); BType type = (BType) bValueDeserializer.deserialize(packet.get(MAP_STORAGE_TYPE), BType.class); BMap bMap = new BMap(type); bMap.getMap().putAll(deserializedMap); return bMap; } }