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