/**
* Copyright 2006 by Know-Center, Graz, Austria
* PDF-AS has been contracted by the E-Government Innovation Center EGIZ, a
* joint initiative of the Federal Chancellery Austria and Graz University of
* Technology.
*
* Licensed under the EUPL, Version 1.1 or - as soon they will be approved by
* the European Commission - subsequent versions of the EUPL (the "Licence");
* You may not use this work except in compliance with the Licence.
* You may obtain a copy of the Licence at:
* http://www.osor.eu/eupl/
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the Licence is distributed on an "AS IS" basis,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the Licence for the specific language governing permissions and
* limitations under the Licence.
*
* This product combines work with different licenses. See the "NOTICE" text
* file for details on the various modules and licenses.
* The "NOTICE" text file is part of the distribution. Any derivative works
* that you distribute must include a readable copy of the "NOTICE" text file.
*/
package at.gv.egiz.pdfas.web.i18n;
import java.io.File;
import java.util.Locale;
import java.util.Properties;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.log4j.Logger;
import com.opensymphony.module.sitemesh.Config;
import com.opensymphony.module.sitemesh.Decorator;
import com.opensymphony.module.sitemesh.DecoratorMapper;
import com.opensymphony.module.sitemesh.Page;
import com.opensymphony.module.sitemesh.mapper.AbstractDecoratorMapper;
import com.opensymphony.module.sitemesh.mapper.DefaultDecorator;
public class LanguageDecoratorMapper extends AbstractDecoratorMapper {
private String decoratorParameter = null;
private static final Logger LOG = Logger.getLogger(LanguageDecoratorMapper.class);
public void init(Config config, Properties properties, DecoratorMapper parent) throws InstantiationException {
super.init(config, properties, parent);
}
public static Object getFirstNotNull(Object[] objects) {
if (objects == null) {
return null;
}
for (int i = 0; i < objects.length; i++) {
if (objects[i] != null) {
return objects[i];
}
}
return null;
}
public static Locale getLocale(HttpServletRequest request, String decoratorParameter) {
Locale locale = null;
HttpSession session = request.getSession();
LOG.trace("Looking for locale in session (ID=" + session.getId() + ").");
locale = (Locale) session.getAttribute(LocaleParamFilter.LOCALE_SESSION_KEY);
if (locale == null) {
LOG.debug("Unable to find locale in session. Trying to create new locale based on Accept-Language header entry.");
String acceptLanguage = request.getHeader("Accept-Language");
if (acceptLanguage != null) {
LOG.trace("Accept-Language header entry: " + acceptLanguage);
} else {
LOG.trace("No header entry found.");
}
if (acceptLanguage != null && acceptLanguage.length() >= 2) {
locale = new Locale(acceptLanguage.substring(0, 2));
LOG.debug("New Locale created: " + locale);
LOG.trace("Setting language to " + locale.getDisplayLanguage() + ".");
session.setAttribute(LocaleParamFilter.LOCALE_SESSION_KEY, locale);
}
} else {
LOG.trace("Locale found: " + locale);
}
return locale;
}
public Decorator getDecorator(HttpServletRequest request, Page page) {
LOG.trace("SiteMesh language resource decorator mapper invoked.");
try {
Decorator result = null;
final Decorator d = super.getDecorator(request, page);
Locale locale = getLocale(request, this.decoratorParameter);
String path;
if (locale != null) {
path = modifyPath(d.getPage(), locale.getLanguage());
} else {
path = d.getPage();
}
File decFile = new File(config.getServletContext().getRealPath(path));
LOG.trace("Looking for decorator \"" + path + "\".");
if (decFile.isFile()) {
result = new DefaultDecorator(d.getName(), path, null) {
public String getInitParameter(String paramName) {
return d.getInitParameter(paramName);
}
};
}
if (result != null) {
LOG.trace("Decorator found (\"" + result.getName() + "\", \"" + getFirstNotNull(new Object[] { result.getURIPath(), result.getPage(), ""}) + "\").");
return result;
} else {
LOG.trace("No decorator found. Delegating to super class.");
result = super.getDecorator(request, page);
if (result != null) {
LOG.trace("Super class returned decorator (\"" + result.getName() + "\", \"" + getFirstNotNull(new Object[] { result.getURIPath(), result.getPage(), ""}) + "\").");
} else {
LOG.trace("Super class did not return a decorator.");
}
return result;
}
} catch (NullPointerException e) {
return super.getDecorator(request, page);
}
}
private static String modifyPath(String path, String ext) {
if (ext == null || ext.length() == 0) {
return path;
}
int dot = path.lastIndexOf('.');
if (dot > -1) {
return path.substring(0, dot) + '-' + ext + path.substring(dot);
} else {
return path + '-' + ext;
}
}
}