Skip to content

Commit

Permalink
代码优化
Browse files Browse the repository at this point in the history
  • Loading branch information
smthing committed Aug 13, 2024
1 parent 354d2f4 commit 720fc41
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@


import jakarta.servlet.DispatcherType;
import jakarta.servlet.RequestDispatcher;
import jakarta.servlet.ServletConfig;
import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletException;
Expand Down Expand Up @@ -113,7 +114,7 @@ private void loadDefaultFavicon() {

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String fileName = request.getRequestURI();
String fileName = request.getDispatcherType() == DispatcherType.INCLUDE ? (String) request.getAttribute(RequestDispatcher.INCLUDE_REQUEST_URI) : request.getRequestURI();
String method = request.getMethod();
URL url = request.getServletContext().getResource(fileName.substring(request.getContextPath().length()));
File file = null;
Expand Down Expand Up @@ -195,7 +196,6 @@ private void forwardWelcome(HttpServletRequest request, HttpServletResponse resp
// 而且请求的资源不存在,那么默认的 servlet 必须抛出 FileNotFoundException 异常。
// 如果这个异常没有被捕获和处理,以及响应还未􏰀交,则响应状态 码必须被设置为 500。
if (request.getDispatcherType() == DispatcherType.INCLUDE) {
response.sendError(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase());
throw new FileNotFoundException();
}

Expand All @@ -213,7 +213,7 @@ private void forwardWelcome(HttpServletRequest request, HttpServletResponse resp
}

private String matchForwardWelcome(HttpServletRequest request) throws MalformedURLException {
String requestUri = request.getRequestURI();
String requestUri = request.getDispatcherType() == DispatcherType.INCLUDE ? (String) request.getAttribute(RequestDispatcher.INCLUDE_REQUEST_URI) : request.getRequestURI();
ServletContext servletContext = request.getServletContext();
if (requestUri.endsWith("/")) {
for (String file : deploymentInfo.getWelcomeFiles()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,19 +219,23 @@ private void parseErrorPage(WebAppInfo webAppInfo, Element parentElement) {
private void parseFilterMapping(WebAppInfo webAppInfo, Element parentElement) {
List<Node> childNodeList = getChildNodes(parentElement, "filter-mapping");
for (Node node : childNodeList) {
Map<String, String> nodeData = getNodeValue(node, Arrays.asList("filter-name", "url-pattern", "servlet-name"));
Map<String, String> nodeData = getNodeValue(node, Arrays.asList("filter-name"));
String filterName = nodeData.get("filter-name");
String urlPattern = nodeData.get("url-pattern");
String servletName = nodeData.get("servlet-name");
List<Node> dispatcher = getChildNodes(node, "dispatcher");
List<String> dispatchers = getNodeValues(node, "dispatcher");
Set<DispatcherType> dispatcherTypes = new HashSet<>();
if (CollectionUtils.isEmpty(dispatcher)) {
if (CollectionUtils.isEmpty(dispatchers)) {
dispatcherTypes.add(DispatcherType.REQUEST);
} else {
dispatcher.forEach(dispatcherElement -> dispatcherTypes.add(DispatcherType.valueOf(StringUtils.trim(dispatcherElement.getFirstChild().getNodeValue()))));
dispatchers.forEach(dispatcher -> dispatcherTypes.add(DispatcherType.valueOf(dispatcher)));
}
FilterMappingInfo filterInfo = new FilterMappingInfo(filterName, StringUtils.isBlank(urlPattern) ? FilterMappingType.SERVLET : FilterMappingType.URL, servletName, urlPattern, dispatcherTypes);
webAppInfo.getFilterMappingInfos().add(filterInfo);
getNodeValues(node, "url-pattern").forEach(urlPattern -> {
FilterMappingInfo filterInfo = new FilterMappingInfo(filterName, FilterMappingType.URL, null, urlPattern, dispatcherTypes);
webAppInfo.getFilterMappingInfos().add(filterInfo);
});
getNodeValues(node, "servlet-name").forEach(servletName -> {
FilterMappingInfo filterInfo = new FilterMappingInfo(filterName, FilterMappingType.SERVLET, servletName, null, dispatcherTypes);
webAppInfo.getFilterMappingInfos().add(filterInfo);
});
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.Collection;
import java.util.Locale;
Expand Down Expand Up @@ -96,10 +97,6 @@ public String encodeRedirectURL(String url) {

@Override
public void sendError(int sc, String msg) throws IOException {
//If the response has already been committed, this method throws an IllegalStateException.
if (isCommitted()) {
throw new IllegalStateException();
}
//After using this method, the response should be considered to be committed and should not be written to.
if (servletOutputStream != null) {
servletOutputStream.resetBuffer();
Expand Down Expand Up @@ -351,13 +348,16 @@ public void flushServletBuffer() throws IOException {

@Override
public void resetBuffer() {
if (servletOutputStream == null) {
return;
if (servletOutputStream != null) {
servletOutputStream.resetBuffer();
}
if (servletOutputStream.isCommitted()) {
throw new IllegalStateException();
if (writer != null) {
try {
writer = new PrintWriter(new ServletPrintWriter(servletOutputStream, getCharacterEncoding()));
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
}
servletOutputStream.resetBuffer();
}

@Override
Expand All @@ -367,8 +367,8 @@ public boolean isCommitted() {

@Override
public void reset() {
if (isCommitted()) {
throw new IllegalStateException();
if (servletOutputStream != null) {
servletOutputStream.resetBuffer();
}
response.getHeaderNames().forEach(headerName -> response.setHeader(headerName, null));
setContentLength(-1);
Expand All @@ -377,9 +377,6 @@ public void reset() {
response.setHttpStatus(HttpStatus.OK);
writer = null;
responseType = RESPONSE_TYPE_NONE;
if (servletOutputStream != null) {
servletOutputStream.resetBuffer();
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ public boolean isCommitted() {
}

public void resetBuffer() {
if (committed) {
throw new IllegalStateException();
}
written = 0;
}

Expand Down

0 comments on commit 720fc41

Please sign in to comment.