Example usage for javax.servlet ServletRequest setCharacterEncoding

List of usage examples for javax.servlet ServletRequest setCharacterEncoding

Introduction

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

Prototype

public void setCharacterEncoding(String env) throws UnsupportedEncodingException;

Source Link

Document

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

Usage

From source file:org.apache.roller.weblogger.ui.core.filters.CharEncodingFilter.java

/**
 * Set the character encoding and sync up Struts and JSTL locales.  This filter should normally be first (and last)
 * in the chain.//from   www. j ava2s  . c  o m
 */
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
        throws IOException, ServletException {
    if (mLogger.isDebugEnabled())
        mLogger.debug("Processing CharEncodingFilter");
    try {
        if (!"UTF-8".equals(req.getCharacterEncoding())) {
            // only set encoding if not already UTF-8
            // despite the fact that this is the first filter in the chain, on Glassfish it 
            // is already too late to set request encoding without getting a WARN level log message
            req.setCharacterEncoding("UTF-8");
        }
        if (mLogger.isDebugEnabled())
            mLogger.debug("Set request character encoding to UTF-8");

    } catch (UnsupportedEncodingException e) {
        // This should never happen since UTF-8 is a Java-specified required encoding.
        throw new ServletException("Can't set incoming encoding to UTF-8");
    }

    chain.doFilter(req, res);
}

From source file:com.github.safrain.remotegsh.server.RgshFilter.java

@Override
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain)
        throws IOException, ServletException {
    HttpServletRequest request = (HttpServletRequest) req;
    HttpServletResponse response = (HttpServletResponse) resp;
    req.setCharacterEncoding(charset);
    resp.setCharacterEncoding(charset);/* w ww  .  ja v  a 2 s  .co m*/

    if ("GET".equals(request.getMethod())) {
        String res = request.getParameter("r");
        if (res != null && !res.isEmpty()) {
            if ("install".equals(res)) {
                performInstall(request, response);
            } else if ("shell".equals(res)) {
                performStartShell(request, response);
            } else if ("jar".equals(res)) {
                performJar(response);
            } else if ("rgsh".equals(res)) {
                performRgsh(request, response);
            }

        } else {
            performWelcomeScreen(request, response);
        }
    } else if ("POST".equals(request.getMethod())) {
        String sid = request.getParameter("sid");
        if (sid != null && !sid.isEmpty()) {
            performShellExecute(request, response);
        } else {
            performRunScript(request, response);
        }
    }
}

From source file:org.hi.framework.security.acegi.ExceptionTranslationFilter.java

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {
    if (!(request instanceof HttpServletRequest)) {
        throw new ServletException("HttpServletRequest required");
    }/*www.  j  a v a 2 s  .c o  m*/

    if (!(response instanceof HttpServletResponse)) {
        throw new ServletException("HttpServletResponse required");
    }

    if (encoding != null)
        request.setCharacterEncoding(this.encoding);

    try {
        chain.doFilter(request, response);

        if (logger.isDebugEnabled()) {
            logger.debug("Chain processed normally");
        }
    } catch (AuthenticationException ex) {
        handleException(request, response, chain, ex);
    } catch (AccessDeniedException ex) {
        handleException(request, response, chain, ex);
    } catch (ServletException ex) {
        if (ex.getRootCause() instanceof AuthenticationException
                || ex.getRootCause() instanceof AccessDeniedException) {
            handleException(request, response, chain, (AcegiSecurityException) ex.getRootCause());
        } else {
            throw ex;
        }
    } catch (IOException ex) {
        throw ex;
    }
}

From source file:ro.cs.cm.web.filter.GeneralFilter.java

