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    
023    import javax.servlet.http.HttpServletRequest;
024    import javax.servlet.http.HttpServletResponse;
025    
026    import org.apache.oozie.ErrorCode;
027    import org.apache.oozie.client.OozieClient.SYSTEM_MODE;
028    import org.apache.oozie.client.rest.JsonTags;
029    import org.apache.oozie.client.rest.RestConstants;
030    import org.apache.oozie.service.Services;
031    import org.json.simple.JSONObject;
032    
033    public class V0AdminServlet extends BaseAdminServlet {
034        private static final long serialVersionUID = 1L;
035        private static final String INSTRUMENTATION_NAME = "v0admin";
036        private static final ResourceInfo RESOURCES_INFO[] = new ResourceInfo[6];
037    
038        static {
039            RESOURCES_INFO[0] = new ResourceInfo(RestConstants.ADMIN_STATUS_RESOURCE, Arrays.asList("PUT", "GET"),
040                                                 Arrays.asList(new ParameterInfo(RestConstants.ADMIN_SAFE_MODE_PARAM, Boolean.class, true,
041                                                                                 Arrays.asList("PUT"))));
042            RESOURCES_INFO[1] = new ResourceInfo(RestConstants.ADMIN_OS_ENV_RESOURCE, Arrays.asList("GET"),
043                    Collections.EMPTY_LIST);
044            RESOURCES_INFO[2] = new ResourceInfo(RestConstants.ADMIN_JAVA_SYS_PROPS_RESOURCE, Arrays.asList("GET"),
045                    Collections.EMPTY_LIST);
046            RESOURCES_INFO[3] = new ResourceInfo(RestConstants.ADMIN_CONFIG_RESOURCE, Arrays.asList("GET"),
047                    Collections.EMPTY_LIST);
048            RESOURCES_INFO[4] = new ResourceInfo(RestConstants.ADMIN_INSTRUMENTATION_RESOURCE, Arrays.asList("GET"),
049                    Collections.EMPTY_LIST);
050            RESOURCES_INFO[5] = new ResourceInfo(RestConstants.ADMIN_BUILD_VERSION_RESOURCE, Arrays.asList("GET"),
051                    Collections.EMPTY_LIST);
052        }
053    
054        public V0AdminServlet() {
055            super(INSTRUMENTATION_NAME, RESOURCES_INFO);
056            modeTag = RestConstants.ADMIN_SAFE_MODE_PARAM;
057        }
058    
059        /*
060         * (non-Javadoc)
061         *
062         * @see
063         * org.apache.oozie.servlet.BaseAdminServlet#populateOozieMode(org.json.
064         * simple.JSONObject)
065         */
066        @Override
067        protected void populateOozieMode(JSONObject json) {
068            if (Services.get().getSystemMode() != SYSTEM_MODE.NORMAL) {
069                json.put(JsonTags.OOZIE_SAFE_MODE, true);
070            }
071            else {
072                json.put(JsonTags.OOZIE_SAFE_MODE, false);
073            }
074        }
075    
076        /*
077         * (non-Javadoc)
078         *
079         * @see
080         * org.apache.oozie.servlet.BaseAdminServlet#setOozieMode(javax.servlet.
081         * http.HttpServletRequest, javax.servlet.http.HttpServletResponse,
082         * java.lang.String)
083         */
084        @Override
085        protected void setOozieMode(HttpServletRequest request, HttpServletResponse response, String resourceName)
086                throws XServletException {
087            if (resourceName.equals(RestConstants.ADMIN_STATUS_RESOURCE)) {
088                boolean safeMode = Boolean.parseBoolean(request.getParameter(modeTag));
089                //Services.get().setSafeMode(safeMode);
090                SYSTEM_MODE sysMode = safeMode == true ? SYSTEM_MODE.NOWEBSERVICE : SYSTEM_MODE.NORMAL;
091                System.out.println(modeTag + " DDDD " + sysMode);
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         * (non-Javadoc)
103         *
104         * @see
105         * org.apache.oozie.servlet.BaseAdminServlet#getQueueDump(org.json.simple
106         * .JSONObject)
107         */
108        @Override
109        protected void getQueueDump(JSONObject json) throws XServletException {
110            throw new XServletException(HttpServletResponse.SC_BAD_REQUEST, ErrorCode.E0301, "cannot get queue dump");
111        }
112    }