/** * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this file, * You can obtain one at http://mozilla.org/MPL/2.0/. */ package org.mifosplatform.infrastructure.jobs.api; import javax.ws.rs.Consumes; import javax.ws.rs.GET; import javax.ws.rs.POST; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.QueryParam; import javax.ws.rs.core.Context; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import javax.ws.rs.core.UriInfo; import org.apache.commons.lang.StringUtils; import org.mifosplatform.infrastructure.core.api.ApiRequestParameterHelper; import org.mifosplatform.infrastructure.core.exception.UnrecognizedQueryParamException; import org.mifosplatform.infrastructure.core.serialization.ApiRequestJsonSerializationSettings; import org.mifosplatform.infrastructure.core.serialization.ToApiJsonSerializer; import org.mifosplatform.infrastructure.jobs.data.SchedulerDetailData; import org.mifosplatform.infrastructure.jobs.service.JobRegisterService; import org.mifosplatform.infrastructure.security.exception.NoAuthorizationException; import org.mifosplatform.infrastructure.security.service.PlatformSecurityContext; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @Path("/scheduler") @Component public class SchedulerApiResource { private final PlatformSecurityContext context; private final JobRegisterService jobRegisterService; private final ToApiJsonSerializer<SchedulerDetailData> toApiJsonSerializer; private final ApiRequestParameterHelper apiRequestParameterHelper; @Autowired public SchedulerApiResource(final PlatformSecurityContext context, final JobRegisterService jobRegisterService, final ToApiJsonSerializer<SchedulerDetailData> toApiJsonSerializer, final ApiRequestParameterHelper apiRequestParameterHelper) { this.context = context; this.jobRegisterService = jobRegisterService; this.toApiJsonSerializer = toApiJsonSerializer; this.apiRequestParameterHelper = apiRequestParameterHelper; } @GET @Consumes({ MediaType.APPLICATION_JSON }) @Produces({ MediaType.APPLICATION_JSON }) public String retrieveStatus(@Context final UriInfo uriInfo) { this.context.authenticatedUser().validateHasReadPermission(SchedulerJobApiConstants.SCHEDULER_RESOURCE_NAME); final boolean isSchedulerRunning = this.jobRegisterService.isSchedulerRunning(); final ApiRequestJsonSerializationSettings settings = this.apiRequestParameterHelper.process(uriInfo.getQueryParameters()); final SchedulerDetailData schedulerDetailData = new SchedulerDetailData(isSchedulerRunning); return this.toApiJsonSerializer.serialize(settings, schedulerDetailData, SchedulerJobApiConstants.SCHEDULER_DETAIL_RESPONSE_DATA_PARAMETERS); } @POST @Consumes({ MediaType.APPLICATION_JSON }) @Produces({ MediaType.APPLICATION_JSON }) public Response changeSchedulerStatus(@QueryParam(SchedulerJobApiConstants.COMMAND) final String commandParam) { // check the logged in user have permissions to update scheduler status final boolean hasNotPermission = this.context.authenticatedUser().hasNotPermissionForAnyOf("ALL_FUNCTIONS", "UPDATE_SCHEDULER"); if (hasNotPermission) { final String authorizationMessage = "User has no authority to update scheduler status"; throw new NoAuthorizationException(authorizationMessage); } Response response = Response.status(400).build(); if (is(commandParam, SchedulerJobApiConstants.COMMAND_START_SCHEDULER)) { this.jobRegisterService.startScheduler(); response = Response.status(202).build(); } else if (is(commandParam, SchedulerJobApiConstants.COMMAND_STOP_SCHEDULER)) { this.jobRegisterService.pauseScheduler(); response = Response.status(202).build(); } else { throw new UnrecognizedQueryParamException(SchedulerJobApiConstants.COMMAND, commandParam); } return response; } private boolean is(final String commandParam, final String commandValue) { return StringUtils.isNotBlank(commandParam) && commandParam.trim().equalsIgnoreCase(commandValue); } }