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