001 /** 002 * Copyright (c) 2010 Yahoo! Inc. All rights reserved. 003 * Licensed under the Apache License, Version 2.0 (the "License"); 004 * you may not use this file except in compliance with the License. 005 * You may obtain a copy of the License at 006 * 007 * http://www.apache.org/licenses/LICENSE-2.0 008 * 009 * Unless required by applicable law or agreed to in writing, software 010 * distributed under the License is distributed on an "AS IS" BASIS, 011 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 012 * See the License for the specific language governing permissions and 013 * limitations under the License. See accompanying LICENSE file. 014 */ 015 package org.apache.oozie.servlet; 016 017 import org.apache.oozie.XException; 018 import org.apache.oozie.ErrorCode; 019 import org.apache.oozie.util.XLog; 020 021 import javax.servlet.ServletException; 022 023 /** 024 * Specialized Oozie servlet exception that uses Oozie error codes. <p/> It extends ServletException so it can be 025 * handled in the <code>Servlet.service</code> method of the {@link JsonRestServlet}. 026 */ 027 public class XServletException extends ServletException { 028 private static final long serialVersionUID = 1L; 029 private ErrorCode errorCode; 030 private int httpStatusCode; 031 032 /** 033 * Create a DagXServletException that triggers a HTTP BAD_REQUEST (400). 034 * 035 * @param httpStatusCode HTTP error code to return. 036 * @param ex cause 037 */ 038 public XServletException(int httpStatusCode, XException ex) { 039 super(ex.getMessage(), ex); 040 this.errorCode = ex.getErrorCode(); 041 this.httpStatusCode = httpStatusCode; 042 } 043 044 /** 045 * Create a XServletException that triggers a specified HTTP error code. 046 * 047 * @param httpStatusCode HTTP error code to return. 048 * @param errorCode Oozie error code. 049 * @param params paramaters to use in the error code template. If the last parameter is an Exception, 050 */ 051 public XServletException(int httpStatusCode, ErrorCode errorCode, Object... params) { 052 super(errorCode.format(params), XLog.getCause(params)); 053 this.errorCode = errorCode; 054 this.httpStatusCode = httpStatusCode; 055 } 056 057 /** 058 * Return the Oozie error code for the exception. 059 * 060 * @return error code for the exception. 061 */ 062 public ErrorCode getErrorCode() { 063 return errorCode; 064 } 065 066 /** 067 * Return the HTTP error code to return to the client. 068 * 069 * @return HTTP error code. 070 */ 071 public int getHttpStatusCode() { 072 return httpStatusCode; 073 } 074 075 }