Example usage for javax.servlet.jsp.jstl.core Config set

List of usage examples for javax.servlet.jsp.jstl.core Config set

Introduction

In this page you can find the example usage for javax.servlet.jsp.jstl.core Config set.

Prototype

public static void set(ServletContext context, String name, Object value) 

Source Link

Document

Sets the value of a configuration variable in the "application" scope.

Usage

From source file:com.jslsolucoes.tagria.lib.servlet.Tagria.java

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    String uri = request.getRequestURI().replaceAll(";jsessionid=.*", "");
    String etag = DigestUtils.sha256Hex(uri);

    if (uri.endsWith("blank")) {
        response.setStatus(HttpServletResponse.SC_OK);
        return;//w w w. j  a  va2s  .  c o m
    }

    if (uri.endsWith("locale")) {
        Config.set(request.getSession(), Config.FMT_LOCALE,
                Locale.forLanguageTag(request.getParameter("locale")));
        response.setStatus(HttpServletResponse.SC_OK);
        return;
    }

    if (request.getHeader("If-None-Match") != null && etag.equals(request.getHeader("If-None-Match"))) {
        response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
        return;
    }

    String charset = "utf-8";
    if (TagUtil.getInitParam(TagriaConfigParameter.ENCODING) != null) {
        charset = TagUtil.getInitParam(TagriaConfigParameter.ENCODING);
    }
    response.setCharacterEncoding(charset);
    try {

        DateTime today = new DateTime();
        DateTime expires = new DateTime().plusDays(CACHE_EXPIRES_DAY);

        if (uri.endsWith(".css")) {
            response.setContentType("text/css");
        } else if (uri.endsWith(".js")) {
            response.setContentType("text/javascript");
        } else if (uri.endsWith(".png")) {
            response.setContentType("image/png");
        }

        SimpleDateFormat sdf = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss 'GMT'", Locale.ENGLISH);

        if (Boolean.valueOf(TagUtil.getInitParam(TagriaConfigParameter.CDN_ENABLED))) {
            response.setHeader(HttpHeaderParameter.ACCESS_CONTROL_ALLOW_ORIGIN.getName(), "*");
        }

        response.setHeader(HttpHeaderParameter.ETAG.getName(), etag);
        response.setHeader(HttpHeaderParameter.EXPIRES.getName(), sdf.format(expires.toDate()));
        response.setHeader(HttpHeaderParameter.CACHE_CONTROL.getName(),
                "public,max-age=" + Seconds.secondsBetween(today, expires).getSeconds());

        String url = "/com/jslsolucoes"
                + uri.replaceFirst(request.getContextPath(), "").replaceAll(";jsessionid=.*", "");
        InputStream in = getClass().getResourceAsStream(url);
        IOUtils.copy(in, response.getOutputStream());
        in.close();

    } catch (Exception exception) {
        logger.error("Could not load resource", exception);
        response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
    }
}

From source file:org.openmrs.contrib.metadatarepository.webapp.filter.LocaleFilter.java

/**
 * This method looks for a "locale" request parameter. If it finds one, it sets it as the preferred locale
 * and also configures it to work with JSTL.
 * //from w w  w .  ja  v a 2 s  .  c o  m
 * @param request the current request
 * @param response the current response
 * @param chain the chain
 * @throws IOException when something goes wrong
 * @throws ServletException when a communication failure happens
 */
