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.BaseEngineException;
028import org.apache.oozie.DagEngine;
029import org.apache.oozie.DagEngineException;
030import org.apache.oozie.client.rest.JsonBean;
031import org.apache.oozie.service.DagEngineService;
032import org.apache.oozie.service.Services;
033import org.json.simple.JSONArray;
034import org.json.simple.JSONObject;
035import org.apache.oozie.ErrorCode;
036
037@SuppressWarnings("serial")
038public class V0JobServlet extends BaseJobServlet {
039
040    private static final String INSTRUMENTATION_NAME = "v0job";
041
042    final static String NOT_SUPPORTED_MESSAGE = "Not supported in v0";
043
044    public V0JobServlet() {
045        super(INSTRUMENTATION_NAME);
046    }
047
048    /*
049     * v0 service method to start a job
050     */
051    @Override
052    protected void startJob(HttpServletRequest request, HttpServletResponse response) throws XServletException,
053            IOException {
054        DagEngine dagEngine = Services.get().get(DagEngineService.class).getDagEngine(getUser(request));
055
056        String jobId = getResourceName(request);
057        try {
058            dagEngine.start(jobId);
059        }
060        catch (DagEngineException ex) {
061            throw new XServletException(HttpServletResponse.SC_BAD_REQUEST, ex);
062        }
063    }
064
065    /*
066     * v0 service method to resume a job
067     */
068    @Override
069    protected void resumeJob(HttpServletRequest request, HttpServletResponse response) throws XServletException,
070            IOException {
071        DagEngine dagEngine = Services.get().get(DagEngineService.class).getDagEngine(getUser(request));
072
073        String jobId = getResourceName(request);
074        try {
075            dagEngine.resume(jobId);
076        }
077        catch (DagEngineException ex) {
078            throw new XServletException(HttpServletResponse.SC_BAD_REQUEST, ex);
079        }
080    }
081
082    /*
083     * v0 service method to suspend a job
084     */
085    @Override
086    protected void suspendJob(HttpServletRequest request, HttpServletResponse response) throws XServletException,
087            IOException {
088        DagEngine dagEngine = Services.get().get(DagEngineService.class).getDagEngine(getUser(request));
089
090        String jobId = getResourceName(request);
091        try {
092            dagEngine.suspend(jobId);
093        }
094        catch (DagEngineException ex) {
095            throw new XServletException(HttpServletResponse.SC_BAD_REQUEST, ex);
096        }
097    }
098
099    /*
100     * v0 service method to kill a job
101     */
102    @Override
103    protected JSONObject killJob(HttpServletRequest request, HttpServletResponse response) throws XServletException,
104            IOException {
105        DagEngine dagEngine = Services.get().get(DagEngineService.class).getDagEngine(getUser(request));
106
107        String jobId = getResourceName(request);
108        try {
109            dagEngine.kill(jobId);
110        }
111        catch (DagEngineException ex) {
112            throw new XServletException(HttpServletResponse.SC_BAD_REQUEST, ex);
113        }
114        return null;
115    }
116
117    /*
118     * v0 service method to change a job
119     */
120    protected void changeJob(HttpServletRequest request, HttpServletResponse response) throws XServletException,
121            IOException {
122        // This code should not be reached. But if it happens somehow, we throw
123        // bad request exception.
124        throw new XServletException(HttpServletResponse.SC_BAD_REQUEST, ErrorCode.E1014);
125    }
126
127    /*
128     * v0 service method to reRun a job
129     */
130    @Override
131    protected JSONObject reRunJob(HttpServletRequest request, HttpServletResponse response, Configuration conf)
132            throws XServletException, IOException {
133        DagEngine dagEngine = Services.get().get(DagEngineService.class).getDagEngine(getUser(request));
134
135        String jobId = getResourceName(request);
136        try {
137            dagEngine.reRun(jobId, conf);
138        }
139        catch (DagEngineException ex) {
140            throw new XServletException(HttpServletResponse.SC_BAD_REQUEST, ex);
141        }
142        return null;
143    }
144
145    /*
146     * v0 service method to get a job in JsonBean representation
147     */
148    @Override
149    protected JsonBean getJob(HttpServletRequest request, HttpServletResponse response) throws XServletException,
150            IOException {
151        DagEngine dagEngine = Services.get().get(DagEngineService.class).getDagEngine(getUser(request));
152
153        JsonBean jobBean = null;
154        String jobId = getResourceName(request);
155        try {
156            jobBean = (JsonBean) dagEngine.getJob(jobId);
157        }
158        catch (DagEngineException ex) {
159            throw new XServletException(HttpServletResponse.SC_BAD_REQUEST, ex);
160        }
161
162        return jobBean;
163    }
164
165    /*
166     * v0 service method to get a job definition in String format
167     */
168    @Override
169    protected String getJobDefinition(HttpServletRequest request, HttpServletResponse response)
170            throws XServletException, IOException {
171        DagEngine dagEngine = Services.get().get(DagEngineService.class).getDagEngine(getUser(request));
172
173        String wfDefinition = null;
174        String jobId = getResourceName(request);
175        try {
176            wfDefinition = dagEngine.getDefinition(jobId);
177        }
178        catch (DagEngineException ex) {
179            throw new XServletException(HttpServletResponse.SC_BAD_REQUEST, ex);
180        }
181        return wfDefinition;
182    }
183
184    /*
185     * v0 service method to stream a job log into response object
186     */
187    @Override
188    protected void streamJobLog(HttpServletRequest request, HttpServletResponse response) throws XServletException,
189            IOException {
190        DagEngine dagEngine = Services.get().get(DagEngineService.class).getDagEngine(getUser(request));
191
192        String jobId = getResourceName(request);
193        try {
194            dagEngine.streamLog(jobId, response.getWriter(), request.getParameterMap());
195        }
196        catch (BaseEngineException ex) {
197            throw new XServletException(HttpServletResponse.SC_BAD_REQUEST, ex);
198        }
199    }
200
201    @Override
202    protected void streamJobErrorLog(HttpServletRequest request, HttpServletResponse response) throws XServletException,
203            IOException {
204        throw new XServletException(HttpServletResponse.SC_BAD_REQUEST, ErrorCode.E0306);
205    }
206
207    @Override
208    protected void streamJobAuditLog(HttpServletRequest request, HttpServletResponse response) throws XServletException,
209            IOException {
210        throw new XServletException(HttpServletResponse.SC_BAD_REQUEST, ErrorCode.E0306);
211    }
212
213    /*
214     * Not implemented in v0
215     */
216    @Override
217    protected void streamJobGraph(HttpServletRequest request, HttpServletResponse response)
218            throws XServletException, IOException {
219        // Should this error code be NOT_IMPLEMENTED?
220        throw new XServletException(HttpServletResponse.SC_BAD_REQUEST, ErrorCode.E0306);
221    }
222
223    @Override
224    protected String getJMSTopicName(HttpServletRequest request, HttpServletResponse response) throws XServletException,
225            IOException {
226        throw new XServletException(HttpServletResponse.SC_BAD_REQUEST, ErrorCode.E0306);
227    }
228
229    @Override
230    protected JSONObject getJobsByParentId(HttpServletRequest request, HttpServletResponse response) throws XServletException,
231            IOException {
232        throw new XServletException(HttpServletResponse.SC_BAD_REQUEST, ErrorCode.E0302, NOT_SUPPORTED_MESSAGE);
233    }
234
235    @Override
236    protected JSONObject updateJob(HttpServletRequest request, HttpServletResponse response, Configuration conf)
237            throws XServletException, IOException {
238        throw new XServletException(HttpServletResponse.SC_BAD_REQUEST, ErrorCode.E0302, NOT_SUPPORTED_MESSAGE);
239    }
240
241    @Override
242    protected JSONObject ignoreJob(HttpServletRequest request, HttpServletResponse response) throws XServletException, IOException {
243        throw new XServletException(HttpServletResponse.SC_BAD_REQUEST, ErrorCode.E0302, NOT_SUPPORTED_MESSAGE);
244    }
245
246    @Override
247    protected String getJobStatus(HttpServletRequest request, HttpServletResponse response) throws XServletException,
248            IOException {
249        throw new XServletException(HttpServletResponse.SC_BAD_REQUEST, ErrorCode.E0302, NOT_SUPPORTED_MESSAGE);
250    }
251
252    @Override
253    void slaEnableAlert(HttpServletRequest request, HttpServletResponse response) throws XServletException, IOException {
254        throw new XServletException(HttpServletResponse.SC_BAD_REQUEST, ErrorCode.E0302, NOT_SUPPORTED_MESSAGE);
255    }
256
257    @Override
258    void slaDisableAlert(HttpServletRequest request, HttpServletResponse response) throws XServletException,
259            IOException {
260        throw new XServletException(HttpServletResponse.SC_BAD_REQUEST, ErrorCode.E0302, NOT_SUPPORTED_MESSAGE);
261    }
262
263    @Override
264    void slaChange(HttpServletRequest request, HttpServletResponse response) throws XServletException, IOException {
265        throw new XServletException(HttpServletResponse.SC_BAD_REQUEST, ErrorCode.E0302, NOT_SUPPORTED_MESSAGE);
266    }
267
268    @Override
269    JSONArray getActionRetries(HttpServletRequest request, HttpServletResponse response) throws XServletException,
270            IOException {
271        throw new XServletException(HttpServletResponse.SC_BAD_REQUEST, ErrorCode.E0302, NOT_SUPPORTED_MESSAGE);
272    }
273
274    @Override
275    protected JSONObject getCoordActionMissingDependencies(HttpServletRequest request, HttpServletResponse response)
276            throws XServletException, IOException {
277        throw new XServletException(HttpServletResponse.SC_BAD_REQUEST, ErrorCode.E0302, NOT_SUPPORTED_MESSAGE);
278
279    }
280
281}