View Javadoc

1   /**
2    * Copyright (C) 2005-2009 Alfresco Software Limited.
3    *
4    * This file is part of the Spring Surf Extension project.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *  http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.springframework.extensions.webscripts;
20  
21  import java.util.Collections;
22  import java.util.Map;
23  
24  import javax.servlet.http.HttpServletResponse;
25  
26  import org.springframework.extensions.surf.exception.PlatformRuntimeException;
27  
28  /**
29   * Web Script Exceptions.
30   * 
31   * @author David Caruana
32   */
33  public class WebScriptException extends PlatformRuntimeException implements StatusTemplateFactory
34  {
35      private static final long serialVersionUID = -7338963365877285084L;
36  
37      private int status = HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
38      private StatusTemplateFactory statusTemplateFactory;
39  
40  
41      public WebScriptException(String msgId)
42      {
43         super(msgId);
44      }
45  
46      public WebScriptException(int status, String msgId)
47      {
48          this(msgId);
49          this.status = status;
50      }
51      
52      public WebScriptException(String msgId, Throwable cause)
53      {
54         super(msgId, cause);
55      }
56  
57      public WebScriptException(int status, String msgId, Throwable cause)
58      {
59         super(msgId, cause);
60         this.status = status;
61      }
62  
63      public WebScriptException(String msgId, Object ... args)
64      {
65          super(msgId, args);
66      }
67  
68      public WebScriptException(int status, String msgId, Object ... args)
69      {
70          super(msgId, args);
71          this.status = status;
72      }
73  
74      public WebScriptException(String msgId, Throwable cause, Object ... args)
75      {
76          super(msgId, args, cause);
77      }
78  
79      public WebScriptException(int status, String msgId, Throwable cause, Object ... args)
80      {
81          super(msgId, args, cause);
82          this.status = status;
83      }
84  
85      /**
86       * Attach an advanced description of the status code associated to this exception
87       * 
88       * @param template  status template
89       * @param model  template model
90       * @deprecated
91       */
92      public void setStatusTemplate(final StatusTemplate statusTemplate, final Map<String, Object> statusModel)
93      {
94          setStatusTemplateFactory(new StatusTemplateFactory()
95          {
96  
97              public Map<String, Object> getStatusModel()
98              {
99                  return statusModel;
100             }
101 
102             public StatusTemplate getStatusTemplate()
103             {
104                 return statusTemplate;
105             }
106         });
107     }
108 
109     
110     /**
111      * Associates a factory for the lazy retrieval of an advanced description of the status code associated with this
112      * exception
113      * 
114      * @param statusTemplateFactory
115      *            the factory to set
116      */
117     public void setStatusTemplateFactory(StatusTemplateFactory statusTemplateFactory)
118     {
119         this.statusTemplateFactory = statusTemplateFactory;
120     }
121 
122     /**
123      * Get status code
124      * 
125      * @return  status code
126      */
127     public int getStatus()
128     {
129         return status;
130     }
131 
132     /**
133      * Get status template
134      * 
135      * @return  template
136      */
137     public StatusTemplate getStatusTemplate()
138     {
139         return this.statusTemplateFactory == null ? null : this.statusTemplateFactory.getStatusTemplate();
140     }
141 
142     /**
143      * Get status model
144      * 
145      * @return  model
146      */
147     public Map<String, Object> getStatusModel()
148     {
149         Map <String,Object> statusModel = null;
150         if (this.statusTemplateFactory != null)
151         {
152             statusModel = this.statusTemplateFactory.getStatusModel();
153         }
154         return statusModel == null ? Collections.<String, Object> emptyMap() : statusModel;
155     }
156 
157 }