001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.portal.impl;
018:
019: import java.io.UnsupportedEncodingException;
020: import java.util.ArrayList;
021: import java.util.Enumeration;
022:
023: import org.apache.cocoon.environment.Request;
024: import org.apache.cocoon.environment.wrapper.RequestParameters;
025: import org.apache.cocoon.util.NetUtils;
026: import org.apache.commons.lang.BooleanUtils;
027:
028: /**
029: * Helper class containing the information about common parts for each link
030: * that will be generated in the portal page.
031: *
032: * @version $Id: LinkInfo.java 433543 2006-08-22 06:22:54Z crossley $
033: */
034: public class LinkInfo {
035:
036: /** Link base contains the base url for the http protocol. */
037: protected final String httpLinkBase;
038: protected final String secureLinkBase;
039: protected boolean hasParameters = false;
040: protected final ArrayList comparableEvents = new ArrayList(5);
041: protected final StringBuffer url = new StringBuffer();
042: protected final Request request;
043:
044: /** Is the page called using https? */
045: protected final boolean isSecure;
046:
047: public LinkInfo(Request request, int defaultPort,
048: int defaultSecurePort) {
049: this .isSecure = request.getScheme().equals("https");
050: // create relative url
051: String relativeURI = getRelativeURI(request);
052:
053: this .request = request;
054: this .secureLinkBase = getSecureLinkBase(request, relativeURI,
055: defaultSecurePort);
056: this .httpLinkBase = getHttpLinkBase(request, relativeURI,
057: defaultPort);
058: }
059:
060: protected String getRelativeURI(Request request) {
061: String relativeURI = request.getSitemapURI();
062: final int pos = relativeURI.lastIndexOf('/');
063: if (pos != -1) {
064: relativeURI = relativeURI.substring(pos + 1);
065: }
066: return relativeURI;
067: }
068:
069: /**
070: * Return the base url for a secure http request
071: * @param relativeURI The current relative URI
072: * @param defaultPort The default port to use
073: * @return A String containing the base url for a secure http request
074: */
075: protected String getSecureLinkBase(Request request,
076: String relativeURI, int defaultPort) {
077: return (this .isSecure) ? relativeURI : getAbsoluteUrl(request,
078: true, defaultPort);
079: }
080:
081: /**
082: * Return the url for an http request
083: * @param relativeURI The current relative URI
084: * @param defaultPort The default port to use
085: * @return A string containing the base url for an http request
086: */
087: protected String getHttpLinkBase(Request request,
088: String relativeURI, int defaultPort) {
089: return (this .isSecure) ? getAbsoluteUrl(request, false,
090: defaultPort) : relativeURI;
091: }
092:
093: /**
094: * Return the absolute URL for a reqeust
095: * @param request The current Request
096: * @param useSecure true if the Request should be secure
097: * @param port The port to use
098: * @return A String containing the absolute url for a request
099: */
100: protected String getAbsoluteUrl(Request request, boolean useSecure,
101: int port) {
102: final StringBuffer buffer = new StringBuffer();
103: if (useSecure) {
104: buffer.append("https://");
105: } else {
106: buffer.append("http://");
107: }
108: buffer.append(request.getServerName());
109: if ((useSecure && port != 443) || (!useSecure && port != 80)) {
110: buffer.append(':');
111: buffer.append(port);
112: }
113: if (request.getContextPath().length() > 0) {
114: buffer.append(request.getContextPath());
115: }
116: buffer.append('/');
117: if (request.getSitemapURIPrefix().length() > 0) {
118: buffer.append(request.getSitemapURIPrefix());
119: }
120: buffer.append(request.getSitemapURI());
121: return buffer.toString();
122: }
123:
124: public String getBase(Boolean secure) {
125: // if no information is provided, we stay with the same protocol
126: if (secure == null) {
127: secure = BooleanUtils.toBooleanObject(this .isSecure);
128: }
129: if (secure.booleanValue()) {
130: return this .secureLinkBase + this .url.toString();
131: }
132: return this .httpLinkBase + this .url.toString();
133: }
134:
135: public LinkInfo appendToBase(String value) {
136: this .url.append(value);
137: return this ;
138: }
139:
140: public LinkInfo appendToBase(char c) {
141: this .url.append(c);
142: return this ;
143: }
144:
145: public void deleteParameterFromBase(String parameterName) {
146: if (this .hasParameters) {
147: final int pos = this .url.toString().indexOf("?");
148: final String queryString = this .url.substring(pos + 1);
149: final RequestParameters params = new RequestParameters(
150: queryString);
151: if (params.getParameter(parameterName) != null) {
152: // the parameter is available, so remove it
153: this .url.delete(pos, this .url.length() + 1);
154: this .hasParameters = false;
155:
156: Enumeration enumeration = params.getParameterNames();
157: while (enumeration.hasMoreElements()) {
158: final String paramName = (String) enumeration
159: .nextElement();
160: if (!paramName.equals(parameterName)) {
161: String[] values = params
162: .getParameterValues(paramName);
163: for (int i = 0; i < values.length; i++) {
164: this .addParameterToBase(paramName,
165: values[i]);
166: }
167: }
168: }
169: }
170: }
171: }
172:
173: public void addParameterToBase(String name, String value) {
174: if (this .hasParameters) {
175: this .appendToBase('&');
176: } else {
177: this .appendToBase('?');
178: }
179: try {
180: this .appendToBase(name).appendToBase('=').appendToBase(
181: NetUtils.encode(value, "utf-8"));
182: } catch (UnsupportedEncodingException uee) {
183: // ignore this as utf-8 is always supported
184: }
185: this .hasParameters = true;
186: }
187:
188: public boolean hasParameters() {
189: return this.hasParameters;
190: }
191: }
|