|
| 1 | +/* |
| 2 | + * Copyright 2018 ThoughtWorks, Inc. |
| 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 | + |
| 17 | +package com.thoughtworks.go.plugin.access.artifact; |
| 18 | + |
| 19 | +import com.google.gson.Gson; |
| 20 | +import com.google.gson.GsonBuilder; |
| 21 | +import com.google.gson.reflect.TypeToken; |
| 22 | +import com.thoughtworks.go.config.ArtifactStore; |
| 23 | +import com.thoughtworks.go.domain.ArtifactPlan; |
| 24 | +import com.thoughtworks.go.domain.config.Configuration; |
| 25 | +import com.thoughtworks.go.plugin.access.artifact.model.PublishArtifactResponse; |
| 26 | +import com.thoughtworks.go.plugin.access.artifact.models.FetchArtifactEnvironmentVariable; |
| 27 | +import com.thoughtworks.go.plugin.access.common.handler.JSONResultMessageHandler; |
| 28 | +import com.thoughtworks.go.plugin.access.common.models.ImageDeserializer; |
| 29 | +import com.thoughtworks.go.plugin.access.common.models.PluginProfileMetadataKeys; |
| 30 | +import com.thoughtworks.go.plugin.api.response.validation.ValidationResult; |
| 31 | +import com.thoughtworks.go.plugin.domain.common.Image; |
| 32 | +import com.thoughtworks.go.plugin.domain.common.PluginConfiguration; |
| 33 | +import org.apache.commons.lang3.StringUtils; |
| 34 | + |
| 35 | +import java.util.ArrayList; |
| 36 | +import java.util.HashMap; |
| 37 | +import java.util.List; |
| 38 | +import java.util.Map; |
| 39 | + |
| 40 | +public class ArtifactMessageConverterV2 implements ArtifactMessageConverter { |
| 41 | + public static final String VERSION = ArtifactExtensionConstants.V2; |
| 42 | + private static final Gson GSON = new GsonBuilder().serializeNulls().create(); |
| 43 | + |
| 44 | + @Override |
| 45 | + public String publishArtifactMessage(ArtifactPlan artifactPlan, ArtifactStore artifactStore, String agentWorkingDirectory, |
| 46 | + Map<String, String> environmentVariables) { |
| 47 | + final Map<String, Object> messageObject = new HashMap<>(); |
| 48 | + messageObject.put("artifact_store", getArtifactStore(artifactStore)); |
| 49 | + messageObject.put("artifact_plan", artifactPlan.getPluggableArtifactConfiguration()); |
| 50 | + messageObject.put("agent_working_directory", agentWorkingDirectory); |
| 51 | + messageObject.put("environment_variables", environmentVariables); |
| 52 | + return GSON.toJson(messageObject); |
| 53 | + } |
| 54 | + |
| 55 | + private Map getArtifactStore(ArtifactStore artifactStore) { |
| 56 | + final HashMap<String, Object> artifactStoreAndPlans = new HashMap<>(); |
| 57 | + artifactStoreAndPlans.put("id", artifactStore.getId()); |
| 58 | + artifactStoreAndPlans.put("configuration", artifactStore.getConfigurationAsMap(true)); |
| 59 | + return artifactStoreAndPlans; |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public PublishArtifactResponse publishArtifactResponse(String responseBody) { |
| 64 | + return PublishArtifactResponse.fromJSON(responseBody); |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public List<PluginConfiguration> getMetadataResponseFromBody(String responseBody) { |
| 69 | + return PluginProfileMetadataKeys.fromJSON(responseBody).toPluginConfigurations(); |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + public String getViewFromResponseBody(String responseBody, final String viewLabel) { |
| 74 | + return getTemplateFromResponse(responseBody, String.format("%s `template` was blank!", viewLabel)); |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + public String validateConfigurationRequestBody(Map<String, String> configuration) { |
| 79 | + return GSON.toJson(configuration); |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + public ValidationResult getConfigurationValidationResultFromResponseBody(String responseBody) { |
| 84 | + return new JSONResultMessageHandler().toValidationResult(responseBody); |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + public String fetchArtifactMessage(ArtifactStore artifactStore, Configuration configuration, Map<String, Object> metadata, String agentWorkingDirectory) { |
| 89 | + final Map<String, Object> map = new HashMap<>(); |
| 90 | + map.put("store_configuration", artifactStore.getConfigurationAsMap(true)); |
| 91 | + map.put("fetch_artifact_configuration", configuration.getConfigurationAsMap(true)); |
| 92 | + map.put("artifact_metadata", metadata); |
| 93 | + map.put("agent_working_directory", agentWorkingDirectory); |
| 94 | + return GSON.toJson(map); |
| 95 | + } |
| 96 | + |
| 97 | + @Override |
| 98 | + public Image getImageResponseFromBody(String responseBody) { |
| 99 | + return new ImageDeserializer().fromJSON(responseBody); |
| 100 | + } |
| 101 | + |
| 102 | + @Override |
| 103 | + public com.thoughtworks.go.plugin.domain.artifact.Capabilities getCapabilitiesFromResponseBody(String responseBody) { |
| 104 | + return com.thoughtworks.go.plugin.access.artifact.models.Capabilities.fromJSON(responseBody).toCapabilities(); |
| 105 | + } |
| 106 | + |
| 107 | + @Override |
| 108 | + public List<FetchArtifactEnvironmentVariable> getFetchArtifactEnvironmentVariablesFromResponseBody(String responseBody) { |
| 109 | + return new Gson().fromJson(responseBody, new TypeToken<List<FetchArtifactEnvironmentVariable>>() {}.getType()); |
| 110 | + } |
| 111 | + |
| 112 | + private String getTemplateFromResponse(String responseBody, String message) { |
| 113 | + String template = (String) new Gson().fromJson(responseBody, Map.class).get("template"); |
| 114 | + if (StringUtils.isBlank(template)) { |
| 115 | + throw new RuntimeException(message); |
| 116 | + } |
| 117 | + return template; |
| 118 | + } |
| 119 | +} |
0 commit comments