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.List;
023    
024    import javax.servlet.ServletException;
025    import javax.servlet.http.HttpServletRequest;
026    import javax.servlet.http.HttpServletResponse;
027    
028    import org.apache.oozie.ErrorCode;
029    import org.apache.oozie.SLAEventBean;
030    import org.apache.oozie.client.rest.RestConstants;
031    import org.apache.oozie.command.CommandException;
032    import org.apache.oozie.command.coord.SLAEventsXCommand;
033    import org.apache.oozie.util.XLog;
034    import org.apache.oozie.util.XmlUtils;
035    import org.jdom.Element;
036    
037    public class SLAServlet extends JsonRestServlet {
038        private static final String INSTRUMENTATION_NAME = "sla";
039    
040        private static final JsonRestServlet.ResourceInfo RESOURCES_INFO[] = new JsonRestServlet.ResourceInfo[1];
041    
042        static {
043            RESOURCES_INFO[0] = new JsonRestServlet.ResourceInfo("", Arrays
044                    .asList("GET"), Arrays.asList(
045                    new JsonRestServlet.ParameterInfo(
046                            RestConstants.SLA_GT_SEQUENCE_ID, String.class, true,
047                            Arrays.asList("GET")),
048                    new JsonRestServlet.ParameterInfo(RestConstants.MAX_EVENTS,
049                                                      String.class, false, Arrays.asList("GET"))));
050        }
051    
052        public SLAServlet() {
053            super(INSTRUMENTATION_NAME, RESOURCES_INFO);
054        }
055    
056        /**
057         * Return information about SLA Events.
058         */
059        public void doGet(HttpServletRequest request, HttpServletResponse response)
060                throws ServletException, IOException {
061    
062            try {
063                String gtSequenceNum = request
064                        .getParameter(RestConstants.SLA_GT_SEQUENCE_ID);
065                String strMaxEvents = request
066                        .getParameter(RestConstants.MAX_EVENTS);
067                int maxNoEvents = 100; // Default
068                XLog.getLog(getClass()).debug(
069                        "Got SLA GET request for :" + gtSequenceNum
070                                + " and max-events :" + strMaxEvents);
071                if (strMaxEvents != null && strMaxEvents.length() > 0) {
072                    maxNoEvents = Integer.parseInt(strMaxEvents);
073                }
074                if (gtSequenceNum != null) {
075                    long seqId = Long.parseLong(gtSequenceNum);
076                    stopCron();
077                    SLAEventsXCommand seCommand = new SLAEventsXCommand(seqId, maxNoEvents);
078                    List<SLAEventBean> slaEvntList = seCommand.call();
079                    long lastSeqId = seCommand.getLastSeqId();
080    
081                    Element eResponse = new Element("sla-message");
082                    for (SLAEventBean event : slaEvntList) {
083                        eResponse.addContent(event.toXml());
084                    }
085                    Element eLastSeq = new Element("last-sequence-id");
086                    eLastSeq.addContent(String.valueOf(lastSeqId));
087                    eResponse.addContent(eLastSeq);
088                    response.setContentType(XML_UTF8);
089                    XLog.getLog(getClass()).debug("Writing back SLA Servlet  Caller with last-seq-id " + lastSeqId);
090                    startCron();
091                    response.setStatus(HttpServletResponse.SC_OK);
092                    response.getWriter().write(
093                            XmlUtils.prettyPrint(eResponse) + "\n");
094                }
095                else {
096                    XLog.getLog(getClass()).error(
097                            "Not implemented witout gt_seq_id");
098                    throw new XServletException(HttpServletResponse.SC_BAD_REQUEST,
099                                                ErrorCode.E0401, "Not implemented without gtSeqID");
100                }
101            }
102            catch (CommandException ce) {
103                ce.printStackTrace();
104                XLog.getLog(getClass()).error("Command exception ", ce);
105                throw new XServletException(HttpServletResponse.SC_BAD_REQUEST, ce);
106            }
107            catch (RuntimeException re) {
108                re.printStackTrace();
109                XLog.getLog(getClass()).error("Runtime error ", re);
110                throw new XServletException(HttpServletResponse.SC_BAD_REQUEST, ErrorCode.E0307, re.getMessage());
111            }
112        }
113    
114    }