Adding a working version of the rest template plugin executor.

This commit is contained in:
Arpit Mohan 2019-04-02 21:10:36 +05:30
parent 6d91ab32ce
commit bbb6e511d7
3 changed files with 85 additions and 6 deletions

View File

@ -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<Object> execute(Query query, CommandQueryParams params) {
String requestBody = query.getCommandTemplate();
Map<String, Property> 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<ClientResponse> responseMono = request.exchange();
ClientResponse clientResponse = responseMono.block();
List<String> 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()");
}
}

View File

@ -5,5 +5,12 @@ import com.mobtools.server.domains.PluginType;
public interface PluginService extends CrudService<Plugin, String> {
/**
* 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);
}

View File

@ -31,12 +31,7 @@ public class PluginServiceImpl extends BaseService<PluginRepository, Plugin, Str
Class<?> 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);
}