diff --git a/fineract-provider/src/main/java/org/apache/fineract/commands/service/SynchronousCommandProcessingService.java b/fineract-provider/src/main/java/org/apache/fineract/commands/service/SynchronousCommandProcessingService.java index af93b88d1..17646ff69 100644 --- a/fineract-provider/src/main/java/org/apache/fineract/commands/service/SynchronousCommandProcessingService.java +++ b/fineract-provider/src/main/java/org/apache/fineract/commands/service/SynchronousCommandProcessingService.java @@ -18,16 +18,9 @@ */ package org.apache.fineract.commands.service; -import com.google.gson.Gson; -import com.google.gson.reflect.TypeToken; -import java.lang.reflect.Type; -import java.time.Instant; import java.time.ZoneId; import java.time.ZonedDateTime; -import java.util.HashMap; import java.util.Map; -import org.apache.fineract.batch.exception.ErrorHandler; -import org.apache.fineract.batch.exception.ErrorInfo; import org.apache.fineract.commands.domain.CommandSource; import org.apache.fineract.commands.domain.CommandSourceRepository; import org.apache.fineract.commands.domain.CommandWrapper; @@ -45,8 +38,6 @@ import org.apache.fineract.infrastructure.hooks.event.HookEvent; import org.apache.fineract.infrastructure.hooks.event.HookEventSource; import org.apache.fineract.infrastructure.security.service.PlatformSecurityContext; import org.apache.fineract.useradministration.domain.AppUser; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.stereotype.Service; @@ -55,7 +46,6 @@ import org.springframework.transaction.annotation.Transactional; @Service public class SynchronousCommandProcessingService implements CommandProcessingService { - private static final Logger LOG = LoggerFactory.getLogger(SynchronousCommandProcessingService.class); private PlatformSecurityContext context; private final ApplicationContext applicationContext; private final ToApiJsonSerializer> toApiJsonSerializer; @@ -90,15 +80,7 @@ public class SynchronousCommandProcessingService implements CommandProcessingSer final NewCommandSourceHandler handler = findCommandHandler(wrapper); - // final CommandProcessingResult result = handler.processCommand(command); - final CommandProcessingResult result; - try { - result = handler.processCommand(command); - } catch (Throwable t) { - // publish error event - publishErrorEvent(wrapper, command, t); - throw t; - } + final CommandProcessingResult result = handler.processCommand(command); final AppUser maker = this.context.authenticatedUser(wrapper); @@ -143,8 +125,7 @@ public class SynchronousCommandProcessingService implements CommandProcessingSer } result.setRollbackTransaction(null); - // publishEvent(wrapper.entityName(), wrapper.actionName(), result); - publishEvent(wrapper.entityName(), wrapper.actionName(), command, result); + publishEvent(wrapper.entityName(), wrapper.actionName(), result); return result; } @@ -225,68 +206,18 @@ public class SynchronousCommandProcessingService implements CommandProcessingSer return rollbackTransaction; } - private void publishErrorEvent(CommandWrapper wrapper, JsonCommand command, Throwable t) { + private void publishEvent(final String entityName, final String actionName, final CommandProcessingResult result) { - ErrorInfo ex; - if (t instanceof RuntimeException) { - final RuntimeException e = (RuntimeException) t; - ex = ErrorHandler.handler(e); - } else { - ex = new ErrorInfo(500, 9999, "{\"Exception\": " + t.toString() + "}"); - } + final String authToken = ThreadLocalContextUtil.getAuthToken(); + final String tenantIdentifier = ThreadLocalContextUtil.getTenant().getTenantIdentifier(); + final AppUser appUser = this.context.authenticatedUser(CommandWrapper.wrap(actionName, entityName, null, null)); - publishEvent(wrapper.entityName(), wrapper.actionName(), command, ex); + final HookEventSource hookEventSource = new HookEventSource(entityName, actionName); + + final String serializedResult = this.toApiResultJsonSerializer.serialize(result); + + final HookEvent applicationEvent = new HookEvent(hookEventSource, serializedResult, tenantIdentifier, appUser, authToken); + + applicationContext.publishEvent(applicationEvent); } - - private void publishEvent(final String entityName, final String actionName, JsonCommand command, final Object result) { - Gson gson = new Gson(); - try { - final String authToken = ThreadLocalContextUtil.getAuthToken(); - final String tenantIdentifier = ThreadLocalContextUtil.getTenant().getTenantIdentifier(); - final AppUser appUser = this.context.authenticatedUser(CommandWrapper.wrap(actionName, entityName, null, null)); - - final HookEventSource hookEventSource = new HookEventSource(entityName, actionName); - - // TODO: Add support for publishing array events - if (command.json() != null && command.json().startsWith("{")) { - Type type = new TypeToken>() {}.getType(); - Map myMap = gson.fromJson(command.json(), type); - - Map reqmap = new HashMap<>(); - reqmap.put("entityName", entityName); - reqmap.put("actionName", actionName); - reqmap.put("createdBy", context.authenticatedUser().getId()); - reqmap.put("createdByName", context.authenticatedUser().getUsername()); - reqmap.put("createdByFullName", context.authenticatedUser().getDisplayName()); - - reqmap.put("request", myMap); - if (result instanceof CommandProcessingResult) { - CommandProcessingResult resultCopy = CommandProcessingResult - .fromCommandProcessingResult((CommandProcessingResult) result); - - reqmap.put("response", resultCopy); - } else if (result instanceof ErrorInfo) { - ErrorInfo ex = (ErrorInfo) result; - reqmap.put("status", "Exception"); - - Map errorMap = gson.fromJson(ex.getMessage(), type); - errorMap.put("errorCode", ex.getErrorCode()); - errorMap.put("statusCode", ex.getStatusCode()); - - reqmap.put("response", errorMap); - } - - reqmap.put("timestamp", Instant.now().toString()); - - final String serializedResult = this.toApiResultJsonSerializer.serialize(reqmap); - - final HookEvent applicationEvent = new HookEvent(hookEventSource, serializedResult, tenantIdentifier, appUser, authToken); - - applicationContext.publishEvent(applicationEvent); - } - } catch (Exception e) { - LOG.error("Error", e); - } - } - } diff --git a/fineract-provider/src/main/java/org/apache/fineract/infrastructure/core/data/CommandProcessingResult.java b/fineract-provider/src/main/java/org/apache/fineract/infrastructure/core/data/CommandProcessingResult.java index 707b8dbf7..526ebbd94 100644 --- a/fineract-provider/src/main/java/org/apache/fineract/infrastructure/core/data/CommandProcessingResult.java +++ b/fineract-provider/src/main/java/org/apache/fineract/infrastructure/core/data/CommandProcessingResult.java @@ -44,13 +44,6 @@ public class CommandProcessingResult implements Serializable { private final Long glimId; private Boolean rollbackTransaction; - public static CommandProcessingResult fromCommandProcessingResult(CommandProcessingResult commandResult) { - return new CommandProcessingResult(commandResult.commandId, commandResult.officeId, commandResult.groupId, commandResult.clientId, - commandResult.loanId, commandResult.savingsId, commandResult.resourceIdentifier, commandResult.resourceId, - commandResult.transactionId, commandResult.changes, commandResult.productId, commandResult.gsimId, commandResult.glimId, - commandResult.rollbackTransaction, commandResult.subResourceId); - } - public static CommandProcessingResult fromDetails(final Long commandId, final Long officeId, final Long groupId, final Long clientId, final Long loanId, final Long savingsId, final String resourceIdentifier, final Long entityId, final Long gsimId, final Long glimId, final String transactionId, final Map changes, final Long productId, diff --git a/fineract-provider/src/main/java/org/apache/fineract/infrastructure/hooks/api/HookApiConstants.java b/fineract-provider/src/main/java/org/apache/fineract/infrastructure/hooks/api/HookApiConstants.java index c11deea34..36cc1b160 100644 --- a/fineract-provider/src/main/java/org/apache/fineract/infrastructure/hooks/api/HookApiConstants.java +++ b/fineract-provider/src/main/java/org/apache/fineract/infrastructure/hooks/api/HookApiConstants.java @@ -38,8 +38,6 @@ public final class HookApiConstants { public static final String webTemplateName = "Web"; - public static final String elasticSearchTemplateName = "Elastic Search"; - public static final String smsTemplateName = "SMS Bridge"; public static final String payloadURLName = "Payload URL"; diff --git a/fineract-provider/src/main/java/org/apache/fineract/infrastructure/hooks/processor/ElasticSearchHookProcessor.java b/fineract-provider/src/main/java/org/apache/fineract/infrastructure/hooks/processor/ElasticSearchHookProcessor.java deleted file mode 100644 index 514c06832..000000000 --- a/fineract-provider/src/main/java/org/apache/fineract/infrastructure/hooks/processor/ElasticSearchHookProcessor.java +++ /dev/null @@ -1,88 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.fineract.infrastructure.hooks.processor; - -import static org.apache.fineract.infrastructure.hooks.api.HookApiConstants.contentTypeName; -import static org.apache.fineract.infrastructure.hooks.api.HookApiConstants.payloadURLName; - -import com.google.gson.Gson; -import com.google.gson.JsonObject; -import java.io.PrintWriter; -import java.util.HashMap; -import java.util.Map; -import java.util.Set; -import org.apache.fineract.infrastructure.hooks.domain.Hook; -import org.apache.fineract.infrastructure.hooks.domain.HookConfiguration; -import org.apache.fineract.useradministration.domain.AppUser; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Service; -import retrofit.Callback; - -@Service -public class ElasticSearchHookProcessor implements HookProcessor { - - @Autowired - private ProcessorHelper processorHelper; - - private PrintWriter out = null; - - @Override - public void process(final Hook hook, @SuppressWarnings("unused") final AppUser appUser, final String payload, final String entityName, - final String actionName, final String tenantIdentifier, final String authToken) { - - final Set config = hook.getHookConfig(); - - String url = ""; - String contentType = ""; - - for (final HookConfiguration conf : config) { - final String fieldName = conf.getFieldName(); - if (fieldName.equals(payloadURLName)) { - url = conf.getFieldValue(); - } - if (fieldName.equals(contentTypeName)) { - contentType = conf.getFieldValue(); - } - } - - sendRequest(url, contentType, payload, entityName, actionName, tenantIdentifier, authToken); - - } - - @SuppressWarnings("unchecked") - private void sendRequest(final String url, final String contentType, final String payload, final String entityName, - final String actionName, final String tenantIdentifier, @SuppressWarnings("unused") final String authToken) { - - final String fineractEndpointUrl = System.getProperty("baseUrl"); - final WebHookService service = processorHelper.createWebHookService(url); - - @SuppressWarnings("rawtypes") - final Callback callback = processorHelper.createCallback(url, payload); - - if (contentType.equalsIgnoreCase("json") || contentType.contains("json")) { - final JsonObject json = new Gson().fromJson(payload, JsonObject.class); - - service.sendJsonRequest(entityName, actionName, tenantIdentifier, fineractEndpointUrl, json, callback); - } else { - Map map = new HashMap<>(); - map = new Gson().fromJson(payload, map.getClass()); - service.sendFormRequest(entityName, actionName, tenantIdentifier, fineractEndpointUrl, map, callback); - } - } -} diff --git a/fineract-provider/src/main/java/org/apache/fineract/infrastructure/hooks/processor/HookProcessorProvider.java b/fineract-provider/src/main/java/org/apache/fineract/infrastructure/hooks/processor/HookProcessorProvider.java index 23d57096f..bd4e73f70 100644 --- a/fineract-provider/src/main/java/org/apache/fineract/infrastructure/hooks/processor/HookProcessorProvider.java +++ b/fineract-provider/src/main/java/org/apache/fineract/infrastructure/hooks/processor/HookProcessorProvider.java @@ -18,7 +18,6 @@ */ package org.apache.fineract.infrastructure.hooks.processor; -import static org.apache.fineract.infrastructure.hooks.api.HookApiConstants.elasticSearchTemplateName; import static org.apache.fineract.infrastructure.hooks.api.HookApiConstants.smsTemplateName; import static org.apache.fineract.infrastructure.hooks.api.HookApiConstants.webTemplateName; @@ -45,8 +44,6 @@ public class HookProcessorProvider implements ApplicationContextAware { processor = this.applicationContext.getBean("twilioHookProcessor", TwilioHookProcessor.class); } else if (templateName.equals(webTemplateName)) { processor = this.applicationContext.getBean("webHookProcessor", WebHookProcessor.class); - } else if (templateName.equals(elasticSearchTemplateName)) { - processor = this.applicationContext.getBean("elasticSearchHookProcessor", ElasticSearchHookProcessor.class); } else { processor = null; } diff --git a/fineract-provider/src/main/java/org/apache/fineract/infrastructure/hooks/processor/ProcessorHelper.java b/fineract-provider/src/main/java/org/apache/fineract/infrastructure/hooks/processor/ProcessorHelper.java index 4c74070e3..29392c1d3 100644 --- a/fineract-provider/src/main/java/org/apache/fineract/infrastructure/hooks/processor/ProcessorHelper.java +++ b/fineract-provider/src/main/java/org/apache/fineract/infrastructure/hooks/processor/ProcessorHelper.java @@ -121,20 +121,4 @@ public final class ProcessorHelper { final RestAdapter restAdapter = new RestAdapter.Builder().setEndpoint(url).setClient(new OkClient(client)).build(); return restAdapter.create(WebHookService.class); } - - public Callback createCallback(final String url, String payload) { - - return new Callback() { - - @Override - public void success(final Object o, final Response response) { - LOG.info("URL : {} \tStatus : {}", url, response.getStatus()); - } - - @Override - public void failure(final RetrofitError retrofitError) { - LOG.error("URL: {} - RetrofitError occured", url, retrofitError); - } - }; - } } diff --git a/fineract-provider/src/main/java/org/apache/fineract/useradministration/domain/AppUser.java b/fineract-provider/src/main/java/org/apache/fineract/useradministration/domain/AppUser.java index 449ec233e..b2ab5098b 100644 --- a/fineract-provider/src/main/java/org/apache/fineract/useradministration/domain/AppUser.java +++ b/fineract-provider/src/main/java/org/apache/fineract/useradministration/domain/AppUser.java @@ -39,7 +39,6 @@ import javax.persistence.Table; import javax.persistence.Temporal; import javax.persistence.TemporalType; import javax.persistence.UniqueConstraint; -import org.apache.commons.lang3.StringUtils; import org.apache.fineract.infrastructure.core.api.JsonCommand; import org.apache.fineract.infrastructure.core.data.EnumOptionData; import org.apache.fineract.infrastructure.core.domain.AbstractPersistableCustom; @@ -401,17 +400,6 @@ public class AppUser extends AbstractPersistableCustom implements PlatformUser { return this.username; } - public String getDisplayName() { - if (this.staff != null && StringUtils.isNotBlank(this.staff.displayName())) { - return this.staff.displayName(); - } - String firstName = StringUtils.isNotBlank(this.firstname) ? this.firstname : ""; - if (StringUtils.isNotBlank(this.lastname)) { - return firstName + " " + this.lastname; - } - return firstName; - } - @Override public boolean isAccountNonExpired() { return this.accountNonExpired; diff --git a/fineract-provider/src/main/resources/sql/migrations/core_db/V364__elastic_hook_template.sql b/fineract-provider/src/main/resources/sql/migrations/core_db/V364__elastic_hook_template.sql deleted file mode 100644 index c1e2a089e..000000000 --- a/fineract-provider/src/main/resources/sql/migrations/core_db/V364__elastic_hook_template.sql +++ /dev/null @@ -1,23 +0,0 @@ --- --- Licensed to the Apache Software Foundation (ASF) under one --- or more contributor license agreements. See the NOTICE file --- distributed with this work for additional information --- regarding copyright ownership. The ASF licenses this file --- to you under the Apache License, Version 2.0 (the --- "License"); you may not use this file except in compliance --- with the License. You may obtain a copy of the License at --- --- http://www.apache.org/licenses/LICENSE-2.0 --- --- Unless required by applicable law or agreed to in writing, --- software distributed under the License is distributed on an --- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY --- KIND, either express or implied. See the License for the --- specific language governing permissions and limitations --- under the License. --- - - -INSERT INTO m_hook_templates -( name) -VALUES('Elastic Search');