Search in sources :

Example 61 with SlingHttpServletRequest

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;
}
Also used : Locale(java.util.Locale) ResourceBundleProvider(org.apache.sling.i18n.ResourceBundleProvider) SlingScriptHelper(org.apache.sling.api.scripting.SlingScriptHelper) SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest)

Example 62 with SlingHttpServletRequest

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();
}
Also used : StringWriter(java.io.StringWriter) RuntimeObjectModel(org.apache.sling.scripting.sightly.render.RuntimeObjectModel) Map(java.util.Map) Bindings(javax.script.Bindings) SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest) PrintWriter(java.io.PrintWriter)

Example 63 with SlingHttpServletRequest

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);
    }
}
Also used : SightlyException(org.apache.sling.scripting.sightly.SightlyException) Resource(org.apache.sling.api.resource.Resource) SyntheticResource(org.apache.sling.api.resource.SyntheticResource) SyntheticResource(org.apache.sling.api.resource.SyntheticResource) SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest)

Example 64 with SlingHttpServletRequest

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);
        }
    }
}
Also used : SlingHttpServletResponse(org.apache.sling.api.SlingHttpServletResponse) RequestDispatcherOptions(org.apache.sling.api.request.RequestDispatcherOptions) SightlyException(org.apache.sling.scripting.sightly.SightlyException) SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest) RequestDispatcher(javax.servlet.RequestDispatcher) SightlyException(org.apache.sling.scripting.sightly.SightlyException)

Example 65 with SlingHttpServletRequest

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);
}
Also used : MockSlingHttpServletResponse(org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletResponse) SlingHttpServletResponse(org.apache.sling.api.SlingHttpServletResponse) MockSlingHttpServletRequest(org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest) MockSlingHttpServletResponse(org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletResponse) MockSlingHttpServletRequest(org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest) SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest)

Aggregations

SlingHttpServletRequest (org.apache.sling.api.SlingHttpServletRequest)98 Resource (org.apache.sling.api.resource.Resource)52 SlingHttpServletResponse (org.apache.sling.api.SlingHttpServletResponse)49 Test (org.junit.Test)48 Expectations (org.jmock.Expectations)32 RewriterResponse (org.apache.sling.security.impl.ContentDispositionFilter.RewriterResponse)31 ValueMap (org.apache.sling.api.resource.ValueMap)27 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)15 IOException (java.io.IOException)10 Bindings (javax.script.Bindings)10 SlingBindings (org.apache.sling.api.scripting.SlingBindings)9 ServletException (javax.servlet.ServletException)8 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)8 AbstractPipeTest (org.apache.sling.pipes.AbstractPipeTest)8 ContainerPipeTest (org.apache.sling.pipes.ContainerPipeTest)8 PrintWriter (java.io.PrintWriter)7 Map (java.util.Map)7 SlingScriptHelper (org.apache.sling.api.scripting.SlingScriptHelper)7 MockSlingHttpServlet3Request (org.apache.sling.servlets.post.impl.helper.MockSlingHttpServlet3Request)6 HttpServletResponse (javax.servlet.http.HttpServletResponse)5