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,
060                        action);
061            }
062            boolean startJob = (action != null);
063            String user = conf.get(OozieClient.USER_NAME);
064            DagEngine dagEngine = Services.get().get(DagEngineService.class).getDagEngine(user);
065            String id = dagEngine.submitJob(conf, startJob);
066            json.put(JsonTags.JOB_ID, id);
067        }
068        catch (DagEngineException ex) {
069            throw new XServletException(HttpServletResponse.SC_BAD_REQUEST, ex);
070        }
071
072        return json;
073    }
074
075    /**
076     * v0 service implementation to get a JSONObject representation of a job from its external ID
077     */
078    @Override
079    protected JSONObject getJobIdForExternalId(HttpServletRequest request, String externalId) throws XServletException,
080    IOException {
081        JSONObject json = new JSONObject();
082        try {
083            DagEngine dagEngine = Services.get().get(DagEngineService.class)
084            .getDagEngine(getUser(request));
085            String jobId = dagEngine.getJobIdForExternalId(externalId);
086            json.put(JsonTags.JOB_ID, jobId);
087        }
088        catch (DagEngineException ex) {
089            throw new XServletException(HttpServletResponse.SC_BAD_REQUEST, ex);
090        }
091        return json;
092    }
093
094    /**
095     * v0 service implementation to get a list of workflows, with filtering or interested windows embedded in the
096     * request object
097     */
098    @Override
099    protected JSONObject getJobs(HttpServletRequest request) throws XServletException, IOException {
100        JSONObject json;
101        try {
102            String filter = request.getParameter(RestConstants.JOBS_FILTER_PARAM);
103            String startStr = request.getParameter(RestConstants.OFFSET_PARAM);
104            String lenStr = request.getParameter(RestConstants.LEN_PARAM);
105            int start = (startStr != null) ? Integer.parseInt(startStr) : 1;
106            start = (start < 1) ? 1 : start;
107            int len = (lenStr != null) ? Integer.parseInt(lenStr) : 50;
108            len = (len < 1) ? 50 : len;
109            DagEngine dagEngine = Services.get().get(DagEngineService.class)
110            .getDagEngine(getUser(request));
111            WorkflowsInfo jobs = dagEngine.getJobs(filter, start, len);
112            json = OozieJsonFactory.getWFJSONObject(jobs, "GMT");
113        }
114        catch (DagEngineException ex) {
115            throw new XServletException(HttpServletResponse.SC_BAD_REQUEST, ex);
116        }
117
118        return json;
119    }
120
121
122    /**
123     * service implementation to bulk kill jobs
124     * @param request
125     * @param response
126     * @throws XServletException
127     * @throws IOException
128     */
129    @Override
130    protected JSONObject killJobs(HttpServletRequest request, HttpServletResponse response) throws XServletException,
131            IOException {
132        throw new UnsupportedOperationException("method not implemented in V0 API");
133    }
134
135    /**
136     * service implementation to bulk suspend jobs
137     * @param request
138     * @param response
139     * @throws XServletException
140     * @throws IOException
141     */
142    @Override
143    protected JSONObject suspendJobs(HttpServletRequest request, HttpServletResponse response) throws XServletException,
144            IOException {
145        throw new UnsupportedOperationException("method not implemented in V0 API");
146    }
147
148    /**
149     * service implementation to bulk resume jobs
150     * @param request
151     * @param response
152     * @throws XServletException
153     * @throws IOException
154     */
155    @Override
156    protected JSONObject resumeJobs(HttpServletRequest request, HttpServletResponse response) throws XServletException,
157            IOException {
158        throw new UnsupportedOperationException("method not implemented in V0 API");
159    }
160}