@SuppressWarnings("unchecked")
public void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
        throws IOException, ServletException {

    String locale = request.getParameter("locale");
    Locale preferredLocale = null;

    if (locale != null) {
        int indexOfUnderscore = locale.indexOf('_');
        if (indexOfUnderscore != -1) {
            String language = locale.substring(0, indexOfUnderscore);
            String country = locale.substring(indexOfUnderscore + 1);
            preferredLocale = new Locale(language, country);
        } else {
            preferredLocale = new Locale(locale);
        }
    }

    HttpSession session = request.getSession(false);

    if (session != null) {
        if (preferredLocale == null) {
            preferredLocale = (Locale) session.getAttribute(Constants.PREFERRED_LOCALE_KEY);
        } else {
            session.setAttribute(Constants.PREFERRED_LOCALE_KEY, preferredLocale);
            Config.set(session, Config.FMT_LOCALE, preferredLocale);
        }

        if (preferredLocale != null && !(request instanceof LocaleRequestWrapper)) {
            request = new LocaleRequestWrapper(request, preferredLocale);
            LocaleContextHolder.setLocale(preferredLocale);
        }
    }

    String theme = request.getParameter("theme");
    if (theme != null && request.isUserInRole(Constants.ADMIN_ROLE)) {
        Map<String, Object> config = (Map) getServletContext().getAttribute(Constants.CONFIG);
        config.put(Constants.CSS_THEME, theme);
    }

    chain.doFilter(request, response);

    // Reset thread-bound LocaleContext.
    LocaleContextHolder.setLocaleContext(null);
}

From source file:org.musicrecital.webapp.filter.LocaleFilter.java

/**
 * This method looks for a "locale" request parameter. If it finds one, it sets it as the preferred locale
 * and also configures it to work with JSTL.
 * //from w w w . j a v  a2 s  .c o m
 * @param request the current request
 * @param response the current response
 * @param chain the chain
 * @throws IOException when something goes wrong
 * @throws ServletException when a communication failure happens
 */
@SuppressWarnings("unchecked")
public void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
        throws IOException, ServletException {

    String locale = request.getParameter("locale");
    Locale preferredLocale = null;

    if (locale != null) {
        int indexOfUnderscore = locale.indexOf('_');
        if (indexOfUnderscore != -1) {
            String language = locale.substring(0, indexOfUnderscore);
            String country = locale.substring(indexOfUnderscore + 1);
            preferredLocale = new Locale(language, country);
        } else {
            preferredLocale = new Locale(locale);
        }
    }

    HttpSession session = request.getSession(false);

    if (session != null) {
        if (preferredLocale == null) {
            preferredLocale = (Locale) session.getAttribute(Constants.PREFERRED_LOCALE_KEY);
        } else {
            session.setAttribute(Constants.PREFERRED_LOCALE_KEY, preferredLocale);
            Config.set(session, Config.FMT_LOCALE, preferredLocale);
        }

        if (preferredLocale != null && !(request instanceof LocaleRequestWrapper)) {
            request = new LocaleRequestWrapper(request, preferredLocale);
            LocaleContextHolder.setLocale(preferredLocale);
        }
    }

    chain.doFilter(request, response);

    // Reset thread-bound LocaleContext.
    LocaleContextHolder.setLocaleContext(null);
}

From source file:ejportal.webapp.filter.LocaleFilter.java

/**
 * This method looks for a "locale" request parameter. If it finds one, it
 * sets it as the preferred locale and also configures it to work with JSTL.
 * /*from w  ww.  j  a  va 2s  .c  o m*/
 * @param request
 *            the current request
 * @param response
 *            the current response
 * @param chain
 *            the chain
 * @throws IOException
 *             when something goes wrong
 * @throws ServletException
 *             when a communication failure happens
 */
