diff --git a/app/server/src/main/java/com/mobtools/server/plugins/RestTemplatePluginExecutor.java b/app/server/src/main/java/com/mobtools/server/plugins/RestTemplatePluginExecutor.java new file mode 100644 index 0000000000..e31d4b8198 --- /dev/null +++ b/app/server/src/main/java/com/mobtools/server/plugins/RestTemplatePluginExecutor.java @@ -0,0 +1,77 @@ +package com.mobtools.server.plugins; + +import com.mobtools.server.domains.Property; +import com.mobtools.server.domains.Query; +import com.mobtools.server.dtos.CommandQueryParams; +import com.mobtools.server.services.PluginExecutor; +import lombok.extern.slf4j.Slf4j; +import net.minidev.json.JSONObject; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpMethod; +import org.springframework.http.MediaType; +import org.springframework.stereotype.Component; +import org.springframework.web.reactive.function.BodyInserters; +import org.springframework.web.reactive.function.client.ClientResponse; +import org.springframework.web.reactive.function.client.WebClient; +import reactor.core.publisher.Flux; +import reactor.core.publisher.Mono; + +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +@Component +@Slf4j +public class RestTemplatePluginExecutor extends PluginExecutor { + + final String PROP_URL = "url"; + final String PROP_HTTP_METHOD = "method"; + + @Override + protected Flux execute(Query query, CommandQueryParams params) { + String requestBody = query.getCommandTemplate(); + Map propertyMap = query.getProperties() + .stream() + .collect(Collectors.toMap(Property::getKey, prop -> prop)); + + String url = propertyMap.get(PROP_URL).getValue(); + String httpMethod = propertyMap.get(PROP_HTTP_METHOD).getValue(); + + WebClient webClient = WebClient.builder() + .baseUrl(url) + .defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE) + .build(); + + WebClient.RequestHeadersSpec request = webClient.method(HttpMethod.resolve(httpMethod)) + .body(BodyInserters.fromObject(requestBody)); + + Mono responseMono = request.exchange(); + ClientResponse clientResponse = responseMono.block(); + + List contentTypes = clientResponse.headers().header(HttpHeaders.CONTENT_TYPE); + + Class clazz = String.class; + if(contentTypes != null && contentTypes.size() > 0) { + String contentType = contentTypes.get(0); + boolean isJson = MediaType.APPLICATION_JSON_UTF8_VALUE.toLowerCase() + .equals(contentType.toLowerCase() + .replaceAll("\\s", "")) + || MediaType.APPLICATION_JSON_VALUE.equals(contentType.toLowerCase()); + + if(isJson) { + clazz = JSONObject.class; + } + } + return clientResponse.bodyToFlux(clazz); + } + + @Override + protected void init() { + log.debug("In the RestTemplatePluginExecutor init()"); + } + + @Override + protected void destroy() { + log.debug("In the RestTemplatePluginExecutor destroy()"); + } +} diff --git a/app/server/src/main/java/com/mobtools/server/services/PluginService.java b/app/server/src/main/java/com/mobtools/server/services/PluginService.java index fe28a2170e..630322c88f 100644 --- a/app/server/src/main/java/com/mobtools/server/services/PluginService.java +++ b/app/server/src/main/java/com/mobtools/server/services/PluginService.java @@ -5,5 +5,12 @@ import com.mobtools.server.domains.PluginType; public interface PluginService extends CrudService { + /** + * Return an instance of PluginExecutor based on the classname available. + * If the classname is not available, null is returned. + * @param pluginType + * @param className + * @return PluginExecutor + */ PluginExecutor getPluginExecutor(PluginType pluginType, String className); } diff --git a/app/server/src/main/java/com/mobtools/server/services/PluginServiceImpl.java b/app/server/src/main/java/com/mobtools/server/services/PluginServiceImpl.java index 62be27dd42..2da7b76b24 100644 --- a/app/server/src/main/java/com/mobtools/server/services/PluginServiceImpl.java +++ b/app/server/src/main/java/com/mobtools/server/services/PluginServiceImpl.java @@ -31,12 +31,7 @@ public class PluginServiceImpl extends BaseService clazz; try { clazz = Class.forName(className); - switch(pluginType) { - case DB: - return (PluginExecutor) applicationContext.getBean(clazz); - case REST: break; - default: break; - } + return (PluginExecutor) applicationContext.getBean(clazz); } catch (ClassNotFoundException e) { log.error("Unable to find class {}. ", className, e); }