001/* 002 * Copyright 2013 Anyware Services 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.ametys.cms; 017 018import org.apache.avalon.framework.component.Component; 019import org.apache.avalon.framework.context.Context; 020import org.apache.avalon.framework.context.ContextException; 021import org.apache.avalon.framework.context.Contextualizable; 022import org.apache.cocoon.components.ContextHelper; 023import org.apache.cocoon.environment.Request; 024import org.apache.commons.lang.StringUtils; 025 026import org.ametys.cms.transformation.URIResolver; 027import org.ametys.runtime.workspace.WorkspaceMatcher; 028 029/** 030 * Component providing base paths for computing URIs. 031 */ 032public class URIPrefixHandler implements Contextualizable, Component 033{ 034 /** Avalon role */ 035 public static final String ROLE = URIPrefixHandler.class.getName(); 036 037 private Context _context; 038 039 @Override 040 public void contextualize(Context context) throws ContextException 041 { 042 _context = context; 043 } 044 045 /** 046 * Computes an URI prefix. Uses by various {@link URIResolver}. 047 * @param absolute if the resulting prefix should be absolute. 048 * @param internal if the resulting prefix should be internal. 049 * @return the computes prefix. 050 */ 051 public String computeUriPrefix(boolean absolute, boolean internal) 052 { 053 if (internal) 054 { 055 return "cocoon://"; 056 } 057 else if (absolute) 058 { 059 return getAbsoluteUriPrefix(); 060 } 061 else 062 { 063 return getUriPrefix(); 064 } 065 } 066 067 /** 068 * Get the application root. Can be empty if the application resides in the root context. <br> 069 * Use it to create a link beginning with the application root. 070 * @return The application root uri. 071 */ 072 public String getUriPrefix() 073 { 074 Request request = ContextHelper.getRequest(_context); 075 String prefix = (String) request.getAttribute(CmsConstants.PATH_PREFIX_ATTRIBUTE); 076 String workspaceURI = (String) request.getAttribute(WorkspaceMatcher.WORKSPACE_URI); 077 078 return request.getContextPath() + workspaceURI + StringUtils.trimToEmpty(prefix); 079 } 080 081 /** 082 * Get the absolutized version of the context path.<br> 083 * Use it to create an absolute link beginning with the application root, 084 * for instance when sending a mail linking to the application. 085 * @return The absolute context path. 086 */ 087 public String getAbsoluteUriPrefix() 088 { 089 Request request = ContextHelper.getRequest(_context); 090 091 String scheme = request.getScheme(); 092 093 int normalPort = scheme.equals("http") ? 80 : scheme.equals("https") ? 443 : -1; 094 int requestPort = request.getServerPort(); 095 096 String uriPrefix = getUriPrefix(); 097 uriPrefix = request.getScheme() + "://" + request.getServerName() + (requestPort != normalPort ? ":" + requestPort : "") + uriPrefix; 098 099 return uriPrefix; 100 } 101}