Merge branch 'master' into develop

This commit is contained in:
2024-06-04 18:59:24 +02:00
4 changed files with 39 additions and 82 deletions

View File

@@ -2,8 +2,15 @@ package it.integry.ems.schedule.new_cron_job.dto.operations;
import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonInclude;
import it.integry.ems.schedule.new_cron_job.dto.operations.base_classes.BaseScheduledOperationDTO; 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 org.springframework.web.bind.annotation.RequestMethod;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
@JsonInclude @JsonInclude
public class ServiceCallAutomatedOperationDTO extends BaseScheduledOperationDTO { public class ServiceCallAutomatedOperationDTO extends BaseScheduledOperationDTO {
@@ -47,6 +54,11 @@ public class ServiceCallAutomatedOperationDTO extends BaseScheduledOperationDTO
return this; return this;
} }
public List<NameValuePair> parseQueryParams() {
if(UtilityString.isNullOrEmpty(getQueryParams())) return new ArrayList<>();
return URLEncodedUtils.parse(getQueryParams(), StandardCharsets.UTF_8);
}
public String getQueryParams() { public String getQueryParams() {
return queryParams; return queryParams;
} }

View File

@@ -16,7 +16,9 @@ public class CheckB2BScheduledOperationRunner extends BaseScheduledOperationRunn
final Object internalGetResponse = httpRestWrapper.callInternalMethod(getDtoInstance().getProfileDb(), final Object internalGetResponse = httpRestWrapper.callInternalMethod(getDtoInstance().getProfileDb(),
EmsRestConstants.PATH_READ_FPX_INBOX, EmsRestConstants.PATH_READ_FPX_INBOX,
getDtoInstance().getUsername(), getDtoInstance().getUsername(),
RequestMethod.POST); RequestMethod.POST,
null,
null);
} }

View File

@@ -1,90 +1,26 @@
package it.integry.ems.schedule.new_cron_job.dto.operations.runners; 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.schedule.new_cron_job.dto.operations.ServiceCallAutomatedOperationDTO;
import it.integry.ems.service.HttpRestWrapper; import it.integry.ems.service.HttpRestWrapper;
import it.integry.ems_model.utility.UtilityServer; import org.springframework.web.context.ContextLoader;
import it.integry.ems_model.utility.UtilityString;
import org.apache.http.entity.ContentType; import javax.ws.rs.client.Entity;
import org.springframework.web.bind.annotation.RequestMethod;
public class ServiceCallScheduledOperationRunner extends BaseScheduledOperationRunner<ServiceCallAutomatedOperationDTO> { public class ServiceCallScheduledOperationRunner extends BaseScheduledOperationRunner<ServiceCallAutomatedOperationDTO> {
@Override @Override
public void run() throws Exception { 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; final Object internalGetResponse = httpRestWrapper.callInternalMethod(getDtoInstance().getProfileDb(),
getDtoInstance().getMethodName(),
RequestMethod methodType = getDtoInstance().getMethodType(); getDtoInstance().getUsername(),
getDtoInstance().getMethodType(),
if (methodType == null) { getDtoInstance().parseQueryParams(),
methodType = RequestMethod.POST; Entity.json(getDtoInstance().getBody())
}
switch (methodType) {
case GET:
status = HttpRestWrapper.callGenericGet(
baseUrl,
getDtoInstance().getUsername(),
getDtoInstance().getPassword(),
responseBody,
null
); );
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);
}
} }
} }

View File

@@ -11,6 +11,7 @@ import it.integry.ems_model.utility.UtilityServer;
import it.integry.ems_model.utility.UtilityString; import it.integry.ems_model.utility.UtilityString;
import it.integry.security.utility.RestUtil; import it.integry.security.utility.RestUtil;
import org.apache.commons.io.FileUtils; import org.apache.commons.io.FileUtils;
import org.apache.http.NameValuePair;
import org.apache.http.entity.ContentType; import org.apache.http.entity.ContentType;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; 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 String endPoint = String.format("http://localhost:%s/ems-api/", UtilityServer.getLocalServerPort());
final Client client = ClientBuilder.newClient(); final Client client = ClientBuilder.newClient();
final WebTarget resource = client.target(endPoint) WebTarget resource = client.target(endPoint)
.path(service); .path(service);
if (queryParams != null) {
for (NameValuePair queryParam : queryParams) {
resource = resource.queryParam(queryParam.getName(), queryParam.getValue());
}
}
final Invocation.Builder requestBuilder = resource final Invocation.Builder requestBuilder = resource
.queryParam(CommonConstants.PROFILE_DB, profileDb) .queryParam(CommonConstants.PROFILE_DB, profileDb)
.request() .request()
@@ -74,16 +81,16 @@ public class HttpRestWrapper {
switch (requestMethod) { switch (requestMethod) {
default: default:
case POST:
response = requestBuilder.post(bodyEntity);
break;
case GET: case GET:
response = requestBuilder.get(); response = requestBuilder.get();
break; break;
case POST:
response = requestBuilder.post(null);
break;
case PUT: case PUT:
response = requestBuilder.put(null); response = requestBuilder.put(bodyEntity);
break; break;
} }