@Override
@SuppressWarnings("unchecked")
public void doFilterInternal(HttpServletRequest request, final HttpServletResponse response,
        final FilterChain chain) throws IOException, ServletException {

    final String locale = request.getParameter("locale");
    Locale preferredLocale = null;

    if (locale != null) {
        final int indexOfUnderscore = locale.indexOf('_');
        if (indexOfUnderscore != -1) {
            final String language = locale.substring(0, indexOfUnderscore);
            final String country = locale.substring(indexOfUnderscore + 1);
            preferredLocale = new Locale(language, country);
        } else {
            preferredLocale = new Locale(locale);
        }
    }

    final HttpSession session = request.getSession(false);

    if (session != null) {
        if (preferredLocale == null) {
            preferredLocale = (Locale) session.getAttribute(Constants.PREFERRED_LOCALE_KEY);
        } else {
            session.setAttribute(Constants.PREFERRED_LOCALE_KEY, preferredLocale);
            Config.set(session, Config.FMT_LOCALE, preferredLocale);
        }

        if ((preferredLocale != null) && !(request instanceof LocaleRequestWrapper)) {
            request = new LocaleRequestWrapper(request, preferredLocale);
            LocaleContextHolder.setLocale(preferredLocale);
        }
    }

    final String theme = request.getParameter("theme");
    // if (theme != null && request.isUserInRole(Constants.ADMIN_ROLE)) {
    // TOD hartkodiert
    if ((theme != null) && request.isUserInRole("ROLE_SYSTEMADMIN")) {
        final Map<String, Object> config = (Map) this.getServletContext().getAttribute(Constants.CONFIG);
        config.put(Constants.CSS_THEME, theme);
    }

    chain.doFilter(request, response);

    // Reset thread-bound LocaleContext.
    LocaleContextHolder.setLocaleContext(null);
}

From source file:alpha.portal.webapp.filter.LocaleFilter.java

/**
 * This method looks for a "locale" request parameter. If it finds one, it
 * sets it as the preferred locale and also configures it to work with JSTL.
 * /*from ww w .j  av a 2  s.  com*/
 * @param request
 *            the current request
 * @param response
 *            the current response
 * @param chain
 *            the chain
 * @throws IOException
 *             when something goes wrong
 * @throws ServletException
 *             when a communication failure happens
 */
@Override
@SuppressWarnings("unchecked")
public void doFilterInternal(HttpServletRequest request, final HttpServletResponse response,
        final FilterChain chain) throws IOException, ServletException {

    final String locale = request.getParameter("locale");
    Locale preferredLocale = null;

    if (locale != null) {
        final int indexOfUnderscore = locale.indexOf('_');
        if (indexOfUnderscore != -1) {
            final String language = locale.substring(0, indexOfUnderscore);
            final String country = locale.substring(indexOfUnderscore + 1);
            preferredLocale = new Locale(language, country);
        } else {
            preferredLocale = new Locale(locale);
        }
    }

    final HttpSession session = request.getSession(false);

    if (session != null) {
        if (preferredLocale == null) {
            preferredLocale = (Locale) session.getAttribute(Constants.PREFERRED_LOCALE_KEY);
        } else {
            session.setAttribute(Constants.PREFERRED_LOCALE_KEY, preferredLocale);
            Config.set(session, Config.FMT_LOCALE, preferredLocale);
        }

        if ((preferredLocale != null) && !(request instanceof LocaleRequestWrapper)) {
            request = new LocaleRequestWrapper(request, preferredLocale);
            LocaleContextHolder.setLocale(preferredLocale);
        }
    }

    final String theme = request.getParameter("theme");
    if ((theme != null) && request.isUserInRole(Constants.ADMIN_ROLE)) {
        final Map<String, Object> config = (Map) this.getServletContext().getAttribute(Constants.CONFIG);
        config.put(Constants.CSS_THEME, theme);
    }

    chain.doFilter(request, response);

    // Reset thread-bound LocaleContext.
    LocaleContextHolder.setLocaleContext(null);
}

From source file:com.sun.faces.application.ViewHandlerImpl.java

