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.Map;
022    
023    import javax.servlet.ServletException;
024    import javax.servlet.http.HttpServletRequest;
025    import javax.servlet.http.HttpServletResponse;
026    
027    import org.apache.oozie.BuildInfo;
028    import org.apache.oozie.client.rest.JsonTags;
029    import org.apache.oozie.client.rest.RestConstants;
030    import org.apache.oozie.service.AuthorizationException;
031    import org.apache.oozie.service.AuthorizationService;
032    import org.apache.oozie.service.InstrumentationService;
033    import org.apache.oozie.service.Services;
034    import org.apache.oozie.util.Instrumentation;
035    import org.json.simple.JSONArray;
036    import org.json.simple.JSONObject;
037    
038    public abstract class BaseAdminServlet extends JsonRestServlet {
039    
040        private static final long serialVersionUID = 1L;
041        protected String modeTag;
042    
043        public BaseAdminServlet(String instrumentationName, ResourceInfo[] RESOURCES_INFO) {
044            super(instrumentationName, RESOURCES_INFO);
045            setAllowSafeModeChanges(true);
046        }
047    
048        /**
049         * Change safemode state.
050         */
051        @Override
052        protected void doPut(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
053            String resourceName = getResourceName(request);
054            request.setAttribute(AUDIT_OPERATION, resourceName);
055            request.setAttribute(AUDIT_PARAM, request.getParameter(modeTag));
056    
057            try {
058                AuthorizationService auth = Services.get().get(AuthorizationService.class);
059                auth.authorizeForAdmin(getUser(request), true);
060            }
061            catch (AuthorizationException ex) {
062                throw new XServletException(HttpServletResponse.SC_UNAUTHORIZED, ex);
063            }
064    
065            setOozieMode(request, response, resourceName);
066            /*if (resourceName.equals(RestConstants.ADMIN_STATUS_RESOURCE)) {
067                boolean safeMode = Boolean.parseBoolean(request.getParameter(RestConstants.ADMIN_SAFE_MODE_PARAM));
068                Services.get().setSafeMode(safeMode);
069                response.setStatus(HttpServletResponse.SC_OK);
070            }
071            else {
072                throw new XServletException(HttpServletResponse.SC_BAD_REQUEST, ErrorCode.E0301, resourceName);
073            }*/
074        }
075    
076        /**
077         * Return safemode state, instrumentation, configuration, osEnv or
078         * javaSysProps
079         */
080        @Override
081        @SuppressWarnings("unchecked")
082        public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
083            String resource = getResourceName(request);
084            Instrumentation instr = Services.get().get(InstrumentationService.class).get();
085    
086            if (resource.equals(RestConstants.ADMIN_STATUS_RESOURCE)) {
087                JSONObject json = new JSONObject();
088                populateOozieMode(json);
089                // json.put(JsonTags.SYSTEM_SAFE_MODE, getOozeMode());
090                sendJsonResponse(response, HttpServletResponse.SC_OK, json);
091            }
092            else if (resource.equals(RestConstants.ADMIN_OS_ENV_RESOURCE)) {
093                JSONObject json = new JSONObject();
094                json.putAll(instr.getOSEnv());
095                sendJsonResponse(response, HttpServletResponse.SC_OK, json);
096            }
097            else if (resource.equals(RestConstants.ADMIN_JAVA_SYS_PROPS_RESOURCE)) {
098                JSONObject json = new JSONObject();
099                json.putAll(instr.getJavaSystemProperties());
100                sendJsonResponse(response, HttpServletResponse.SC_OK, json);
101            }
102            else if (resource.equals(RestConstants.ADMIN_CONFIG_RESOURCE)) {
103                JSONObject json = new JSONObject();
104                json.putAll(instr.getConfiguration());
105                sendJsonResponse(response, HttpServletResponse.SC_OK, json);
106            }
107            else if (resource.equals(RestConstants.ADMIN_INSTRUMENTATION_RESOURCE)) {
108                sendJsonResponse(response, HttpServletResponse.SC_OK, instrToJson(instr));
109            }
110            else if (resource.equals(RestConstants.ADMIN_BUILD_VERSION_RESOURCE)) {
111                JSONObject json = new JSONObject();
112                json.put(JsonTags.BUILD_VERSION, BuildInfo.getBuildInfo().getProperty(BuildInfo.BUILD_VERSION));
113                sendJsonResponse(response, HttpServletResponse.SC_OK, json);
114            }
115            else if (resource.equals(RestConstants.ADMIN_QUEUE_DUMP_RESOURCE)) {
116                JSONObject json = new JSONObject();
117                getQueueDump(json);
118                sendJsonResponse(response, HttpServletResponse.SC_OK, json);
119            }
120        }
121    
122        @Override
123        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
124                IOException {
125        }
126    
127        @SuppressWarnings("unchecked")
128        private <T> JSONArray instrElementsToJson(Map<String, Map<String, Instrumentation.Element<T>>> instrElements) {
129            JSONArray array = new JSONArray();
130            for (Map.Entry<String, Map<String, Instrumentation.Element<T>>> group : instrElements.entrySet()) {
131                JSONObject json = new JSONObject();
132                String groupName = group.getKey();
133                json.put(JsonTags.INSTR_GROUP, groupName);
134                JSONArray dataArray = new JSONArray();
135                for (Map.Entry<String, Instrumentation.Element<T>> elementEntry : group.getValue().entrySet()) {
136                    String samplerName = elementEntry.getKey();
137                    JSONObject dataJson = new JSONObject();
138                    dataJson.put(JsonTags.INSTR_NAME, samplerName);
139                    Object value = elementEntry.getValue().getValue();
140                    if (value instanceof Instrumentation.Timer) {
141                        Instrumentation.Timer timer = (Instrumentation.Timer) value;
142                        dataJson.put(JsonTags.INSTR_TIMER_TICKS, timer.getTicks());
143                        dataJson.put(JsonTags.INSTR_TIMER_OWN_TIME_AVG, timer.getOwnAvg());
144                        dataJson.put(JsonTags.INSTR_TIMER_TOTAL_TIME_AVG, timer.getTotalAvg());
145                        dataJson.put(JsonTags.INSTR_TIMER_OWN_STD_DEV, timer.getOwnStdDev());
146                        dataJson.put(JsonTags.INSTR_TIMER_TOTAL_STD_DEV, timer.getTotalStdDev());
147                        dataJson.put(JsonTags.INSTR_TIMER_OWN_MIN_TIME, timer.getOwnMin());
148                        dataJson.put(JsonTags.INSTR_TIMER_OWN_MAX_TIME, timer.getOwnMax());
149                        dataJson.put(JsonTags.INSTR_TIMER_TOTAL_MIN_TIME, timer.getTotalMin());
150                        dataJson.put(JsonTags.INSTR_TIMER_TOTAL_MAX_TIME, timer.getTotalMax());
151                    }
152                    else {
153                        dataJson.put(JsonTags.INSTR_VARIABLE_VALUE, value);
154                    }
155                    dataArray.add(dataJson);
156                }
157                json.put(JsonTags.INSTR_DATA, dataArray);
158                array.add(json);
159            }
160            return array;
161        }
162    
163        @SuppressWarnings("unchecked")
164        private JSONObject instrToJson(Instrumentation instr) {
165            JSONObject json = new JSONObject();
166            json.put(JsonTags.INSTR_VARIABLES, instrElementsToJson(instr.getVariables()));
167            json.put(JsonTags.INSTR_SAMPLERS, instrElementsToJson(instr.getSamplers()));
168            json.put(JsonTags.INSTR_COUNTERS, instrElementsToJson(instr.getCounters()));
169            json.put(JsonTags.INSTR_TIMERS, instrElementsToJson(instr.getTimers()));
170            return json;
171        }
172    
173        protected abstract void populateOozieMode(JSONObject json);
174    
175        protected abstract void setOozieMode(HttpServletRequest request, HttpServletResponse response, String resourceName)
176                throws XServletException;
177    
178        protected abstract void getQueueDump(JSONObject json) throws XServletException;
179    
180    }