Skip to content

Commit

Permalink
Fixing charset issue
Browse files Browse the repository at this point in the history
Signed-off-by: Jorge Bescos Gascon <[email protected]>
  • Loading branch information
jbescos committed May 19, 2020
1 parent de93f8c commit 8daf7b8
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright (c) 2020 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/

package org.glassfish.jersey.tests.integration.servlet_25_mvc_3;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

public class CharacterEncodingFilter implements Filter {

public static final String CHARACTER_ENCODING = "character-encoding-workaround";

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
String attr = ((HttpServletRequest) request).getHeader(CHARACTER_ENCODING);
String charset = attr != null ? attr : "UTF-8";
response.setCharacterEncoding(charset);
chain.doFilter(request, response);
}

@Override
public void init(FilterConfig filterConfig) throws ServletException {}

@Override
public void destroy() {}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@
-->

<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.glassfish.jersey.tests.integration.servlet_25_mvc_3.CharacterEncodingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>org.glassfish.jersey.tests.integration.servlet_25_mvc_3.MyApplication</filter-name>
<filter-class>org.glassfish.jersey.servlet.ServletContainer</filter-class>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<%--
Copyright (c) 2010, 2018 Oracle and/or its affiliates. All rights reserved.
Copyright (c) 2010, 2020 Oracle and/or its affiliates. All rights reserved.
This program and the accompanying materials are made available under the
terms of the Eclipse Public License v. 2.0, which is available at
Expand All @@ -16,8 +16,6 @@
--%>

<%@page contentType="text/html"%>
<%@page pageEncoding="ISO-8859-2"%>
<%--
The taglib directive below imports the JSTL library. If you uncomment it,
you must also add the JSTL library to the project. The Add Library... action
Expand All @@ -34,7 +32,6 @@ on Libraries node in Projects view can be used to add the JSTL 1.1 library.
<title>Book</title>
</head>
<body>

<h1>${it.title}</h1>

Book from ${it.author}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<%--
Copyright (c) 2010, 2018 Oracle and/or its affiliates. All rights reserved.
Copyright (c) 2010, 2020 Oracle and/or its affiliates. All rights reserved.
This program and the accompanying materials are made available under the
terms of the Eclipse Public License v. 2.0, which is available at
Expand All @@ -16,8 +16,6 @@
--%>

<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%--
The taglib directive below imports the JSTL library. If you uncomment it,
you must also add the JSTL library to the project. The Add Library... action
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2013, 2018 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2013, 2020 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -53,7 +53,10 @@ public void testResourceAsHtmlUtf8() throws Exception {

@Test
public void testResourceAsHtmlIso88592() throws Exception {
final Response response = item1resource().path("iso").request().get();
final Response response = item1resource().path("iso").request()
// In Jetty 9 it has no effect <%@page contentType="text/html"%> <%@page pageEncoding="ISO-8859-2"%>
// We need to set it in a filter.
.header(CharacterEncodingFilter.CHARACTER_ENCODING, "ISO-8859-2").get();
response.bufferEntity();

final String htmlUtf8 = response.readEntity(String.class);
Expand Down

0 comments on commit 8daf7b8

Please sign in to comment.