public void renderView(FacesContext context, UIViewRoot viewToRender) throws IOException, FacesException {

    if (null == context || null == viewToRender) {
        String message = Util.getExceptionMessageString(Util.NULL_PARAMETERS_ERROR_MESSAGE_ID);
        message = message + " context " + context + " viewToRender " + viewToRender;
        throw new NullPointerException(message);
    }//from  w  ww  .  j a v  a2  s .  co m

    ApplicationAssociate associate = ApplicationAssociate.getInstance(context.getExternalContext());

    if (null != associate) {
        associate.responseRendered();
    }
    String requestURI = viewToRender.getViewId();
    if (log.isDebugEnabled()) {
        log.debug("About to render view " + requestURI);
    }

    String mapping = getFacesMapping(context);
    String newViewId = requestURI;
    // If we have a valid mapping (meaning we were invoked via the
    // FacesServlet) and we're extension mapped, do the replacement.
    if (mapping != null && !isPrefixMapped(mapping)) {
        if (log.isDebugEnabled()) {
            log.debug("Found URL pattern mapping to FacesServlet " + mapping);
        }
        newViewId = convertViewId(context, requestURI);
    } else {
        if (log.isDebugEnabled()) {
            log.debug("Found no URL patterns mapping to FacesServlet ");
        }
    }

    viewToRender.setViewId(newViewId);

    // update the JSTL locale attribute in request scope so that JSTL
    // picks up the locale from viewRoot. This attribute must be updated
    // before the JSTL setBundle tag is called because that is when the
    // new LocalizationContext object is created based on the locale.
    // PENDING: this only works for servlet based requests
    if (context.getExternalContext().getRequest() instanceof ServletRequest) {
        Config.set((ServletRequest) context.getExternalContext().getRequest(), Config.FMT_LOCALE,
                context.getViewRoot().getLocale());
    }
    if (log.isTraceEnabled()) {
        log.trace("Before dispacthMessage to newViewId " + newViewId);
    }
    context.getExternalContext().dispatch(newViewId);
    if (log.isTraceEnabled()) {
        log.trace("After dispacthMessage to newViewId " + newViewId);
    }

}

From source file:com.edgenius.core.webapp.filter.LocaleFilter.java

public void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
        throws IOException, ServletException {
    //       if(log.isDebugEnabled()){
    //          log.debug("Request URL: " + request.getRequestURI());
    //       }//from   w ww .j av  a2s  .  c o  m

    //charset encoding
    if (!StringUtils.isEmpty(this.encoding))
        request.setCharacterEncoding(encoding);
    else
        request.setCharacterEncoding(Constants.UTF8);

    String direction = null;
    Locale preferredLocale = null;
    TimeZone timezone = null;
    HttpSession session = request.getSession(false);
    if (getUserService() != null) { //for Install mode, it will return null
        User user = getUserService().getUserByName(request.getRemoteUser());
        if (user != null && !user.isAnonymous()) {
            //locale
            UserSetting set = user.getSetting();
            String userLang = set.getLocaleLanguage();
            String userCountry = set.getLocaleCountry();
            if (userLang != null && userCountry != null) {
                preferredLocale = new Locale(userLang, userCountry);
            }
            //text direction in HTML 
            direction = set.getDirection();
            //timezone
            if (set.getTimeZone() != null)
                timezone = TimeZone.getTimeZone(set.getTimeZone());
        }
    }
    if (preferredLocale == null) {
        if (Global.DetectLocaleFromRequest) {
            Locale locale = request.getLocale();
            if (locale != null) {
                preferredLocale = locale;
            }
        }
        if (preferredLocale == null) {
            preferredLocale = Global.getDefaultLocale();
        }
    }

    if (direction == null) {
        direction = Global.DefaultDirection;
    }

    if (timezone == null) {
        if (session != null) {
            //try to get timezone from HttpSession, which will be intial set in SecurityControllerImpl.checkLogin() method
            timezone = (TimeZone) session.getAttribute(Constants.TIMEZONE);
        }
        if (timezone == null)
            timezone = TimeZone.getTimeZone(Global.DefaultTimeZone);
    }

    //set locale for STURTS and JSTL
    // set the time zone - must be set for dates to display the time zone
    if (session != null) {
        Config.set(session, Config.FMT_LOCALE, preferredLocale);
        session.setAttribute(Constants.DIRECTION, direction);
        Config.set(session, Config.FMT_TIME_ZONE, timezone);
    }

    //replace request by LocaleRequestWrapper
    if (!(request instanceof LocaleRequestWrapper)) {
        request = new LocaleRequestWrapper(request, preferredLocale);
        LocaleContextConfHolder.setLocale(preferredLocale);
    }

    if (chain != null) {
        request.setAttribute(PREFERRED_LOCALE, preferredLocale.toString());
        chain.doFilter(request, response);
    }
    // Reset thread-bound LocaleContext.
    LocaleContextConfHolder.setLocaleContext(null);
}

