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:com.lm.lic.manager.util.GenUtil.java

public static Date resetClientTimezone(HttpServletRequest request, Date date) {
    final TimeZone timeZone = TimeZone.getDefault();
    final boolean daylight = timeZone.inDaylightTime(date);
    final Locale locale = request.getLocale();
    String tzName = timeZone.getDisplayName(daylight, TimeZone.LONG, locale);
    // String countryCode = locale.getCountry();
    // TimeZone ltz = TimeZone.getTimeZone(countryCode);
    // TimeZoneNameUtility.getZoneStrings(locale);
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);//  w w  w. j  a  v a 2s .c  o  m
    cal.setTimeZone(TimeZone.getTimeZone(tzName));
    return cal.getTime();
}

From source file:fr.paris.lutece.plugins.directory.modules.pdfproducer.utils.PDFUtils.java

/**
 * method to create PDF/*from   www .  ja va2 s. c o  m*/
 * @param request request
 * @param strNameFile PDF name
 * @param out OutputStream
 * @param nIdRecord the id record
 * @param listIdEntryConfig list of config id entry
 * @param bExtractNotFilledField if true, extract empty fields, false
 *            otherwise
 */
public static void doCreateDocumentPDF(HttpServletRequest request, String strNameFile, OutputStream out,
        int nIdRecord, List<Integer> listIdEntryConfig, Boolean bExtractNotFilledField) {
    AdminUser adminUser = AdminUserService.getAdminUser(request);
    Locale locale = request.getLocale();
    doCreateDocumentPDF(adminUser, locale, strNameFile, out, nIdRecord, listIdEntryConfig,
            bExtractNotFilledField);
}

From source file:com.google.visualization.datasource.DataSourceHelper.java

/**
 * Get the locale from the given request.
 *
 * @param req The http serlvet request/*from  ww  w  .  j  a v a  2 s. c  om*/
 *
 * @return The locale for the given request.
 */
public static ULocale getLocaleFromRequest(HttpServletRequest req) {
    Locale locale;
    String requestLocale = req.getParameter(LOCALE_REQUEST_PARAMETER);
    if (requestLocale != null) {
        // Try to take the locale from the 'hl' parameter in the request.
        locale = LocaleUtil.getLocaleFromLocaleString(requestLocale);
    } else {
        // Else, take the browser locale.
        locale = req.getLocale();
    }
    return ULocale.forLocale(locale);
}

From source file:com.salesmanager.core.util.CheckoutUtil.java

/**
 * OrderProductAttribute is configured from javascript This code needs to
 * invoke catalog objects require getProductOptionValueId
 * /*from   ww  w.j av  a  2s  .  com*/
 * @param attributes
 * @param lineId
 * @param currency
 * @param request
 * @return
 * @throws Exception
 */
