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 */
018package org.apache.oozie.servlet;
019
020import org.apache.oozie.XException;
021import org.apache.oozie.ErrorCode;
022import org.apache.oozie.util.XLog;
023
024import javax.servlet.ServletException;
025
026/**
027 * Specialized Oozie servlet exception that uses Oozie error codes. <p/> It extends ServletException so it can be
028 * handled in the <code>Servlet.service</code> method of the {@link JsonRestServlet}.
029 */
030public class XServletException extends ServletException {
031    private static final long serialVersionUID = 1L;
032    private ErrorCode errorCode;
033    private int httpStatusCode;
034
035    /**
036     * Create a DagXServletException that triggers a HTTP BAD_REQUEST (400).
037     *
038     * @param httpStatusCode HTTP error code to return.
039     * @param ex cause
040     */
041    public XServletException(int httpStatusCode, XException ex) {
042        super(ex.getMessage(), ex);
043        this.errorCode = ex.getErrorCode();
044        this.httpStatusCode = httpStatusCode;
045    }
046
047    /**
048     * Create a XServletException that triggers a specified HTTP error code.
049     *
050     * @param httpStatusCode HTTP error code to return.
051     * @param errorCode Oozie error code.
052     * @param params paramaters to use in the error code template. If the last parameter is an Exception,
053     */
054    public XServletException(int httpStatusCode, ErrorCode errorCode, Object... params) {
055        super(errorCode.format(params), XLog.getCause(params));
056        this.errorCode = errorCode;
057        this.httpStatusCode = httpStatusCode;
058    }
059
060    /**
061     * Return the Oozie error code for the exception.
062     *
063     * @return error code for the exception.
064     */
065    public ErrorCode getErrorCode() {
066        return errorCode;
067    }
068
069    /**
070     * Return the HTTP error code to return to the client.
071     *
072     * @return HTTP error code.
073     */
074    public int getHttpStatusCode() {
075        return httpStatusCode;
076    }
077
078}