Merge branch 'master' into develop
This commit is contained in:
@@ -2,8 +2,15 @@ package it.integry.ems.schedule.new_cron_job.dto.operations;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
import it.integry.ems.schedule.new_cron_job.dto.operations.base_classes.BaseScheduledOperationDTO;
|
||||
import it.integry.ems_model.utility.UtilityString;
|
||||
import org.apache.http.NameValuePair;
|
||||
import org.apache.http.client.utils.URLEncodedUtils;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@JsonInclude
|
||||
public class ServiceCallAutomatedOperationDTO extends BaseScheduledOperationDTO {
|
||||
|
||||
@@ -47,6 +54,11 @@ public class ServiceCallAutomatedOperationDTO extends BaseScheduledOperationDTO
|
||||
return this;
|
||||
}
|
||||
|
||||
public List<NameValuePair> parseQueryParams() {
|
||||
if(UtilityString.isNullOrEmpty(getQueryParams())) return new ArrayList<>();
|
||||
return URLEncodedUtils.parse(getQueryParams(), StandardCharsets.UTF_8);
|
||||
}
|
||||
|
||||
public String getQueryParams() {
|
||||
return queryParams;
|
||||
}
|
||||
|
||||
@@ -16,7 +16,9 @@ public class CheckB2BScheduledOperationRunner extends BaseScheduledOperationRunn
|
||||
final Object internalGetResponse = httpRestWrapper.callInternalMethod(getDtoInstance().getProfileDb(),
|
||||
EmsRestConstants.PATH_READ_FPX_INBOX,
|
||||
getDtoInstance().getUsername(),
|
||||
RequestMethod.POST);
|
||||
RequestMethod.POST,
|
||||
null,
|
||||
null);
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -1,90 +1,26 @@
|
||||
package it.integry.ems.schedule.new_cron_job.dto.operations.runners;
|
||||
|
||||
import it.integry.common.var.CommonConstants;
|
||||
import it.integry.ems.json.ResponseJSONObjectMapper;
|
||||
import it.integry.ems.response.EsitoType;
|
||||
import it.integry.ems.response.ServiceRestResponse;
|
||||
import it.integry.ems.schedule.new_cron_job.dto.operations.ServiceCallAutomatedOperationDTO;
|
||||
import it.integry.ems.service.HttpRestWrapper;
|
||||
import it.integry.ems_model.utility.UtilityServer;
|
||||
import it.integry.ems_model.utility.UtilityString;
|
||||
import org.apache.http.entity.ContentType;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.context.ContextLoader;
|
||||
|
||||
import javax.ws.rs.client.Entity;
|
||||
|
||||
public class ServiceCallScheduledOperationRunner extends BaseScheduledOperationRunner<ServiceCallAutomatedOperationDTO> {
|
||||
|
||||
@Override
|
||||
public void run() throws Exception {
|
||||
String port = UtilityServer.getLocalServerPort();
|
||||
String baseUrl = "http://localhost:" + port + "/ems-api"
|
||||
+ (getDtoInstance().getMethodName().startsWith("/") ? "" : "/") + getDtoInstance().getMethodName() + "?"
|
||||
+ CommonConstants.PROFILE_DB + "=" + getDtoInstance().getProfileDb()
|
||||
+ (getDtoInstance().getQueryParams() != null ? ("&" + getDtoInstance().getQueryParams()) : "");
|
||||
|
||||
StringBuilder responseBody = new StringBuilder();
|
||||
final HttpRestWrapper httpRestWrapper = ContextLoader.getCurrentWebApplicationContext().getBean(HttpRestWrapper.class);
|
||||
|
||||
int status;
|
||||
|
||||
RequestMethod methodType = getDtoInstance().getMethodType();
|
||||
|
||||
if (methodType == null) {
|
||||
methodType = RequestMethod.POST;
|
||||
}
|
||||
|
||||
switch (methodType) {
|
||||
case GET:
|
||||
status = HttpRestWrapper.callGenericGet(
|
||||
baseUrl,
|
||||
getDtoInstance().getUsername(),
|
||||
getDtoInstance().getPassword(),
|
||||
responseBody,
|
||||
null
|
||||
final Object internalGetResponse = httpRestWrapper.callInternalMethod(getDtoInstance().getProfileDb(),
|
||||
getDtoInstance().getMethodName(),
|
||||
getDtoInstance().getUsername(),
|
||||
getDtoInstance().getMethodType(),
|
||||
getDtoInstance().parseQueryParams(),
|
||||
Entity.json(getDtoInstance().getBody())
|
||||
);
|
||||
|
||||
break;
|
||||
default:
|
||||
case POST:
|
||||
status = HttpRestWrapper.callGeneric(
|
||||
baseUrl,
|
||||
getDtoInstance().getUsername(),
|
||||
getDtoInstance().getPassword(),
|
||||
getDtoInstance().getBody(),
|
||||
ContentType.APPLICATION_JSON,
|
||||
responseBody
|
||||
);
|
||||
|
||||
break;
|
||||
case PUT:
|
||||
status = HttpRestWrapper.callGeneric(
|
||||
baseUrl,
|
||||
getDtoInstance().getUsername(),
|
||||
getDtoInstance().getPassword(),
|
||||
getDtoInstance().getBody(),
|
||||
ContentType.APPLICATION_JSON,
|
||||
responseBody,
|
||||
null,
|
||||
RequestMethod.PUT
|
||||
);
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
String responseJson = responseBody.toString();
|
||||
|
||||
if (!UtilityString.isNullOrEmpty(responseJson)) {
|
||||
ResponseJSONObjectMapper mapper = new ResponseJSONObjectMapper();
|
||||
|
||||
ServiceRestResponse serviceRestResponse = mapper.readValue(responseJson, ServiceRestResponse.class);
|
||||
|
||||
if (serviceRestResponse.getEsito() != null && serviceRestResponse.getEsito().equals(EsitoType.KO)) {
|
||||
logger.error(String.format("Eccezione %s generata dall'operazione %s", serviceRestResponse.getErrorMessage(), this.getDtoInstance().getName()));
|
||||
}
|
||||
}
|
||||
|
||||
if (status != 200) {
|
||||
throw new Exception("Il servizio ha restituito lo status code " + status);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -11,6 +11,7 @@ import it.integry.ems_model.utility.UtilityServer;
|
||||
import it.integry.ems_model.utility.UtilityString;
|
||||
import it.integry.security.utility.RestUtil;
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.apache.http.NameValuePair;
|
||||
import org.apache.http.entity.ContentType;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
@@ -57,13 +58,19 @@ public class HttpRestWrapper {
|
||||
}
|
||||
|
||||
|
||||
public Object callInternalMethod(String profileDb, String service, String username, RequestMethod requestMethod) throws Exception {
|
||||
public Object callInternalMethod(String profileDb, String service, String username, RequestMethod requestMethod, List<NameValuePair> queryParams, Entity<?> bodyEntity) throws Exception {
|
||||
final String endPoint = String.format("http://localhost:%s/ems-api/", UtilityServer.getLocalServerPort());
|
||||
|
||||
final Client client = ClientBuilder.newClient();
|
||||
final WebTarget resource = client.target(endPoint)
|
||||
WebTarget resource = client.target(endPoint)
|
||||
.path(service);
|
||||
|
||||
if (queryParams != null) {
|
||||
for (NameValuePair queryParam : queryParams) {
|
||||
resource = resource.queryParam(queryParam.getName(), queryParam.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
final Invocation.Builder requestBuilder = resource
|
||||
.queryParam(CommonConstants.PROFILE_DB, profileDb)
|
||||
.request()
|
||||
@@ -74,16 +81,16 @@ public class HttpRestWrapper {
|
||||
|
||||
switch (requestMethod) {
|
||||
default:
|
||||
case POST:
|
||||
response = requestBuilder.post(bodyEntity);
|
||||
break;
|
||||
|
||||
case GET:
|
||||
response = requestBuilder.get();
|
||||
break;
|
||||
|
||||
case POST:
|
||||
response = requestBuilder.post(null);
|
||||
break;
|
||||
|
||||
case PUT:
|
||||
response = requestBuilder.put(null);
|
||||
response = requestBuilder.put(bodyEntity);
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user