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.util.Arrays;
021    import java.util.Collections;
022    import java.util.List;
023    
024    import javax.servlet.http.HttpServletRequest;
025    import javax.servlet.http.HttpServletResponse;
026    
027    import org.apache.oozie.ErrorCode;
028    import org.apache.oozie.client.OozieClient.SYSTEM_MODE;
029    import org.apache.oozie.client.rest.JsonTags;
030    import org.apache.oozie.client.rest.RestConstants;
031    import org.apache.oozie.service.CallableQueueService;
032    import org.apache.oozie.service.Services;
033    import org.json.simple.JSONArray;
034    import org.json.simple.JSONObject;
035    
036    public class V1AdminServlet extends BaseAdminServlet {
037    
038        private static final long serialVersionUID = 1L;
039        private static final String INSTRUMENTATION_NAME = "v1admin";
040        private static final ResourceInfo RESOURCES_INFO[] = new ResourceInfo[7];
041    
042        static {
043            RESOURCES_INFO[0] = new ResourceInfo(RestConstants.ADMIN_STATUS_RESOURCE, Arrays.asList("PUT", "GET"),
044                                                 Arrays.asList(new ParameterInfo(RestConstants.ADMIN_SYSTEM_MODE_PARAM, String.class, true,
045                                                                                 Arrays.asList("PUT"))));
046            RESOURCES_INFO[1] = new ResourceInfo(RestConstants.ADMIN_OS_ENV_RESOURCE, Arrays.asList("GET"),
047                    Collections.EMPTY_LIST);
048            RESOURCES_INFO[2] = new ResourceInfo(RestConstants.ADMIN_JAVA_SYS_PROPS_RESOURCE, Arrays.asList("GET"),
049                    Collections.EMPTY_LIST);
050            RESOURCES_INFO[3] = new ResourceInfo(RestConstants.ADMIN_CONFIG_RESOURCE, Arrays.asList("GET"),
051                    Collections.EMPTY_LIST);
052            RESOURCES_INFO[4] = new ResourceInfo(RestConstants.ADMIN_INSTRUMENTATION_RESOURCE, Arrays.asList("GET"),
053                    Collections.EMPTY_LIST);
054            RESOURCES_INFO[5] = new ResourceInfo(RestConstants.ADMIN_BUILD_VERSION_RESOURCE, Arrays.asList("GET"),
055                    Collections.EMPTY_LIST);
056            RESOURCES_INFO[6] = new ResourceInfo(RestConstants.ADMIN_QUEUE_DUMP_RESOURCE, Arrays.asList("GET"),
057                    Collections.EMPTY_LIST);
058        }
059    
060        public V1AdminServlet() {
061            super(INSTRUMENTATION_NAME, RESOURCES_INFO);
062            modeTag = RestConstants.ADMIN_SYSTEM_MODE_PARAM;
063        }
064    
065        /*
066         * (non-Javadoc)
067         *
068         * @see
069         * org.apache.oozie.servlet.BaseAdminServlet#populateOozieMode(org.json.
070         * simple.JSONObject)
071         */
072        @SuppressWarnings("unchecked")
073        @Override
074        protected void populateOozieMode(JSONObject json) {
075            json.put(JsonTags.OOZIE_SYSTEM_MODE, Services.get().getSystemMode().toString());
076        }
077    
078        /*
079         * (non-Javadoc)
080         *
081         * @see
082         * org.apache.oozie.servlet.BaseAdminServlet#setOozieMode(javax.servlet.
083         * http.HttpServletRequest, javax.servlet.http.HttpServletResponse,
084         * java.lang.String)
085         */
086        @Override
087        protected void setOozieMode(HttpServletRequest request,
088                                    HttpServletResponse response, String resourceName)
089                throws XServletException {
090            if (resourceName.equals(RestConstants.ADMIN_STATUS_RESOURCE)) {
091                SYSTEM_MODE sysMode = SYSTEM_MODE.valueOf(request.getParameter(modeTag));
092                Services.get().setSystemMode(sysMode);
093                response.setStatus(HttpServletResponse.SC_OK);
094            }
095            else {
096                throw new XServletException(HttpServletResponse.SC_BAD_REQUEST,
097                                            ErrorCode.E0301, resourceName);
098            }
099        }
100    
101        /**
102         * Get a json array of queue dump and a json array of unique map dump
103         *
104         * @param JSONObject the result json object that contains a JSONArray for the callable dump
105         *
106         * @see
107         * org.apache.oozie.servlet.BaseAdminServlet#getQueueDump(org.json.simple
108         * .JSONObject)
109         */
110        @SuppressWarnings("unchecked")
111        @Override
112        protected void getQueueDump(JSONObject json) throws XServletException {
113            List<String> queueDumpList = Services.get().get(CallableQueueService.class).getQueueDump();
114            JSONArray queueDumpArray = new JSONArray();
115            for (String str: queueDumpList) {
116                JSONObject jObject = new JSONObject();
117                jObject.put(JsonTags.CALLABLE_DUMP, str);
118                queueDumpArray.add(jObject);
119            }
120            json.put(JsonTags.QUEUE_DUMP, queueDumpArray);
121    
122            List<String> uniqueDumpList = Services.get().get(CallableQueueService.class).getUniqueDump();
123            JSONArray uniqueDumpArray = new JSONArray();
124            for (String str: uniqueDumpList) {
125                JSONObject jObject = new JSONObject();
126                jObject.put(JsonTags.UNIQUE_ENTRY_DUMP, str);
127                uniqueDumpArray.add(jObject);
128            }
129            json.put(JsonTags.UNIQUE_MAP_DUMP, uniqueDumpArray);
130        }
131    
132    }