Example usage for javax.servlet.http HttpServletRequest getLocale

List of usage examples for javax.servlet.http HttpServletRequest getLocale

Introduction

In this page you can find the example usage for javax.servlet.http HttpServletRequest getLocale.

Prototype

public Locale getLocale();

Source Link

Document

Returns the preferred Locale that the client will accept content in, based on the Accept-Language header.

Usage

From source file:fr.paris.lutece.plugins.extend.modules.rating.web.component.RatingResourceExtenderComponent.java

/**
 * {@inheritDoc}/*from   w  w w .j ava  2 s  .  c o m*/
 */
@Override
public void doSaveConfig(HttpServletRequest request, IExtenderConfig config) throws ExtendErrorException {
    if (request.getParameter("limitedConnectedUser") == null) {
        ((RatingExtenderConfig) config).setLimitedConnectedUser(false);
        ((RatingExtenderConfig) config).setDeleteVote(false);
    }

    if (request.getParameter("deleteVote") == null) {
        ((RatingExtenderConfig) config).setDeleteVote(false);
    }

    if (StringUtils.isNotBlank(request.getParameter("date_start"))) {
        ((RatingExtenderConfig) config).setDateStart(
                DateUtil.formatTimestamp(request.getParameter("date_start"), request.getLocale()));
    } else {
        ((RatingExtenderConfig) config).setDateStart(null);
    }

    if (StringUtils.isNotBlank(request.getParameter("date_end"))) {
        ((RatingExtenderConfig) config)
                .setDateEnd(DateUtil.formatTimestamp(request.getParameter("date_end"), request.getLocale()));
    } else {
        ((RatingExtenderConfig) config).setDateEnd(null);
    }

    _configService.update(config);
}

From source file:fr.paris.lutece.plugins.helpdesk.web.HelpdeskApp.java

/**
 * Returns the contact form's result page
 * @param request The Http request/*from w  w  w .  ja va2s  .com*/
 * @param faq The {@link Faq} concerned by contact error
 * @return The Html template
 */
public String getContactFormError(HttpServletRequest request, Faq faq) //Error must be handled by Message Service
{
    HashMap<String, Object> model = new HashMap<String, Object>();
    model.put(MARK_FAQ, faq);
    //useful if you want to work with Portal.jsp and RunStandaloneApp.jsp
    model.put(FULL_URL, request.getRequestURL());

    HtmlTemplate template = AppTemplateService.getTemplate(TEMPLATE_CONTACT_FORM_ERROR, request.getLocale(),
            model);

    return template.getHtml();
}

From source file:alpha.portal.webapp.controller.CardFormController.java

/**
 * Sets the a card deleted status.//from  www.  j av  a 2  s  .co m
 * 
 * @param jspCard
 *            the jsp card
 * @param request
 *            the request
 * @return the string
 * @throws Exception
 *             the exception
 */
private String setACardDeletedStatus(final AlphaCard jspCard, final HttpServletRequest request)
        throws Exception {

    final AlphaCard alphaCard = this.alphaCardManager.get(jspCard.getAlphaCardIdentifier());

    final Adornment contributor = alphaCard.getAlphaCardDescriptor()
            .getAdornment(AdornmentType.Contributor.getName());
    if ((contributor.getValue() == null) || contributor.getValue().isEmpty()) {
        this.saveError(request, this.getText("adornment.noAccess", request.getLocale()));
        final String cardId = alphaCard.getAlphaCardIdentifier().getCardId();
        final String caseId = alphaCard.getAlphaCardIdentifier().getCaseId();
        return "redirect:/caseform?activeCardId=" + cardId + "&caseId=" + caseId;
    } else {
        final Long contributorID = Long.parseLong(contributor.getValue());
        final User currentUser = this.getUserManager().getUserByUsername(request.getRemoteUser());
        if (contributorID != currentUser.getId()) {
            this.saveError(request, this.getText("adornment.noAccess", request.getLocale()));
            final String cardId = alphaCard.getAlphaCardIdentifier().getCardId();
            final String caseId = alphaCard.getAlphaCardIdentifier().getCaseId();
            return "redirect:/caseform?activeCardId=" + cardId + "&caseId=" + caseId;
        }
    }

    Adornment deletedAdornment = null;
    if (alphaCard.getAlphaCardDescriptor().getAdornment(AdornmentType.Deleted.getName()) == null) {
        deletedAdornment = new Adornment(AdornmentType.Deleted.getName());
        deletedAdornment.setValue(AdornmentTypeDeleted.FALSE.value());
        alphaCard.getAlphaCardDescriptor().setAdornment(deletedAdornment);
    }

    deletedAdornment = alphaCard.getAlphaCardDescriptor().getAdornment(AdornmentType.Deleted.getName());

    if (request.getParameter("setDeleted") != null) {
        deletedAdornment.setValue(AdornmentTypeDeleted.TRUE.value());
        this.alphaCardManager.save(alphaCard);
        this.saveMessage(request, this.getText("card.deleted", request.getLocale()));
    } else if (request.getParameter("setNotDeleted") != null) {
        deletedAdornment.setValue(AdornmentTypeDeleted.FALSE.value());
        this.alphaCardManager.save(alphaCard);
        this.saveMessage(request, this.getText("card.updated", request.getLocale()));
    }

    final AlphaCardIdentifier identifier = alphaCard.getAlphaCardIdentifier();
    return "redirect:/caseform?caseId=" + identifier.getCaseId() + "&activeCardId=" + identifier.getCardId();
}