public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
        throws IOException, ServletException {

    HttpServletResponse response = (HttpServletResponse) res;

    // set the provided HTTP response parameters
    for (Enumeration<String> e = fc.getInitParameterNames(); e.hasMoreElements();) {
        String headerName = (String) e.nextElement();
        response.addHeader(headerName, fc.getInitParameter(headerName));
    }/*  w  ww  .jav a  2s  .c  om*/

    interceptLanguagePreference((HttpServletRequest) req);

    log(req);
    // setam enconding pentru request
    req.setCharacterEncoding(CHAR_ENCODING);
    // setam si encoding pentru response
    res.setCharacterEncoding(CHAR_ENCODING);
    chain.doFilter(req, response);
}

From source file:com.ultrapower.eoms.common.plugin.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 w w  .java  2  s .c  o m
   }

From source file:org.commonfarm.web.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);
        } else {/*from   ww  w. j a  v  a  2  s. c om*/
            request.setCharacterEncoding(Constant.DEFAULT_ENCODING);
        }
    } else {
        request.setCharacterEncoding(Constant.DEFAULT_ENCODING);
    }
}

From source file:ro.cs.om.web.filter.GeneralFilter.java

public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
        throws IOException, ServletException {

    logger.debug("doFilter - START - xxxxx ");
    HttpServletResponse response = (HttpServletResponse) res;

    // set the provided HTTP response parameters
    for (Enumeration<String> e = fc.getInitParameterNames(); e.hasMoreElements();) {
        String headerName = (String) e.nextElement();
        response.addHeader(headerName, fc.getInitParameter(headerName));
    }//from   www  .j  a  v  a 2  s .  c o  m

    interceptLanguagePreference((HttpServletRequest) req);

    log(req);
    // setam enconding pentru request
    req.setCharacterEncoding(CHAR_ENCODING);
    // setam si encoding pentru response
    res.setCharacterEncoding(CHAR_ENCODING);
    chain.doFilter(req, response);
    logger.debug("doFilter - END - xxxxx ");
}

From source file:de.betterform.agent.web.filter.XFormsFilter.java

/**
 * The actual filtering method/*w  ww .j  av  a 2s.  c  o m*/
 * todo: add request attribute to set baseURI 
 *
 * @see http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/servlet/Filter.html#doFilter(javax.servlet.ServletRequest,%20javax.servlet.ServletResponse,%20javax.servlet.FilterChain)
 */
