KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > portal > impl > LinkInfo


1 /*
2  * Copyright 2005 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 package org.apache.cocoon.portal.impl;
17
18 import java.io.UnsupportedEncodingException JavaDoc;
19 import java.util.ArrayList JavaDoc;
20 import java.util.Enumeration JavaDoc;
21
22 import org.apache.cocoon.environment.Request;
23 import org.apache.cocoon.environment.wrapper.RequestParameters;
24 import org.apache.cocoon.util.NetUtils;
25 import org.apache.commons.lang.BooleanUtils;
26
27 /**
28  * Helper class containing the information about common parts for each link
29  * that will be generated in the portal page.
30  *
31  * @version $Id: LinkInfo.java 291586 2005-09-26 09:02:39Z cziegeler $
32  */

33 public class LinkInfo {
34
35     /** Link base contains the base url for the http protocol. */
36     protected final String JavaDoc httpLinkBase;
37     protected final String JavaDoc secureLinkBase;
38     protected boolean hasParameters = false;
39     protected final ArrayList JavaDoc comparableEvents = new ArrayList JavaDoc(5);
40     protected final StringBuffer JavaDoc url = new StringBuffer JavaDoc();
41
42     /** Is the page called using https? */
43     protected final boolean isSecure;
44
45     public LinkInfo(Request request, int defaultPort, int defaultSecurePort) {
46         this.isSecure = request.getScheme().equals("https");
47         // create relative url
48
String JavaDoc relativeURI = request.getSitemapURI();
49         final int pos = relativeURI.lastIndexOf('/');
50         if ( pos != -1 ) {
51             relativeURI = relativeURI.substring(pos+1);
52         }
53
54         // do we need a protocol shift for link base?
55
if ( this.isSecure ) {
56             this.secureLinkBase = relativeURI;
57             this.httpLinkBase = this.getAbsoluteUrl(request, false, defaultPort);
58         } else {
59             httpLinkBase = relativeURI;
60             this.secureLinkBase = this.getAbsoluteUrl(request, true, defaultSecurePort);
61         }
62     }
63
64     protected String JavaDoc getAbsoluteUrl(Request request, boolean useSecure, int port) {
65         final StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
66         if ( useSecure ) {
67             buffer.append("https://");
68         } else {
69             buffer.append("http://");
70         }
71         buffer.append(request.getServerName());
72         if ( ( useSecure && port != 443)
73            || ( !useSecure && port != 80 ) ) {
74             buffer.append(':');
75             buffer.append(port);
76         }
77         if ( request.getContextPath().length() > 0 ) {
78             buffer.append(request.getContextPath());
79         }
80         buffer.append('/');
81         if ( request.getSitemapURIPrefix().length() > 0 ) {
82             buffer.append(request.getSitemapURIPrefix());
83         }
84         buffer.append(request.getSitemapURI());
85         return buffer.toString();
86     }
87         
88     public String JavaDoc getBase(Boolean JavaDoc secure) {
89         // if no information is provided, we stay with the same protocol
90
if ( secure == null ) {
91             secure = BooleanUtils.toBooleanObject(this.isSecure);
92         }
93         if ( secure.booleanValue() ) {
94             return this.secureLinkBase + this.url.toString();
95         }
96         return this.httpLinkBase + this.url.toString();
97     }
98
99     public LinkInfo appendToBase(String JavaDoc value) {
100         this.url.append(value);
101         return this;
102     }
103
104     public LinkInfo appendToBase(char c) {
105         this.url.append(c);
106         return this;
107     }
108
109     public void deleteParameterFromBase(String JavaDoc parameterName) {
110         if ( this.hasParameters ) {
111             final int pos = this.url.toString().indexOf("?");
112             final String JavaDoc queryString = this.url.substring(pos + 1);
113             final RequestParameters params = new RequestParameters(queryString);
114             if ( params.getParameter(parameterName) != null ) {
115                 // the parameter is available, so remove it
116
this.url.delete(pos, this.url.length() + 1);
117                 this.hasParameters = false;
118                 
119                 Enumeration JavaDoc enumeration = params.getParameterNames();
120                 while ( enumeration.hasMoreElements() ) {
121                     final String JavaDoc paramName = (String JavaDoc)enumeration.nextElement();
122                     if ( !paramName.equals(parameterName) ) {
123                         String JavaDoc[] values = params.getParameterValues(paramName);
124                         for( int i = 0; i < values.length; i++ ) {
125                             this.addParameterToBase(paramName, values[i]);
126                         }
127                     }
128                 }
129             }
130         }
131     }
132
133     public void addParameterToBase(String JavaDoc name, String JavaDoc value) {
134         if ( this.hasParameters ) {
135             this.appendToBase('&');
136         } else {
137             this.appendToBase('?');
138         }
139         try {
140             this.appendToBase(name).appendToBase('=').appendToBase(NetUtils.encode(value, "utf-8"));
141         } catch (UnsupportedEncodingException JavaDoc uee) {
142             // ignore this as utf-8 is always supported
143
}
144         this.hasParameters = true;
145     }
146
147     public boolean hasParameters() {
148         return this.hasParameters;
149     }
150 }
Popular Tags