From source file:alpha.portal.webapp.controller.CaseFormController.java

/**
 * Adds the case.//w w  w. j a  v a  2s  . c  o  m
 * 
 * @param alphaCase
 *            the alpha case
 * @param errors
 *            the errors
 * @param request
 *            the request
 * @param response
 *            the response
 * @return the string
 * @throws Exception
 *             the exception
 */
@RequestMapping(method = RequestMethod.POST, params = { "addCase" })
public String addCase(AlphaCase alphaCase, final BindingResult errors, final HttpServletRequest request,
        final HttpServletResponse response) throws Exception {

    final User currentUser = this.userManager.getUserByUsername(request.getRemoteUser());
    alphaCase.addParticipant(currentUser);
    alphaCase = this.caseManager.save(alphaCase);

    this.saveMessage(request, this.getText("case.added", request.getLocale()));
    return "redirect:/caseform?caseId=" + alphaCase.getCaseId();

}

From source file:fr.paris.lutece.plugins.directory.modules.rest.rs.DirectoryRest.java

/**
 * Get the wadl.xml content//  w w  w.j  ava 2s . c  o  m
 * @param request {@link HttpServletRequest}
 * @return the content of wadl.xml
 */
@GET
@Path(DirectoryRestConstants.PATH_WADL)
@Produces(MediaType.APPLICATION_XML)
public String getWADL(@Context HttpServletRequest request) {
    StringBuilder sbBase = new StringBuilder(AppPathService.getBaseUrl(request));

    if (sbBase.toString().endsWith(DirectoryRestConstants.SLASH)) {
        sbBase.deleteCharAt(sbBase.length() - 1);
    }

    sbBase.append(RestConstants.BASE_PATH + DirectoryPlugin.PLUGIN_NAME);

    Map<String, Object> model = new HashMap<String, Object>();
    model.put(DirectoryRestConstants.MARK_BASE_URL, sbBase.toString());

    HtmlTemplate t = AppTemplateService.getTemplate(DirectoryRestConstants.TEMPLATE_WADL, request.getLocale(),
            model);

    return t.getHtml();
}

From source file:fr.paris.lutece.plugins.extend.modules.comment.web.CommentApp.java

/**
 * Gets the adds the comment page.//from  w  w  w  . j  av  a2  s  .co m
 * 
 * @param request the request
 * @param strIdExtendableResource the str id extendable resource
 * @param strExtendableResourceType the str extendable resource type
 * @return the adds the comment page
 */
