1 16 package org.apache.cocoon.portal.impl; 17 18 import java.io.UnsupportedEncodingException ; 19 import java.util.ArrayList ; 20 import java.util.Enumeration ; 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 33 public class LinkInfo { 34 35 36 protected final String httpLinkBase; 37 protected final String secureLinkBase; 38 protected boolean hasParameters = false; 39 protected final ArrayList comparableEvents = new ArrayList (5); 40 protected final StringBuffer url = new StringBuffer (); 41 42 43 protected final boolean isSecure; 44 45 public LinkInfo(Request request, int defaultPort, int defaultSecurePort) { 46 this.isSecure = request.getScheme().equals("https"); 47 String relativeURI = request.getSitemapURI(); 49 final int pos = relativeURI.lastIndexOf('/'); 50 if ( pos != -1 ) { 51 relativeURI = relativeURI.substring(pos+1); 52 } 53 54 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 getAbsoluteUrl(Request request, boolean useSecure, int port) { 65 final StringBuffer buffer = new StringBuffer (); 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 getBase(Boolean secure) { 89 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 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 parameterName) { 110 if ( this.hasParameters ) { 111 final int pos = this.url.toString().indexOf("?"); 112 final String queryString = this.url.substring(pos + 1); 113 final RequestParameters params = new RequestParameters(queryString); 114 if ( params.getParameter(parameterName) != null ) { 115 this.url.delete(pos, this.url.length() + 1); 117 this.hasParameters = false; 118 119 Enumeration enumeration = params.getParameterNames(); 120 while ( enumeration.hasMoreElements() ) { 121 final String paramName = (String )enumeration.nextElement(); 122 if ( !paramName.equals(parameterName) ) { 123 String [] 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 name, String 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 uee) { 142 } 144 this.hasParameters = true; 145 } 146 147 public boolean hasParameters() { 148 return this.hasParameters; 149 } 150 } | Popular Tags |