1 package com.blandware.atleap.webapp.action.core.contentLocale; 2 3 import com.blandware.atleap.model.core.ContentLocale; 4 import com.blandware.atleap.webapp.struts.ContentTilesRequestProcessor; 5 import com.blandware.atleap.webapp.util.core.LocaleUtil; 6 import com.blandware.atleap.webapp.util.core.RequestUtil; 7 import com.blandware.atleap.webapp.util.core.WebappConstants; 8 import com.blandware.atleap.webapp.util.core.WebappUtil; 9 import com.blandware.atleap.common.Constants; 10 import org.apache.commons.validator.GenericValidator; 11 import org.apache.struts.tiles.ComponentContext; 12 import org.apache.struts.tiles.ControllerSupport; 13 14 import javax.servlet.ServletContext ; 15 import javax.servlet.http.HttpServletRequest ; 16 import javax.servlet.http.HttpServletResponse ; 17 import java.util.HashMap ; 18 import java.util.Iterator ; 19 import java.util.List ; 20 import java.util.Map ; 21 22 23 31 public final class LocaleListController extends ControllerSupport { 32 34 42 public void execute(ComponentContext tilesContext, HttpServletRequest request, HttpServletResponse response, ServletContext servletContext) 43 throws Exception { 44 List localesList = LocaleUtil.getInstance(servletContext).getActiveLocales(); 45 String processedUrl = (String ) request.getAttribute(ContentTilesRequestProcessor.PROCESSED_URL); 46 if ( processedUrl == null ) { 47 processedUrl = WebappUtil.findGlobalForwardConfig("admin", null, request).getPath(); 48 } 49 50 String anchor = null; 52 int sharp = processedUrl.indexOf("#"); 53 if ( sharp > 0 ) { 54 anchor = processedUrl.substring(sharp + 1); 55 processedUrl = processedUrl.substring(0, sharp); 56 } 57 58 String queryString = null; 60 int questionMark = processedUrl.indexOf("?"); 61 if ( questionMark > 0 ) { 62 queryString = processedUrl.substring(questionMark + 1); 63 processedUrl = processedUrl.substring(0, questionMark); 64 } 65 66 if ( !GenericValidator.isBlankOrNull(queryString) ) { 68 Map params = RequestUtil.getRequestParametersFromString(queryString); 69 params.remove("language"); 70 if ( !params.isEmpty() ) { 71 queryString = RequestUtil.createQueryStringFromMap(params, "&", true).toString(); 72 } else { 73 queryString = null; 74 } 75 } 76 77 Map urls = new HashMap (); 78 for ( Iterator i = localesList.iterator(); i.hasNext(); ) { 79 ContentLocale locale = (ContentLocale) i.next(); 80 String language = locale.getIdentifier(); 81 StringBuffer url = new StringBuffer (processedUrl); 82 83 String temp = new String (processedUrl); 84 85 int extDot = temp.lastIndexOf("."); 87 if ( extDot > 0 ) { 88 89 int suffixPos = extDot; 91 92 int fromIndex = 0; 94 String contextPath = request.getContextPath(); 95 if ( contextPath == null || contextPath.length() == 0 ) { 96 contextPath = "/"; 97 } 98 99 if ( WebappUtil.isAbsoluteURL(processedUrl) ) { 101 fromIndex = processedUrl.indexOf("://"); 103 if ( fromIndex < 0 ) { 104 fromIndex = processedUrl.indexOf(":") + 1; 105 } else { 106 fromIndex += 3; 107 } 108 } else { 109 if ( !processedUrl.startsWith(contextPath) ) { 110 url = new StringBuffer (contextPath).append(url); 111 suffixPos += contextPath.length(); 112 } 113 } 114 115 int insertPos; 117 if ("/".equals(contextPath)) { 118 insertPos = url.indexOf(contextPath, fromIndex); 119 } else { 120 insertPos = url.indexOf(contextPath, fromIndex) + contextPath.length(); 121 } 122 123 if ( url.indexOf(Constants.LOCALIZED_URI_PREFIX) != insertPos ) { 124 url.insert(insertPos, Constants.LOCALIZED_URI_PREFIX); 125 suffixPos += Constants.LOCALIZED_URI_PREFIX.length(); 126 } 127 128 temp = temp.substring(0, extDot); 129 130 int langDot = temp.lastIndexOf("."); 132 if ( langDot == extDot - 3 ) { 133 url.replace(langDot, suffixPos, "." + language); 134 } else { 135 url.insert(suffixPos, "." + language); 136 } 137 138 } 139 if ( !GenericValidator.isBlankOrNull(queryString) ) { 141 url.append("?").append(queryString); 142 } 143 144 if ( !GenericValidator.isBlankOrNull(anchor) ) { 146 url.append("#").append(anchor); 147 } 148 149 urls.put(locale, url.toString()); 150 } 151 152 request.setAttribute(WebappConstants.SWITCH_LOCALE_LINKS_COLLECTION_KEY, urls); 153 } 154 } 155 | Popular Tags |