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:de.innovationgate.wgpublisher.filter.WGAFilter.java

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {
    WGARequestInformation info = new WGARequestInformation();
    try {/*w ww  .  j av  a2 s .com*/
        info.setStartTime(System.currentTimeMillis());
        info.setThread(Thread.currentThread());
        request.setAttribute(WGARequestInformation.REQUEST_ATTRIBUTENAME, info);

        _wgaFilterChain.init(request, chain);

        _currentRequests.put(request, info);

        // F000037B2
        if (_core.getCharacterEncoding() != null) {
            request.setCharacterEncoding(_core.getCharacterEncoding());
            // B000041DA
            response.setCharacterEncoding(_core.getCharacterEncoding());
        }

        // add/ delete jvmRoute
        String lbRoute = _core.getClusterService().getLBRoute();
        if (lbRoute != null && !lbRoute.trim().equals("")) {
            WGCookie jvmRouteCookie = new WGCookie(COOKIE_NAME_LBROUTE, lbRoute);
            jvmRouteCookie.setPath("/");
            jvmRouteCookie.setMaxAge(-1);
            jvmRouteCookie.addCookieHeader((HttpServletResponse) response);
        } else {
            Cookie cookie = getCookie((HttpServletRequest) request, COOKIE_NAME_LBROUTE);
            if (cookie != null) {
                WGCookie jvmRouteCookie = WGCookie.from(cookie);
                jvmRouteCookie.setMaxAge(0);
                jvmRouteCookie.addCookieHeader((HttpServletResponse) response);
            }
        }

        RequestWrapper wrappedRequest = createRequestWrapper(response, (HttpServletRequest) request);
        /*
         *  #00005078: don't store original URL. Store converted URL instead.
         *  Will be used in WGA.urlBuilder() and other sources 
         */
        request.setAttribute(REQATTRIB_ORIGINAL_URL, wrappedRequest.getRequestURL().toString());
        request.setAttribute(REQATTRIB_ORIGINAL_URI, wrappedRequest.getRequestURI());
        request.setAttribute(REQATTRIB_ORIGINAL_QUERYSTRING, wrappedRequest.getQueryString());

        FinalCharacterEncodingResponseWrapper wrappedResponse = createResponseWrapper(response, wrappedRequest);

        _holdRequestsLock.readLock().lock();
        try {
            _wgaFilterChain.doFilter(wrappedRequest, wrappedResponse);
        } finally {
            _holdRequestsLock.readLock().unlock();
        }

        info.setStatusCode(wrappedResponse.getStatusCode());
        info.setStatusMessage(wrappedResponse.getStatusMessage());

        AbstractWGAHttpSessionManager sessionManager = _core.getHttpSessionManager();
        if (sessionManager != null) {
            sessionManager.requestFinished(wrappedRequest, wrappedResponse);
        }
    } catch (ServletException e) {
        info.setStatusCode(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        info.setStatusMessage(e.getMessage());
        _core.getLog().error("Internal Server Error.", e);
        throw e;
    } finally {
        info.setEndTime(System.currentTimeMillis());
        try {
            WGALoggerWrapper logger = _core.getAccessLogger();
            if (logger != null) {
                logger.logRequest(request);
            }
        } catch (Exception e) {
            _core.getLog().error("Unable to log request.", e);
        }

        WGFactory.getInstance().closeSessions();

        if (!Boolean.TRUE.equals(request.getAttribute(WGPDeployer.REQATTRIB_TML_DEPLOYED))
                && info.getEndTime() > info.getStartTime() + REQUEST_LENGTH_NOTIFICATION_THRESHOLD) {

            String uri = ((HttpServletRequest) request).getRequestURI();
            Problem.Vars vars = Problem.var("reqinfo", info)
                    .var("completeurl", ((HttpServletRequest) request).getRequestURL().toString())
                    .var("host", ((HttpServletRequest) request).getServerName()).var("uri", uri);

            String problemKey = "requestProblem.longRequest#" + uri;

            if (info.getDatabase() != null) {
                _core.getProblemRegistry()
                        .addProblem(Problem.create(new FilterRequestOccasion(),
                                new DatabaseScope(info.getDatabase().getDbReference()), problemKey,
                                ProblemSeverity.LOW, vars));
            } else {
                _core.getProblemRegistry().addProblem(
                        Problem.create(new FilterRequestOccasion(), problemKey, ProblemSeverity.LOW, vars));
            }
        }

        // close opened lucene-resultsets
        LuceneManager luceneManager = _core.getLuceneManager();
        if (luceneManager != null) {
            luceneManager.closeOpenedResultSets();
        }

        _currentRequests.remove(request);
    }
}

From source file:com.doculibre.constellio.filters.SetCharacterEncodingFilter.java

/**
 * Select and set (if specified) the character encoding to be used to
 * interpret request parameters for this request.
 * //w w w  .  j  av a  2 s.c om
 * @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
 */
@SuppressWarnings("unchecked")
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {
    if (false) {
        chain.doFilter(request, response);
    } else {
        String lookupCharSet = lookupCharSet(request);

        SimpleParams simpleParams = new SimpleParams();
        Enumeration<String> paramNames = request.getParameterNames();
        while (paramNames.hasMoreElements()) {
            String paramName = paramNames.nextElement();
            String convertedParamName = convertIfNeeded(paramName, lookupCharSet);
            String[] paramValues = request.getParameterValues(paramName);
            List<String> convertedParamValues = new ArrayList<String>();
            if (paramValues != null) {
                for (String paramValue : paramValues) {
                    String convertedParamValue = convertIfNeeded(paramValue, lookupCharSet);
                    if (convertedParamValue != null) {
                        convertedParamValues.add(convertedParamValue);
                    }
                }
            }
            simpleParams.add(convertedParamName, convertedParamValues.toArray(new String[0]));
        }
        HttpServletRequest requestWrapper = new SimpleParamsHttpServletRequestWrapper(
                (HttpServletRequest) request, simpleParams);

        // 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
        chain.doFilter(requestWrapper, response);
    }
}