Skip to content

Commit

Permalink
add configuration of InferredEncodings (#102)
Browse files Browse the repository at this point in the history
* add configuration of InferredEncodings

Signed-off-by: Olivier Lamy <olamy@apache.org>
  • Loading branch information
olamy authored Mar 31, 2022
1 parent e5dcb17 commit af15800
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public abstract class AbstractJettyEmbeddedConfiguration implements ContainerCon

private Map<String, String> mimeTypes;

private Map<String, String> inferredEncodings;

private int headerBufferSize = 0;

private File realmProperties;
Expand Down Expand Up @@ -167,4 +169,25 @@ public boolean isUseArchiveNameAsContext() {
public void setUseArchiveNameAsContext(boolean useArchiveNameAsContext) {
this.useArchiveNameAsContext = useArchiveNameAsContext;
}

public void setInferredEncodings(String inferredEncodings) {
this.inferredEncodings = new HashMap<>();
String[] splittedLines = inferredEncodings.split(" ");
for (int i = 0; i < splittedLines.length; i += 2) {
if (i + 1 >= splittedLines.length) {
throw new ConfigurationException(String.format(
"Mime Type definition should follow the format <extension> <type>[ <extension> <type>]*, for example js application/javascript but %s definition has been found.",
inferredEncodings));
}
this.inferredEncodings.put(splittedLines[i], splittedLines[i + 1]);
}
}

public boolean areInferredEncodings() {
return this.inferredEncodings != null;
}

public Map<String, String> getInferredEncodings() {
return inferredEncodings;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,10 @@ public void start() throws LifecycleException {
server.addBean(hashUserRealm);
}

if (containerConfig.areInferredEncodings()) {
containerConfig.getInferredEncodings().forEach((s, s2) -> MimeTypes.getInferredEncodings().put(s, s2));
}

server.setDumpAfterStart(containerConfig.isDumpServerAfterStart());
log.info("Starting Jetty Embedded Server " + Server.getVersion() + " [id:" + server.hashCode() + "]");
server.start();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public void shouldEnrichTestWithServletContext() {
assertThat(servletContext, notNullValue());
}

private String readAllAndClose(InputStream is) throws Exception {
public static String readAllAndClose(InputStream is) throws Exception {
try (is;ByteArrayOutputStream out = new ByteArrayOutputStream()) {
int read;
while ((read = is.read()) != -1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,34 @@
*/
package org.jboss.arquillian.container.jetty.embedded_11;

import java.net.URL;
import java.sql.Connection;

import jakarta.annotation.Resource;
import jakarta.inject.Inject;
import javax.sql.DataSource;

import org.hamcrest.core.StringContains;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.container.test.api.OperateOnDeployment;
import org.jboss.arquillian.junit5.ArquillianExtension;
import org.jboss.arquillian.test.api.ArquillianResource;
import org.jboss.shrinkwrap.api.GenericArchive;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
import org.jboss.shrinkwrap.api.asset.StringAsset;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.jboss.shrinkwrap.descriptor.api.Descriptors;
import org.jboss.shrinkwrap.descriptor.api.webapp30.WebAppDescriptor;
import org.jboss.shrinkwrap.resolver.api.maven.Maven;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.jboss.arquillian.container.jetty.embedded_11.JettyEmbeddedClientTestCase.readAllAndClose;

/**
* In-container test case for the Jetty Embedded 9 container
Expand Down Expand Up @@ -65,6 +74,24 @@ public static WebArchive getTestArchive() {
.setWebXML("in-container-web.xml");
}

@Deployment(name="encoding")
public static WebArchive getEncodingTestArchive() {
return ShrinkWrap.create(WebArchive.class)
.addClass(MyEncodingServlet.class)
.setWebXML(new StringAsset(Descriptors.create(WebAppDescriptor.class)
.version("4.0")
.createServlet()
.servletClass(MyEncodingServlet.class.getName())
.servletName("encoding").up()
.createServletMapping()
.servletName("encoding")
.urlPattern(MyEncodingServlet.URL_PATTERN).up()
.exportAsString()));
}

@ArquillianResource @OperateOnDeployment("encoding")
private URL encodingUrl;

// defined in jetty-env.xml, scoped to global
@Resource(mappedName = "version") Integer version;

Expand Down Expand Up @@ -95,4 +122,14 @@ public void shouldBeAbleToInjectMembersIntoTestClass() throws Exception {
//Assert.assertNotNull(testBean);
//Assert.assertEquals("Jetty", testBean.getName());
}

@Test
public void shouldBeEncodingDefined() throws Exception {
String body = readAllAndClose(new URL(encodingUrl, MyEncodingServlet.URL_PATTERN).openStream());

assertThat(
"Should contains iso-8859-1",
body,
StringContains.containsString("iso-8859-1"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2010, Red Hat Middleware LLC, and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jboss.arquillian.container.jetty.embedded_11;

import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

/**
* MyEncodingServlet
*
*/
public class MyEncodingServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

public static final String URL_PATTERN = "encoding";


@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
String encoding = response.getCharacterEncoding();
response.getWriter().append(encoding);
}
}
3 changes: 3 additions & 0 deletions jetty-embedded-11/src/test/resources/arquillian.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
js application/js
txt text/plain
</property>
<property name="inferredEncodings">
text/html iso-8859-1
</property>
</configuration>
</container>
</arquillian>

0 comments on commit af15800

Please sign in to comment.