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    import java.util.TimeZone;
023    
024    import javax.servlet.ServletException;
025    import javax.servlet.http.HttpServletRequest;
026    import javax.servlet.http.HttpServletResponse;
027    
028    import org.apache.oozie.BuildInfo;
029    import org.apache.oozie.client.rest.JsonBean;
030    import org.apache.oozie.client.rest.JsonTags;
031    import org.apache.oozie.client.rest.RestConstants;
032    import org.apache.oozie.service.AuthorizationException;
033    import org.apache.oozie.service.AuthorizationService;
034    import org.apache.oozie.service.InstrumentationService;
035    import org.apache.oozie.service.Services;
036    import org.apache.oozie.util.Instrumentation;
037    import org.json.simple.JSONArray;
038    import org.json.simple.JSONObject;
039    
040    public abstract class BaseAdminServlet extends JsonRestServlet {
041    
042        private static final long serialVersionUID = 1L;
043        protected String modeTag;
044    
045        public BaseAdminServlet(String instrumentationName, ResourceInfo[] RESOURCES_INFO) {
046            super(instrumentationName, RESOURCES_INFO);
047            setAllowSafeModeChanges(true);
048        }
049    
050        /**
051         * Change safemode state.
052         */
053        @Override
054        protected void doPut(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
055            String resourceName = getResourceName(request);
056            request.setAttribute(AUDIT_OPERATION, resourceName);
057            request.setAttribute(AUDIT_PARAM, request.getParameter(modeTag));
058    
059            try {
060                AuthorizationService auth = Services.get().get(AuthorizationService.class);
061                auth.authorizeForAdmin(getUser(request), true);
062            }
063            catch (AuthorizationException ex) {
064                throw new XServletException(HttpServletResponse.SC_UNAUTHORIZED, ex);
065            }
066    
067            setOozieMode(request, response, resourceName);
068            /*if (resourceName.equals(RestConstants.ADMIN_STATUS_RESOURCE)) {
069                boolean safeMode = Boolean.parseBoolean(request.getParameter(RestConstants.ADMIN_SAFE_MODE_PARAM));
070                Services.get().setSafeMode(safeMode);
071                response.setStatus(HttpServletResponse.SC_OK);
072            }
073            else {
074                throw new XServletException(HttpServletResponse.SC_BAD_REQUEST, ErrorCode.E0301, resourceName);
075            }*/
076        }
077    
078    
079        /**
080         * Get JMS connection Info
081         * @param request
082         * @param response
083         * @throws XServletException
084         * @throws IOException
085         */
086        abstract JsonBean getJMSConnectionInfo(HttpServletRequest request, HttpServletResponse response)
087                throws XServletException, IOException;
088    
089    
090        /**
091         * Return safemode state, instrumentation, configuration, osEnv or
092         * javaSysProps
093         */
094        @Override
095        @SuppressWarnings("unchecked")
096        public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
097            String resource = getResourceName(request);
098            Instrumentation instr = Services.get().get(InstrumentationService.class).get();
099    
100            if (resource.equals(RestConstants.ADMIN_STATUS_RESOURCE)) {
101                JSONObject json = new JSONObject();
102                populateOozieMode(json);
103                // json.put(JsonTags.SYSTEM_SAFE_MODE, getOozeMode());
104                sendJsonResponse(response, HttpServletResponse.SC_OK, json);
105            }
106            else if (resource.equals(RestConstants.ADMIN_OS_ENV_RESOURCE)) {
107                JSONObject json = new JSONObject();
108                json.putAll(instr.getOSEnv());
109                sendJsonResponse(response, HttpServletResponse.SC_OK, json);
110            }
111            else if (resource.equals(RestConstants.ADMIN_JAVA_SYS_PROPS_RESOURCE)) {
112                JSONObject json = new JSONObject();
113                json.putAll(instr.getJavaSystemProperties());
114                sendJsonResponse(response, HttpServletResponse.SC_OK, json);
115            }
116            else if (resource.equals(RestConstants.ADMIN_CONFIG_RESOURCE)) {
117                JSONObject json = new JSONObject();
118                json.putAll(instr.getConfiguration());
119                sendJsonResponse(response, HttpServletResponse.SC_OK, json);
120            }
121            else if (resource.equals(RestConstants.ADMIN_INSTRUMENTATION_RESOURCE)) {
122                sendJsonResponse(response, HttpServletResponse.SC_OK, instrToJson(instr));
123            }
124            else if (resource.equals(RestConstants.ADMIN_BUILD_VERSION_RESOURCE)) {
125                JSONObject json = new JSONObject();
126                json.put(JsonTags.BUILD_VERSION, BuildInfo.getBuildInfo().getProperty(BuildInfo.BUILD_VERSION));
127                sendJsonResponse(response, HttpServletResponse.SC_OK, json);
128            }
129            else if (resource.equals(RestConstants.ADMIN_QUEUE_DUMP_RESOURCE)) {
130                JSONObject json = new JSONObject();
131                getQueueDump(json);
132                sendJsonResponse(response, HttpServletResponse.SC_OK, json);
133            }
134            else if (resource.equals(RestConstants.ADMIN_TIME_ZONES_RESOURCE)) {
135                JSONObject json = new JSONObject();
136                json.put(JsonTags.AVAILABLE_TIME_ZONES, availableTimeZonesToJsonArray());
137                sendJsonResponse(response, HttpServletResponse.SC_OK, json);
138            }
139            else if (resource.equals(RestConstants.ADMIN_JMS_INFO)) {
140                String timeZoneId = request.getParameter(RestConstants.TIME_ZONE_PARAM) == null ? "GMT" : request
141                        .getParameter(RestConstants.TIME_ZONE_PARAM);
142                JsonBean jmsBean = getJMSConnectionInfo(request, response);
143                sendJsonResponse(response, HttpServletResponse.SC_OK, jmsBean, timeZoneId);
144            }
145    
146        }
147    
148    
149        @Override
150        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
151                IOException {
152        }
153    
154        @SuppressWarnings("unchecked")
155        private <T> JSONArray instrElementsToJson(Map<String, Map<String, Instrumentation.Element<T>>> instrElements) {
156            JSONArray array = new JSONArray();
157            for (Map.Entry<String, Map<String, Instrumentation.Element<T>>> group : instrElements.entrySet()) {
158                JSONObject json = new JSONObject();
159                String groupName = group.getKey();
160                json.put(JsonTags.INSTR_GROUP, groupName);
161                JSONArray dataArray = new JSONArray();
162                for (Map.Entry<String, Instrumentation.Element<T>> elementEntry : group.getValue().entrySet()) {
163                    String samplerName = elementEntry.getKey();
164                    JSONObject dataJson = new JSONObject();
165                    dataJson.put(JsonTags.INSTR_NAME, samplerName);
166                    Object value = elementEntry.getValue().getValue();
167                    if (value instanceof Instrumentation.Timer) {
168                        Instrumentation.Timer timer = (Instrumentation.Timer) value;
169                        dataJson.put(JsonTags.INSTR_TIMER_TICKS, timer.getTicks());
170                        dataJson.put(JsonTags.INSTR_TIMER_OWN_TIME_AVG, timer.getOwnAvg());
171                        dataJson.put(JsonTags.INSTR_TIMER_TOTAL_TIME_AVG, timer.getTotalAvg());
172                        dataJson.put(JsonTags.INSTR_TIMER_OWN_STD_DEV, timer.getOwnStdDev());
173                        dataJson.put(JsonTags.INSTR_TIMER_TOTAL_STD_DEV, timer.getTotalStdDev());
174                        dataJson.put(JsonTags.INSTR_TIMER_OWN_MIN_TIME, timer.getOwnMin());
175                        dataJson.put(JsonTags.INSTR_TIMER_OWN_MAX_TIME, timer.getOwnMax());
176                        dataJson.put(JsonTags.INSTR_TIMER_TOTAL_MIN_TIME, timer.getTotalMin());
177                        dataJson.put(JsonTags.INSTR_TIMER_TOTAL_MAX_TIME, timer.getTotalMax());
178                    }
179                    else {
180                        dataJson.put(JsonTags.INSTR_VARIABLE_VALUE, value);
181                    }
182                    dataArray.add(dataJson);
183                }
184                json.put(JsonTags.INSTR_DATA, dataArray);
185                array.add(json);
186            }
187            return array;
188        }
189    
190        @SuppressWarnings("unchecked")
191        private JSONObject instrToJson(Instrumentation instr) {
192            JSONObject json = new JSONObject();
193            json.put(JsonTags.INSTR_VARIABLES, instrElementsToJson(instr.getVariables()));
194            json.put(JsonTags.INSTR_SAMPLERS, instrElementsToJson(instr.getSamplers()));
195            json.put(JsonTags.INSTR_COUNTERS, instrElementsToJson(instr.getCounters()));
196            json.put(JsonTags.INSTR_TIMERS, instrElementsToJson(instr.getTimers()));
197            return json;
198        }
199    
200        protected abstract void populateOozieMode(JSONObject json);
201    
202        protected abstract void setOozieMode(HttpServletRequest request, HttpServletResponse response, String resourceName)
203                throws XServletException;
204    
205        protected abstract void getQueueDump(JSONObject json) throws XServletException;
206    
207        private static final JSONArray GMTOffsetTimeZones = new JSONArray();
208        static {
209            prepareGMTOffsetTimeZones();
210        }
211    
212        @SuppressWarnings({"unchecked", "rawtypes"})
213        private static void prepareGMTOffsetTimeZones() {
214            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",
215                                            "GMT-05:00", "GMT-04:00", "GMT-03:00", "GMT-02:00", "GMT-01:00", "GMT+01:00", "GMT+02:00",
216                                            "GMT+03:00", "GMT+04:00", "GMT+05:00", "GMT+06:00", "GMT+07:00", "GMT+08:00", "GMT+09:00",
217                                            "GMT+10:00", "GMT+11:00", "GMT+12:00"}) {
218                TimeZone tz = TimeZone.getTimeZone(tzId);
219                JSONObject json = new JSONObject();
220                json.put(JsonTags.TIME_ZOME_DISPLAY_NAME, tz.getDisplayName(false, TimeZone.SHORT) + " (" + tzId + ")");
221                json.put(JsonTags.TIME_ZONE_ID, tzId);
222                GMTOffsetTimeZones.add(json);
223            }
224        }
225    
226        @SuppressWarnings({"unchecked", "rawtypes"})
227        private JSONArray availableTimeZonesToJsonArray() {
228            JSONArray array = new JSONArray();
229            for (String tzId : TimeZone.getAvailableIDs()) {
230                // skip id's that are like "Etc/GMT+01:00" because their display names are like "GMT-01:00", which is confusing
231                if (!tzId.startsWith("Etc/GMT")) {
232                    JSONObject json = new JSONObject();
233                    TimeZone tZone = TimeZone.getTimeZone(tzId);
234                    json.put(JsonTags.TIME_ZOME_DISPLAY_NAME, tZone.getDisplayName(false, TimeZone.SHORT) + " (" + tzId + ")");
235                    json.put(JsonTags.TIME_ZONE_ID, tzId);
236                    array.add(json);
237                }
238            }
239    
240            // 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
241            // in some hourly offsets here (though the user will not be able to use other offsets without editing the cookie manually
242            // and they are not in order)
243            array.addAll(GMTOffsetTimeZones);
244    
245            return array;
246        }
247    
248    }