private XPage getAddCommentPage(HttpServletRequest request, String strIdExtendableResource,
        String strExtendableResourceType) {
    XPage page = new XPage();

    page.setTitle(I18nService.getLocalizedString(CommentConstants.PROPERTY_XPAGE_ADD_COMMENT_PAGE_TITLE,
            request.getLocale()));
    page.setPathLabel(I18nService.getLocalizedString(CommentConstants.PROPERTY_XPAGE_ADD_COMMENT_PAGE_LABEL,
            request.getLocale()));

    CommentExtenderConfig config = getConfigService().find(CommentResourceExtender.EXTENDER_TYPE_COMMENT,
            strIdExtendableResource, strExtendableResourceType);

    String strFromUrl = request.getParameter(CommentConstants.PARAMETER_FROM_URL);
    if (FROM_SESSION.equals(strFromUrl)) {
        strFromUrl = (String) request.getSession()
                .getAttribute(ExtendPlugin.PLUGIN_NAME + CommentConstants.PARAMETER_FROM_URL);
    }
    if (StringUtils.isEmpty(strFromUrl)) {
        strFromUrl = request.getHeader(CommentConstants.PARAMETER_REFERER);
    }
    if (strFromUrl != null) {
        strFromUrl = strFromUrl.replace(CONSTANT_AND, CONSTANT_AND_HTML);
    }
    request.getSession().setAttribute(ExtendPlugin.PLUGIN_NAME + CommentConstants.PARAMETER_FROM_URL,
            strFromUrl);

    Map<String, Object> model = new HashMap<String, Object>();
    model.put(CommentConstants.MARK_COMMENT_CONFIG, config);
    model.put(CommentConstants.MARK_ID_EXTENDABLE_RESOURCE, strIdExtendableResource);
    model.put(CommentConstants.MARK_EXTENDABLE_RESOURCE_TYPE, strExtendableResourceType);
    model.put(CommentConstants.PARAMETER_FROM_URL, strFromUrl);
    model.put(CommentConstants.MARK_RETURN_TO_COMMENT_LIST,
            Boolean.parseBoolean(request.getParameter(CommentConstants.MARK_RETURN_TO_COMMENT_LIST)));
    model.put(CommentConstants.PARAMETER_ID_COMMENT,
            request.getParameter(CommentConstants.PARAMETER_ID_COMMENT));
    model.put(CommentConstants.MARK_WEBAPP_URL, AppPathService.getBaseUrl(request));
    model.put(CommentConstants.MARK_LOCALE, Locale.getDefault());

    // Add Captcha
    model.put(MARK_IS_ACTIVE_CAPTCHA, _bIsCaptchaEnabled);

    if (_bIsCaptchaEnabled) {
        CaptchaSecurityService captchaService = new CaptchaSecurityService();
        model.put(MARK_CAPTCHA, captchaService.getHtmlCode());
    }

    if (SecurityService.isAuthenticationEnable()) {
        LuteceUser user = SecurityService.getInstance().getRegisteredUser(request);

        if (user != null) {
            model.put(CommentConstants.MARK_MYLUTECE_USER, user);
        }
    }

    HtmlTemplate template = AppTemplateService.getTemplate(TEMPLATE_XPAGE_ADD_COMMENT, request.getLocale(),
            model);

    page.setContent(template.getHtml());

    return page;
}

From source file:org.itracker.web.util.LoginUtilities.java

/**
 * Get a locale from request/* w  w w  . jav a 2s. c  om*/
 * <p/>
 * <p>
 * TODO the order of retrieving locale from request should be:
 * <ol>
 * <li>request-attribute Constants.LOCALE_KEY</li>
 * <li>request-param 'loc'</li>
 * <li>session attribute <code>Constants.LOCALE_KEY</code></li>
 * <li>cookie 'loc'</li>
 * <li>request.getLocale()/request.getLocales()</li>
 * <li>ITrackerResources.DEFAULT_LOCALE</li>
 * </ol>
 * </p>
 */
