KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > vfs > webdav > DAVException


1 /* ========================================================================== *
2  * Copyright (C) 2004-2005 Pier Fumagalli <http://www.betaversion.org/~pier/> *
3  * All rights reserved. *
4  * ========================================================================== *
5  * *
6  * Licensed under the Apache License, Version 2.0 (the "License"). You may *
7  * not use this file except in compliance with the License. You may obtain a *
8  * copy of the License at <http://www.apache.org/licenses/LICENSE-2.0>. *
9  * *
10  * Unless required by applicable law or agreed to in writing, software *
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT *
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the *
13  * License for the specific language governing permissions and limitations *
14  * under the License. *
15  * *
16  * ========================================================================== */

17 package com.sslexplorer.vfs.webdav;
18
19 import java.io.IOException JavaDoc;
20 import java.io.PrintWriter JavaDoc;
21
22 import com.sslexplorer.vfs.VFSResource;
23
24
25 /**
26  * <p>A {@link RuntimeException} representing a
27  * <a HREF="http://www.rfc-editor.org/rfc/rfc2518.txt">WebDAV</a>
28  * response for a specified {@link VFSResource}.</p>
29  *
30  * @author <a HREF="http://www.betaversion.org/~pier/">Pier Fumagalli</a>
31  */

32 public class DAVException extends RuntimeException JavaDoc {
33     
34     private VFSResource resource = null;
35     private int status = 0;
36
37     /**
38      * <p>Create a new {@link DAVException} instance.</p>
39      */

40     public DAVException(int status, String JavaDoc message) {
41         this(status, message, null, null);
42     }
43
44     /**
45      * <p>Create a new {@link DAVException} instance.</p>
46      */

47     public DAVException(int status, String JavaDoc message, Throwable JavaDoc throwable) {
48         this(status, message, throwable, null);
49     }
50
51     /**
52      * <p>Create a new {@link DAVException} instance.</p>
53      */

54     public DAVException(int status, String JavaDoc message, VFSResource resource) {
55         this(status, message, null, resource);
56     }
57
58     /**
59      * <p>Create a new {@link DAVException} instance.</p>
60      */

61     public DAVException(int s, String JavaDoc m, Throwable JavaDoc t, VFSResource r) {
62         super(m, t);
63         this.resource = r;
64         this.status = s;
65     }
66
67     /**
68      * <p>Return the status code associated with this instance.</p>
69      */

70     public int getStatus() {
71         return this.status;
72     }
73
74     /**
75      * <p>Return the {@link VFSResource} associated with this instance.</p>
76      */

77     public VFSResource getResource() {
78         return this.resource;
79     }
80
81     /**
82      * <p>Write the body of this {@link DAVException} to the specified
83      * {@link DAVTransaction}'s output.</p>
84      */

85     public void write(DAVTransaction transaction)
86     throws IOException JavaDoc {
87         transaction.setContentType("text/html; charset=\"utf-8\"");
88         transaction.setStatus(this.getStatus());
89
90         /* Prepare and log the error message */
91         String JavaDoc message = DAVUtilities.getStatusMessage(this.getStatus());
92         if (message == null) {
93             transaction.setStatus(500);
94             message = Integer.toString(this.getStatus()) + " Unknown";
95         }
96
97         /* Write the error message to the client */
98         PrintWriter JavaDoc out = transaction.write("utf-8");
99         out.println("<html>");
100         out.print("<head><title>Error ");
101         out.print(message);
102         out.println("</title></head>");
103         out.println("<body>");
104         out.print("<p><b>Error ");
105         out.print(message);
106         out.println("</b></p>");
107         
108         /* Check if we have a resource associated with the extension */
109         if (this.getResource() != null) {
110             String JavaDoc r = transaction.lookup(this.getResource()).toASCIIString();
111             out.print("<p>Resource in error: <a HREF=\"");
112             out.print(r);
113             out.println("\">");
114             out.print(r);
115             out.println("</a></p>");
116         }
117
118         /* Process any exception and its cause */
119         Throwable JavaDoc throwable = this;
120         out.println("<hr /><p>Exception details:</p>");
121         while (throwable != null) {
122             out.print("<pre>");
123             throwable.printStackTrace(out);
124             out.println("</pre>");
125             throwable = throwable.getCause();
126             if (throwable != null) out.println("<hr /><p>Caused by:</p>");
127         }
128
129         /* Close up the HTML */
130         out.println("</body>");
131         out.println("</html>");
132         out.flush();
133     }
134 }
135
Popular Tags