KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > blandware > atleap > webapp > action > core > contentLocale > LocaleListController


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 JavaDoc;
15 import javax.servlet.http.HttpServletRequest JavaDoc;
16 import javax.servlet.http.HttpServletResponse JavaDoc;
17 import java.util.HashMap JavaDoc;
18 import java.util.Iterator JavaDoc;
19 import java.util.List JavaDoc;
20 import java.util.Map JavaDoc;
21
22
23 /**
24  * <p>Locale list controller. This class is used to grab all locales, construct URLs for switching and put 'em to request
25  * </p>
26  * <p><a HREF="LocaleListController.java.htm"><i>View Source</i></a></p>
27  *
28  * @author Sergey Zubtcovskii <a HREF="mailto:sergey.zubtcovskii@blandware.com">&lt;sergey.zubtcovskii@blandware.com&gt;</a>
29  * @version $Revision: 1.16 $ $Date: 2005/08/25 09:02:30 $
30  */

31 public final class LocaleListController extends ControllerSupport {
32     //~ Methods ================================================================
33

34     /**
35      * Retrieves all locales and puts them into request
36      *
37      * @param tilesContext Current tile context
38      * @param request Current request
39      * @param response Current response
40      * @param servletContext Current Servlet Context
41      */

42     public void execute(ComponentContext tilesContext, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response, ServletContext JavaDoc servletContext)
43             throws Exception JavaDoc {
44         List JavaDoc localesList = LocaleUtil.getInstance(servletContext).getActiveLocales();
45         String JavaDoc processedUrl = (String JavaDoc) request.getAttribute(ContentTilesRequestProcessor.PROCESSED_URL);
46         if ( processedUrl == null ) {
47             processedUrl = WebappUtil.findGlobalForwardConfig("admin", null, request).getPath();
48         }
49
50         // detach anchor if any
51
String JavaDoc 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         // detach query string if any
59
String JavaDoc 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         // remove 'language' parameter if any
67
if ( !GenericValidator.isBlankOrNull(queryString) ) {
68             Map JavaDoc 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 JavaDoc urls = new HashMap JavaDoc();
78         for ( Iterator JavaDoc i = localesList.iterator(); i.hasNext(); ) {
79             ContentLocale locale = (ContentLocale) i.next();
80             String JavaDoc language = locale.getIdentifier();
81             StringBuffer JavaDoc url = new StringBuffer JavaDoc(processedUrl);
82
83             String JavaDoc temp = new String JavaDoc(processedUrl);
84
85             // dot before extension
86
int extDot = temp.lastIndexOf(".");
87             if ( extDot > 0 ) {
88
89                 // save suffix pos for later use
90
int suffixPos = extDot;
91
92                 // first insert an /rw prefix
93
int fromIndex = 0;
94                 String JavaDoc contextPath = request.getContextPath();
95                 if ( contextPath == null || contextPath.length() == 0 ) {
96                     contextPath = "/";
97                 }
98
99                 // we have a choice: processed URL is either absolute or context-relative
100
if ( WebappUtil.isAbsoluteURL(processedUrl) ) {
101                     // look up scheme
102
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 JavaDoc(contextPath).append(url);
111                         suffixPos += contextPath.length();
112                     }
113                 }
114
115                 // insert prefix if it does not exist
116
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                 // dot before language
131
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             // reattach query string
140
if ( !GenericValidator.isBlankOrNull(queryString) ) {
141                 url.append("?").append(queryString);
142             }
143
144             // reattach anchor
145
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