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 */
018package org.apache.oozie.servlet;
019
020import java.io.IOException;
021import java.util.List;
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.WorkflowJobBean;
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 = new JSONObject();
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            List<WorkflowJobBean> jsonWorkflows = jobs.getWorkflows();
111            json.put(JsonTags.WORKFLOWS_JOBS, WorkflowJobBean.toJSONArray(jsonWorkflows, "GMT"));
112            json.put(JsonTags.WORKFLOWS_TOTAL, jobs.getTotal());
113            json.put(JsonTags.WORKFLOWS_OFFSET, jobs.getStart());
114            json.put(JsonTags.WORKFLOWS_LEN, jobs.getLen());
115
116        }
117        catch (DagEngineException ex) {
118            throw new XServletException(HttpServletResponse.SC_BAD_REQUEST, ex);
119        }
120
121        return json;
122    }
123}