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;
022import java.util.List;
023
024import javax.servlet.http.HttpServletRequest;
025import javax.servlet.http.HttpServletResponse;
026
027import org.apache.hadoop.conf.Configuration;
028import org.apache.oozie.DagEngine;
029import org.apache.oozie.DagEngineException;
030import org.apache.oozie.ErrorCode;
031import org.apache.oozie.WorkflowJobBean;
032import org.apache.oozie.WorkflowsInfo;
033import org.apache.oozie.client.OozieClient;
034import org.apache.oozie.client.rest.JsonTags;
035import org.apache.oozie.client.rest.RestConstants;
036import org.apache.oozie.service.DagEngineService;
037import org.apache.oozie.service.Services;
038import org.json.simple.JSONObject;
039
040public class V0JobsServlet extends BaseJobsServlet {
041
042    private static final String INSTRUMENTATION_NAME = "v0jobs";
043
044    public V0JobsServlet() {
045        super(INSTRUMENTATION_NAME);
046    }
047
048
049    /**
050     * v0 service implementation to submit a workflow job
051     */
052    @Override
053    protected JSONObject submitJob(HttpServletRequest request, Configuration conf) throws XServletException, IOException {
054
055        JSONObject json = new JSONObject();
056
057        try {
058            String action = request.getParameter(RestConstants.ACTION_PARAM);
059            if (action != null && !action.equals(RestConstants.JOB_ACTION_START)) {
060                throw new XServletException(HttpServletResponse.SC_BAD_REQUEST, ErrorCode.E0303, RestConstants.ACTION_PARAM, 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, IOException {
080        JSONObject json = new JSONObject();
081        try {
082            DagEngine dagEngine = Services.get().get(DagEngineService.class)
083            .getDagEngine(getUser(request));
084            String jobId = dagEngine.getJobIdForExternalId(externalId);
085            json.put(JsonTags.JOB_ID, jobId);
086        }
087        catch (DagEngineException ex) {
088            throw new XServletException(HttpServletResponse.SC_BAD_REQUEST, ex);
089        }
090        return json;
091    }
092
093    /**
094     * v0 service implementation to get a list of workflows, with filtering or interested windows embedded in the
095     * request object
096     */
097    @Override
098    protected JSONObject getJobs(HttpServletRequest request) throws XServletException, IOException {
099        JSONObject json = new JSONObject();
100        try {
101            String filter = request.getParameter(RestConstants.JOBS_FILTER_PARAM);
102            String startStr = request.getParameter(RestConstants.OFFSET_PARAM);
103            String lenStr = request.getParameter(RestConstants.LEN_PARAM);
104            int start = (startStr != null) ? Integer.parseInt(startStr) : 1;
105            start = (start < 1) ? 1 : start;
106            int len = (lenStr != null) ? Integer.parseInt(lenStr) : 50;
107            len = (len < 1) ? 50 : len;
108            DagEngine dagEngine = Services.get().get(DagEngineService.class)
109            .getDagEngine(getUser(request));
110            WorkflowsInfo jobs = dagEngine.getJobs(filter, start, len);
111            List<WorkflowJobBean> jsonWorkflows = jobs.getWorkflows();
112            json.put(JsonTags.WORKFLOWS_JOBS, WorkflowJobBean.toJSONArray(jsonWorkflows, "GMT"));
113            json.put(JsonTags.WORKFLOWS_TOTAL, jobs.getTotal());
114            json.put(JsonTags.WORKFLOWS_OFFSET, jobs.getStart());
115            json.put(JsonTags.WORKFLOWS_LEN, jobs.getLen());
116
117        }
118        catch (DagEngineException ex) {
119            throw new XServletException(HttpServletResponse.SC_BAD_REQUEST, ex);
120        }
121
122        return json;
123    }
124
125
126    /**
127     * service implementation to bulk kill jobs
128     * @param request
129     * @param response
130     * @return
131     * @throws XServletException
132     * @throws IOException
133     */
134    @Override
135    protected JSONObject killJobs(HttpServletRequest request, HttpServletResponse response) throws XServletException,
136            IOException {
137        throw new UnsupportedOperationException("method not implemented in V0 API");
138    }
139
140    /**
141     * service implementation to bulk suspend jobs
142     * @param request
143     * @param response
144     * @return
145     * @throws XServletException
146     * @throws IOException
147     */
148    @Override
149    protected JSONObject suspendJobs(HttpServletRequest request, HttpServletResponse response) throws XServletException,
150            IOException {
151        throw new UnsupportedOperationException("method not implemented in V0 API");
152    }
153
154    /**
155     * service implementation to bulk resume jobs
156     * @param request
157     * @param response
158     * @return
159     * @throws XServletException
160     * @throws IOException
161     */
162    @Override
163    protected JSONObject resumeJobs(HttpServletRequest request, HttpServletResponse response) throws XServletException,
164            IOException {
165        throw new UnsupportedOperationException("method not implemented in V0 API");
166    }
167}