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