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.Arrays;
022    import java.util.Collections;
023    import java.util.Map;
024    
025    import javax.servlet.http.HttpServletRequest;
026    import javax.servlet.http.HttpServletResponse;
027    
028    import org.apache.oozie.ErrorCode;
029    import org.apache.oozie.client.OozieClient.SYSTEM_MODE;
030    import org.apache.oozie.client.rest.JsonBean;
031    import org.apache.oozie.client.rest.JsonTags;
032    import org.apache.oozie.client.rest.RestConstants;
033    import org.apache.oozie.service.Services;
034    import org.json.simple.JSONObject;
035    
036    @SuppressWarnings("unchecked")
037    public class V0AdminServlet extends BaseAdminServlet {
038        private static final long serialVersionUID = 1L;
039        private static final String INSTRUMENTATION_NAME = "v0admin";
040        private static final ResourceInfo RESOURCES_INFO[] = new ResourceInfo[6];
041    
042        static {
043            RESOURCES_INFO[0] = new ResourceInfo(RestConstants.ADMIN_STATUS_RESOURCE, Arrays.asList("PUT", "GET"),
044                                                 Arrays.asList(new ParameterInfo(RestConstants.ADMIN_SAFE_MODE_PARAM, Boolean.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        }
057    
058        public V0AdminServlet() {
059            super(INSTRUMENTATION_NAME, RESOURCES_INFO);
060            modeTag = RestConstants.ADMIN_SAFE_MODE_PARAM;
061        }
062    
063        /*
064         * (non-Javadoc)
065         *
066         * @see
067         * org.apache.oozie.servlet.BaseAdminServlet#populateOozieMode(org.json.
068         * simple.JSONObject)
069         */
070        @Override
071        protected void populateOozieMode(JSONObject json) {
072            if (Services.get().getSystemMode() != SYSTEM_MODE.NORMAL) {
073                json.put(JsonTags.OOZIE_SAFE_MODE, true);
074            }
075            else {
076                json.put(JsonTags.OOZIE_SAFE_MODE, false);
077            }
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, HttpServletResponse response, String resourceName)
090                throws XServletException {
091            if (resourceName.equals(RestConstants.ADMIN_STATUS_RESOURCE)) {
092                boolean safeMode = Boolean.parseBoolean(request.getParameter(modeTag));
093                //Services.get().setSafeMode(safeMode);
094                SYSTEM_MODE sysMode = safeMode == true ? SYSTEM_MODE.NOWEBSERVICE : SYSTEM_MODE.NORMAL;
095                System.out.println(modeTag + " DDDD " + sysMode);
096                Services.get().setSystemMode(sysMode);
097                response.setStatus(HttpServletResponse.SC_OK);
098            }
099            else {
100                throw new XServletException(HttpServletResponse.SC_BAD_REQUEST,
101                                            ErrorCode.E0301, resourceName);
102            }
103        }
104    
105        /*
106         * (non-Javadoc)
107         *
108         * @see
109         * org.apache.oozie.servlet.BaseAdminServlet#getQueueDump(org.json.simple
110         * .JSONObject)
111         */
112        @Override
113        protected void getQueueDump(JSONObject json) throws XServletException {
114            throw new XServletException(HttpServletResponse.SC_BAD_REQUEST, ErrorCode.E0301, "cannot get queue dump");
115        }
116    
117        @Override
118        protected JsonBean getJMSConnectionInfo(HttpServletRequest request, HttpServletResponse response) throws XServletException,
119                IOException {
120            throw new XServletException(HttpServletResponse.SC_BAD_REQUEST, ErrorCode.E0302, "Not supported in v0");
121        }
122    
123        @Override
124        protected Map<String, String> getOozieURLs() throws XServletException {
125            throw new XServletException(HttpServletResponse.SC_BAD_REQUEST, ErrorCode.E0302, "Not supported in v0");
126        }
127    
128        @Override
129        protected void sendMetricsResponse(HttpServletResponse response) throws IOException, XServletException {
130            throw new XServletException(HttpServletResponse.SC_BAD_REQUEST, ErrorCode.E0302, "Not supported in v0");
131        }
132    }