public void doFilter(ServletRequest srvRequest, ServletResponse srvResponse, FilterChain filterChain)
        throws IOException, ServletException {

    //ensure correct Request encoding
    if (srvRequest.getCharacterEncoding() == null) {
        srvRequest.setCharacterEncoding(defaultRequestEncoding);
    }

    HttpServletRequest request = (HttpServletRequest) srvRequest;
    HttpServletResponse response = (HttpServletResponse) srvResponse;
    HttpSession session = request.getSession(true);

    /*
    if header "betterform-internal is present this means that the internal http client of the processor is the
    originator of the incoming request. Since this may not be processed we skip these requests by
    simply calling the servlet chain.
            
    As the contenttype may not be present for such requests we have to determine the mimetype by the file ending
    of the requested resource.
    */

    //Check if  MEDIATYPE is set if so we will handle response as XForms
    if (request.getHeader(BetterFORMConstants.BETTERFORM_INTERNAL) != null) {
        LOG.warn("Request from internal betterForm HTTP Client arrived in XFormsFilter");
        String requestURI = request.getRequestURI();

        String mimeType = webFactory.getServletContext().getMimeType(requestURI);

        if (LOG.isDebugEnabled()) {
            LOG.debug("request URI: " + requestURI);
            LOG.debug("mimeType: " + mimeType);
        }

        if (mimeType != null) {
            srvResponse.setContentType(mimeType);
        } else {
            LOG.warn("no contenttype set for internal request");
            //                throw new ServletException("Contenttype of " + requestURI + " unknown. Please configure your webcontainer appropriately.");
        }

        filterChain.doFilter(srvRequest, srvResponse);
        return;
    }

    if (request.getParameter("source") != null) {
        srvResponse.setContentType("text/plain");
        // override setContentType to keep "text/plain" as content type.
        HttpServletResponseWrapper resp = new HttpServletResponseWrapper((HttpServletResponse) srvResponse) {
            public void setContentType(String s) {
                return;
            }
        };
        filterChain.doFilter(srvRequest, resp);
        return;
    }

    if (request.getParameter("isUpload") != null) {
        //Got an upload...
        handleUpload(request, response, session);
        //TODO: XFORMS  PROCESSING: do we need to exit?
    } else if ("GET".equalsIgnoreCase(request.getMethod())
            && request.getParameter(BetterFORMConstants.SUBMISSION_RESPONSE) != null) {
        doSubmissionReplaceAll(request, response);
    } else if ("GET".equalsIgnoreCase(request.getMethod())
            && request.getParameter(BetterFORMConstants.SUBMISSION_RESPONSE_XFORMS) != null) {
        doSubmissionReplaceAllXForms(request, response, session);
    } else {
        /* do servlet request */
        LOG.info("Passing to Chain");
        BufferedHttpServletResponseWrapper bufResponse = new BufferedHttpServletResponseWrapper(
                (HttpServletResponse) srvResponse);
        filterChain.doFilter(srvRequest, bufResponse);
        LOG.info("Returned from Chain");

        //handleResponse(srvResponse, request, response, session, bufResponse, webFactory);

        // response is contains no betterFORM relevant content so it is not buffered and cannot be processed
        if (!bufResponse.isBuffered()) {
            return;
        }

        // response is already committed to the client, so nothing is to
        // be done
        if (bufResponse.isCommitted()) {
            return;
        }

        //pass to request object
        request.setAttribute(WebFactory.USER_AGENT, XFormsFilter.USERAGENT);

        /* dealing with response from chain */
        if (handleResponseBody(request, bufResponse)) {
            byte[] data = prepareData(bufResponse);
            if (data.length > 0) {
                request.setAttribute(WebFactory.XFORMS_INPUTSTREAM, new ByteArrayInputStream(data));
            }
        }

        if (handleRequestAttributes(request)) {
            bufResponse.getOutputStream().close();
            LOG.info("Start Filter XForm");
            processXForms(request, response, session);
            LOG.info("End Render XForm");
        } else {
            srvResponse.getOutputStream().write(bufResponse.getData());
            srvResponse.getOutputStream().close();
        }
    }
}

From source file:uk.ac.ed.epcc.webapp.servlet.DefaultServletService.java

public DefaultServletService(AppContext conn, ServletContext ctx, ServletRequest req, ServletResponse res) {
    super();/*from   ww w.  j ava  2 s .  c o m*/
    this.conn = conn;
    this.ctx = ctx;
    this.req = req;
    this.res = res;
    // check the expected charset of parameters etc.
    String ce = req.getCharacterEncoding();
    if (ce == null || ce.trim().length() == 0) {
        String default_charset = defaultCharset();
        if (default_charset != null) {
            try {
                req.setCharacterEncoding(default_charset);
            } catch (UnsupportedEncodingException e) {
                error(e, "Problem with default charset");
            }
        }
    }
    if (req instanceof HttpServletRequest) {
        web_path = ((HttpServletRequest) req).getContextPath();
    }
}

From source file:no.sesat.search.http.filters.SiteLocatorFilter.java

private void doBeforeProcessing(final ServletRequest request, final ServletResponse response)
        throws IOException, ServletException {

    LOG.trace("doBeforeProcessing()");

    final Site site = getSite(request);

    if (null != site) {

        request.setAttribute(Site.NAME_KEY, site);
        request.setAttribute("startTime", FindResource.START_TIME);
        MDC.put(Site.NAME_KEY, site.getName());
        MDC.put("UNIQUE_ID", getRequestId(request));

        /* Setting default encoding */
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");

    } else {//from  ww w  . java  2s.  c o  m
        throw new ServletException("SiteLocatorFilter with no Site :-(");
    }
}