Skip to content

Commit

Permalink
Update to latest draft of Servlet 4.0
Browse files Browse the repository at this point in the history
  • Loading branch information
stuartwdouglas committed Apr 20, 2017
1 parent 16647a7 commit 66514e6
Show file tree
Hide file tree
Showing 14 changed files with 133 additions and 72 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Expand Up @@ -73,7 +73,7 @@
<version.org.jboss.logging>3.2.1.Final</version.org.jboss.logging>
<version.org.jboss.logging.processor>2.0.0.Final</version.org.jboss.logging.processor>
<version.org.jboss.logmanager>2.0.0.Final</version.org.jboss.logmanager>
<version.org.jboss.spec.javax.servlet.jboss-servlet-api_4.0_spec>1.0.0.Alpha1</version.org.jboss.spec.javax.servlet.jboss-servlet-api_4.0_spec>
<version.org.jboss.spec.javax.servlet.jboss-servlet-api_4.0_spec>1.0.0.Alpha2</version.org.jboss.spec.javax.servlet.jboss-servlet-api_4.0_spec>
<version.org.jboss.spec.javax.annotation.jboss-annotations-api_1.2_spec>1.0.0.Final</version.org.jboss.spec.javax.annotation.jboss-annotations-api_1.2_spec>
<version.org.jboss.spec.javax.servlet.jsp>1.0.0.Final</version.org.jboss.spec.javax.servlet.jsp>
<version.org.jboss.spec.javax.websockets>1.1.0.Final</version.org.jboss.spec.javax.websockets>
Expand Down
6 changes: 6 additions & 0 deletions servlet/src/main/java/io/undertow/servlet/api/Deployment.java
Expand Up @@ -82,8 +82,14 @@ public interface Deployment {
*/
Executor getAsyncExecutor();


@Deprecated
Charset getDefaultCharset();

Charset getDefaultRequestCharset();

Charset getDefaultResponseCharset();

/**
*
* @return The list of authentication mechanisms configured for this deployment
Expand Down
22 changes: 22 additions & 0 deletions servlet/src/main/java/io/undertow/servlet/api/DeploymentInfo.java
Expand Up @@ -90,6 +90,8 @@ public class DeploymentInfo implements Cloneable {
private int defaultCookieVersion = 0;
private SessionPersistenceManager sessionPersistenceManager;
private String defaultEncoding;
private String defaultRequestEncoding;
private String defaultResponseEncoding;
private String urlEncoding = null;
private boolean ignoreFlush = false;
private AuthorizationManager authorizationManager = DefaultAuthorizationManager.INSTANCE;
Expand Down Expand Up @@ -1286,6 +1288,24 @@ public DeploymentInfo setCheckOtherSessionManagers(boolean checkOtherSessionMana
return this;
}

public String getDefaultRequestEncoding() {
return defaultRequestEncoding;
}

public DeploymentInfo setDefaultRequestEncoding(String defaultRequestEncoding) {
this.defaultRequestEncoding = defaultRequestEncoding;
return this;
}

public String getDefaultResponseEncoding() {
return defaultResponseEncoding;
}

public DeploymentInfo setDefaultResponseEncoding(String defaultResponseEncoding) {
this.defaultResponseEncoding = defaultResponseEncoding;
return this;
}

@Override
public DeploymentInfo clone() {
final DeploymentInfo info = new DeploymentInfo()
Expand Down Expand Up @@ -1373,6 +1393,8 @@ public DeploymentInfo clone() {
info.securityDisabled = securityDisabled;
info.useCachedAuthenticationMechanism = useCachedAuthenticationMechanism;
info.checkOtherSessionManagers = checkOtherSessionManagers;
info.defaultRequestEncoding = defaultRequestEncoding;
info.defaultResponseEncoding = defaultResponseEncoding;
return info;
}

Expand Down
Expand Up @@ -68,7 +68,9 @@ public class DeploymentImpl implements Deployment {
private volatile ErrorPages errorPages;
private volatile Map<String, String> mimeExtensionMappings;
private volatile SessionManager sessionManager;
@Deprecated
private volatile Charset defaultCharset = StandardCharsets.ISO_8859_1;

private volatile List<AuthenticationMechanism> authenticationMechanisms;
private volatile List<ThreadSetupHandler> threadSetupActions;

Expand Down Expand Up @@ -200,6 +202,7 @@ public Executor getAsyncExecutor() {
return deploymentInfo.getAsyncExecutor();
}

@Deprecated
public Charset getDefaultCharset() {
return defaultCharset;
}
Expand All @@ -218,6 +221,7 @@ public DeploymentManager.State getDeploymentState() {
return deploymentManager.getState();
}

@Deprecated
public void setDefaultCharset(Charset defaultCharset) {
this.defaultCharset = defaultCharset;
}
Expand Down
Expand Up @@ -345,8 +345,12 @@ private HttpHandler setupSecurityHandlers(HttpHandler initialHandler) {

//we don't allow multipart requests, and use the default encoding when it's set
FormEncodedDataDefinition formEncodedDataDefinition = new FormEncodedDataDefinition();
if (deploymentInfo.getDefaultEncoding() != null) {
formEncodedDataDefinition.setDefaultEncoding(deploymentInfo.getDefaultEncoding());
String reqEncoding = deploymentInfo.getDefaultRequestEncoding();
if(reqEncoding == null) {
deploymentInfo.getDefaultEncoding();
}
if (reqEncoding != null) {
formEncodedDataDefinition.setDefaultEncoding(reqEncoding);
}
FormParserFactory parser = FormParserFactory.builder(false)
.addParser(formEncodedDataDefinition)
Expand Down
Expand Up @@ -74,7 +74,7 @@ public ManagedServlet(final ServletInfo servletInfo, final ServletContextImpl se

public void setupMultipart(ServletContextImpl servletContext) {
FormEncodedDataDefinition formDataParser = new FormEncodedDataDefinition()
.setDefaultEncoding(servletContext.getDeployment().getDefaultCharset().name());
.setDefaultEncoding(servletContext.getDeployment().getDefaultRequestCharset().name());
MultipartConfigElement multipartConfig = servletInfo.getMultipartConfig();
if(multipartConfig == null) {
multipartConfig = servletContext.getDeployment().getDeploymentInfo().getDefaultMultipartConfig();
Expand Down Expand Up @@ -105,7 +105,7 @@ public void setupMultipart(ServletContextImpl servletContext) {
if(config.getMaxFileSize() > 0) {
multiPartParserDefinition.setMaxIndividualFileSize(config.getMaxFileSize());
}
multiPartParserDefinition.setDefaultEncoding(servletContext.getDeployment().getDefaultCharset().name());
multiPartParserDefinition.setDefaultEncoding(servletContext.getDeployment().getDefaultRequestCharset().name());

formParserFactory = FormParserFactory.builder(false)
.addParser(formDataParser)
Expand Down
Expand Up @@ -382,9 +382,9 @@ private ServletPathMatchesData setupServletChains() {
}
}
if (filtersByDispatcher.isEmpty()) {
builder.addNameMatch(entry.getKey(), servletChain(entry.getValue(), entry.getValue().getManagedServlet(), null, deploymentInfo, false, MappingMatch.UNKNOWN, ""));
builder.addNameMatch(entry.getKey(), servletChain(entry.getValue(), entry.getValue().getManagedServlet(), null, deploymentInfo, false, MappingMatch.EXACT, ""));
} else {
builder.addNameMatch(entry.getKey(), servletChain(new FilterHandler(filtersByDispatcher, deploymentInfo.isAllowNonStandardWrappers(), entry.getValue()), entry.getValue().getManagedServlet(), null, deploymentInfo, false, MappingMatch.UNKNOWN, ""));
builder.addNameMatch(entry.getKey(), servletChain(new FilterHandler(filtersByDispatcher, deploymentInfo.isAllowNonStandardWrappers(), entry.getValue()), entry.getValue().getManagedServlet(), null, deploymentInfo, false, MappingMatch.EXACT, ""));
}
}

Expand Down
Expand Up @@ -24,6 +24,7 @@
import io.undertow.server.handlers.form.FormData;
import io.undertow.server.handlers.form.FormDataParser;
import io.undertow.server.handlers.form.MultiPartParserDefinition;
import io.undertow.server.protocol.http.HttpAttachments;
import io.undertow.server.session.Session;
import io.undertow.server.session.SessionConfig;
import io.undertow.servlet.UndertowServletMessages;
Expand All @@ -42,6 +43,7 @@
import io.undertow.util.CanonicalPathUtils;
import io.undertow.util.DateUtils;
import io.undertow.util.HeaderMap;
import io.undertow.util.HeaderValues;
import io.undertow.util.Headers;
import io.undertow.util.HttpString;
import io.undertow.util.LocaleUtils;
Expand Down Expand Up @@ -86,9 +88,9 @@
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpUpgradeHandler;
import javax.servlet.http.Mapping;
import javax.servlet.http.Part;
import javax.servlet.http.PushBuilder;
import javax.servlet.http.ServletMapping;

/**
* The http servlet request implementation. This class is not thread safe
Expand Down Expand Up @@ -222,7 +224,7 @@ public Enumeration<String> getHeaderNames() {
}

@Override
public Mapping getMapping() {
public ServletMapping getServletMapping() {
ServletRequestContext src = exchange.getAttachment(ServletRequestContext.ATTACHMENT_KEY);
ServletPathMatch match = src.getOriginalServletPathMatch();
String matchValue;
Expand All @@ -245,7 +247,7 @@ public Mapping getMapping() {
default:
matchValue = match.getRemaining();
}
return new MappingImpl(matchValue, match.getMatchString(), match.getMappingMatch());
return new MappingImpl(matchValue, match.getMatchString(), match.getMappingMatch(), match.getServletChain().getManagedServlet().getServletInfo().getName());
}

@Override
Expand Down Expand Up @@ -580,8 +582,9 @@ public String getCharacterEncoding() {
return characterEncodingFromHeader;
}

if (servletContext.getDeployment().getDeploymentInfo().getDefaultEncoding() != null) {
return servletContext.getDeployment().getDefaultCharset().name();
if (servletContext.getDeployment().getDeploymentInfo().getDefaultRequestEncoding() != null ||
servletContext.getDeployment().getDeploymentInfo().getDefaultEncoding() != null) {
return servletContext.getDeployment().getDefaultRequestCharset().name();
}

return null;
Expand Down Expand Up @@ -837,7 +840,7 @@ public BufferedReader getReader() throws IOException {
if (servletInputStream != null) {
throw UndertowServletMessages.MESSAGES.getInputStreamAlreadyCalled();
}
Charset charSet = servletContext.getDeployment().getDefaultCharset();
Charset charSet = servletContext.getDeployment().getDefaultRequestCharset();
if (characterEncoding != null) {
charSet = characterEncoding;
} else {
Expand Down Expand Up @@ -1151,7 +1154,23 @@ public void clearAttributes() {
}

@Override
public PushBuilder getPushBuilder() {
return new PushBuilderImpl(this);
public PushBuilder newPushBuilder() {
if(exchange.getConnection().isPushSupported()) {
return new PushBuilderImpl(this);
}
return null;
}

@Override
public Map<String, String> getTrailers() {
HeaderMap trailers = exchange.getAttachment(HttpAttachments.REQUEST_TRAILERS);
if(trailers == null) {
return null;
}
Map<String, String> ret = new HashMap<>();
for(HeaderValues entry : trailers) {
ret.put(entry.getHeaderName().toString(), entry.getFirst());
}
return ret;
}
}
Expand Up @@ -318,7 +318,7 @@ public Collection<String> getHeaderNames() {
@Override
public String getCharacterEncoding() {
if (charset == null) {
return servletContext.getDeployment().getDefaultCharset().name();
return servletContext.getDeployment().getDefaultResponseCharset().name();
}
return charset;
}
Expand Down
15 changes: 11 additions & 4 deletions servlet/src/main/java/io/undertow/servlet/spec/MappingImpl.java
Expand Up @@ -18,22 +18,24 @@

package io.undertow.servlet.spec;

import javax.servlet.http.Mapping;
import javax.servlet.http.MappingMatch;
import javax.servlet.http.ServletMapping;

/**
* @author Stuart Douglas
*/
public class MappingImpl implements Mapping {
public class MappingImpl implements ServletMapping {

private final String matchValue;
private final String pattern;
private final MappingMatch matchType;
private final String servletName;

public MappingImpl(String matchValue, String pattern, MappingMatch matchType) {
public MappingImpl(String matchValue, String pattern, MappingMatch matchType, String servletName) {
this.matchValue = matchValue;
this.pattern = pattern;
this.matchType = matchType;
this.servletName = servletName;
}

@Override
Expand All @@ -47,7 +49,12 @@ public String getPattern() {
}

@Override
public MappingMatch getMatchType() {
public String getServletName() {
return servletName;
}

@Override
public MappingMatch getMappingMatch() {
return matchType;
}
}
Expand Up @@ -66,7 +66,7 @@ public InputStream getInputStream() throws IOException {
return new BufferedInputStream(Files.newInputStream(formValue.getPath()));
} else {
String requestedCharset = servletRequest.getCharacterEncoding();
String charset = requestedCharset != null ? requestedCharset : servletContext.getDeployment().getDefaultCharset().name();
String charset = requestedCharset != null ? requestedCharset : servletContext.getDeployment().getDefaultRequestCharset().name();
return new ByteArrayInputStream(formValue.getValue().getBytes(charset));
}
}
Expand Down

0 comments on commit 66514e6

Please sign in to comment.