001 package hirondelle.web4j.model;
002
003 /**
004 Thrown by {@link hirondelle.web4j.security.ApplicationFirewall}
005 when a problem with an incoming HTTP request is detected.
006
007 <P><span class="highlight">This class is intended only for bugs and malicious attacks.
008 It is not intended for normal business logic.</span> If a <tt>BadRequestException</tt> is
009 thrown by {@link hirondelle.web4j.security.ApplicationFirewall}, then the
010 {@link hirondelle.web4j.Controller} will reply with a short, unpolished
011 response (often a default page defined by the server). Under normal operating conditions,
012 the end user should not see such a response.
013
014 <P>See {@link hirondelle.web4j.security.ApplicationFirewall} for more information.
015
016 <P><em>Design Note</em>
017 <br>This class is not an {@link AppException}, since it is meant to encapsulate only a
018 single item at a time.
019 */
020 public final class BadRequestException extends Exception {
021
022 /**
023 Construct using a standard HTTP status code.
024
025 <P>The caller is highly encouraged to pass a field defined in
026 {@link javax.servlet.http.HttpServletResponse} to this constructor
027 ({@link javax.servlet.http.HttpServletResponse#SC_NOT_IMPLEMENTED}, and so on).
028
029 <P>See <a href="http://www.w3.org/Protocols/rfc2616/rfc2616.html">W3C HTTP Specification</a>
030 for more information on status codes.
031 */
032 public BadRequestException(int aHTTPStatusCode){
033 fStatusCode = aHTTPStatusCode;
034 fErrorMessage = null;
035 }
036
037 /**
038 Construct using a standard HTTP status code and an error message to be presented to the
039 user.
040
041 See {@link #BadRequestException(int)} for more information.
042 */
043 public BadRequestException(int aHTTPStatusCode, String aErrorMessage){
044 fStatusCode = aHTTPStatusCode;
045 fErrorMessage = aErrorMessage;
046 }
047
048 /** Return the status code passed to the constructor. */
049 public int getStatusCode(){
050 return fStatusCode;
051 }
052
053 /**
054 Return the error message passed to the constructor. If constructed without a message, then
055 return <tt>null</tt>.
056 */
057 public String getErrorMessage(){
058 return fErrorMessage;
059 }
060
061 /** Intended for debugging only. */
062 @Override public String toString(){
063 return ToStringUtil.getText(this);
064 }
065
066 //PRIVATE //
067 private final int fStatusCode;
068 private final String fErrorMessage;
069 }