Created
March 25, 2012 16:28
-
-
Save armhold/2198074 to your computer and use it in GitHub Desktop.
a Wicket Mapper that sets the locale based on the hostname of the request
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Licensed to the Apache Software Foundation (ASF) under one or more | |
* contributor license agreements. See the NOTICE file distributed with | |
* this work for additional information regarding copyright ownership. | |
* The ASF licenses this file to You under the Apache License, Version 2.0 | |
* (the "License"); you may not use this file except in compliance with | |
* the License. You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
*/ | |
package com.example; | |
import org.apache.wicket.examples.requestmapper.LocaleHelper; | |
import org.apache.wicket.Session; | |
import org.apache.wicket.request.IRequestHandler; | |
import org.apache.wicket.request.IRequestMapper; | |
import org.apache.wicket.request.Request; | |
import org.apache.wicket.request.Url; | |
import org.apache.wicket.request.mapper.AbstractComponentMapper; | |
import org.apache.wicket.util.string.Strings; | |
import java.util.Locale; | |
/** | |
* A modification of LocaleFirstMapper that uses the hostname of the Request to infer and set the user's locale. | |
* | |
* see: http://www.wicket-library.com/wicket-examples/mappers/en_US | |
* | |
* @author ivaynberg | |
* @author matej.knopp | |
* @author george armhold | |
*/ | |
public class LocaleFromHostnameMapper extends AbstractComponentMapper | |
{ | |
/** | |
* the base hostname, without any locale prefix, i.e. "example.com" | |
*/ | |
private String baseHostName; | |
private final IRequestMapper chain; | |
/** | |
* Construct. | |
* | |
* @param chain | |
*/ | |
public LocaleFromHostnameMapper(final IRequestMapper chain, String baseHostName) | |
{ | |
this.chain = chain; | |
this.baseHostName = baseHostName; | |
} | |
/** | |
* @see org.apache.wicket.request.IRequestMapper#getCompatibilityScore(org.apache.wicket.request.Request) | |
*/ | |
public int getCompatibilityScore(Request request) | |
{ | |
if (getLocaleFromUrl(request) != null) | |
{ | |
request = stripLocaleSegment(request); | |
} | |
// since we match all urls the score is simply delegated to the chain | |
return chain.getCompatibilityScore(request); | |
} | |
private Request stripLocaleSegment(Request request) | |
{ | |
Url url = request.getUrl(); | |
// url.getSegments().remove(0); | |
url.setHost(baseHostName); | |
return request.cloneWithUrl(url); | |
} | |
private Locale getLocaleFromUrl(Request request) | |
{ | |
String host = request.getClientUrl().getHost(); | |
if (host == null) host = ""; | |
int langEnd = host.indexOf("." + baseHostName); | |
if (langEnd > 0) | |
{ | |
String localeAsString = host.substring(0, langEnd); | |
if (!Strings.isEmpty(localeAsString)) | |
{ | |
return LocaleHelper.parseLocale(localeAsString); | |
} | |
} | |
return null; | |
} | |
/** | |
* @see org.apache.wicket.request.IRequestMapper#mapRequest(org.apache.wicket.request.Request) | |
*/ | |
public IRequestHandler mapRequest(Request request) | |
{ | |
Locale locale = getLocaleFromUrl(request); | |
if (locale != null) | |
{ | |
Session.get().setLocale(locale); | |
// now that we have proccessed the first segment we need to strip from the url | |
request = stripLocaleSegment(request); | |
} | |
// chain url processing | |
return chain.mapRequest(request); | |
} | |
/** | |
* @see org.apache.wicket.request.IRequestMapper#mapHandler(org.apache.wicket.request.IRequestHandler) | |
*/ | |
public Url mapHandler(IRequestHandler handler) | |
{ | |
// let the chain create the url | |
Url url = chain.mapHandler(handler); | |
if (url != null) | |
{ | |
Locale locale = Session.get().getLocale(); | |
if (locale == null) | |
{ | |
locale = Locale.US; | |
} | |
// url.getSegments().add(0, locale.toString()); | |
url.setHost(locale.getLanguage() + "." + baseHostName); | |
} | |
return url; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment