001/** 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018 019package org.apache.oozie.servlet; 020 021import java.io.IOException; 022 023import javax.servlet.http.HttpServletRequest; 024import javax.servlet.http.HttpServletResponse; 025 026import org.apache.hadoop.conf.Configuration; 027import org.apache.oozie.DagEngine; 028import org.apache.oozie.DagEngineException; 029import org.apache.oozie.ErrorCode; 030import org.apache.oozie.OozieJsonFactory; 031import org.apache.oozie.WorkflowsInfo; 032import org.apache.oozie.client.OozieClient; 033import org.apache.oozie.client.rest.JsonTags; 034import org.apache.oozie.client.rest.RestConstants; 035import org.apache.oozie.service.DagEngineService; 036import org.apache.oozie.service.Services; 037import org.json.simple.JSONObject; 038 039public class V0JobsServlet extends BaseJobsServlet { 040 041 private static final String INSTRUMENTATION_NAME = "v0jobs"; 042 043 public V0JobsServlet() { 044 super(INSTRUMENTATION_NAME); 045 } 046 047 048 /** 049 * v0 service implementation to submit a workflow job 050 */ 051 @Override 052 protected JSONObject submitJob(HttpServletRequest request, Configuration conf) throws XServletException, IOException { 053 054 JSONObject json = new JSONObject(); 055 056 try { 057 String action = request.getParameter(RestConstants.ACTION_PARAM); 058 if (action != null && !action.equals(RestConstants.JOB_ACTION_START)) { 059 throw new XServletException(HttpServletResponse.SC_BAD_REQUEST, ErrorCode.E0303, RestConstants.ACTION_PARAM, action); 060 } 061 boolean startJob = (action != null); 062 String user = conf.get(OozieClient.USER_NAME); 063 DagEngine dagEngine = Services.get().get(DagEngineService.class).getDagEngine(user); 064 String id = dagEngine.submitJob(conf, startJob); 065 json.put(JsonTags.JOB_ID, id); 066 } 067 catch (DagEngineException ex) { 068 throw new XServletException(HttpServletResponse.SC_BAD_REQUEST, ex); 069 } 070 071 return json; 072 } 073 074 /** 075 * v0 service implementation to get a JSONObject representation of a job from its external ID 076 */ 077 @Override 078 protected JSONObject getJobIdForExternalId(HttpServletRequest request, String externalId) throws XServletException, IOException { 079 JSONObject json = new JSONObject(); 080 try { 081 DagEngine dagEngine = Services.get().get(DagEngineService.class) 082 .getDagEngine(getUser(request)); 083 String jobId = dagEngine.getJobIdForExternalId(externalId); 084 json.put(JsonTags.JOB_ID, jobId); 085 } 086 catch (DagEngineException ex) { 087 throw new XServletException(HttpServletResponse.SC_BAD_REQUEST, ex); 088 } 089 return json; 090 } 091 092 /** 093 * v0 service implementation to get a list of workflows, with filtering or interested windows embedded in the 094 * request object 095 */ 096 @Override 097 protected JSONObject getJobs(HttpServletRequest request) throws XServletException, IOException { 098 JSONObject json; 099 try { 100 String filter = request.getParameter(RestConstants.JOBS_FILTER_PARAM); 101 String startStr = request.getParameter(RestConstants.OFFSET_PARAM); 102 String lenStr = request.getParameter(RestConstants.LEN_PARAM); 103 int start = (startStr != null) ? Integer.parseInt(startStr) : 1; 104 start = (start < 1) ? 1 : start; 105 int len = (lenStr != null) ? Integer.parseInt(lenStr) : 50; 106 len = (len < 1) ? 50 : len; 107 DagEngine dagEngine = Services.get().get(DagEngineService.class) 108 .getDagEngine(getUser(request)); 109 WorkflowsInfo jobs = dagEngine.getJobs(filter, start, len); 110 json = OozieJsonFactory.getWFJSONObject(jobs, "GMT"); 111 } 112 catch (DagEngineException ex) { 113 throw new XServletException(HttpServletResponse.SC_BAD_REQUEST, ex); 114 } 115 116 return json; 117 } 118 119 120 /** 121 * service implementation to bulk kill jobs 122 * @param request 123 * @param response 124 * @throws XServletException 125 * @throws IOException 126 */ 127 @Override 128 protected JSONObject killJobs(HttpServletRequest request, HttpServletResponse response) throws XServletException, 129 IOException { 130 throw new UnsupportedOperationException("method not implemented in V0 API"); 131 } 132 133 /** 134 * service implementation to bulk suspend jobs 135 * @param request 136 * @param response 137 * @throws XServletException 138 * @throws IOException 139 */ 140 @Override 141 protected JSONObject suspendJobs(HttpServletRequest request, HttpServletResponse response) throws XServletException, 142 IOException { 143 throw new UnsupportedOperationException("method not implemented in V0 API"); 144 } 145 146 /** 147 * service implementation to bulk resume jobs 148 * @param request 149 * @param response 150 * @throws XServletException 151 * @throws IOException 152 */ 153 @Override 154 protected JSONObject resumeJobs(HttpServletRequest request, HttpServletResponse response) throws XServletException, 155 IOException { 156 throw new UnsupportedOperationException("method not implemented in V0 API"); 157 } 158}