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