public static OrderProduct addAttributesFromRawObjects(List<OrderProductAttribute> attributes, OrderProduct scp,
        String currency, HttpServletRequest request) throws Exception {

    Locale loc = request.getLocale();
    String lang = loc.getLanguage();

    CatalogService cservice = (CatalogService) ServiceFactory.getService(ServiceFactory.CatalogService);

    BigDecimal sumPrice = null;

    Locale locale = (Locale) request.getSession().getAttribute("WW_TRANS_I18N_LOCALE");
    if (locale == null)
        locale = request.getLocale();

    // get attributes for this product
    Collection productAttributes = cservice.getProductAttributes(scp.getProductId(), locale.getLanguage());
    Map mapAttributes = new HashMap();
    if (productAttributes != null) {

        Iterator i = productAttributes.iterator();
        while (i.hasNext()) {
            ProductAttribute p = (ProductAttribute) i.next();
            mapAttributes.put(p.getOptionValueId(), p);
        }

    }

    if (scp != null) {

        StringBuffer attributesLine = null;

        if (attributes != null) {
            attributesLine = new StringBuffer();
            int count = 0;
            Iterator i = attributes.iterator();
            while (i.hasNext()) {
                OrderProductAttribute opa = (OrderProductAttribute) i.next();
                String attrPriceText = opa.getPrice();
                BigDecimal attrPrice = null;
                if (attrPriceText != null) {
                    attrPrice = CurrencyUtil.validateCurrency(attrPriceText, currency);
                } else {
                    attrPrice = opa.getOptionValuePrice();
                }
                // get all information from the attribute
                ProductAttribute pa = (ProductAttribute) mapAttributes.get(opa.getProductOptionValueId());
                if (pa != null) {
                    if (attrPrice == null) {
                        attrPrice = pa.getOptionValuePrice();
                    }
                }
                if (attrPrice != null) {
                    opa.setOptionValuePrice(attrPrice);
                    opa.setPrice(CurrencyUtil.displayFormatedAmountNoCurrency(attrPrice, currency));
                    if (sumPrice == null) {
                        sumPrice = new BigDecimal(attrPrice.doubleValue()).setScale(2);
                    } else {

                        BigDecimal currentPrice = sumPrice;
                        sumPrice = currentPrice.add(attrPrice);
                    }
                }
                // opa.setOrderId(Long.parseLong(orderId));
                opa.setOrderProductId(scp.getProductId());
                opa.setProductAttributeIsFree(pa.isProductAttributeIsFree());

                opa.setProductOption("");
                if (StringUtils.isBlank(opa.getProductOptionValue())) {
                    opa.setProductOptionValue("");
                }

                ProductOption po = pa.getProductOption();
                Set poDescriptions = po.getDescriptions();
                if (poDescriptions != null) {
                    Iterator pi = poDescriptions.iterator();
                    while (pi.hasNext()) {
                        ProductOptionDescription pod = (ProductOptionDescription) pi.next();
                        if (pod.getId().getLanguageId() == LanguageUtil.getLanguageNumberCode(lang)) {
                            opa.setProductOption(pod.getProductOptionName());
                            break;
                        }
                    }
                }

                if (StringUtils.isBlank(opa.getProductOptionValue())) {
                    ProductOptionValue pov = pa.getProductOptionValue();
                    if (pov != null) {
                        Set povDescriptions = pov.getDescriptions();
                        if (povDescriptions != null) {
                            Iterator povi = povDescriptions.iterator();
                            while (povi.hasNext()) {
                                ProductOptionValueDescription povd = (ProductOptionValueDescription) povi
                                        .next();
                                if (povd.getId().getLanguageId() == LanguageUtil.getLanguageNumberCode(lang)) {
                                    opa.setProductOptionValue(povd.getProductOptionValueName());
                                    break;
                                }
                            }
                        }
                    }
                }
                opa.setProductAttributeWeight(pa.getProductAttributeWeight());
                if (count == 0) {
                    attributesLine.append("[ ");
                }
                attributesLine.append(opa.getProductOption()).append(" -> ")
                        .append(opa.getProductOptionValue());
                if (count + 1 == attributes.size()) {
                    attributesLine.append("]");
                } else {
                    attributesLine.append(", ");
                }
                count++;
            }
        }

        // add attribute price to productprice
        if (sumPrice != null) {
            // sumPrice = sumPrice.multiply(new
            // BigDecimal(scp.getProductQuantity()));

            // get product price
            BigDecimal productPrice = scp.getProductPrice();
            productPrice = productPrice.add(sumPrice);

            // added
            scp.setProductPrice(productPrice);

            // BigDecimal finalPrice = scp.getFinalPrice();
            BigDecimal finalPrice = productPrice.multiply(new BigDecimal(scp.getProductQuantity()));
            // finalPrice = finalPrice.add(sumPrice);
            // BigDecimal cost = scp.get

            scp.setPriceText(CurrencyUtil.displayFormatedAmountNoCurrency(productPrice, currency));
            scp.setPriceFormated(CurrencyUtil.displayFormatedAmountWithCurrency(finalPrice, currency));
        }

        if (attributesLine != null) {
            scp.setAttributesLine(attributesLine.toString());
        }

        Set attributesSet = new HashSet(attributes);

        scp.setOrderattributes(attributesSet);

    }

    return scp;
}

From source file:com.jsmartframework.web.manager.WebContext.java

/**
 * Returns the {@link Locale} of the client associated to the request being processed.
 *
 * @return {@link Locale} instance./*from   w w w.  ja va2 s .com*/
 */
public static Locale getLocale() {
    HttpServletRequest request = getRequest();
    return request != null ? request.getLocale() : null;
}

From source file:com.salesmanager.core.util.CheckoutUtil.java

/**
 * OrderProductAttribute is configured from javascript This code needs to
 * invoke catalog objects because it requires getProductOptionValueId, will
 * not change any price Add attributes to product, add attribute offset
 * price to original product price// ww  w  . java2  s  .  co  m
 * 
 * @param attributes
 * @param lineId
 * @param currency
 * @param request
 * @return
 * @throws Exception
 */
