use of org.apache.sling.api.SlingHttpServletRequest in project sling by apache.
the class I18nRuntimeExtension method get.
private String get(final Bindings bindings, String text, String locale, String basename, String hint) {
final SlingScriptHelper slingScriptHelper = BindingsUtils.getHelper(bindings);
final SlingHttpServletRequest request = BindingsUtils.getRequest(bindings);
final ResourceBundleProvider resourceBundleProvider = slingScriptHelper.getService(ResourceBundleProvider.class);
if (resourceBundleProvider != null) {
String key = text;
if (StringUtils.isNotEmpty(hint)) {
key += " ((" + hint + "))";
}
if (StringUtils.isEmpty(locale)) {
Enumeration<Locale> requestLocales = request.getLocales();
while (requestLocales.hasMoreElements()) {
Locale l = requestLocales.nextElement();
String translation = getTranslation(resourceBundleProvider, basename, key, l);
if (translation != null) {
return translation;
}
}
} else {
try {
Locale l = LocaleUtils.toLocale(locale);
String translation = getTranslation(resourceBundleProvider, basename, key, l);
if (translation != null) {
return translation;
}
} catch (IllegalArgumentException e) {
LOG.warn("Invalid locale detected: {}.", locale);
return text;
}
}
}
LOG.warn("No translation found for string '{}' using expression provided locale '{}' or default locale '{}'", text, locale, request.getLocale().getLanguage());
return text;
}
use of org.apache.sling.api.SlingHttpServletRequest in project sling by apache.
the class IncludeRuntimeExtension method call.
@Override
public Object call(final RenderContext renderContext, Object... arguments) {
ExtensionUtils.checkArgumentCount(RuntimeFunction.INCLUDE, arguments, 2);
RuntimeObjectModel runtimeObjectModel = renderContext.getObjectModel();
String originalPath = runtimeObjectModel.toString(arguments[0]);
Map options = (Map) arguments[1];
String path = buildPath(originalPath, options);
StringWriter output = new StringWriter();
final Bindings bindings = renderContext.getBindings();
SlingHttpServletRequest request = BindingsUtils.getRequest(bindings);
Map originalAttributes = ExtensionUtils.setRequestAttributes(request, (Map) options.remove(OPTION_REQUEST_ATTRIBUTES));
includeScript(bindings, path, new PrintWriter(output));
ExtensionUtils.setRequestAttributes(request, originalAttributes);
return output.toString();
}
use of org.apache.sling.api.SlingHttpServletRequest in project sling by apache.
the class ResourceRuntimeExtension method includeResource.
private void includeResource(final Bindings bindings, PrintWriter out, String path, String dispatcherOptions, String resourceType) {
if (StringUtils.isEmpty(path)) {
throw new SightlyException("Resource path cannot be empty");
} else {
SlingHttpServletRequest request = BindingsUtils.getRequest(bindings);
Resource includeRes = request.getResourceResolver().resolve(path);
if (ResourceUtil.isNonExistingResource(includeRes)) {
includeRes = new SyntheticResource(request.getResourceResolver(), path, resourceType);
}
includeResource(bindings, out, includeRes, dispatcherOptions, resourceType);
}
}
use of org.apache.sling.api.SlingHttpServletRequest in project sling by apache.
the class ResourceRuntimeExtension method includeResource.
private void includeResource(final Bindings bindings, PrintWriter out, Resource includeRes, String dispatcherOptions, String resourceType) {
if (includeRes == null) {
throw new SightlyException("Resource cannot be null");
} else {
SlingHttpServletResponse customResponse = new PrintWriterResponseWrapper(out, BindingsUtils.getResponse(bindings));
SlingHttpServletRequest request = BindingsUtils.getRequest(bindings);
RequestDispatcherOptions opts = new RequestDispatcherOptions(dispatcherOptions);
if (StringUtils.isNotEmpty(resourceType)) {
opts.setForceResourceType(resourceType);
}
RequestDispatcher dispatcher = request.getRequestDispatcher(includeRes, opts);
try {
if (dispatcher != null) {
dispatcher.include(request, customResponse);
} else {
throw new SightlyException("Failed to include resource " + includeRes.getPath());
}
} catch (Exception e) {
throw new SightlyException("Failed to include resource " + includeRes.getPath(), e);
}
}
}
use of org.apache.sling.api.SlingHttpServletRequest in project sling by apache.
the class MockSling method newSlingScriptHelper.
/**
* Creates a new sling script helper instance using
* {@link #DEFAULT_RESOURCERESOLVER_TYPE} for the resource resolver.
* @param bundleContext Bundle context
* @return Sling script helper instance
*/
public static SlingScriptHelper newSlingScriptHelper(BundleContext bundleContext) {
SlingHttpServletRequest request = new MockSlingHttpServletRequest(newResourceResolver(bundleContext), bundleContext);
SlingHttpServletResponse response = new MockSlingHttpServletResponse();
return newSlingScriptHelper(request, response, bundleContext);
}
Aggregations