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.io.Reader;
023import java.util.List;
024import java.util.Map;
025import java.util.TimeZone;
026import java.util.regex.Pattern;
027
028import javax.servlet.ServletException;
029import javax.servlet.http.HttpServletRequest;
030import javax.servlet.http.HttpServletResponse;
031
032import org.apache.hadoop.fs.Path;
033import org.apache.oozie.BuildInfo;
034import org.apache.oozie.client.rest.JsonBean;
035import org.apache.oozie.client.rest.JsonTags;
036import org.apache.oozie.client.rest.RestConstants;
037import org.apache.oozie.service.AuthorizationException;
038import org.apache.oozie.service.AuthorizationService;
039import org.apache.oozie.service.InstrumentationService;
040import org.apache.oozie.service.JobsConcurrencyService;
041import org.apache.oozie.service.Services;
042import org.apache.oozie.service.ShareLibService;
043import org.apache.oozie.util.AuthUrlClient;
044import org.apache.oozie.util.ConfigUtils;
045import org.apache.oozie.util.Instrumentation;
046import org.json.simple.JSONArray;
047import org.json.simple.JSONObject;
048import org.json.simple.JSONValue;
049
050public abstract class BaseAdminServlet extends JsonRestServlet {
051
052    private static final long serialVersionUID = 1L;
053    protected String modeTag;
054
055
056    public BaseAdminServlet(String instrumentationName, ResourceInfo[] RESOURCES_INFO) {
057        super(instrumentationName, RESOURCES_INFO);
058        setAllowSafeModeChanges(true);
059    }
060
061    /**
062     * Change safemode state.
063     */
064    @Override
065    protected void doPut(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
066        String resourceName = getResourceName(request);
067        request.setAttribute(AUDIT_OPERATION, resourceName);
068        request.setAttribute(AUDIT_PARAM, request.getParameter(modeTag));
069
070        authorizeRequest(request);
071
072        setOozieMode(request, response, resourceName);
073        /*if (resourceName.equals(RestConstants.ADMIN_STATUS_RESOURCE)) {
074            boolean safeMode = Boolean.parseBoolean(request.getParameter(RestConstants.ADMIN_SAFE_MODE_PARAM));
075            Services.get().setSafeMode(safeMode);
076            response.setStatus(HttpServletResponse.SC_OK);
077        }
078        else {
079            throw new XServletException(HttpServletResponse.SC_BAD_REQUEST, ErrorCode.E0301, resourceName);
080        }*/
081    }
082
083
084    /**
085     * Get JMS connection Info
086     * @param request
087     * @param response
088     * @throws XServletException
089     * @throws IOException
090     */
091    abstract JsonBean getJMSConnectionInfo(HttpServletRequest request, HttpServletResponse response)
092            throws XServletException, IOException;
093
094
095    /**
096     * Return safemode state, instrumentation, configuration, osEnv or
097     * javaSysProps
098     */
099    @Override
100    @SuppressWarnings("unchecked")
101    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
102        String resource = getResourceName(request);
103        Instrumentation instr = Services.get().get(InstrumentationService.class).get();
104
105        if (resource.equals(RestConstants.ADMIN_STATUS_RESOURCE)) {
106            JSONObject json = new JSONObject();
107            populateOozieMode(json);
108            // json.put(JsonTags.SYSTEM_SAFE_MODE, getOozeMode());
109            sendJsonResponse(response, HttpServletResponse.SC_OK, json);
110        }
111        else if (resource.equals(RestConstants.ADMIN_OS_ENV_RESOURCE)) {
112            JSONObject json = new JSONObject();
113            json.putAll(instr.getOSEnv());
114            sendJsonResponse(response, HttpServletResponse.SC_OK, json);
115        }
116        else if (resource.equals(RestConstants.ADMIN_JAVA_SYS_PROPS_RESOURCE)) {
117            JSONObject json = new JSONObject();
118            json.putAll(instr.getJavaSystemProperties());
119            sendJsonResponse(response, HttpServletResponse.SC_OK, json);
120        }
121        else if (resource.equals(RestConstants.ADMIN_CONFIG_RESOURCE)) {
122            JSONObject json = new JSONObject();
123            json.putAll(instr.getConfiguration());
124            sendJsonResponse(response, HttpServletResponse.SC_OK, json);
125        }
126        else if (resource.equals(RestConstants.ADMIN_INSTRUMENTATION_RESOURCE)) {
127            sendInstrumentationResponse(response, instr);
128        }
129        else if (resource.equals(RestConstants.ADMIN_BUILD_VERSION_RESOURCE)) {
130            JSONObject json = new JSONObject();
131            json.put(JsonTags.BUILD_VERSION, BuildInfo.getBuildInfo().getProperty(BuildInfo.BUILD_VERSION));
132            sendJsonResponse(response, HttpServletResponse.SC_OK, json);
133        }
134        else if (resource.equals(RestConstants.ADMIN_QUEUE_DUMP_RESOURCE)) {
135            JSONObject json = new JSONObject();
136            getQueueDump(json);
137            sendJsonResponse(response, HttpServletResponse.SC_OK, json);
138        }
139        else if (resource.equals(RestConstants.ADMIN_TIME_ZONES_RESOURCE)) {
140            JSONObject json = new JSONObject();
141            json.put(JsonTags.AVAILABLE_TIME_ZONES, availableTimeZonesToJsonArray());
142            sendJsonResponse(response, HttpServletResponse.SC_OK, json);
143        }
144        else if (resource.equals(RestConstants.ADMIN_JMS_INFO)) {
145            String timeZoneId = request.getParameter(RestConstants.TIME_ZONE_PARAM) == null ? "GMT" : request
146                    .getParameter(RestConstants.TIME_ZONE_PARAM);
147            JsonBean jmsBean = getJMSConnectionInfo(request, response);
148            sendJsonResponse(response, HttpServletResponse.SC_OK, jmsBean, timeZoneId);
149        }
150        else if (resource.equals(RestConstants.ADMIN_AVAILABLE_OOZIE_SERVERS_RESOURCE)) {
151            JSONObject json = new JSONObject();
152            json.putAll(getOozieURLs());
153            sendJsonResponse(response, HttpServletResponse.SC_OK, json);
154        }
155        else if (resource.equals(RestConstants.ADMIN_UPDATE_SHARELIB)) {
156            authorizeRequest(request);
157            updateShareLib(request, response);
158        }
159        else if (resource.equals(RestConstants.ADMIN_LIST_SHARELIB)) {
160            String sharelibKey = request.getParameter(RestConstants.SHARE_LIB_REQUEST_KEY);
161            sendJsonResponse(response, HttpServletResponse.SC_OK, getShareLib(sharelibKey));
162        }
163        else if (resource.equals(RestConstants.ADMIN_METRICS_RESOURCE)) {
164            sendMetricsResponse(response);
165        }
166    }
167
168    /**
169     * Gets the list of share lib.
170     *
171     * @param sharelibKey the sharelib key
172     * @return the list of supported share lib
173     * @throws IOException
174     */
175    @SuppressWarnings("unchecked")
176    private JSONObject getShareLib(String sharelibKey) throws IOException {
177        JSONObject json = new JSONObject();
178
179        ShareLibService shareLibService = Services.get().get(ShareLibService.class);
180
181        // for testcases.
182        if (shareLibService == null) {
183            return json;
184        }
185        JSONArray shareLibList = new JSONArray();
186
187        Map<String, List<Path>> shareLibLauncherMap = shareLibService.getShareLib();
188        if (sharelibKey != null && !sharelibKey.isEmpty()) {
189            Pattern pattern = Pattern.compile(sharelibKey);
190            for (String key : shareLibLauncherMap.keySet()) {
191                if (pattern.matcher(key).matches() == true) {
192                    JSONObject object = new JSONObject();
193                    JSONArray fileList = new JSONArray();
194                    List<Path> pathList = shareLibLauncherMap.get(key);
195
196                    for (Path file : pathList) {
197                        fileList.add(file.toString());
198                    }
199                    object.put(JsonTags.SHARELIB_LIB_NAME, key);
200                    object.put(JsonTags.SHARELIB_LIB_FILES, fileList);
201                    shareLibList.add(object);
202
203                }
204            }
205        }
206        else {
207            for (String key : shareLibLauncherMap.keySet()) {
208                JSONObject object = new JSONObject();
209                object.put(JsonTags.SHARELIB_LIB_NAME, key);
210                shareLibList.add(object);
211            }
212
213        }
214        json.put(JsonTags.SHARELIB_LIB, shareLibList);
215
216        return json;
217    }
218
219    /**
220     * Update share lib. support HA
221     *
222     * @param request the request
223     * @param response the response
224     * @throws IOException Signals that an I/O exception has occurred.
225     */
226    @SuppressWarnings("unchecked")
227    public void updateShareLib(HttpServletRequest request, HttpServletResponse response) throws IOException {
228        JSONArray jsonArray = new JSONArray();
229        JobsConcurrencyService jc = Services.get().get(JobsConcurrencyService.class);
230        if (jc.isAllServerRequest(request.getParameterMap())) {
231            Map<String, String> servers = jc.getOtherServerUrls();
232            for (String otherUrl : servers.values()) {
233                // It's important that we specify ALL_SERVERS_PARAM=false, so that other oozie server should not call other oozie
234                //servers to update sharelib (and creating an infinite recursion)
235                String serverUrl = otherUrl + "/v2/admin/" + RestConstants.ADMIN_UPDATE_SHARELIB + "?"
236                        + RestConstants.ALL_SERVER_REQUEST + "=false";
237                try {
238                    Reader reader = AuthUrlClient.callServer(serverUrl);
239                    JSONObject json = (JSONObject) JSONValue.parse(reader);
240                    jsonArray.add(json);
241                }
242                catch (Exception e) {
243                    JSONObject errorJson = new JSONObject();
244                    errorJson.put(JsonTags.SHARELIB_UPDATE_HOST, otherUrl);
245                    errorJson.put(JsonTags.SHARELIB_UPDATE_STATUS, e.getMessage());
246                    JSONObject newJson = new JSONObject();
247                    newJson.put(JsonTags.SHARELIB_LIB_UPDATE, errorJson);
248                    jsonArray.add(newJson);
249                }
250            }
251            //For current server
252            JSONObject newJson = new JSONObject();
253            newJson.put(JsonTags.SHARELIB_LIB_UPDATE, updateLocalShareLib(request));
254            jsonArray.add(newJson);
255            sendJsonResponse(response, HttpServletResponse.SC_OK, jsonArray);
256        }
257        else {
258            JSONObject newJson = new JSONObject();
259            newJson.put(JsonTags.SHARELIB_LIB_UPDATE, updateLocalShareLib(request));
260            sendJsonResponse(response, HttpServletResponse.SC_OK, newJson);
261        }
262    }
263
264    @SuppressWarnings("unchecked")
265    private JSONObject updateLocalShareLib(HttpServletRequest request) {
266        ShareLibService shareLibService = Services.get().get(ShareLibService.class);
267        JSONObject json = new JSONObject();
268        json.put(JsonTags.SHARELIB_UPDATE_HOST, ConfigUtils.getOozieEffectiveUrl());
269        try {
270            json.putAll(shareLibService.updateShareLib());
271            json.put(JsonTags.SHARELIB_UPDATE_STATUS, "Successful");
272        }
273        catch (Exception e) {
274            json.put(JsonTags.SHARELIB_UPDATE_STATUS, e.getClass().getName() + ": " + e.getMessage());
275        }
276        return json;
277    }
278
279    /**
280     * Authorize request.
281     *
282     * @param request the HttpServletRequest
283     * @throws XServletException the x servlet exception
284     */
285    private void authorizeRequest(HttpServletRequest request) throws XServletException {
286        try {
287            AuthorizationService auth = Services.get().get(AuthorizationService.class);
288            auth.authorizeForAdmin(getUser(request), true);
289        }
290        catch (AuthorizationException ex) {
291            throw new XServletException(HttpServletResponse.SC_UNAUTHORIZED, ex);
292        }
293    }
294
295    @Override
296    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
297            IOException {
298    }
299
300    @SuppressWarnings("unchecked")
301    private <T> JSONArray instrElementsToJson(Map<String, Map<String, Instrumentation.Element<T>>> instrElements) {
302        JSONArray array = new JSONArray();
303        for (Map.Entry<String, Map<String, Instrumentation.Element<T>>> group : instrElements.entrySet()) {
304            JSONObject json = new JSONObject();
305            String groupName = group.getKey();
306            json.put(JsonTags.INSTR_GROUP, groupName);
307            JSONArray dataArray = new JSONArray();
308            for (Map.Entry<String, Instrumentation.Element<T>> elementEntry : group.getValue().entrySet()) {
309                String samplerName = elementEntry.getKey();
310                JSONObject dataJson = new JSONObject();
311                dataJson.put(JsonTags.INSTR_NAME, samplerName);
312                Object value = elementEntry.getValue().getValue();
313                if (value instanceof Instrumentation.Timer) {
314                    Instrumentation.Timer timer = (Instrumentation.Timer) value;
315                    dataJson.put(JsonTags.INSTR_TIMER_TICKS, timer.getTicks());
316                    dataJson.put(JsonTags.INSTR_TIMER_OWN_TIME_AVG, timer.getOwnAvg());
317                    dataJson.put(JsonTags.INSTR_TIMER_TOTAL_TIME_AVG, timer.getTotalAvg());
318                    dataJson.put(JsonTags.INSTR_TIMER_OWN_STD_DEV, timer.getOwnStdDev());
319                    dataJson.put(JsonTags.INSTR_TIMER_TOTAL_STD_DEV, timer.getTotalStdDev());
320                    dataJson.put(JsonTags.INSTR_TIMER_OWN_MIN_TIME, timer.getOwnMin());
321                    dataJson.put(JsonTags.INSTR_TIMER_OWN_MAX_TIME, timer.getOwnMax());
322                    dataJson.put(JsonTags.INSTR_TIMER_TOTAL_MIN_TIME, timer.getTotalMin());
323                    dataJson.put(JsonTags.INSTR_TIMER_TOTAL_MAX_TIME, timer.getTotalMax());
324                }
325                else {
326                    dataJson.put(JsonTags.INSTR_VARIABLE_VALUE, value);
327                }
328                dataArray.add(dataJson);
329            }
330            json.put(JsonTags.INSTR_DATA, dataArray);
331            array.add(json);
332        }
333        return array;
334    }
335
336    @SuppressWarnings("unchecked")
337    private JSONObject instrToJson(Instrumentation instr) {
338        JSONObject json = new JSONObject();
339        json.put(JsonTags.INSTR_VARIABLES, instrElementsToJson(instr.getVariables()));
340        json.put(JsonTags.INSTR_SAMPLERS, instrElementsToJson(instr.getSamplers()));
341        json.put(JsonTags.INSTR_COUNTERS, instrElementsToJson(instr.getCounters()));
342        json.put(JsonTags.INSTR_TIMERS, instrElementsToJson(instr.getTimers()));
343        return json;
344    }
345
346    protected abstract void populateOozieMode(JSONObject json);
347
348    protected abstract void setOozieMode(HttpServletRequest request, HttpServletResponse response, String resourceName)
349            throws XServletException;
350
351    protected abstract void getQueueDump(JSONObject json) throws XServletException;
352
353    private static final JSONArray GMTOffsetTimeZones = new JSONArray();
354    static {
355        prepareGMTOffsetTimeZones();
356    }
357
358    @SuppressWarnings({"unchecked", "rawtypes"})
359    private static void prepareGMTOffsetTimeZones() {
360        for (String tzId : new String[]{"GMT-12:00", "GMT-11:00", "GMT-10:00", "GMT-09:00", "GMT-08:00", "GMT-07:00", "GMT-06:00",
361                                        "GMT-05:00", "GMT-04:00", "GMT-03:00", "GMT-02:00", "GMT-01:00", "GMT+01:00", "GMT+02:00",
362                                        "GMT+03:00", "GMT+04:00", "GMT+05:00", "GMT+06:00", "GMT+07:00", "GMT+08:00", "GMT+09:00",
363                                        "GMT+10:00", "GMT+11:00", "GMT+12:00"}) {
364            TimeZone tz = TimeZone.getTimeZone(tzId);
365            JSONObject json = new JSONObject();
366            json.put(JsonTags.TIME_ZOME_DISPLAY_NAME, tz.getDisplayName(false, TimeZone.SHORT) + " (" + tzId + ")");
367            json.put(JsonTags.TIME_ZONE_ID, tzId);
368            GMTOffsetTimeZones.add(json);
369        }
370    }
371
372    @SuppressWarnings({"unchecked", "rawtypes"})
373    private JSONArray availableTimeZonesToJsonArray() {
374        JSONArray array = new JSONArray();
375        for (String tzId : TimeZone.getAvailableIDs()) {
376            // skip id's that are like "Etc/GMT+01:00" because their display names are like "GMT-01:00", which is confusing
377            if (!tzId.startsWith("Etc/GMT")) {
378                JSONObject json = new JSONObject();
379                TimeZone tZone = TimeZone.getTimeZone(tzId);
380                json.put(JsonTags.TIME_ZOME_DISPLAY_NAME, tZone.getDisplayName(false, TimeZone.SHORT) + " (" + tzId + ")");
381                json.put(JsonTags.TIME_ZONE_ID, tzId);
382                array.add(json);
383            }
384        }
385
386        // The combo box this populates cannot be edited, so the user can't type in GMT offsets (like in the CLI), so we'll add
387        // in some hourly offsets here (though the user will not be able to use other offsets without editing the cookie manually
388        // and they are not in order)
389        array.addAll(GMTOffsetTimeZones);
390
391        return array;
392    }
393
394    protected void sendInstrumentationResponse(HttpServletResponse response, Instrumentation instr)
395            throws IOException, XServletException {
396        sendJsonResponse(response, HttpServletResponse.SC_OK, instrToJson(instr));
397    }
398
399    protected abstract Map<String, String> getOozieURLs() throws XServletException;
400
401    protected abstract void sendMetricsResponse(HttpServletResponse response) throws IOException, XServletException;
402}