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.List;
024    import java.util.Properties;
025    
026    import javax.servlet.http.HttpServletRequest;
027    import javax.servlet.http.HttpServletResponse;
028    
029    import org.apache.hadoop.conf.Configuration;
030    import org.apache.oozie.ErrorCode;
031    import org.apache.oozie.client.OozieClient.SYSTEM_MODE;
032    import org.apache.oozie.client.rest.JMSConnectionInfoBean;
033    import org.apache.oozie.client.rest.JsonBean;
034    import org.apache.oozie.client.rest.JsonTags;
035    import org.apache.oozie.client.rest.RestConstants;
036    import org.apache.oozie.jms.JMSConnectionInfo;
037    import org.apache.oozie.jms.JMSJobEventListener;
038    import org.apache.oozie.service.CallableQueueService;
039    import org.apache.oozie.service.JMSTopicService;
040    import org.apache.oozie.service.Services;
041    import org.json.simple.JSONArray;
042    import org.json.simple.JSONObject;
043    
044    public class V1AdminServlet extends BaseAdminServlet {
045    
046        private static final long serialVersionUID = 1L;
047        private static final String INSTRUMENTATION_NAME = "v1admin";
048        private static final ResourceInfo RESOURCES_INFO[] = new ResourceInfo[9];
049    
050        static {
051            RESOURCES_INFO[0] = new ResourceInfo(RestConstants.ADMIN_STATUS_RESOURCE, Arrays.asList("PUT", "GET"),
052                                                 Arrays.asList(new ParameterInfo(RestConstants.ADMIN_SYSTEM_MODE_PARAM, String.class, true,
053                                                                                 Arrays.asList("PUT"))));
054            RESOURCES_INFO[1] = new ResourceInfo(RestConstants.ADMIN_OS_ENV_RESOURCE, Arrays.asList("GET"),
055                    Collections.EMPTY_LIST);
056            RESOURCES_INFO[2] = new ResourceInfo(RestConstants.ADMIN_JAVA_SYS_PROPS_RESOURCE, Arrays.asList("GET"),
057                    Collections.EMPTY_LIST);
058            RESOURCES_INFO[3] = new ResourceInfo(RestConstants.ADMIN_CONFIG_RESOURCE, Arrays.asList("GET"),
059                    Collections.EMPTY_LIST);
060            RESOURCES_INFO[4] = new ResourceInfo(RestConstants.ADMIN_INSTRUMENTATION_RESOURCE, Arrays.asList("GET"),
061                    Collections.EMPTY_LIST);
062            RESOURCES_INFO[5] = new ResourceInfo(RestConstants.ADMIN_BUILD_VERSION_RESOURCE, Arrays.asList("GET"),
063                    Collections.EMPTY_LIST);
064            RESOURCES_INFO[6] = new ResourceInfo(RestConstants.ADMIN_QUEUE_DUMP_RESOURCE, Arrays.asList("GET"),
065                    Collections.EMPTY_LIST);
066            RESOURCES_INFO[7] = new ResourceInfo((RestConstants.ADMIN_TIME_ZONES_RESOURCE), Arrays.asList("GET"),
067                    Collections.EMPTY_LIST);
068            RESOURCES_INFO[8] = new ResourceInfo((RestConstants.ADMIN_JMS_INFO), Arrays.asList("GET"),
069                    Collections.EMPTY_LIST);
070    
071        }
072    
073        protected V1AdminServlet(String name) {
074            super(name, RESOURCES_INFO);
075            modeTag = RestConstants.ADMIN_SYSTEM_MODE_PARAM;
076        }
077    
078        public V1AdminServlet() {
079            this(INSTRUMENTATION_NAME);
080        }
081    
082        /*
083         * (non-Javadoc)
084         *
085         * @see
086         * org.apache.oozie.servlet.BaseAdminServlet#populateOozieMode(org.json.
087         * simple.JSONObject)
088         */
089        @SuppressWarnings("unchecked")
090        @Override
091        protected void populateOozieMode(JSONObject json) {
092            json.put(JsonTags.OOZIE_SYSTEM_MODE, Services.get().getSystemMode().toString());
093        }
094    
095        /*
096         * (non-Javadoc)
097         *
098         * @see
099         * org.apache.oozie.servlet.BaseAdminServlet#setOozieMode(javax.servlet.
100         * http.HttpServletRequest, javax.servlet.http.HttpServletResponse,
101         * java.lang.String)
102         */
103        @Override
104        protected void setOozieMode(HttpServletRequest request,
105                                    HttpServletResponse response, String resourceName)
106                throws XServletException {
107            if (resourceName.equals(RestConstants.ADMIN_STATUS_RESOURCE)) {
108                SYSTEM_MODE sysMode = SYSTEM_MODE.valueOf(request.getParameter(modeTag));
109                Services.get().setSystemMode(sysMode);
110                response.setStatus(HttpServletResponse.SC_OK);
111            }
112            else {
113                throw new XServletException(HttpServletResponse.SC_BAD_REQUEST,
114                                            ErrorCode.E0301, resourceName);
115            }
116        }
117    
118        /**
119         * Get a json array of queue dump and a json array of unique map dump
120         *
121         * @param JSONObject the result json object that contains a JSONArray for the callable dump
122         *
123         * @see
124         * org.apache.oozie.servlet.BaseAdminServlet#getQueueDump(org.json.simple
125         * .JSONObject)
126         */
127        @SuppressWarnings("unchecked")
128        @Override
129        protected void getQueueDump(JSONObject json) throws XServletException {
130            List<String> queueDumpList = Services.get().get(CallableQueueService.class).getQueueDump();
131            JSONArray queueDumpArray = new JSONArray();
132            for (String str: queueDumpList) {
133                JSONObject jObject = new JSONObject();
134                jObject.put(JsonTags.CALLABLE_DUMP, str);
135                queueDumpArray.add(jObject);
136            }
137            json.put(JsonTags.QUEUE_DUMP, queueDumpArray);
138    
139            List<String> uniqueDumpList = Services.get().get(CallableQueueService.class).getUniqueDump();
140            JSONArray uniqueDumpArray = new JSONArray();
141            for (String str: uniqueDumpList) {
142                JSONObject jObject = new JSONObject();
143                jObject.put(JsonTags.UNIQUE_ENTRY_DUMP, str);
144                uniqueDumpArray.add(jObject);
145            }
146            json.put(JsonTags.UNIQUE_MAP_DUMP, uniqueDumpArray);
147        }
148    
149        @Override
150        protected JsonBean getJMSConnectionInfo(HttpServletRequest request, HttpServletResponse response) throws XServletException,
151                IOException {
152            throw new XServletException(HttpServletResponse.SC_BAD_REQUEST, ErrorCode.E0302, "Not supported in v1");
153        }
154    
155    
156    }