public static OrderProduct addAttributesFromRawObjects(List<OrderProductAttribute> attributes, long productId,
        String lineId, String currency, HttpServletRequest request) throws Exception {

    Locale loc = request.getLocale();
    String lang = loc.getLanguage();

    HttpSession session = request.getSession();

    Map cartLines = SessionUtil.getOrderProducts(request);

    if (cartLines == null) {
        throw new Exception("No OrderProduct exixt yet, cannot assign attributes");
    }

    OrderProduct scp = (OrderProduct) cartLines.get(lineId);

    if (scp == null) {
        throw new Exception("No OrderProduct exixt for lineId " + lineId);
    }

    CatalogService cservice = (CatalogService) ServiceFactory.getService(ServiceFactory.CatalogService);

    BigDecimal sumPrice = null;

    // make sure OrderProduct and productId match
    if (scp.getProductId() == productId) {

        Locale locale = (Locale) request.getSession().getAttribute("WW_TRANS_I18N_LOCALE");
        if (locale == null)
            locale = request.getLocale();

        // get attributes for this product
        Collection productAttributes = cservice.getProductAttributes(productId, locale.getLanguage());
        Map mapAttributes = new HashMap();
        if (productAttributes != null) {

            Iterator i = productAttributes.iterator();
            while (i.hasNext()) {
                ProductAttribute p = (ProductAttribute) i.next();
                mapAttributes.put(p.getOptionValueId(), p);
            }

        }

        if (scp != null) {

            StringBuffer attributesLine = null;

            if (attributes != null) {
                attributesLine = new StringBuffer();
                int count = 0;
                Iterator i = attributes.iterator();
                while (i.hasNext()) {
                    OrderProductAttribute opa = (OrderProductAttribute) i.next();
                    String attrPriceText = opa.getPrice();
                    BigDecimal attrPrice = null;
                    if (attrPriceText != null) {
                        attrPrice = CurrencyUtil.validateCurrency(attrPriceText, currency);
                    } else {
                        attrPrice = opa.getOptionValuePrice();
                    }
                    // get all information from the attribute
                    ProductAttribute pa = (ProductAttribute) mapAttributes.get(opa.getProductOptionValueId());
                    if (pa != null) {
                        if (attrPrice == null) {
                            attrPrice = pa.getOptionValuePrice();
                        }
                    }
                    if (attrPrice != null) {
                        opa.setOptionValuePrice(attrPrice);
                        opa.setPrice(CurrencyUtil.displayFormatedAmountNoCurrency(attrPrice, currency));
                        if (sumPrice == null) {
                            sumPrice = new BigDecimal(attrPrice.doubleValue()).setScale(2);
                        } else {
                            // double pr = sumPrice.doubleValue() +
                            // attrPrice.doubleValue();
                            // sumPrice = new
                            // BigDecimal(sumPrice.doubleValue() +
                            // attrPrice.doubleValue());
                            BigDecimal currentPrice = sumPrice;
                            sumPrice = currentPrice.add(attrPrice);
                        }
                    }
                    // opa.setOrderId(Long.parseLong(orderId));
                    opa.setOrderProductId(productId);
                    opa.setProductAttributeIsFree(pa.isProductAttributeIsFree());

                    opa.setProductOption("");
                    if (StringUtils.isBlank(opa.getProductOptionValue())) {
                        opa.setProductOptionValue("");
                    }

                    ProductOption po = pa.getProductOption();
                    Set poDescriptions = po.getDescriptions();
                    if (poDescriptions != null) {
                        Iterator pi = poDescriptions.iterator();
                        while (pi.hasNext()) {
                            ProductOptionDescription pod = (ProductOptionDescription) pi.next();
                            if (pod.getId().getLanguageId() == LanguageUtil.getLanguageNumberCode(lang)) {
                                opa.setProductOption(pod.getProductOptionName());
                                break;
                            }
                        }
                    }

                    if (StringUtils.isBlank(opa.getProductOptionValue())) {
                        ProductOptionValue pov = pa.getProductOptionValue();
                        if (pov != null) {
                            Set povDescriptions = pov.getDescriptions();
                            if (povDescriptions != null) {
                                Iterator povi = povDescriptions.iterator();
                                while (povi.hasNext()) {
                                    ProductOptionValueDescription povd = (ProductOptionValueDescription) povi
                                            .next();
                                    if (povd.getId().getLanguageId() == LanguageUtil
                                            .getLanguageNumberCode(lang)) {
                                        opa.setProductOptionValue(povd.getProductOptionValueName());
                                        break;
                                    }
                                }
                            }
                        }
                    }
                    opa.setProductAttributeWeight(pa.getProductAttributeWeight());
                    if (count == 0) {
                        attributesLine.append("[ ");
                    }
                    attributesLine.append(opa.getProductOption()).append(" -> ")
                            .append(opa.getProductOptionValue());
                    if (count + 1 == attributes.size()) {
                        attributesLine.append("]");
                    } else {
                        attributesLine.append(", ");
                    }
                    count++;
                }
            }

            // add attribute price to productprice
            if (sumPrice != null) {
                // sumPrice = sumPrice.multiply(new
                // BigDecimal(scp.getProductQuantity()));

                scp.setAttributeAdditionalCost(sumPrice);// add additional
                // attribute
                // price

                // get product price
                BigDecimal productPrice = scp.getProductPrice();
                productPrice = productPrice.add(sumPrice);

                // added
                scp.setProductPrice(productPrice);

                // BigDecimal finalPrice = scp.getFinalPrice();
                BigDecimal finalPrice = productPrice.multiply(new BigDecimal(scp.getProductQuantity()));
                // finalPrice = finalPrice.add(sumPrice);
                // BigDecimal cost = scp.get

                scp.setPriceText(CurrencyUtil.displayFormatedAmountNoCurrency(productPrice, currency));
                scp.setPriceFormated(CurrencyUtil.displayFormatedAmountWithCurrency(finalPrice, currency));
            }

            if (attributesLine != null) {
                scp.setAttributesLine(attributesLine.toString());
            }

            Set attributesSet = new HashSet(attributes);

            scp.setOrderattributes(attributesSet);

        }

    }

    return scp;
}