From source file:com.sun.faces.taglib.jsf_core.ViewTag.java

protected void setProperties(UIComponent component) {
    super.setProperties(component);
    Locale viewLocale = null;//from www .j  a  v a2 s.  co m
    ValueBinding vb = null;
    if (null != locale) {
        if (isValueReference(locale)) {
            component.setValueBinding("locale", vb = Util.getValueBinding(locale));
            Object resultLocale = vb.getValue(FacesContext.getCurrentInstance());
            if (resultLocale instanceof Locale) {
                viewLocale = (Locale) resultLocale;
            } else if (resultLocale instanceof String) {
                viewLocale = getLocaleFromString((String) resultLocale);
            }
        } else {
            viewLocale = getLocaleFromString(locale);
        }
        ((UIViewRoot) component).setLocale(viewLocale);
        // update the JSTL locale attribute in request scope so that
        // JSTL picks up the locale from viewRoot. This attribute
        // must be updated before the JSTL setBundle tag is called
        // because that is when the new LocalizationContext object
        // is created based on the locale.
        Config.set(pageContext.getRequest(), Config.FMT_LOCALE, viewLocale);
    }

}

From source file:opa.filter.LoadResourceBundle.java

@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain fc)
        throws IOException, ServletException {

    HttpServletRequest request = (HttpServletRequest) req;
    HttpSession session = request.getSession();

    DatabaseResourceBundle bundle = null;

    InitiativeSetup setup = (InitiativeSetup) session.getAttribute("initiativeSetup");

    LocalizationContext context = (LocalizationContext) Config.get(session, Config.FMT_LOCALIZATION_CONTEXT);

    if (context != null)
        bundle = (DatabaseResourceBundle) context.getResourceBundle();

    if (setup == null || setup.getLang_id() == null) {
        throw new ServletException(
                "Could not get language from InitiativeSetup object in application context.");
    }//w w w  .  jav  a 2 s.  c  o  m

    if (bundle == null || !bundle.getLangId().equals(setup.getLang_id())) {
        if (bundle != null) {
            bundle.destroy();
            bundle = null;
        }

        bundle = new DatabaseResourceBundle(jdbcJndi, setup.getLang_id());
        bundle.init();

        /*
          * set the localization context of the session for jsp to process  
          */
        Config.set(session, Config.FMT_LOCALIZATION_CONTEXT, new LocalizationContext(bundle, null));

        log.info("setting language for current user to: " + bundle.getLangId());
    }

    fc.doFilter(req, res);
}

From source file:org.apache.myfaces.taglib.core.ViewTag.java

protected void setProperties(UIComponent component) {
    super.setProperties(component);

    if (_locale != null) {
        Locale locale;//  ww  w  .j  av a2s .  co m
        if (UIComponentTag.isValueReference(_locale)) {
            FacesContext context = FacesContext.getCurrentInstance();
            ValueBinding vb = context.getApplication().createValueBinding(_locale);
            Object localeValue = vb.getValue(context);
            if (localeValue instanceof Locale) {
                locale = (Locale) localeValue;
            } else if (localeValue instanceof String) {
                locale = LocaleUtils.toLocale((String) localeValue);
            } else {
                if (localeValue != null) {
                    throw new IllegalArgumentException("Locale or String class expected. Expression: " + _locale
                            + ". Return class: " + localeValue.getClass().getName());
                } else {
                    throw new IllegalArgumentException(
                            "Locale or String class expected. Expression: " + _locale + ". Return value null");
                }
            }
        } else {
            locale = LocaleUtils.toLocale(_locale);
        }
        ((UIViewRoot) component).setLocale(locale);
        Config.set((ServletRequest) getFacesContext().getExternalContext().getRequest(), Config.FMT_LOCALE,
                locale);
    }
}