Example usage for javax.servlet ServletRequest getCharacterEncoding

List of usage examples for javax.servlet ServletRequest getCharacterEncoding

Introduction

In this page you can find the example usage for javax.servlet ServletRequest getCharacterEncoding.

Prototype

public String getCharacterEncoding();

Source Link

Document

Returns the name of the character encoding used in the body of this request.

Usage

From source file:com.flexive.war.filter.FxRequestUtils.java

/**
 * Set the request and response character encodings to UTF-8 if they are not so already.
 * Either of the parameters may be set to null, in this case no action is performed.
 *
 * @param request   the servlet request/*from w  ww  . ja  v  a  2  s  .c  o m*/
 * @param response  the servlet response
 */
public static void setCharacterEncoding(ServletRequest request, ServletResponse response) {
    if (request != null && !"UTF-8".equals(request.getCharacterEncoding())) {
        try {
            request.setCharacterEncoding("UTF-8");
        } catch (UnsupportedEncodingException e) {
            throw new IllegalArgumentException(e);
        }
    }
    if (response != null && !"UTF-8".equals(response.getCharacterEncoding())) {
        response.setCharacterEncoding("UTF-8");
    }
}

From source file:br.com.caelum.vraptor.observer.upload.CommonsUploadMultipartObserver.java

protected String getValue(FileItem item, ServletRequest request) {
    String encoding = request.getCharacterEncoding();
    if (!isNullOrEmpty(encoding)) {
        try {//from  w  w  w  .  ja v  a 2 s. c om
            return item.getString(encoding);
        } catch (UnsupportedEncodingException e) {
            logger.warn("Request have an invalid encoding. Ignoring it");
        }
    }
    return item.getString();
}

From source file:com.zyeeda.framework.web.CharacterEncodingFilter.java

@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
        throws IOException, ServletException {
    if (this.encoding != null && (this.forceEncoding || StringUtils.isBlank(req.getCharacterEncoding()))) {
        req.setCharacterEncoding(encoding);
        if (this.forceEncoding) {
            res.setCharacterEncoding(this.encoding);
        }//from w w  w. j a  v  a2s.c o  m
    }

    chain.doFilter(req, res);
}

From source file:com.sinosoft.one.mvc.web.impl.thread.RootEngine.java

/**
 * {@link RootEngine} ?.?.??/*from  w w w.j  av  a 2  s .  c  om*/
 * ?;
 * 
 * @return
 * @throws ServletException
 */

public Object execute(Mvc mvc) throws Throwable {

    InvocationBean inv = mvc.getInvocation();
    ServletRequest request = inv.getRequest();
    //
    if (request.getCharacterEncoding() == null) {
        request.setCharacterEncoding("UTF-8");
        if (logger.isDebugEnabled()) {
            logger.debug("set request.characterEncoding by default:" + request.getCharacterEncoding());
        }
    }

    //
    final RequestPath requestPath = inv.getRequestPath();

    // save before include
    if (requestPath.isIncludeRequest()) {
        saveAttributesBeforeInclude(inv);
        // ??include???(Model)
        mvc.addAfterCompletion(new AfterCompletion() {

            public void afterCompletion(Invocation inv, Throwable ex) throws Exception {
                restoreRequestAttributesAfterInclude(inv);
            }
        });
    }

    // ?
    inv.addModel("invocation", inv);
    inv.addModel("ctxpath", requestPath.getCtxpath());

    // instructionactionInstruction(???)
    Object instruction = mvc.doNext();

    if (Thread.currentThread().isInterrupted()) {
        logger.info("stop to render: thread is interrupted");
    } else {
        // flash?Cookie (include?)
        if (!requestPath.isIncludeRequest()) {
            FlashImpl flash = (FlashImpl) inv.getFlash(false);
            if (flash != null) {
                flash.writeNewMessages();
            }
        }

        // ?
        instructionExecutor.render(inv, instruction);
    }
    return instruction;
}

From source file:net.paoding.rose.web.impl.thread.RootEngine.java

/**
 * {@link RootEngine} ?.?.??/*ww w. j  a  v  a 2s .  c  o m*/
 * ?;
 * 
 * @param request
 * @param response
 * @return
 * @throws ServletException
 */

@Override
public Object execute(Rose rose) throws Throwable {

    InvocationBean inv = rose.getInvocation();
    ServletRequest request = inv.getRequest();
    //
    if (request.getCharacterEncoding() == null) {
        request.setCharacterEncoding("UTF-8");
        if (logger.isDebugEnabled()) {
            logger.debug("set request.characterEncoding by default:" + request.getCharacterEncoding());
        }
    }

    //
    final RequestPath requestPath = inv.getRequestPath();

    // save before include
    if (requestPath.isIncludeRequest()) {
        saveAttributesBeforeInclude(inv);
        // ??include???(Model)
        rose.addAfterCompletion(new AfterCompletion() {

            @Override
            public void afterCompletion(Invocation inv, Throwable ex) throws Exception {
                restoreRequestAttributesAfterInclude(inv);
            }
        });
    }

    // ?
    inv.addModel("invocation", inv);
    inv.addModel("ctxpath", requestPath.getCtxpath());

    // instructionactionInstruction(???)
    Object instruction = rose.doNext();

    if (Thread.currentThread().isInterrupted()) {
        logger.info("stop to render: thread is interrupted");
    } else {
        // flash?Cookie (include?)
        if (!requestPath.isIncludeRequest()) {
            FlashImpl flash = (FlashImpl) inv.getFlash(false);
            if (flash != null) {
                flash.writeNewMessages();
            }
        }

        // ?
        instructionExecutor.render(inv, instruction);
    }
    return instruction;
}