From source file:fr.paris.lutece.plugins.mylutece.util.SecurityUtils.java

/**
 * Gets the admin message saying that the password does not match the
 * required format/*  w  w w.  ja v  a 2s .c  o m*/
 * @param request The request
 * @param parameterService Parameter service to use
 * @param plugin The plugin
 * @return the url of the admin message saying that the password does not
 *         match the required format
 */
protected static String getMessageBackPasswordFormat(HttpServletRequest request,
        IUserParameterService parameterService, Plugin plugin) {
    Object[] param = { getMessagePasswordFormat(parameterService, request.getLocale(), plugin) };

    return AdminMessageService.getMessageUrl(request, MESSAGE_PASSWORD_FORMAT, param, AdminMessage.TYPE_STOP);
}

From source file:com.google.sampling.experiential.server.EventServlet.java

public static DateTimeZone getTimeZoneForClient(HttpServletRequest req) {
    String tzStr = getParam(req, "tz");
    if (tzStr != null && !tzStr.isEmpty()) {
        DateTimeZone jodaTimeZone = DateTimeZone.forID(tzStr);
        return jodaTimeZone;
    } else {//from   w w  w .  j  a  v  a 2s  . c  o m
        Locale clientLocale = req.getLocale();
        Calendar calendar = Calendar.getInstance(clientLocale);
        TimeZone clientTimeZone = calendar.getTimeZone();
        DateTimeZone jodaTimeZone = DateTimeZone.forTimeZone(clientTimeZone);
        return jodaTimeZone;
    }
}

From source file:com.seajas.search.utilities.spring.i18n.AcceptHeaderSessionLocaleResolver.java

/**
 * Determine the default locale from the HTTP request header.
 * //from   w w  w  .  j av a  2s.  co  m
 * @param request
 * @return Locale
 */
@Override
protected Locale determineDefaultLocale(HttpServletRequest request) {
    Locale requestLocale = request.getLocale();

    if (availableLanguages.contains(requestLocale.getLanguage()))
        return requestLocale;
    else
        return getDefaultLocale();
}

From source file:fr.paris.lutece.portal.service.portal.PortalService.java

/**
 * Formats the path specified in parameter and returns it
 *
 * @param strPath The path to format/*ww w .  java  2  s  .  c  om*/
 * @param nMode The mode to use for the formatting
 * @param request The HTTP request
 * @return the html code to display the path
 */
public static String formatPath(String strPath, int nMode, HttpServletRequest request) {
    HashMap<String, Object> model = new HashMap<String, Object>();
    model.put(Markers.PAGE_PATH, strPath);

    List<PageInclude> listIncludes = PageIncludeService.getIncludes();
    PageData data = new PageData();

    for (PageInclude pic : listIncludes) {
        pic.fillTemplate(model, data, nMode, request);
    }

    HtmlTemplate template = AppTemplateService.getTemplate(TEMPLATE_PAGE_PATH,
            (request == null) ? null : request.getLocale(), model);

    return template.getHtml();
}