001    /**
002     * Copyright (c) 2010 Yahoo! Inc. All rights reserved.
003     * Licensed under the Apache License, Version 2.0 (the "License");
004     * you may not use this file except in compliance with the License.
005     * You may obtain a copy of the License at
006     *
007     *   http://www.apache.org/licenses/LICENSE-2.0
008     *
009     *  Unless required by applicable law or agreed to in writing, software
010     *  distributed under the License is distributed on an "AS IS" BASIS,
011     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012     *  See the License for the specific language governing permissions and
013     *  limitations under the License. See accompanying LICENSE file.
014     */
015    package org.apache.oozie.servlet;
016    
017    import java.util.Arrays;
018    import java.util.Collections;
019    
020    import javax.servlet.http.HttpServletRequest;
021    import javax.servlet.http.HttpServletResponse;
022    
023    import org.apache.oozie.ErrorCode;
024    import org.apache.oozie.client.OozieClient.SYSTEM_MODE;
025    import org.apache.oozie.client.rest.JsonTags;
026    import org.apache.oozie.client.rest.RestConstants;
027    import org.apache.oozie.service.Services;
028    import org.json.simple.JSONObject;
029    
030    public class V0AdminServlet extends BaseAdminServlet {
031        private static final long serialVersionUID = 1L;
032        private static final String INSTRUMENTATION_NAME = "v0admin";
033        private static final ResourceInfo RESOURCES_INFO[] = new ResourceInfo[6];
034    
035        static {
036            RESOURCES_INFO[0] = new ResourceInfo(RestConstants.ADMIN_STATUS_RESOURCE, Arrays.asList("PUT", "GET"),
037                                                 Arrays.asList(new ParameterInfo(RestConstants.ADMIN_SAFE_MODE_PARAM, Boolean.class, true,
038                                                                                 Arrays.asList("PUT"))));
039            RESOURCES_INFO[1] = new ResourceInfo(RestConstants.ADMIN_OS_ENV_RESOURCE, Arrays.asList("GET"),
040                    Collections.EMPTY_LIST);
041            RESOURCES_INFO[2] = new ResourceInfo(RestConstants.ADMIN_JAVA_SYS_PROPS_RESOURCE, Arrays.asList("GET"),
042                    Collections.EMPTY_LIST);
043            RESOURCES_INFO[3] = new ResourceInfo(RestConstants.ADMIN_CONFIG_RESOURCE, Arrays.asList("GET"),
044                    Collections.EMPTY_LIST);
045            RESOURCES_INFO[4] = new ResourceInfo(RestConstants.ADMIN_INSTRUMENTATION_RESOURCE, Arrays.asList("GET"),
046                    Collections.EMPTY_LIST);
047            RESOURCES_INFO[5] = new ResourceInfo(RestConstants.ADMIN_BUILD_VERSION_RESOURCE, Arrays.asList("GET"),
048                    Collections.EMPTY_LIST);
049        }
050    
051        public V0AdminServlet() {
052            super(INSTRUMENTATION_NAME, RESOURCES_INFO);
053            modeTag = RestConstants.ADMIN_SAFE_MODE_PARAM;
054        }
055    
056        /*
057         * (non-Javadoc)
058         *
059         * @see
060         * org.apache.oozie.servlet.BaseAdminServlet#populateOozieMode(org.json.
061         * simple.JSONObject)
062         */
063        @Override
064        protected void populateOozieMode(JSONObject json) {
065            if (Services.get().getSystemMode() != SYSTEM_MODE.NORMAL) {
066                json.put(JsonTags.OOZIE_SAFE_MODE, true);
067            }
068            else {
069                json.put(JsonTags.OOZIE_SAFE_MODE, false);
070            }
071        }
072    
073        /*
074         * (non-Javadoc)
075         *
076         * @see
077         * org.apache.oozie.servlet.BaseAdminServlet#setOozieMode(javax.servlet.
078         * http.HttpServletRequest, javax.servlet.http.HttpServletResponse,
079         * java.lang.String)
080         */
081        @Override
082        protected void setOozieMode(HttpServletRequest request, HttpServletResponse response, String resourceName)
083                throws XServletException {
084            if (resourceName.equals(RestConstants.ADMIN_STATUS_RESOURCE)) {
085                boolean safeMode = Boolean.parseBoolean(request.getParameter(modeTag));
086                //Services.get().setSafeMode(safeMode);
087                SYSTEM_MODE sysMode = safeMode == true ? SYSTEM_MODE.NOWEBSERVICE : SYSTEM_MODE.NORMAL;
088                System.out.println(modeTag + " DDDD " + sysMode);
089                Services.get().setSystemMode(sysMode);
090                response.setStatus(HttpServletResponse.SC_OK);
091            }
092            else {
093                throw new XServletException(HttpServletResponse.SC_BAD_REQUEST,
094                                            ErrorCode.E0301, resourceName);
095            }
096        }
097    
098        /*
099         * (non-Javadoc)
100         *
101         * @see
102         * org.apache.oozie.servlet.BaseAdminServlet#getQueueDump(org.json.simple
103         * .JSONObject)
104         */
105        @Override
106        protected void getQueueDump(JSONObject json) throws XServletException {
107            throw new XServletException(HttpServletResponse.SC_BAD_REQUEST, ErrorCode.E0301);
108        }
109    }