Return-Path: Delivered-To: apmail-cocoon-cvs-archive@www.apache.org Received: (qmail 81521 invoked from network); 26 Sep 2005 09:13:03 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur.apache.org with SMTP; 26 Sep 2005 09:13:03 -0000 Received: (qmail 87626 invoked by uid 500); 26 Sep 2005 09:13:02 -0000 Delivered-To: apmail-cocoon-cvs-archive@cocoon.apache.org Received: (qmail 87595 invoked by uid 500); 26 Sep 2005 09:13:01 -0000 Mailing-List: contact cvs-help@cocoon.apache.org; run by ezmlm Precedence: bulk Reply-To: dev@cocoon.apache.org list-help: list-unsubscribe: List-Post: List-Id: Delivered-To: mailing list cvs@cocoon.apache.org Received: (qmail 87582 invoked by uid 99); 26 Sep 2005 09:13:01 -0000 X-ASF-Spam-Status: No, hits=-9.8 required=10.0 tests=ALL_TRUSTED,NO_REAL_NAME X-Spam-Check-By: apache.org Received: from [209.237.227.194] (HELO minotaur.apache.org) (209.237.227.194) by apache.org (qpsmtpd/0.29) with SMTP; Mon, 26 Sep 2005 02:13:00 -0700 Received: (qmail 81463 invoked by uid 65534); 26 Sep 2005 09:12:40 -0000 Message-ID: <20050926091240.81462.qmail@minotaur.apache.org> Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r291587 - in /cocoon/blocks/portal/trunk: WEB-INF/xconf/ java/org/apache/cocoon/portal/impl/ Date: Mon, 26 Sep 2005 09:12:39 -0000 To: cvs@cocoon.apache.org From: cziegeler@apache.org X-Mailer: svnmailer-1.0.5 X-Virus-Checked: Checked by ClamAV on apache.org X-Spam-Rating: minotaur.apache.org 1.6.2 0/1000/N Author: cziegeler Date: Mon Sep 26 02:12:31 2005 New Revision: 291587 URL: http://svn.apache.org/viewcvs?rev=291587&view=rev Log: Refactor link service: only create absolute urls if the procotol is changing Added: cocoon/blocks/portal/trunk/java/org/apache/cocoon/portal/impl/LinkInfo.java (with props) Modified: cocoon/blocks/portal/trunk/WEB-INF/xconf/cocoon-portal.xconf cocoon/blocks/portal/trunk/java/org/apache/cocoon/portal/impl/DefaultLinkService.java cocoon/blocks/portal/trunk/java/org/apache/cocoon/portal/impl/PageLabelLinkService.java Modified: cocoon/blocks/portal/trunk/WEB-INF/xconf/cocoon-portal.xconf URL: http://svn.apache.org/viewcvs/cocoon/blocks/portal/trunk/WEB-INF/xconf/cocoon-portal.xconf?rev=291587&r1=291586&r2=291587&view=diff ============================================================================== --- cocoon/blocks/portal/trunk/WEB-INF/xconf/cocoon-portal.xconf (original) +++ cocoon/blocks/portal/trunk/WEB-INF/xconf/cocoon-portal.xconf Mon Sep 26 02:12:31 2005 @@ -125,7 +125,12 @@ - + + + + Modified: cocoon/blocks/portal/trunk/java/org/apache/cocoon/portal/impl/DefaultLinkService.java URL: http://svn.apache.org/viewcvs/cocoon/blocks/portal/trunk/java/org/apache/cocoon/portal/impl/DefaultLinkService.java?rev=291587&r1=291586&r2=291587&view=diff ============================================================================== --- cocoon/blocks/portal/trunk/java/org/apache/cocoon/portal/impl/DefaultLinkService.java (original) +++ cocoon/blocks/portal/trunk/java/org/apache/cocoon/portal/impl/DefaultLinkService.java Mon Sep 26 02:12:31 2005 @@ -16,8 +16,6 @@ package org.apache.cocoon.portal.impl; import java.io.UnsupportedEncodingException; -import java.util.ArrayList; -import java.util.Enumeration; import java.util.Iterator; import java.util.List; @@ -26,19 +24,21 @@ import org.apache.avalon.framework.context.ContextException; import org.apache.avalon.framework.context.Contextualizable; import org.apache.avalon.framework.logger.AbstractLogEnabled; +import org.apache.avalon.framework.parameters.ParameterException; +import org.apache.avalon.framework.parameters.Parameterizable; +import org.apache.avalon.framework.parameters.Parameters; import org.apache.avalon.framework.service.ServiceException; import org.apache.avalon.framework.service.ServiceManager; import org.apache.avalon.framework.service.Serviceable; import org.apache.avalon.framework.thread.ThreadSafe; import org.apache.cocoon.components.ContextHelper; import org.apache.cocoon.environment.Request; -import org.apache.cocoon.environment.wrapper.RequestParameters; import org.apache.cocoon.portal.LinkService; import org.apache.cocoon.portal.event.ComparableEvent; +import org.apache.cocoon.portal.event.ConvertableEvent; import org.apache.cocoon.portal.event.Event; import org.apache.cocoon.portal.event.EventConverter; import org.apache.cocoon.portal.event.RequestEvent; -import org.apache.cocoon.portal.event.ConvertableEvent; import org.apache.cocoon.util.NetUtils; /** @@ -50,32 +50,13 @@ */ public class DefaultLinkService extends AbstractLogEnabled - implements ThreadSafe, LinkService, Serviceable, Disposable, Contextualizable { + implements LinkService, + ThreadSafe, + Serviceable, + Disposable, + Contextualizable, + Parameterizable { - /** - * Helper class containing the information about the request uri - */ - static class Info { - StringBuffer linkBase = new StringBuffer(); - boolean hasParameters = false; - ArrayList comparableEvents = new ArrayList(5); - - public String getBase(Boolean secure) { - // TODO - Does null always mean FALSE? - // it might be that the current request is also https, - // in that case we return https even if secure is FALSE. - if ( secure == null || secure.equals(Boolean.FALSE) ) { - return linkBase.toString(); - } - // FIXME - Perhaps there is a better way - String link = linkBase.toString(); - if ( link.startsWith("https") ) { - return link; - } - return "https" + linkBase.toString().substring(4); - } - } - /** The converter used to convert an event into a request parameter */ protected EventConverter converter; /** The service manager */ @@ -85,6 +66,11 @@ protected Boolean eventsMarshalled; + /** Default port used for http. */ + protected int defaultPort = 80; + /** Default port used for https. */ + protected int defaultSecurePort = 443; + /** * @see org.apache.avalon.framework.service.Serviceable#service(org.apache.avalon.framework.service.ServiceManager) */ @@ -94,33 +80,26 @@ } /** - * Return the current info for the request - * @return An Info object + * @see org.apache.avalon.framework.parameters.Parameterizable#parameterize(org.apache.avalon.framework.parameters.Parameters) */ - protected Info getInfo() { + public void parameterize(Parameters params) throws ParameterException { + this.defaultPort = params.getParameterAsInteger("defaultPort", this.defaultPort); + this.defaultSecurePort = params.getParameterAsInteger("defaultSecurePort", this.defaultSecurePort); + } + + /** + * Return the current info for the request. + * @return A LinkInfo object. + */ + protected LinkInfo getInfo() { final Request request = ContextHelper.getRequest( this.context ); - Info info = (Info)request.getAttribute(DefaultLinkService.class.getName()); + LinkInfo info = (LinkInfo)request.getAttribute(DefaultLinkService.class.getName()); if ( info == null ) { synchronized ( this ) { - info = (Info)request.getAttribute(DefaultLinkService.class.getName()); + info = (LinkInfo)request.getAttribute(DefaultLinkService.class.getName()); if ( info == null ) { - info = new Info(); + info = new LinkInfo(request, this.defaultPort, this.defaultSecurePort); request.setAttribute(DefaultLinkService.class.getName(), info); - info.linkBase.append(request.getScheme()); - info.linkBase.append("://"); - info.linkBase.append(request.getServerName()); - if ( request.getServerPort() != 80 ) { - info.linkBase.append(':'); - info.linkBase.append(request.getServerPort()); - } - if ( request.getContextPath().length() > 0 ) { - info.linkBase.append(request.getContextPath()); - } - info.linkBase.append('/'); - if ( request.getSitemapURIPrefix().length() > 0 ) { - info.linkBase.append(request.getSitemapURIPrefix()); - } - info.linkBase.append(request.getSitemapURI()); } } } @@ -155,9 +134,9 @@ if (event == null) { return this.getRefreshLinkURI(secure); } - final Info info = this.getInfo(); + final LinkInfo info = this.getInfo(); final StringBuffer buffer = new StringBuffer(info.getBase(secure)); - boolean hasParams = info.hasParameters; + boolean hasParams = info.hasParameters(); // add comparable events final boolean comparableEvent = event instanceof ComparableEvent; @@ -220,8 +199,8 @@ if (events == null || events.size() == 0) { return this.getRefreshLinkURI(secure); } - final Info info = this.getInfo(); - boolean hasParams = info.hasParameters; + final LinkInfo info = this.getInfo(); + boolean hasParams = info.hasParameters(); final StringBuffer buffer = new StringBuffer(info.getBase(secure)); // add comparable events @@ -283,7 +262,7 @@ StringBuffer value = new StringBuffer(""); String parameterName = processEvent(event, value); - final Info info = this.getInfo(); + final LinkInfo info = this.getInfo(); if (event instanceof ComparableEvent) { // search if we already have an event for this! final Iterator iter = info.comparableEvents.iterator(); @@ -305,47 +284,16 @@ * @see org.apache.cocoon.portal.LinkService#addParameterToLink(java.lang.String, java.lang.String) */ public void addParameterToLink(String name, String value) { - final Info info = this.getInfo(); - if ( info.hasParameters ) { - info.linkBase.append('&'); - } else { - info.linkBase.append('?'); - } - try { - info.linkBase.append(name).append('=').append(NetUtils.encode(value, "utf-8")); - } catch (UnsupportedEncodingException uee) { - // ignore this as utf-8 is always supported - } - info.hasParameters = true; + final LinkInfo info = this.getInfo(); + info.addParameterToBase(name, value); } /** * @see org.apache.cocoon.portal.LinkService#addUniqueParameterToLink(java.lang.String, java.lang.String) */ public void addUniqueParameterToLink(String name, String value) { - final Info info = this.getInfo(); - if ( info.hasParameters ) { - final int pos = info.linkBase.toString().indexOf("?"); - final String queryString = info.linkBase.substring(pos + 1); - final RequestParameters params = new RequestParameters(queryString); - if ( params.getParameter(name) != null ) { - // the parameter is available, so remove it - info.linkBase.delete(pos, info.linkBase.length() + 1); - info.hasParameters = false; - - Enumeration enumeration = params.getParameterNames(); - while ( enumeration.hasMoreElements() ) { - final String paramName = (String)enumeration.nextElement(); - if ( !paramName.equals(name) ) { - String[] values = params.getParameterValues(paramName); - for( int i = 0; i < values.length; i++ ) { - this.addParameterToLink(paramName, values[i]); - } - } - } - } - } - // the parameter is not available, so just add it + final LinkInfo info = this.getInfo(); + info.deleteParameterFromBase(name); this.addParameterToLink(name, value); } @@ -360,13 +308,13 @@ * @see org.apache.cocoon.portal.LinkService#getRefreshLinkURI(java.lang.Boolean) */ public String getRefreshLinkURI(Boolean secure) { - final Info info = this.getInfo(); + final LinkInfo info = this.getInfo(); final StringBuffer buffer = new StringBuffer(info.getBase(secure)); // add comparable events Iterator iter = info.comparableEvents.iterator(); - boolean hasParams = info.hasParameters; + boolean hasParams = info.hasParameters(); while (iter.hasNext()) { Object[] objects = (Object[])iter.next(); if ( hasParams ) { Added: cocoon/blocks/portal/trunk/java/org/apache/cocoon/portal/impl/LinkInfo.java URL: http://svn.apache.org/viewcvs/cocoon/blocks/portal/trunk/java/org/apache/cocoon/portal/impl/LinkInfo.java?rev=291587&view=auto ============================================================================== --- cocoon/blocks/portal/trunk/java/org/apache/cocoon/portal/impl/LinkInfo.java (added) +++ cocoon/blocks/portal/trunk/java/org/apache/cocoon/portal/impl/LinkInfo.java Mon Sep 26 02:12:31 2005 @@ -0,0 +1,150 @@ +/* + * Copyright 2005 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.cocoon.portal.impl; + +import java.io.UnsupportedEncodingException; +import java.util.ArrayList; +import java.util.Enumeration; + +import org.apache.cocoon.environment.Request; +import org.apache.cocoon.environment.wrapper.RequestParameters; +import org.apache.cocoon.util.NetUtils; +import org.apache.commons.lang.BooleanUtils; + +/** + * Helper class containing the information about common parts for each link + * that will be generated in the portal page. + * + * @version $Id$ + */ +public class LinkInfo { + + /** Link base contains the base url for the http protocol. */ + protected final String httpLinkBase; + protected final String secureLinkBase; + protected boolean hasParameters = false; + protected final ArrayList comparableEvents = new ArrayList(5); + protected final StringBuffer url = new StringBuffer(); + + /** Is the page called using https? */ + protected final boolean isSecure; + + public LinkInfo(Request request, int defaultPort, int defaultSecurePort) { + this.isSecure = request.getScheme().equals("https"); + // create relative url + String relativeURI = request.getSitemapURI(); + final int pos = relativeURI.lastIndexOf('/'); + if ( pos != -1 ) { + relativeURI = relativeURI.substring(pos+1); + } + + // do we need a protocol shift for link base? + if ( this.isSecure ) { + this.secureLinkBase = relativeURI; + this.httpLinkBase = this.getAbsoluteUrl(request, false, defaultPort); + } else { + httpLinkBase = relativeURI; + this.secureLinkBase = this.getAbsoluteUrl(request, true, defaultSecurePort); + } + } + + protected String getAbsoluteUrl(Request request, boolean useSecure, int port) { + final StringBuffer buffer = new StringBuffer(); + if ( useSecure ) { + buffer.append("https://"); + } else { + buffer.append("http://"); + } + buffer.append(request.getServerName()); + if ( ( useSecure && port != 443) + || ( !useSecure && port != 80 ) ) { + buffer.append(':'); + buffer.append(port); + } + if ( request.getContextPath().length() > 0 ) { + buffer.append(request.getContextPath()); + } + buffer.append('/'); + if ( request.getSitemapURIPrefix().length() > 0 ) { + buffer.append(request.getSitemapURIPrefix()); + } + buffer.append(request.getSitemapURI()); + return buffer.toString(); + } + + public String getBase(Boolean secure) { + // if no information is provided, we stay with the same protocol + if ( secure == null ) { + secure = BooleanUtils.toBooleanObject(this.isSecure); + } + if ( secure.booleanValue() ) { + return this.secureLinkBase + this.url.toString(); + } + return this.httpLinkBase + this.url.toString(); + } + + public LinkInfo appendToBase(String value) { + this.url.append(value); + return this; + } + + public LinkInfo appendToBase(char c) { + this.url.append(c); + return this; + } + + public void deleteParameterFromBase(String parameterName) { + if ( this.hasParameters ) { + final int pos = this.url.toString().indexOf("?"); + final String queryString = this.url.substring(pos + 1); + final RequestParameters params = new RequestParameters(queryString); + if ( params.getParameter(parameterName) != null ) { + // the parameter is available, so remove it + this.url.delete(pos, this.url.length() + 1); + this.hasParameters = false; + + Enumeration enumeration = params.getParameterNames(); + while ( enumeration.hasMoreElements() ) { + final String paramName = (String)enumeration.nextElement(); + if ( !paramName.equals(parameterName) ) { + String[] values = params.getParameterValues(paramName); + for( int i = 0; i < values.length; i++ ) { + this.addParameterToBase(paramName, values[i]); + } + } + } + } + } + } + + public void addParameterToBase(String name, String value) { + if ( this.hasParameters ) { + this.appendToBase('&'); + } else { + this.appendToBase('?'); + } + try { + this.appendToBase(name).appendToBase('=').appendToBase(NetUtils.encode(value, "utf-8")); + } catch (UnsupportedEncodingException uee) { + // ignore this as utf-8 is always supported + } + this.hasParameters = true; + } + + public boolean hasParameters() { + return this.hasParameters; + } +} \ No newline at end of file Propchange: cocoon/blocks/portal/trunk/java/org/apache/cocoon/portal/impl/LinkInfo.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: cocoon/blocks/portal/trunk/java/org/apache/cocoon/portal/impl/LinkInfo.java ------------------------------------------------------------------------------ svn:keywords = Id Modified: cocoon/blocks/portal/trunk/java/org/apache/cocoon/portal/impl/PageLabelLinkService.java URL: http://svn.apache.org/viewcvs/cocoon/blocks/portal/trunk/java/org/apache/cocoon/portal/impl/PageLabelLinkService.java?rev=291587&r1=291586&r2=291587&view=diff ============================================================================== --- cocoon/blocks/portal/trunk/java/org/apache/cocoon/portal/impl/PageLabelLinkService.java (original) +++ cocoon/blocks/portal/trunk/java/org/apache/cocoon/portal/impl/PageLabelLinkService.java Mon Sep 26 02:12:31 2005 @@ -91,8 +91,8 @@ getKey(item, key); if (this.labelManager.getPageLabelEvents(key.toString()) != null) { - final Info info = this.getInfo(); - boolean hasParams = info.hasParameters; + final LinkInfo info = this.getInfo(); + boolean hasParams = info.hasParameters(); final StringBuffer buffer = new StringBuffer(info.getBase(secure)); if (hasParams) { buffer.append('&'); @@ -141,9 +141,9 @@ } String requestParameterName = this.labelManager.getRequestParameterName(); - final Info info = this.getInfo(); + final LinkInfo info = this.getInfo(); final StringBuffer buffer = new StringBuffer(info.getBase(secure)); - boolean hasParams = info.hasParameters; + boolean hasParams = info.hasParameters(); Iterator iter = events.iterator(); StringBuffer value = new StringBuffer("");