|
| 1 | +/* |
| 2 | + * Copyright 2020-Present The Serverless Workflow Specification Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package io.serverlessworkflow.api.deserializers; |
| 17 | + |
| 18 | +import com.fasterxml.jackson.core.JsonParser; |
| 19 | +import com.fasterxml.jackson.databind.DeserializationContext; |
| 20 | +import com.fasterxml.jackson.databind.JsonNode; |
| 21 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 22 | +import com.fasterxml.jackson.databind.deser.std.StdDeserializer; |
| 23 | +import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; |
| 24 | +import io.serverlessworkflow.api.auth.AuthDefinition; |
| 25 | +import io.serverlessworkflow.api.interfaces.WorkflowPropertySource; |
| 26 | +import io.serverlessworkflow.api.utils.Utils; |
| 27 | +import io.serverlessworkflow.api.workflow.Auth; |
| 28 | +import java.io.IOException; |
| 29 | +import java.util.ArrayList; |
| 30 | +import java.util.List; |
| 31 | +import org.json.JSONObject; |
| 32 | +import org.slf4j.Logger; |
| 33 | +import org.slf4j.LoggerFactory; |
| 34 | + |
| 35 | +public class AuthDeserializer extends StdDeserializer<Auth> { |
| 36 | + |
| 37 | + private static final long serialVersionUID = 520L; |
| 38 | + private static Logger logger = LoggerFactory.getLogger(AuthDeserializer.class); |
| 39 | + |
| 40 | + @SuppressWarnings("unused") |
| 41 | + private WorkflowPropertySource context; |
| 42 | + |
| 43 | + public AuthDeserializer() { |
| 44 | + this(Auth.class); |
| 45 | + } |
| 46 | + |
| 47 | + public AuthDeserializer(Class<?> vc) { |
| 48 | + super(vc); |
| 49 | + } |
| 50 | + |
| 51 | + public AuthDeserializer(WorkflowPropertySource context) { |
| 52 | + this(Auth.class); |
| 53 | + this.context = context; |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public Auth deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException { |
| 58 | + |
| 59 | + ObjectMapper mapper = (ObjectMapper) jp.getCodec(); |
| 60 | + JsonNode node = jp.getCodec().readTree(jp); |
| 61 | + |
| 62 | + Auth auth = new Auth(); |
| 63 | + List<AuthDefinition> authDefinitions = new ArrayList<>(); |
| 64 | + |
| 65 | + if (node.isArray()) { |
| 66 | + for (final JsonNode nodeEle : node) { |
| 67 | + authDefinitions.add(mapper.treeToValue(nodeEle, AuthDefinition.class)); |
| 68 | + } |
| 69 | + } else { |
| 70 | + String authFileDef = node.asText(); |
| 71 | + String authFileSrc = Utils.getResourceFileAsString(authFileDef); |
| 72 | + JsonNode authRefNode; |
| 73 | + ObjectMapper jsonWriter = new ObjectMapper(); |
| 74 | + if (authFileSrc != null && authFileSrc.trim().length() > 0) { |
| 75 | + // if its a yaml def convert to json first |
| 76 | + if (!authFileSrc.trim().startsWith("{")) { |
| 77 | + // convert yaml to json to validate |
| 78 | + ObjectMapper yamlReader = new ObjectMapper(new YAMLFactory()); |
| 79 | + Object obj = yamlReader.readValue(authFileSrc, Object.class); |
| 80 | + |
| 81 | + authRefNode = |
| 82 | + jsonWriter.readTree(new JSONObject(jsonWriter.writeValueAsString(obj)).toString()); |
| 83 | + } else { |
| 84 | + authRefNode = jsonWriter.readTree(new JSONObject(authFileSrc).toString()); |
| 85 | + } |
| 86 | + |
| 87 | + JsonNode refAuth = authRefNode.get("auth"); |
| 88 | + if (refAuth != null) { |
| 89 | + for (final JsonNode nodeEle : refAuth) { |
| 90 | + authDefinitions.add(mapper.treeToValue(nodeEle, AuthDefinition.class)); |
| 91 | + } |
| 92 | + } else { |
| 93 | + logger.error("Unable to find auth definitions in reference file: {}", authFileSrc); |
| 94 | + } |
| 95 | + |
| 96 | + } else { |
| 97 | + logger.error("Unable to load auth defs reference file: {}", authFileSrc); |
| 98 | + } |
| 99 | + } |
| 100 | + auth.setAuthDefs(authDefinitions); |
| 101 | + return auth; |
| 102 | + } |
| 103 | +} |
0 commit comments