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 String requestUser = getUser(request);
094 if (!requestUser.equals(UNDEF)) {
095 conf.set(OozieClient.USER_NAME, requestUser);
096 }
097 BaseJobServlet.checkAuthorizationForApp(conf);
098 JobUtils.normalizeAppPath(conf.get(OozieClient.USER_NAME), conf.get(OozieClient.GROUP_NAME), conf);
099
100 JSONObject json = submitJob(request, conf);
101 startCron();
102 sendJsonResponse(response, HttpServletResponse.SC_CREATED, json);
103 }
104
105 /**
106 * Return information about jobs.
107 */
108 @Override
109 public void doGet(HttpServletRequest request, HttpServletResponse response)
110 throws ServletException, IOException {
111 String externalId = request
112 .getParameter(RestConstants.JOBS_EXTERNAL_ID_PARAM);
113 if (externalId != null) {
114 stopCron();
115 JSONObject json = getJobIdForExternalId(request, externalId);
116 startCron();
117 sendJsonResponse(response, HttpServletResponse.SC_OK, json);
118 }
119 else {
120 stopCron();
121 JSONObject json = getJobs(request);
122 startCron();
123 sendJsonResponse(response, HttpServletResponse.SC_OK, json);
124 }
125 }
126
127 /**
128 * abstract method to submit a job, either workflow or coordinator in the case of workflow job, there is an optional
129 * flag in request to indicate if want this job to be started immediately or not
130 *
131 * @param request
132 * @param conf
133 * @return JSONObject of job id
134 * @throws XServletException
135 * @throws IOException
136 */
137 abstract JSONObject submitJob(HttpServletRequest request, Configuration conf)
138 throws XServletException, IOException;
139
140 /**
141 * abstract method to get a job from external ID
142 *
143 * @param request
144 * @param externalId
145 * @return JSONObject for the requested job
146 * @throws XServletException
147 * @throws IOException
148 */
149 abstract JSONObject getJobIdForExternalId(HttpServletRequest request,
150 String externalId) throws XServletException, IOException;
151
152 /**
153 * abstract method to get a list of workflow jobs
154 *
155 * @param request
156 * @return JSONObject of the requested jobs
157 * @throws XServletException
158 * @throws IOException
159 */
160 abstract JSONObject getJobs(HttpServletRequest request)
161 throws XServletException, IOException;
162
163 static void validateJobConfiguration(Configuration conf) throws XServletException {
164 if (conf.get(OozieClient.USER_NAME) == null) {
165 throw new XServletException(HttpServletResponse.SC_BAD_REQUEST, ErrorCode.E0401,
166 OozieClient.USER_NAME);
167 }
168 }
169 }