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.io.UnsupportedEncodingException;
022    import java.net.URLDecoder;
023    import java.text.ParseException;
024    import java.util.Arrays;
025    import java.util.HashSet;
026    import java.util.List;
027    import java.util.Map;
028    import java.util.Set;
029    
030    import javax.servlet.ServletException;
031    import javax.servlet.http.HttpServletRequest;
032    import javax.servlet.http.HttpServletResponse;
033    import org.apache.oozie.ErrorCode;
034    import org.apache.oozie.XException;
035    import org.apache.oozie.client.OozieClient;
036    import org.apache.oozie.client.rest.RestConstants;
037    import org.apache.oozie.command.CommandException;
038    import org.apache.oozie.executor.jpa.sla.SLASummaryGetForFilterJPAExecutor;
039    import org.apache.oozie.executor.jpa.sla.SLASummaryGetForFilterJPAExecutor.SLASummaryFilter;
040    import org.apache.oozie.service.JPAService;
041    import org.apache.oozie.service.Services;
042    import org.apache.oozie.sla.SLASummaryBean;
043    import org.apache.oozie.util.DateUtils;
044    import org.apache.oozie.util.XLog;
045    import org.json.simple.JSONObject;
046    
047    @SuppressWarnings("serial")
048    public class V2SLAServlet extends SLAServlet {
049    
050        private static final String INSTRUMENTATION_NAME = "v2sla";
051        private static final JsonRestServlet.ResourceInfo RESOURCES_INFO[] = new JsonRestServlet.ResourceInfo[1];
052        private static final Set<String> SLA_FILTER_NAMES = new HashSet<String>();
053    
054        static {
055            SLA_FILTER_NAMES.add(OozieClient.FILTER_SLA_ID);
056            SLA_FILTER_NAMES.add(OozieClient.FILTER_SLA_PARENT_ID);
057            SLA_FILTER_NAMES.add(OozieClient.FILTER_SLA_APPNAME);
058            SLA_FILTER_NAMES.add(OozieClient.FILTER_SLA_NOMINAL_START);
059            SLA_FILTER_NAMES.add(OozieClient.FILTER_SLA_NOMINAL_END);
060        }
061    
062        static {
063            RESOURCES_INFO[0] = new JsonRestServlet.ResourceInfo("", Arrays.asList("GET"),
064                    Arrays.asList(new JsonRestServlet.ParameterInfo(RestConstants.JOBS_FILTER_PARAM, String.class, false,
065                            Arrays.asList("GET"))));
066        }
067    
068        public V2SLAServlet() {
069            super(INSTRUMENTATION_NAME, RESOURCES_INFO);
070        }
071    
072        @Override
073        public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
074    
075            XLog.getLog(getClass()).debug("Got SLA GET request:" + request.getQueryString());
076            try {
077                stopCron();
078                JSONObject json = getSLASummaryList(request, response);
079                startCron();
080                if (json == null) {
081                    response.setStatus(HttpServletResponse.SC_OK);
082                }
083                else {
084                    sendJsonResponse(response, HttpServletResponse.SC_OK, json);
085                }
086            }
087            catch (CommandException ce) {
088                XLog.getLog(getClass()).error("Command exception ", ce);
089                throw new XServletException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, ce);
090            }
091            catch (RuntimeException re) {
092                XLog.getLog(getClass()).error("Runtime error ", re);
093                throw new XServletException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, ErrorCode.E0307, re.getMessage());
094            }
095        }
096    
097        private JSONObject getSLASummaryList(HttpServletRequest request, HttpServletResponse response)
098                throws ServletException, CommandException {
099            String timeZoneId = request.getParameter(RestConstants.TIME_ZONE_PARAM) == null ? null : request
100                    .getParameter(RestConstants.TIME_ZONE_PARAM);
101            String filterString = request.getParameter(RestConstants.JOBS_FILTER_PARAM);
102            String maxResults = request.getParameter(RestConstants.LEN_PARAM);
103            int numMaxResults = 1000; // Default
104    
105            if (maxResults != null) {
106                numMaxResults = Integer.parseInt(maxResults);
107            }
108    
109            if (filterString == null || filterString.equals("")) {
110                throw new XServletException(HttpServletResponse.SC_BAD_REQUEST, ErrorCode.E0305,
111                        RestConstants.JOBS_FILTER_PARAM);
112            }
113    
114            try {
115                Map<String, List<String>> filterList = parseFilter(URLDecoder.decode(filterString, "UTF-8"), SLA_FILTER_NAMES);
116                SLASummaryFilter filter = new SLASummaryFilter();
117                if (filterList.containsKey(OozieClient.FILTER_SLA_ID)) {
118                    filter.setJobId(filterList.get(OozieClient.FILTER_SLA_ID).get(0));
119                }
120                if (filterList.containsKey(OozieClient.FILTER_SLA_PARENT_ID)) {
121                    filter.setParentId(filterList.get(OozieClient.FILTER_SLA_PARENT_ID).get(0));
122                }
123                if (filterList.containsKey(OozieClient.FILTER_SLA_APPNAME)) {
124                    filter.setAppName(filterList.get(OozieClient.FILTER_SLA_APPNAME).get(0));
125                }
126                if (filterList.containsKey(OozieClient.FILTER_SLA_NOMINAL_START)) {
127                    filter.setNominalStart(DateUtils.parseDateUTC(filterList.get(OozieClient.FILTER_SLA_NOMINAL_START).get(0)));
128                }
129                if (filterList.containsKey(OozieClient.FILTER_SLA_NOMINAL_END)) {
130                    filter.setNominalEnd(DateUtils.parseDateUTC(filterList.get(OozieClient.FILTER_SLA_NOMINAL_END).get(0)));
131                }
132    
133                if (filter.getAppName() == null && filter.getJobId() == null && filter.getParentId() == null) {
134                    throw new XServletException(HttpServletResponse.SC_BAD_REQUEST, ErrorCode.E0305,
135                            "At least one of the filter parameters - " + OozieClient.FILTER_SLA_ID + ","
136                                    + OozieClient.FILTER_SLA_PARENT_ID + " or " + OozieClient.FILTER_SLA_APPNAME
137                                    + " should be specified in the filter query parameter");
138                }
139    
140                JPAService jpaService = Services.get().get(JPAService.class);
141                List<SLASummaryBean> slaSummaryList = null;
142                if (jpaService != null) {
143                    slaSummaryList = jpaService.execute(new SLASummaryGetForFilterJPAExecutor(filter, numMaxResults));
144                }
145                else {
146                    XLog.getLog(getClass()).error(ErrorCode.E0610);
147                }
148                return SLASummaryBean.toJSONObject(slaSummaryList, timeZoneId);
149            }
150            catch (XException ex) {
151                throw new CommandException(ex);
152            }
153            catch (UnsupportedEncodingException e) {
154                throw new XServletException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, ErrorCode.E0307,
155                        "Unsupported Encoding", e);
156            }
157            catch (ParseException e) {
158                throw new XServletException(HttpServletResponse.SC_BAD_REQUEST, ErrorCode.E0303,
159                        filterString, e);
160            }
161    
162        }
163    
164    }