@SuppressWarnings("unchecked")
public static Locale getCurrentLocale(HttpServletRequest request) {
    Locale requestLocale = null;
    HttpSession session = request.getSession(true);
    try {

        requestLocale = (Locale) request.getAttribute(Constants.LOCALE_KEY);

        if (logger.isDebugEnabled()) {
            logger.debug("getCurrentLocale: request-attribute was {}", requestLocale);
        }

        if (null == requestLocale) {
            // get locale from request param
            String loc = request.getParameter("loc");
            if (null != loc && loc.trim().length() > 1) {
                requestLocale = ITrackerResources.getLocale(loc);
            }

            logger.debug("getCurrentLocale: request-parameter was {}", loc);

        }

        if (null == requestLocale) {
            // get it from the session
            requestLocale = (Locale) session.getAttribute(Constants.LOCALE_KEY);
            //            if (logger.isDebugEnabled()) {
            //               logger.debug("getCurrentLocale: session-attribute was "
            //                     + requestLocale);
            //            }
        }

        if (null == requestLocale) {
            ResourceBundle bundle = ITrackerResources.getBundle(request.getLocale());
            if (logger.isDebugEnabled()) {
                logger.debug("getCurrentLocale: trying request header locale " + request.getLocale());
            }
            if (bundle.getLocale().getLanguage().equals(request.getLocale().getLanguage())) {
                requestLocale = request.getLocale();
                if (logger.isDebugEnabled()) {
                    logger.debug("getCurrentLocale: request-locale was " + requestLocale);
                }
            }
        }

        // is there no way to detect supported locales of current
        // installation?

        if (null == requestLocale) {
            Enumeration<Locale> locales = (Enumeration<Locale>) request.getLocales();
            ResourceBundle bundle;
            Locale locale;
            while (locales.hasMoreElements()) {
                locale = locales.nextElement();
                bundle = ITrackerResources.getBundle(locale);

                logger.debug("getCurrentLocale: request-locales processing {}, bundle: {}", locale, bundle);

                if (bundle.getLocale().getLanguage().equals(locale.getLanguage())) {
                    requestLocale = locale;

                    logger.debug("getCurrentLocale: request-locales locale was {}", requestLocale);

                }
            }
        }

    } finally {
        if (null == requestLocale) {
            // fall back to default locale
            requestLocale = ITrackerResources.getLocale();

            logger.debug("getCurrentLocale: fallback default locale was {}", requestLocale);

        }
        session.setAttribute(Constants.LOCALE_KEY, requestLocale);
        request.setAttribute(Constants.LOCALE_KEY, requestLocale);
        request.setAttribute("currLocale", requestLocale);

        logger.debug("getCurrentLocale: request and session was setup with {}", requestLocale);

    }

    return requestLocale;
}

From source file:fr.paris.lutece.portal.web.PortalJspBean.java

/**
 * Returns the code for the popup of the credits
 * @param request The HTTP request//from  w  ww. j  a v  a  2 s .co m
 * @return the html code for the popup credits
 */
public String getStartUpFailurePage(HttpServletRequest request) {
    HashMap<String, Object> model = new HashMap<String, Object>();
    fillPageModel(request, model);
    model.put(MARK_FAILURE_MESSAGE, AppInit.getLoadingFailureCause());
    model.put(MARK_FAILURE_DETAILS, AppInit.getLoadingFailureDetails());
    model.put(Markers.PAGE_TITLE,
            I18nService.getLocalizedString(PROPERTY_PAGE_TITLE_STARTUP_FAILURE, request.getLocale()));

    HtmlTemplate template = AppTemplateService.getTemplate(TEMPLATE_STARTUP_FAILURE, request.getLocale(),
            model);

    return template.getHtml();
}

From source file:fr.paris.lutece.plugins.federatedatabasefranceconnect.web.FederateFranceConnectApp.java

/**
 * Build the Login page//from  w  w  w  .j a  va  2 s . c o  m
 * @param request The HTTP request
 * @return The XPage object containing the page content
 */
@View(value = VIEW_FEDERATION)
public XPage getFederationPage(HttpServletRequest request) {
    LuteceUser user = SecurityService.getInstance().getRegisteredUser(request);
    if (user != null
            && FederateLinkService.getInstance().getFederateLinkByLuteceUserName(user.getName()) != null) {

        addInfo(INFO_FEDERATE_SUCCESS, getLocale(request));
        _strBackUrl = FederateService.getRedirectUrl(request);

    }
    Map<String, Object> model = getModel();

    String strKey = FederateService.getSessionFederateKey(request);
    if (StringUtils.isEmpty(strKey)) {
        addError(ERROR_NO_FEDERATE_KEY, getLocale(request));

    }

    model.put(MARK_BACK_URL, _strBackUrl);

    return getXPage(TEMPLATE_FEDERATE_FORM, request.getLocale(), model);
}