This project has retired. For details please refer to its
Attic page.
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 package org.apache.oozie.servlet;
019
020 import java.io.IOException;
021 import java.util.Arrays;
022
023 import javax.servlet.ServletException;
024 import javax.servlet.http.HttpServletRequest;
025 import javax.servlet.http.HttpServletResponse;
026
027 import org.apache.hadoop.conf.Configuration;
028 import org.apache.oozie.ErrorCode;
029 import org.apache.oozie.client.OozieClient;
030 import org.apache.oozie.client.rest.RestConstants;
031 import org.apache.oozie.service.Services;
032 import org.apache.oozie.service.WorkflowAppService;
033 import org.apache.oozie.util.JobUtils;
034 import org.apache.oozie.util.XConfiguration;
035 import org.json.simple.JSONObject;
036
037 public abstract class BaseJobsServlet extends JsonRestServlet {
038
039 private static final JsonRestServlet.ResourceInfo RESOURCES_INFO[] = new JsonRestServlet.ResourceInfo[1];
040
041 static {
042 RESOURCES_INFO[0] = new JsonRestServlet.ResourceInfo("", Arrays.asList(
043 "POST", "GET"), Arrays.asList(
044 new JsonRestServlet.ParameterInfo(RestConstants.ACTION_PARAM,
045 String.class, false, Arrays.asList("POST")),
046 new JsonRestServlet.ParameterInfo(
047 RestConstants.JOBS_FILTER_PARAM, String.class, false,
048 Arrays.asList("GET")),
049 new JsonRestServlet.ParameterInfo(RestConstants.JOBTYPE_PARAM,
050 String.class, false, Arrays.asList("GET", "POST")),
051 new JsonRestServlet.ParameterInfo(RestConstants.OFFSET_PARAM,
052 String.class, false, Arrays.asList("GET")),
053 new JsonRestServlet.ParameterInfo(RestConstants.LEN_PARAM,
054 String.class, false, Arrays.asList("GET")),
055 new JsonRestServlet.ParameterInfo(RestConstants.JOBS_BULK_PARAM,
056 String.class, false, Arrays.asList("GET")),
057 new JsonRestServlet.ParameterInfo(
058 RestConstants.JOBS_EXTERNAL_ID_PARAM, String.class,
059 false, Arrays.asList("GET"))));
060 }
061
062 public BaseJobsServlet(String instrumentationName) {
063 super(instrumentationName, RESOURCES_INFO);
064 }
065
066 /**
067 * Create a job.
068 */
069 @Override
070 @SuppressWarnings("unchecked")
071 protected void doPost(HttpServletRequest request,
072 HttpServletResponse response) throws ServletException, IOException {
073 String authTok = getAuthToken(request);
074 /*
075 * Enumeration p = request.getAttributeNames();
076 * for(;p.hasMoreElements();){ String key = (String)p.nextElement();
077 * XLog.getLog(getClass()).warn(" key "+ key + " val "+ (String)
078 * request.getAttribute(key)); }
079 */
080 validateContentType(request, RestConstants.XML_CONTENT_TYPE);
081
082 request.setAttribute(AUDIT_OPERATION, request
083 .getParameter(RestConstants.ACTION_PARAM));
084
085 XConfiguration conf = new XConfiguration(request.getInputStream());
086
087 stopCron();
088
089 conf = conf.trim();
090 conf = conf.resolve();
091
092 validateJobConfiguration(conf);
093 BaseJobServlet.checkAuthorizationForApp(getUser(request), conf);
094 JobUtils.normalizeAppPath(conf.get(OozieClient.USER_NAME), conf.get(OozieClient.GROUP_NAME), conf);
095
096 JSONObject json = submitJob(request, conf);
097 startCron();
098 sendJsonResponse(response, HttpServletResponse.SC_CREATED, json);
099 }
100
101 /**
102 * Return information about jobs.
103 */
104 @Override
105 public void doGet(HttpServletRequest request, HttpServletResponse response)
106 throws ServletException, IOException {
107 String externalId = request
108 .getParameter(RestConstants.JOBS_EXTERNAL_ID_PARAM);
109 if (externalId != null) {
110 stopCron();
111 JSONObject json = getJobIdForExternalId(request, externalId);
112 startCron();
113 sendJsonResponse(response, HttpServletResponse.SC_OK, json);
114 }
115 else {
116 stopCron();
117 JSONObject json = getJobs(request);
118 startCron();
119 sendJsonResponse(response, HttpServletResponse.SC_OK, json);
120 }
121 }
122
123 /**
124 * abstract method to submit a job, either workflow or coordinator in the case of workflow job, there is an optional
125 * flag in request to indicate if want this job to be started immediately or not
126 *
127 * @param request
128 * @param conf
129 * @return JSONObject of job id
130 * @throws XServletException
131 * @throws IOException
132 */
133 abstract JSONObject submitJob(HttpServletRequest request, Configuration conf)
134 throws XServletException, IOException;
135
136 /**
137 * abstract method to get a job from external ID
138 *
139 * @param request
140 * @param externalId
141 * @return JSONObject for the requested job
142 * @throws XServletException
143 * @throws IOException
144 */
145 abstract JSONObject getJobIdForExternalId(HttpServletRequest request,
146 String externalId) throws XServletException, IOException;
147
148 /**
149 * abstract method to get a list of workflow jobs
150 *
151 * @param request
152 * @return JSONObject of the requested jobs
153 * @throws XServletException
154 * @throws IOException
155 */
156 abstract JSONObject getJobs(HttpServletRequest request)
157 throws XServletException, IOException;
158
159 static void validateJobConfiguration(Configuration conf) throws XServletException {
160 if (conf.get(OozieClient.USER_NAME) == null) {
161 throw new XServletException(HttpServletResponse.SC_BAD_REQUEST, ErrorCode.E0401,
162 OozieClient.USER_NAME);
163 }
164 }
165 }