KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.net.URI JavaDoc;
22
23 import com.sslexplorer.vfs.VFSResource;
24
25
26 /**
27  * <p>A simple {@link DAVException} encapsulating an
28  * <a HREF="http://www.rfc-editor.org/rfc/rfc2616.txt">HTTP</a> redirection
29  * to a given {@link URI}.</p>
30  *
31  * @author <a HREF="http://www.betaversion.org/~pier/">Pier Fumagalli</a>
32  */

33 public class DAVRedirection extends DAVException {
34
35     private VFSResource location = null;
36
37     /**
38      * <p>Create a new {@link DAVRedirection} instance.</p>
39      */

40     public DAVRedirection(boolean permanent, VFSResource location) {
41         super(permanent? 301: 302, "Redirection requested");
42         this.location = location;
43     }
44     
45     /**
46      * <p>Return the target {@link URI} of the redirection.</p>
47      */

48     public VFSResource getLocation() {
49         return this.location;
50     }
51
52     /**
53      * <p>Write the body of this {@link DAVRedirection} to the specified
54      * {@link DAVTransaction}'s output.</p>
55      */

56     public void write(DAVTransaction transaction)
57     throws IOException JavaDoc {
58         transaction.setContentType("text/html; charset=\"utf-8\"");
59         transaction.setStatus(this.getStatus());
60
61         /* Write the error message to the client */
62         PrintWriter JavaDoc out = transaction.write("utf-8");
63         out.println("<html>");
64         out.print("<head><title>Redirection requested</title></head>");
65         out.println("<body>");
66         out.print("<p><b>The requested resource has moved ");
67         out.print(this.getStatus() == 301? "permanently": "temporarily");
68         out.println("</b></p>");
69
70         if(this.location != null) {
71             transaction.setHeader("Location", location.getFullURI().toASCIIString());
72             out.print("<p>The location for the requested resource is <a HREF=\"");
73             out.print("/fs" + location.getFullURI().toASCIIString());
74             out.println("\">");
75             out.print("/fs" + location.getFullPath());
76             out.println("</a></p>");
77         }
78         out.println("</body>");
79         out.println("</html>");
80         out.flush();
81     }
82 }
83
Popular Tags