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[8];
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            RESOURCES_INFO[7] = new ResourceInfo((RestConstants.ADMIN_TIME_ZONES_RESOURCE), Arrays.asList("GET"),
059                    Collections.EMPTY_LIST);
060        }
061    
062        public V1AdminServlet() {
063            super(INSTRUMENTATION_NAME, RESOURCES_INFO);
064            modeTag = RestConstants.ADMIN_SYSTEM_MODE_PARAM;
065        }
066    
067        /*
068         * (non-Javadoc)
069         *
070         * @see
071         * org.apache.oozie.servlet.BaseAdminServlet#populateOozieMode(org.json.
072         * simple.JSONObject)
073         */
074        @SuppressWarnings("unchecked")
075        @Override
076        protected void populateOozieMode(JSONObject json) {
077            json.put(JsonTags.OOZIE_SYSTEM_MODE, Services.get().getSystemMode().toString());
078        }
079    
080        /*
081         * (non-Javadoc)
082         *
083         * @see
084         * org.apache.oozie.servlet.BaseAdminServlet#setOozieMode(javax.servlet.
085         * http.HttpServletRequest, javax.servlet.http.HttpServletResponse,
086         * java.lang.String)
087         */
088        @Override
089        protected void setOozieMode(HttpServletRequest request,
090                                    HttpServletResponse response, String resourceName)
091                throws XServletException {
092            if (resourceName.equals(RestConstants.ADMIN_STATUS_RESOURCE)) {
093                SYSTEM_MODE sysMode = SYSTEM_MODE.valueOf(request.getParameter(modeTag));
094                Services.get().setSystemMode(sysMode);
095                response.setStatus(HttpServletResponse.SC_OK);
096            }
097            else {
098                throw new XServletException(HttpServletResponse.SC_BAD_REQUEST,
099                                            ErrorCode.E0301, resourceName);
100            }
101        }
102    
103        /**
104         * Get a json array of queue dump and a json array of unique map dump
105         *
106         * @param JSONObject the result json object that contains a JSONArray for the callable dump
107         *
108         * @see
109         * org.apache.oozie.servlet.BaseAdminServlet#getQueueDump(org.json.simple
110         * .JSONObject)
111         */
112        @SuppressWarnings("unchecked")
113        @Override
114        protected void getQueueDump(JSONObject json) throws XServletException {
115            List<String> queueDumpList = Services.get().get(CallableQueueService.class).getQueueDump();
116            JSONArray queueDumpArray = new JSONArray();
117            for (String str: queueDumpList) {
118                JSONObject jObject = new JSONObject();
119                jObject.put(JsonTags.CALLABLE_DUMP, str);
120                queueDumpArray.add(jObject);
121            }
122            json.put(JsonTags.QUEUE_DUMP, queueDumpArray);
123    
124            List<String> uniqueDumpList = Services.get().get(CallableQueueService.class).getUniqueDump();
125            JSONArray uniqueDumpArray = new JSONArray();
126            for (String str: uniqueDumpList) {
127                JSONObject jObject = new JSONObject();
128                jObject.put(JsonTags.UNIQUE_ENTRY_DUMP, str);
129                uniqueDumpArray.add(jObject);
130            }
131            json.put(JsonTags.UNIQUE_MAP_DUMP, uniqueDumpArray);
132        }
133    
134    }