use of org.apache.wicket.resource.loader.IStringResourceLoader in project wicket by apache.
the class Localizer method getStringIgnoreSettings.
/**
* This is similar to {@link #getString(String, Component, IModel, String)} except that the
* resource settings are ignored. This allows to to code something like
*
* <pre>
* String option = getLocalizer().getStringIgnoreSettings(getId() + ".null", this, "");
* if (Strings.isEmpty(option))
* {
* option = getLocalizer().getString("null", this, CHOOSE_ONE);
* }
* </pre>
*
* @param key
* The key to obtain the resource for
* @param component
* The component to get the resource for (optional)
* @param model
* The model to use for substitutions in the strings (optional)
* @param locale
* If != null, it'll supersede the component's locale
* @param style
* If != null, it'll supersede the component's style
* @param defaultValue
* The default value (optional)
* @return The string resource
*/
public String getStringIgnoreSettings(final String key, final Component component, final IModel<?> model, Locale locale, String style, final String defaultValue) {
boolean addedToPage = false;
if (component != null) {
if ((component instanceof Page) || (null != component.findParent(Page.class))) {
addedToPage = true;
}
if (!addedToPage && log.isWarnEnabled()) {
log.warn("Tried to retrieve a localized string for a component that has not yet been added to the page. " + "This can sometimes lead to an invalid or no localized resource returned. " + "Make sure you are not calling Component#getString() inside your Component's constructor. " + "Offending component: {}", component);
}
}
String cacheKey = null;
String value;
// Make sure locale, style and variation have the right values
String variation = (component != null ? component.getVariation() : null);
if ((locale == null) && (component != null)) {
locale = component.getLocale();
}
if (locale == null) {
locale = Session.exists() ? Session.get().getLocale() : Locale.getDefault();
}
if ((style == null) && (component != null)) {
style = component.getStyle();
}
if (style == null) {
style = Session.exists() ? Session.get().getStyle() : null;
}
// cache as we can generate an invalid cache key
if ((cache != null) && ((component == null) || addedToPage)) {
cacheKey = getCacheKey(key, component, locale, style, variation);
}
// Value not found are cached as well (value = null)
if ((cacheKey != null) && cache.containsKey(cacheKey)) {
value = getFromCache(cacheKey);
if (log.isDebugEnabled()) {
log.debug("Property found in cache: '" + key + "'; Component: '" + (component != null ? component.toString(false) : null) + "'; value: '" + value + '\'');
}
} else {
if (log.isDebugEnabled()) {
log.debug("Locate property: key: '" + key + "'; Component: '" + (component != null ? component.toString(false) : null) + '\'');
}
// Iterate over all registered string resource loaders until the property has been found
Iterator<IStringResourceLoader> iter = getStringResourceLoaders().iterator();
value = null;
while (iter.hasNext() && (value == null)) {
IStringResourceLoader loader = iter.next();
value = loader.loadStringResource(component, key, locale, style, variation);
}
// Cache the result incl null if not found
if (cacheKey != null) {
putIntoCache(cacheKey, value);
}
if ((value == null) && log.isDebugEnabled()) {
log.debug("Property not found; key: '" + key + "'; Component: '" + (component != null ? component.toString(false) : null) + '\'');
}
}
if (value == null) {
value = defaultValue;
}
// then replace the placeholder and we are done
if (value != null) {
return substitutePropertyExpressions(component, value, model);
}
return null;
}
use of org.apache.wicket.resource.loader.IStringResourceLoader in project wicket by apache.
the class ApplicationSettingsTest method testOverrideStringResourceLoaderSetup.
/**
*/
@Test
public void testOverrideStringResourceLoaderSetup() {
ResourceSettings settings = new ResourceSettings(new MockApplication());
settings.getStringResourceLoaders().clear();
settings.getStringResourceLoaders().add(new BundleStringResourceLoader("org.apache.wicket.resource.DummyResources"));
settings.getStringResourceLoaders().add(new ComponentStringResourceLoader());
List<IStringResourceLoader> loaders = settings.getStringResourceLoaders();
Assert.assertEquals("There should be 2 overridden loaders", 2, loaders.size());
Assert.assertTrue("First loader one should be the bundle one", loaders.get(0) instanceof BundleStringResourceLoader);
Assert.assertTrue("Second loader should be the component one", loaders.get(1) instanceof ComponentStringResourceLoader);
}
use of org.apache.wicket.resource.loader.IStringResourceLoader in project wicket by apache.
the class BundleStringResourceLoaderTest method loaderUnknownResources.
/**
* @see org.apache.wicket.resource.StringResourceLoaderTestBase#testLoaderUnknownResources()
*/
@Override
@Test
public void loaderUnknownResources() {
IStringResourceLoader loader = new BundleStringResourceLoader("unknown.resource");
assertNull("Unknown resource should return null", loader.loadStringResource(component.getClass(), "test.string", Locale.getDefault(), null, null));
}
use of org.apache.wicket.resource.loader.IStringResourceLoader in project wicket by apache.
the class ComponentStringResourceLoaderTest method searchClassHierarchyFromPage.
/**
*/
@Test
public void searchClassHierarchyFromPage() {
DummySubClassPage p = new DummySubClassPage();
IStringResourceLoader loader = new ComponentStringResourceLoader();
assertEquals("Valid resource string should be found", "SubClass Test String", loader.loadStringResource(p.getClass(), "subclass.test.string", Locale.getDefault(), null, null));
assertEquals("Valid resource string should be found", "Another string", loader.loadStringResource(p.getClass(), "another.test.string", Locale.getDefault(), null, null));
}
use of org.apache.wicket.resource.loader.IStringResourceLoader in project midpoint by Evolveum.
the class MidPointApplication method init.
@Override
public void init() {
super.init();
getCspSettings().blocking().disabled();
getJavaScriptLibrarySettings().setJQueryReference(new PackageResourceReference(MidPointApplication.class, // todo no jquery.js is found
"../../../../../webjars/AdminLTE/2.4.18/bower_components/jquery/dist/jquery.min.js"));
getComponentInstantiationListeners().add(new SpringComponentInjector(this, applicationContext, true));
systemConfigurationChangeDispatcher.registerListener(new DeploymentInformationChangeListener(this));
SystemConfigurationType config = getSystemConfigurationIfAvailable();
if (config != null) {
deploymentInfo = config.getDeploymentInformation();
}
ResourceSettings resourceSettings = getResourceSettings();
resourceSettings.setParentFolderPlaceholder("$-$");
resourceSettings.setHeaderItemComparator(new PriorityFirstComparator(true));
SecurePackageResourceGuard guard = (SecurePackageResourceGuard) resourceSettings.getPackageResourceGuard();
guard.addPattern("+*.woff2");
List<IStringResourceLoader> resourceLoaders = resourceSettings.getStringResourceLoaders();
resourceLoaders.add(0, new MidPointStringResourceLoader(localizationService));
IResourceStreamLocator locator = new CachingResourceStreamLocator(new MidPointResourceStreamLocator(resourceSettings.getResourceFinders()));
resourceSettings.setResourceStreamLocator(locator);
resourceSettings.setThrowExceptionOnMissingResource(false);
getMarkupSettings().setStripWicketTags(true);
getMarkupSettings().setStripComments(true);
if (RuntimeConfigurationType.DEVELOPMENT.equals(getConfigurationType())) {
getDebugSettings().setAjaxDebugModeEnabled(true);
getDebugSettings().setDevelopmentUtilitiesEnabled(true);
initializeDevelopmentSerializers();
mount(new MountedMapper("/inspector", InspectorPage.class, new PageParametersEncoder()));
mount(new MountedMapper("/liveSession", LiveSessionsPage.class, new PageParametersEncoder()));
mount(new MountedMapper("/pageStore", PageStorePage.class, new PageParametersEncoder()));
}
// pretty url for resources (e.g. images)
mountFiles(ImgResources.BASE_PATH, ImgResources.class);
// exception handling an error pages
ApplicationSettings appSettings = getApplicationSettings();
appSettings.setAccessDeniedPage(PageError401.class);
appSettings.setInternalErrorPage(PageError.class);
appSettings.setPageExpiredErrorPage(PageError.class);
mount(new MountedMapper(MOUNT_INTERNAL_SERVER_ERROR, PageError.class, new PageParametersEncoder()));
mount(new MountedMapper(MOUNT_UNAUTHORIZED_ERROR, PageError401.class, new PageParametersEncoder()));
mount(new MountedMapper(MOUNT_FORBIDEN_ERROR, PageError403.class, new PageParametersEncoder()));
mount(new MountedMapper(MOUNT_NOT_FOUND_ERROR, PageError404.class, new PageParametersEncoder()));
mount(new MountedMapper(MOUNT_GONE_ERROR, PageError410.class, new PageParametersEncoder()));
getRequestCycleListeners().add(new LoggingRequestCycleListener(this));
getAjaxRequestTargetListeners().add(new AjaxRequestTarget.IListener() {
@Override
public void updateAjaxAttributes(AbstractDefaultAjaxBehavior behavior, AjaxRequestAttributes attributes) {
// check whether behavior will use POST method, if not then don't put CSRF token there
if (!isPostMethodTypeBehavior(behavior, attributes)) {
return;
}
CsrfToken csrfToken = SecurityUtils.getCsrfToken();
if (csrfToken == null) {
return;
}
String parameterName = csrfToken.getParameterName();
String value = csrfToken.getToken();
attributes.getExtraParameters().put(parameterName, value);
}
});
getSessionListeners().add((ISessionListener) asyncWebProcessManager);
// descriptor loader, used for customization
new PageMounter().loadData(this);
descriptorLoader.loadData();
if (applicationContext != null) {
Map<String, MidPointApplicationConfiguration> map = applicationContext.getBeansOfType(MidPointApplicationConfiguration.class);
if (map != null) {
map.forEach((key, value) -> value.init(this));
}
}
// for schrodinger selenide library
initializeSchrodinger();
ServletContext servletContext = getServletContext();
if (servletContext != null) {
taskManager.setWebContextPath(servletContext.getContextPath());
}
}
Aggregations