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
056 new JsonRestServlet.ParameterInfo(
057 RestConstants.JOBS_EXTERNAL_ID_PARAM, String.class,
058 false, Arrays.asList("GET"))));
059 }
060
061 public BaseJobsServlet(String instrumentationName) {
062 super(instrumentationName, RESOURCES_INFO);
063 }
064
065 /**
066 * Create a job.
067 */
068 @Override
069 @SuppressWarnings("unchecked")
070 protected void doPost(HttpServletRequest request,
071 HttpServletResponse response) throws ServletException, IOException {
072 String authTok = getAuthToken(request);
073 /*
074 * Enumeration p = request.getAttributeNames();
075 * for(;p.hasMoreElements();){ String key = (String)p.nextElement();
076 * XLog.getLog(getClass()).warn(" key "+ key + " val "+ (String)
077 * request.getAttribute(key)); }
078 */
079 validateContentType(request, RestConstants.XML_CONTENT_TYPE);
080
081 request.setAttribute(AUDIT_OPERATION, request
082 .getParameter(RestConstants.ACTION_PARAM));
083
084 XConfiguration conf = new XConfiguration(request.getInputStream());
085
086 stopCron();
087
088 conf = conf.trim();
089 conf = conf.resolve();
090
091 validateJobConfiguration(conf);
092 BaseJobServlet.checkAuthorizationForApp(getUser(request), conf);
093 JobUtils.normalizeAppPath(conf.get(OozieClient.USER_NAME), conf.get(OozieClient.GROUP_NAME), conf);
094
095 JSONObject json = submitJob(request, conf);
096 startCron();
097 sendJsonResponse(response, HttpServletResponse.SC_CREATED, json);
098 }
099
100 /**
101 * Return information about jobs.
102 */
103 @Override
104 public void doGet(HttpServletRequest request, HttpServletResponse response)
105 throws ServletException, IOException {
106 String externalId = request
107 .getParameter(RestConstants.JOBS_EXTERNAL_ID_PARAM);
108 if (externalId != null) {
109 stopCron();
110 JSONObject json = getJobIdForExternalId(request, externalId);
111 startCron();
112 sendJsonResponse(response, HttpServletResponse.SC_OK, json);
113 }
114 else {
115 stopCron();
116 JSONObject json = getJobs(request);
117 startCron();
118 sendJsonResponse(response, HttpServletResponse.SC_OK, json);
119 }
120 }
121
122 /**
123 * abstract method to submit a job, either workflow or coordinator in the case of workflow job, there is an optional
124 * flag in request to indicate if want this job to be started immediately or not
125 *
126 * @param request
127 * @param conf
128 * @return JSONObject of job id
129 * @throws XServletException
130 * @throws IOException
131 */
132 abstract JSONObject submitJob(HttpServletRequest request, Configuration conf)
133 throws XServletException, IOException;
134
135 /**
136 * abstract method to get a job from external ID
137 *
138 * @param request
139 * @param externalId
140 * @return JSONObject for the requested job
141 * @throws XServletException
142 * @throws IOException
143 */
144 abstract JSONObject getJobIdForExternalId(HttpServletRequest request,
145 String externalId) throws XServletException, IOException;
146
147 /**
148 * abstract method to get a list of workflow jobs
149 *
150 * @param request
151 * @return JSONObject of the requested jobs
152 * @throws XServletException
153 * @throws IOException
154 */
155 abstract JSONObject getJobs(HttpServletRequest request)
156 throws XServletException, IOException;
157
158 static void validateJobConfiguration(Configuration conf) throws XServletException {
159 if (conf.get(OozieClient.USER_NAME) == null) {
160 throw new XServletException(HttpServletResponse.SC_BAD_REQUEST, ErrorCode.E0401,
161 OozieClient.USER_NAME);
162 }
163 }
164 }