All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.vip.saturn.job.console.controller.rest.JobOperationRestApiController Maven / Gradle / Ivy

/**
 * Copyright 1999-2015 dangdang.com.
 * 

* Licensed 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 com.vip.saturn.job.console.controller.rest; import com.vip.saturn.job.console.aop.annotation.Audit; import com.vip.saturn.job.console.aop.annotation.AuditType; import com.vip.saturn.job.console.domain.BatchJobResult; import com.vip.saturn.job.console.domain.JobConfig; import com.vip.saturn.job.console.domain.JobType; import com.vip.saturn.job.console.domain.RestApiJobInfo; import com.vip.saturn.job.console.exception.SaturnJobConsoleException; import com.vip.saturn.job.console.exception.SaturnJobConsoleHttpException; import com.vip.saturn.job.console.mybatis.entity.JobConfig4DB; import com.vip.saturn.job.console.service.JobService; import com.vip.saturn.job.console.service.NamespaceService; import com.vip.saturn.job.console.service.RestApiService; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import java.util.List; import java.util.Map; /** * RESTful APIs of Job Operations. */ @RequestMapping("/rest/v1") public class JobOperationRestApiController extends AbstractRestController { @Resource private RestApiService restApiService; @Resource private NamespaceService namespaceService; @Resource private JobService jobService; @Audit(type = AuditType.REST) @RequestMapping(value = "/{namespace}/jobs", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE) public ResponseEntity create(@PathVariable("namespace") String namespace, @RequestBody Map reqParams) throws SaturnJobConsoleException { try { JobConfig jobConfig = constructJobConfigOfCreate(namespace, reqParams); restApiService.createJob(namespace, jobConfig); return new ResponseEntity<>(HttpStatus.CREATED); } catch (SaturnJobConsoleException e) { throw e; } catch (Exception e) { throw new SaturnJobConsoleHttpException(HttpStatus.INTERNAL_SERVER_ERROR.value(), e.getMessage(), e); } } @Audit(type = AuditType.REST) @RequestMapping(value = "/{namespace}/jobs/{jobName}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE) public ResponseEntity query(@PathVariable("namespace") String namespace, @PathVariable("jobName") String jobName) throws SaturnJobConsoleException { HttpHeaders httpHeaders = new HttpHeaders(); try { RestApiJobInfo restAPIJobInfo = restApiService.getRestAPIJobInfo(namespace, jobName); return new ResponseEntity(restAPIJobInfo, httpHeaders, HttpStatus.OK); } catch (SaturnJobConsoleException e) { throw e; } catch (Exception e) { throw new SaturnJobConsoleHttpException(HttpStatus.INTERNAL_SERVER_ERROR.value(), e.getMessage(), e); } } @Audit(type = AuditType.REST) @RequestMapping(value = "/all/jobs", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE) public ResponseEntity query(@RequestParam("queue") String queue) throws SaturnJobConsoleException { HttpHeaders httpHeaders = new HttpHeaders(); try { checkMissingParameter("queue", queue); List jobInfo = jobService.getJobsByQueue(queue); return new ResponseEntity(jobInfo, httpHeaders, HttpStatus.OK); } catch (SaturnJobConsoleException e) { throw e; } catch (Exception e) { throw new SaturnJobConsoleHttpException(HttpStatus.INTERNAL_SERVER_ERROR.value(), e.getMessage(), e); } } @Audit(type = AuditType.REST) @RequestMapping(value = "/{namespace}/jobs", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE) public ResponseEntity queryAll(@PathVariable("namespace") String namespace) throws SaturnJobConsoleException { HttpHeaders httpHeaders = new HttpHeaders(); try { List restApiJobInfos = restApiService.getRestApiJobInfos(namespace); return new ResponseEntity(restApiJobInfos, httpHeaders, HttpStatus.OK); } catch (SaturnJobConsoleException e) { throw e; } catch (Exception e) { throw new SaturnJobConsoleHttpException(HttpStatus.INTERNAL_SERVER_ERROR.value(), e.getMessage(), e); } } @Audit(type = AuditType.REST) @RequestMapping(value = "/{namespace}/allJobs", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE) public ResponseEntity queryAllJobs(@PathVariable("namespace") String namespace) throws SaturnJobConsoleException { HttpHeaders httpHeaders = new HttpHeaders(); try { List jobConfigList = restApiService.getJobConfigList(namespace); return new ResponseEntity(jobConfigList, httpHeaders, HttpStatus.OK); } catch (SaturnJobConsoleException e) { throw e; } catch (Exception e) { throw new SaturnJobConsoleHttpException(HttpStatus.INTERNAL_SERVER_ERROR.value(), e.getMessage(), e); } } @Audit(type = AuditType.REST) @RequestMapping(value = {"/{namespace}/{jobName}/enable", "/{namespace}/jobs/{jobName}/enable"}, method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE) public ResponseEntity enable(@PathVariable("namespace") String namespace, @PathVariable("jobName") String jobName) throws SaturnJobConsoleException { HttpHeaders httpHeaders = new HttpHeaders(); try { restApiService.enableJob(namespace, jobName); return new ResponseEntity<>(httpHeaders, HttpStatus.OK); } catch (SaturnJobConsoleException e) { throw e; } catch (Exception e) { throw new SaturnJobConsoleHttpException(HttpStatus.INTERNAL_SERVER_ERROR.value(), e.getMessage(), e); } } @Audit(type = AuditType.REST) @RequestMapping(value = {"/{namespace}/{jobName}/disable", "/{namespace}/jobs/{jobName}/disable"}, method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE) public ResponseEntity disable(@PathVariable("namespace") String namespace, @PathVariable("jobName") String jobName) throws SaturnJobConsoleException { HttpHeaders httpHeaders = new HttpHeaders(); try { restApiService.disableJob(namespace, jobName); return new ResponseEntity<>(httpHeaders, HttpStatus.OK); } catch (SaturnJobConsoleException e) { throw e; } catch (Exception e) { throw new SaturnJobConsoleHttpException(HttpStatus.INTERNAL_SERVER_ERROR.value(), e.getMessage(), e); } } @Audit(type = AuditType.REST) @RequestMapping(value = {"/{namespace}/{jobName}/cron", "/{namespace}/jobs/{jobName}/cron"}, method = RequestMethod.PUT, produces = MediaType.APPLICATION_JSON_UTF8_VALUE) public ResponseEntity updateJobCron(@PathVariable("namespace") String namespace, @PathVariable("jobName") String jobName, @RequestBody Map params, HttpServletRequest request) throws SaturnJobConsoleException { HttpHeaders httpHeaders = new HttpHeaders(); try { String cron = params.remove("cron"); checkMissingParameter("cron", cron); restApiService.updateJobCron(namespace, jobName, cron, params); return new ResponseEntity<>(httpHeaders, HttpStatus.OK); } catch (SaturnJobConsoleException e) { throw e; } catch (Exception e) { throw new SaturnJobConsoleHttpException(HttpStatus.INTERNAL_SERVER_ERROR.value(), e.getMessage(), e); } } @Audit(type = AuditType.REST) @RequestMapping(value = "/{namespace}/jobs/{jobName}/run", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE) public ResponseEntity run(@PathVariable("namespace") String namespace, @PathVariable("jobName") String jobName) throws SaturnJobConsoleException { try { restApiService.runJobAtOnce(namespace, jobName, null); return new ResponseEntity<>(HttpStatus.NO_CONTENT); } catch (SaturnJobConsoleException e) { throw e; } catch (Exception e) { throw new SaturnJobConsoleHttpException(HttpStatus.INTERNAL_SERVER_ERROR.value(), e.getMessage(), e); } } @Audit(type = AuditType.REST) @RequestMapping(value = "/{namespace}/jobs/{jobName}/stop", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE) public ResponseEntity stop(@PathVariable("namespace") String namespace, @PathVariable("jobName") String jobName) throws SaturnJobConsoleException { try { restApiService.stopJobAtOnce(namespace, jobName); return new ResponseEntity<>(HttpStatus.NO_CONTENT); } catch (SaturnJobConsoleException e) { throw e; } catch (Exception e) { throw new SaturnJobConsoleHttpException(HttpStatus.INTERNAL_SERVER_ERROR.value(), e.getMessage(), e); } } @Audit(type = AuditType.REST) @RequestMapping(value = "/{namespace}/jobs/{jobName}", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE) public ResponseEntity delete(@PathVariable("namespace") String namespace, @PathVariable("jobName") String jobName) throws SaturnJobConsoleException { try { restApiService.deleteJob(namespace, jobName); return new ResponseEntity<>(HttpStatus.NO_CONTENT); } catch (SaturnJobConsoleException e) { throw e; } catch (Exception e) { throw new SaturnJobConsoleHttpException(HttpStatus.INTERNAL_SERVER_ERROR.value(), e.getMessage(), e); } } @Audit(type = AuditType.REST) @RequestMapping(value = "/{namespace}/jobs/{jobName}", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE) public ResponseEntity update(@PathVariable("namespace") String namespace, @PathVariable String jobName, @RequestBody Map reqParams) throws SaturnJobConsoleException { try { JobConfig jobConfig = constructJobConfigOfUpdate(namespace, jobName, reqParams); restApiService.updateJob(namespace, jobName, jobConfig); return new ResponseEntity<>(HttpStatus.OK); } catch (SaturnJobConsoleException e) { throw e; } catch (Exception e) { throw new SaturnJobConsoleHttpException(HttpStatus.INTERNAL_SERVER_ERROR.value(), e.getMessage(), e); } } @Audit(type = AuditType.REST) @RequestMapping(value = "/{namespace}/jobs/{jobName}/runDownStream", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE) public ResponseEntity runDownStream(@PathVariable("namespace") String namespace, @PathVariable("jobName") String jobName, @RequestBody Map triggeredData) throws SaturnJobConsoleException { HttpHeaders httpHeaders = new HttpHeaders(); try { List batchJobResultList = restApiService.runDownStream(namespace, jobName, triggeredData); return new ResponseEntity(batchJobResultList, httpHeaders, HttpStatus.OK); } catch (SaturnJobConsoleException e) { throw e; } catch (Exception e) { throw new SaturnJobConsoleHttpException(HttpStatus.INTERNAL_SERVER_ERROR.value(), e.getMessage(), e); } } @Audit(type = AuditType.REST) @RequestMapping(value = "/namespaces/importJobFromNamespaceToNamespace", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE) public ResponseEntity importJobsFromNamespaceToNamespace(@RequestParam("srcNamespace") String srcNamespace, @RequestParam("destNamespace") String destNamespace, @RequestParam("createBy") String createBy) throws SaturnJobConsoleException { try { Map result = namespaceService .importJobsFromNamespaceToNamespace(srcNamespace, destNamespace, createBy); return new ResponseEntity(result, HttpStatus.OK); } catch (SaturnJobConsoleException e) { throw e; } catch (Exception e) { throw new SaturnJobConsoleHttpException(HttpStatus.INTERNAL_SERVER_ERROR.value(), e.getMessage(), e); } } private JobConfig constructJobConfigOfCreate(String namespace, Map reqParams) throws SaturnJobConsoleException { checkMissingParameter("namespace", namespace); if (!reqParams.containsKey("jobConfig")) { throw new SaturnJobConsoleHttpException(HttpStatus.BAD_REQUEST.value(), String.format(INVALID_REQUEST_MSG, "jobConfig", "cannot be blank")); } JobConfig jobConfig = new JobConfig(); Map configParams = (Map) reqParams.get("jobConfig"); jobConfig.setJobName(checkAndGetParametersValueAsString(reqParams, "jobName", true)); jobConfig.setDescription(checkAndGetParametersValueAsString(reqParams, "description", false)); jobConfig.setChannelName(checkAndGetParametersValueAsString(configParams, "channelName", false)); jobConfig.setCron(checkAndGetParametersValueAsString(configParams, "cron", false)); jobConfig.setJobClass(checkAndGetParametersValueAsString(configParams, "jobClass", false)); jobConfig.setJobParameter(checkAndGetParametersValueAsString(configParams, "jobParameter", false)); String jobType = checkAndGetParametersValueAsString(configParams, "jobType", true); if (JobType.UNKNOWN_JOB == JobType.getJobType(jobType)) { throw new SaturnJobConsoleHttpException(HttpStatus.BAD_REQUEST.value(), String.format(INVALID_REQUEST_MSG, "jobType", "is malformed")); } jobConfig.setJobType(jobType); jobConfig.setLoadLevel(checkAndGetParametersValueAsInteger(configParams, "loadLevel", false)); jobConfig.setLocalMode(checkAndGetParametersValueAsBoolean(configParams, "localMode", false)); jobConfig.setPausePeriodDate(checkAndGetParametersValueAsString(configParams, "pausePeriodDate", false)); jobConfig.setPausePeriodTime(checkAndGetParametersValueAsString(configParams, "pausePeriodTime", false)); jobConfig.setPreferList(checkAndGetParametersValueAsString(configParams, "preferList", false)); jobConfig.setQueueName(checkAndGetParametersValueAsString(configParams, "queueName", false)); jobConfig.setShardingItemParameters( checkAndGetParametersValueAsString(configParams, "shardingItemParameters", true)); jobConfig.setShardingTotalCount(checkAndGetParametersValueAsInteger(configParams, "shardingTotalCount", true)); jobConfig.setTimeout4AlarmSeconds( checkAndGetParametersValueAsInteger(configParams, "timeout4AlarmSeconds", false)); jobConfig.setUseDispreferList(checkAndGetParametersValueAsBoolean(configParams, "useDispreferList", false)); jobConfig.setUseSerial(checkAndGetParametersValueAsBoolean(configParams, "useSerial", false)); jobConfig.setJobDegree(checkAndGetParametersValueAsInteger(configParams, "jobDegree", false)); jobConfig.setDependencies(checkAndGetParametersValueAsString(configParams, "dependencies", false)); jobConfig.setTimeZone(checkAndGetParametersValueAsString(configParams, "timeZone", false)); jobConfig.setTimeoutSeconds(checkAndGetParametersValueAsInteger(configParams, "timeoutSeconds", false)); jobConfig.setProcessCountIntervalSeconds( checkAndGetParametersValueAsInteger(configParams, "processCountIntervalSeconds", false)); jobConfig.setGroups(checkAndGetParametersValueAsString(configParams, "groups", false)); jobConfig.setShowNormalLog(checkAndGetParametersValueAsBoolean(configParams, "showNormalLog", false)); jobConfig.setFailover(checkAndGetParametersValueAsBoolean(configParams, "failover", false)); jobConfig.setRerun(checkAndGetParametersValueAsBoolean(configParams, "rerun", false)); jobConfig.setUpStream(checkAndGetParametersValueAsString(configParams, "upStream", false)); jobConfig.setDownStream(checkAndGetParametersValueAsString(configParams, "downStream", false)); return jobConfig; } private JobConfig constructJobConfigOfUpdate(String namespace, String jobName, Map reqParams) throws SaturnJobConsoleException { checkMissingParameter("namespace", namespace); checkMissingParameter("jobName", jobName); if (!reqParams.containsKey("jobConfig")) { throw new SaturnJobConsoleHttpException(HttpStatus.BAD_REQUEST.value(), String.format(INVALID_REQUEST_MSG, "jobConfig", "cannot be blank")); } JobConfig jobConfig = new JobConfig(); Map configParams = (Map) reqParams.get("jobConfig"); jobConfig.setJobName(jobName); jobConfig.setDescription(checkAndGetParametersValueAsString(reqParams, "description", false)); jobConfig.setChannelName(checkAndGetParametersValueAsString(configParams, "channelName", false)); jobConfig.setCron(checkAndGetParametersValueAsString(configParams, "cron", false)); jobConfig.setJobParameter(checkAndGetParametersValueAsString(configParams, "jobParameter", false)); jobConfig.setLoadLevel(checkAndGetParametersValueAsInteger(configParams, "loadLevel", false)); jobConfig.setLocalMode(checkAndGetParametersValueAsBoolean(configParams, "localMode", false)); jobConfig.setPausePeriodDate(checkAndGetParametersValueAsString(configParams, "pausePeriodDate", false)); jobConfig.setPausePeriodTime(checkAndGetParametersValueAsString(configParams, "pausePeriodTime", false)); jobConfig.setPreferList(checkAndGetParametersValueAsString(configParams, "preferList", false)); jobConfig.setQueueName(checkAndGetParametersValueAsString(configParams, "queueName", false)); jobConfig.setShardingItemParameters( checkAndGetParametersValueAsString(configParams, "shardingItemParameters", false)); jobConfig.setShardingTotalCount(checkAndGetParametersValueAsInteger(configParams, "shardingTotalCount", false)); jobConfig.setTimeout4AlarmSeconds( checkAndGetParametersValueAsInteger(configParams, "timeout4AlarmSeconds", false)); jobConfig.setUseDispreferList(checkAndGetParametersValueAsBoolean(configParams, "useDispreferList", false)); jobConfig.setUseSerial(checkAndGetParametersValueAsBoolean(configParams, "useSerial", false)); jobConfig.setJobDegree(checkAndGetParametersValueAsInteger(configParams, "jobDegree", false)); jobConfig.setDependencies(checkAndGetParametersValueAsString(configParams, "dependencies", false)); jobConfig.setTimeZone(checkAndGetParametersValueAsString(configParams, "timeZone", false)); jobConfig.setTimeoutSeconds(checkAndGetParametersValueAsInteger(configParams, "timeoutSeconds", false)); jobConfig.setProcessCountIntervalSeconds( checkAndGetParametersValueAsInteger(configParams, "processCountIntervalSeconds", false)); jobConfig.setGroups(checkAndGetParametersValueAsString(configParams, "groups", false)); jobConfig.setShowNormalLog(checkAndGetParametersValueAsBoolean(configParams, "showNormalLog", false)); jobConfig.setRerun(checkAndGetParametersValueAsBoolean(configParams, "rerun", false)); jobConfig.setFailover(checkAndGetParametersValueAsBoolean(configParams, "failover", false)); jobConfig.setUpStream(checkAndGetParametersValueAsString(configParams, "upStream", false)); jobConfig.setDownStream(checkAndGetParametersValueAsString(configParams, "downStream", false)); return jobConfig; } }