use of org.entando.entando.aps.system.init.model.InvalidPostProcessResultException in project entando-core by entando.
the class SelfRestCaller method printResponse.
private void printResponse(SelfRestCallPostProcess selfRestCall, Object result, Response.Status responseStatus, ApiMethod method, Properties properties) throws InvalidPostProcessResultException, Throwable {
String responseClassName = method.getResponseClassName();
Class responseClass = (null != responseClassName) ? Class.forName(responseClassName) : StringApiResponse.class;
JAXBContext context = JAXBContext.newInstance(responseClass);
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
boolean validResponse = (responseStatus.getStatusCode() == selfRestCall.getExpectedResult());
StringBuilder path = new StringBuilder();
path.append("/api/rs/");
path.append(properties.get(SystemConstants.API_LANG_CODE_PARAMETER)).append("/");
if (null != method.getNamespace()) {
path.append(method.getNamespace()).append("/");
}
path.append(method.getResourceName());
StringBuilder log = new StringBuilder();
StringWriter writer = new StringWriter();
try {
marshaller.marshal(result, writer);
} catch (Exception e) {
_logger.info("Error extracting body response : " + e.getMessage());
writer.append(".......");
}
log.append("*************** Self Rest Call - response ***************\n");
log.append(method.getHttpMethod().toString()).append(" ");
log.append(path.toString()).append("\n");
log.append("Result ").append(responseStatus.getStatusCode()).append("\n");
log.append("Expected ").append(selfRestCall.getExpectedResult()).append("\n");
if (!validResponse) {
log.append("******** INVALID RESPONSE STATUS ********\n");
}
log.append("---------------------------------------------------------\n");
log.append(writer.toString()).append("\n");
log.append("*********************************************************\n");
if (!validResponse) {
if (!selfRestCall.isFailOnError()) {
log.append("failonerror was set to false: continuing anyway.\n");
} else {
log.append("the post processes will be stopped.\n");
}
}
_logger.info(log.toString());
System.out.println(log.toString());
if (!validResponse && selfRestCall.isFailOnError()) {
throw new InvalidPostProcessResultException(responseStatus.getStatusCode(), selfRestCall.getExpectedResult(), path.toString(), method.getHttpMethod());
}
}
use of org.entando.entando.aps.system.init.model.InvalidPostProcessResultException in project entando-core by entando.
the class SelfRestCaller method executePostProcess.
@Override
public int executePostProcess(IPostProcess postProcess) throws InvalidPostProcessResultException, ApsSystemException {
if (!(postProcess instanceof SelfRestCallPostProcess)) {
return 0;
}
SelfRestCallPostProcess selfRestCall = (SelfRestCallPostProcess) postProcess;
IResponseBuilder responseBuilder = this.getResponseBuilder();
try {
Object result = null;
ApiMethod method = responseBuilder.extractApiMethod(selfRestCall.getMethod(), selfRestCall.getNamespace(), selfRestCall.getResourceName());
Properties properties = this.extractParameters(selfRestCall);
if (method.getHttpMethod().equals(ApiMethod.HttpMethod.GET) || method.getHttpMethod().equals(ApiMethod.HttpMethod.DELETE)) {
result = responseBuilder.createResponse(method, properties);
} else {
String contentBody = this.getContentBody(selfRestCall);
Object bodyObject = UnmarshalUtils.unmarshal(method, contentBody, selfRestCall.getContentType());
result = responseBuilder.createResponse(method, bodyObject, properties);
}
Response.Status responseStatus = this.extractResponseStatusCode(result);
if (selfRestCall.isPrintResponse()) {
this.printResponse(selfRestCall, result, responseStatus, method, properties);
}
} catch (InvalidPostProcessResultException t) {
_logger.error("error in executePostProcess", t);
// ApsSystemUtils.logThrowable(t, this, "executePostProcess", t.getMessage());
throw t;
} catch (Throwable t) {
_logger.error("Error invoking api method", t);
// ApsSystemUtils.logThrowable(t, this, "executePostProcess", "Error invoking api method");
throw new ApsSystemException("Error invoking api method", t);
}
return 1;
}
Aggregations