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