KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > pageflow > internal > DefaultURLRewriter


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * 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,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * $Header:$
17  */

18 package org.apache.beehive.netui.pageflow.internal;
19
20 import javax.servlet.ServletContext JavaDoc;
21 import javax.servlet.ServletRequest JavaDoc;
22 import javax.servlet.ServletResponse JavaDoc;
23 import javax.servlet.http.HttpServletRequest JavaDoc;
24
25 import org.apache.beehive.netui.core.urls.MutableURI;
26 import org.apache.beehive.netui.core.urls.URLRewriter;
27 import org.apache.beehive.netui.core.urls.URLType;
28 import org.apache.beehive.netui.pageflow.ServletContainerAdapter;
29 import org.apache.beehive.netui.pageflow.scoping.ScopedServletUtils;
30 import org.apache.beehive.netui.util.logging.Logger;
31
32
33 public class DefaultURLRewriter extends URLRewriter
34 {
35     private static final Logger _log = Logger.getInstance( DefaultURLRewriter.class );
36
37     public String JavaDoc getNamePrefix( ServletContext JavaDoc servletContext, ServletRequest JavaDoc request, String JavaDoc name )
38     {
39         return "";
40     }
41
42     public void rewriteURL( ServletContext JavaDoc servletContext, ServletRequest JavaDoc request,
43                             ServletResponse JavaDoc response, MutableURI url, URLType type, boolean needsToBeSecure )
44     {
45         ServletContainerAdapter servletContainerAdapter = AdapterManager.getServletContainerAdapter( servletContext );
46
47         // If url is not absolute, then do default secure/unsecure rewriting
48
if ( !url.isAbsolute() )
49         {
50             if ( needsToBeSecure )
51             {
52                 if ( !request.isSecure() )
53                 {
54                     int securePort = servletContainerAdapter.getSecureListenPort( (HttpServletRequest JavaDoc) request );
55
56                     if (securePort != -1)
57                     {
58                         internalRewriteUrl( url, "https", securePort, request.getServerName() );
59                     }
60                     else
61                     {
62                         if (_log.isWarnEnabled())
63                         {
64                             _log.warn("Could not rewrite URL " + url.getURIString( null ) + " to be secure because" +
65                                       " a secure port was not provided by the ServletContainerAdapter.");
66                         }
67                     }
68                 }
69             }
70             else
71             {
72                 if ( request.isSecure() )
73                 {
74                     int listenPort = servletContainerAdapter.getListenPort((HttpServletRequest JavaDoc) request);
75
76                     if (listenPort != -1 )
77                     {
78                         internalRewriteUrl( url, "http", listenPort, request.getServerName() );
79                     }
80                     else
81                     {
82                         if (_log.isWarnEnabled())
83                         {
84                             _log.warn("Could not rewrite URL " + url.getURIString( null ) + " to be non-secure" +
85                                       " because a port was not provided by the ServletContainerAdapter.");
86                         }
87                     }
88                 }
89             }
90         }
91
92         //
93
// If the current request has a special parameter that addresses a named 'scope',
94
// add the parameter to the URL.
95
//
96
String JavaDoc scopeID = request.getParameter( ScopedServletUtils.SCOPE_ID_PARAM );
97         if ( scopeID != null )
98         {
99             // check to see if the param is already there.
100
if ( url.getParameter( ScopedServletUtils.SCOPE_ID_PARAM ) == null )
101             {
102                 url.addParameter( ScopedServletUtils.SCOPE_ID_PARAM, scopeID, true );
103             }
104         }
105     }
106
107     private static void internalRewriteUrl( MutableURI url, String JavaDoc protocol, int port, String JavaDoc serverName )
108     {
109         // Need to build up the url
110
url.setScheme( protocol );
111         url.setHost( serverName );
112         url.setPort( port );
113     }
114
115     /**
116      * Determines if the passed-in Object is equivalent to this DefaultURLRewriter.
117      * Since there is no member data for this class they will all be equal.
118      *
119      * @param object the Object to test for equality.
120      * @return true if object is another instance of DefaultURLRewriter.
121      */

122     
123     public boolean equals( Object JavaDoc object )
124     {
125         if ( object == this ) { return true; }
126
127         if ( object == null || !object.getClass().equals( this.getClass() ) )
128         {
129             return false;
130         }
131
132         return true;
133     }
134
135     /**
136      * Returns a hash code value for the object.
137      * Implemented in conjunction with equals() override.
138      * Since there is no member data for this class we
139      * always return the same value.
140      *
141      * @return a hash code value for this object.
142      */

143     
144     public int hashCode()
145     {
146         return 0;
147     }
148 }
149
Popular Tags