From source file:com.laxser.blitz.web.impl.thread.RootEngine.java

/**
 * {@link RootEngine} ?.?.??//from  w w  w.j  a v a  2 s  . c  o m
 * ?;
 * 
 * @param request
 * @param response
 * @return
 * @throws ServletException
 */

@Override
public Object execute(Blitz blitz) throws Throwable {

    InvocationBean inv = blitz.getInvocation();
    ServletRequest request = inv.getRequest();
    //
    if (request.getCharacterEncoding() == null) {
        request.setCharacterEncoding("UTF-8");
        if (logger.isDebugEnabled()) {
            logger.debug("set request.characterEncoding by default:" + request.getCharacterEncoding());
        }
    }

    //
    final RequestPath requestPath = inv.getRequestPath();

    // save before include
    if (requestPath.isIncludeRequest()) {
        saveAttributesBeforeInclude(inv);
        // ??include???(Model)
        blitz.addAfterCompletion(new AfterCompletion() {

            @Override
            public void afterCompletion(Invocation inv, Throwable ex) throws Exception {
                restoreRequestAttributesAfterInclude(inv);
            }
        });
    }

    // ?
    inv.addModel("invocation", inv);
    inv.addModel("ctxpath", requestPath.getCtxpath());

    // instructionactionInstruction(???)
    Object instruction = blitz.doNext();

    if (Thread.currentThread().isInterrupted()) {
        logger.info("stop to render: thread is interrupted");
    } else {
        // flash?Cookie (include?)
        if (!requestPath.isIncludeRequest()) {
            FlashImpl flash = (FlashImpl) inv.getFlash(false);
            if (flash != null) {
                flash.writeNewMessages();
            }
        }

        // ?
        instructionExecutor.render(inv, instruction);
    }
    return instruction;
}

From source file:com.bibisco.filters.FileFilter.java

/**
 * Processing classic form fields./* w  ww .  j  a  v  a 2 s  .  c  o m*/
 *<p>They are decoded and transferred to wrapper's map,
 * which will substitute the original request-object later 
 * (@see dpFilter }
 * @param pField
 */
protected void processRegularFormField(FileItem pField, ServletRequest pRequest) {
    String name = pField.getFieldName();
    try {
        String lStrValue = pField.getString(pRequest.getCharacterEncoding());
        mMultipartWrapper.addParameter(name, lStrValue);
    } catch (UnsupportedEncodingException e) {
        mLog.error(e, "Fatal error decoding parameter ", name);
    }
}

From source file:cn.bc.web.filter.SetCharacterEncodingFilter.java

/**
 * Select and set (if specified) the character encoding to be used to
 * interpret request parameters for this request.
 *
 * @param request The servlet request we are processing
 * @param result The servlet response we are creating
 * @param chain The filter chain we are processing
 *
 * @exception IOException if an input/output error occurs
 * @exception ServletException if a servlet error occurs
 *///w ww  .  j av  a 2 s.  c  o  m
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {
    // Conditionally select and set the character encoding to be used
    if (ignore || (request.getCharacterEncoding() == null)) {
        String encoding = this.encoding;
        if (encoding != null) {
            request.setCharacterEncoding(encoding);
            response.setContentType("text/html;charset=" + encoding);
            if (logger.isDebugEnabled()) {
                logger.debug(":response.setCharacterEncoding(\"" + encoding + "\"");
            }
        }
    }

    // Pass control on to the next filter
    chain.doFilter(request, response);
}

From source file:eionet.gdem.web.filters.SetCharacterEncodingFilter.java

/**
 * Select and set (if specified) the character encoding to be used to interpret request parameters for this request.
 *
 * @param request/*from   w w  w .j av a  2s.com*/
 *            The servlet request we are processing
 * @param response
 *            The servlet response we are creating
 * @param chain
 *            The filter chain we are processing
 *
 * @exception IOException
 *                if an input/output error occurs
 * @exception ServletException
 *                if a servlet error occurs
 */
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {

    // Conditionally select and set the character encoding to be used
    if (ignore || (request.getCharacterEncoding() == null)) {
        String encoding = selectEncoding(request);
        if (encoding != null)
            request.setCharacterEncoding(encoding);
    }

    // Pass control on to the next filter
    try {
        chain.doFilter(request, response);
    } catch (Exception e) {
        // Logging unhandled exception
        LOGGER.error("Unhandled exception caught", e);
        throw new ServletException(e);
    }

}

From source file:com.googlecode.jtiger.modules.ecside.filter.ECSideFilter.java

public void doEncoding(ServletRequest request, ServletResponse response) throws IOException, ServletException {
    if (useEncoding || (request.getCharacterEncoding() == null)) {
        if (encoding != null)
            // Set the same character encoding for the request and the
            // response
            request.setCharacterEncoding(encoding);
        // response.setCharacterEncoding(encoding);
    }/*from   w ww .  ja v  a  2 s  .com*/
}