You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by cz...@apache.org on 2005/09/26 11:02:46 UTC
svn commit: r291586 - in /cocoon/branches/BRANCH_2_1_X/src/blocks/portal:
conf/ java/org/apache/cocoon/portal/impl/
Author: cziegeler
Date: Mon Sep 26 02:02:39 2005
New Revision: 291586
URL: http://svn.apache.org/viewcvs?rev=291586&view=rev
Log:
Refactor link service: only create absolute urls if the procotol is changing
Added:
cocoon/branches/BRANCH_2_1_X/src/blocks/portal/java/org/apache/cocoon/portal/impl/LinkInfo.java (with props)
Modified:
cocoon/branches/BRANCH_2_1_X/src/blocks/portal/conf/portal.xconf
cocoon/branches/BRANCH_2_1_X/src/blocks/portal/java/org/apache/cocoon/portal/impl/DefaultLinkService.java
cocoon/branches/BRANCH_2_1_X/src/blocks/portal/java/org/apache/cocoon/portal/impl/PageLabelLinkService.java
Modified: cocoon/branches/BRANCH_2_1_X/src/blocks/portal/conf/portal.xconf
URL: http://svn.apache.org/viewcvs/cocoon/branches/BRANCH_2_1_X/src/blocks/portal/conf/portal.xconf?rev=291586&r1=291585&r2=291586&view=diff
==============================================================================
--- cocoon/branches/BRANCH_2_1_X/src/blocks/portal/conf/portal.xconf (original)
+++ cocoon/branches/BRANCH_2_1_X/src/blocks/portal/conf/portal.xconf Mon Sep 26 02:02:39 2005
@@ -116,7 +116,12 @@
</component>
<!-- To enable PageLabels change DefaultLinkService to PageLabelLinkService -->
- <component class="org.apache.cocoon.portal.impl.DefaultLinkService" logger="portal" role="org.apache.cocoon.portal.LinkService"/>
+ <component class="org.apache.cocoon.portal.impl.DefaultLinkService"
+ logger="portal"
+ role="org.apache.cocoon.portal.LinkService">
+ <parameter name="defaultPort" value="8888"/>
+ <parameter name="defaultSecurePort" value="443"/>
+ </component>
<!-- To enable PageLabels change DefaultEventConverter to PageLabelEventConverter -->
<component class="org.apache.cocoon.portal.event.impl.DefaultEventConverter" logger="portal" role="org.apache.cocoon.portal.event.EventConverter">
Modified: cocoon/branches/BRANCH_2_1_X/src/blocks/portal/java/org/apache/cocoon/portal/impl/DefaultLinkService.java
URL: http://svn.apache.org/viewcvs/cocoon/branches/BRANCH_2_1_X/src/blocks/portal/java/org/apache/cocoon/portal/impl/DefaultLinkService.java?rev=291586&r1=291585&r2=291586&view=diff
==============================================================================
--- cocoon/branches/BRANCH_2_1_X/src/blocks/portal/java/org/apache/cocoon/portal/impl/DefaultLinkService.java (original)
+++ cocoon/branches/BRANCH_2_1_X/src/blocks/portal/java/org/apache/cocoon/portal/impl/DefaultLinkService.java Mon Sep 26 02:02:39 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,31 +50,12 @@
*/
public class DefaultLinkService
extends AbstractLogEnabled
- implements ThreadSafe, LinkService, Serviceable, Disposable, Contextualizable {
-
- /**
- * 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);
- }
- }
+ implements LinkService,
+ ThreadSafe,
+ Serviceable,
+ Disposable,
+ Contextualizable,
+ Parameterizable {
/** The converter used to convert an event into a request parameter */
protected EventConverter converter;
@@ -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());
}
}
}
@@ -135,7 +114,7 @@
}
/**
- * @see org.apache.cocoon.portal.LinkService#encodeURL(String url).
+ * @see org.apache.cocoon.portal.LinkService#encodeURL(String url)
*/
public String encodeURL(String url) {
return ContextHelper.getResponse(this.context).encodeURL(url);
@@ -149,15 +128,15 @@
}
/**
- * @see org.apache.cocoon.portal.LinkService#getLinkURI(org.apache.cocoon.portal.event.Event, boolean)
+ * @see org.apache.cocoon.portal.LinkService#getLinkURI(org.apache.cocoon.portal.event.Event, Boolean)
*/
public String getLinkURI(Event event, Boolean secure) {
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/branches/BRANCH_2_1_X/src/blocks/portal/java/org/apache/cocoon/portal/impl/LinkInfo.java
URL: http://svn.apache.org/viewcvs/cocoon/branches/BRANCH_2_1_X/src/blocks/portal/java/org/apache/cocoon/portal/impl/LinkInfo.java?rev=291586&view=auto
==============================================================================
--- cocoon/branches/BRANCH_2_1_X/src/blocks/portal/java/org/apache/cocoon/portal/impl/LinkInfo.java (added)
+++ cocoon/branches/BRANCH_2_1_X/src/blocks/portal/java/org/apache/cocoon/portal/impl/LinkInfo.java Mon Sep 26 02:02:39 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/branches/BRANCH_2_1_X/src/blocks/portal/java/org/apache/cocoon/portal/impl/LinkInfo.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: cocoon/branches/BRANCH_2_1_X/src/blocks/portal/java/org/apache/cocoon/portal/impl/LinkInfo.java
------------------------------------------------------------------------------
svn:keywords = Id
Modified: cocoon/branches/BRANCH_2_1_X/src/blocks/portal/java/org/apache/cocoon/portal/impl/PageLabelLinkService.java
URL: http://svn.apache.org/viewcvs/cocoon/branches/BRANCH_2_1_X/src/blocks/portal/java/org/apache/cocoon/portal/impl/PageLabelLinkService.java?rev=291586&r1=291585&r2=291586&view=diff
==============================================================================
--- cocoon/branches/BRANCH_2_1_X/src/blocks/portal/java/org/apache/cocoon/portal/impl/PageLabelLinkService.java (original)
+++ cocoon/branches/BRANCH_2_1_X/src/blocks/portal/java/org/apache/cocoon/portal/impl/PageLabelLinkService.java Mon Sep 26 02:02:39 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("");