-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improved redirects for the webserver
- Loading branch information
Showing
6 changed files
with
33 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,40 @@ | ||
package org.mtr.core.servlet; | ||
|
||
import org.mtr.core.generated.WebserverResources; | ||
import org.mtr.libraries.javax.servlet.AsyncContext; | ||
import org.mtr.libraries.javax.servlet.http.HttpServlet; | ||
import org.mtr.libraries.javax.servlet.http.HttpServletRequest; | ||
import org.mtr.libraries.javax.servlet.http.HttpServletResponse; | ||
|
||
import java.util.function.Function; | ||
|
||
public final class WebServlet extends HttpServlet { | ||
|
||
private final Function<String, String> contentProvider; | ||
private final String expectedPath; | ||
|
||
public WebServlet(Function<String, String> contentProvider, String expectedPath) { | ||
this.contentProvider = contentProvider; | ||
this.expectedPath = expectedPath; | ||
} | ||
|
||
@Override | ||
protected void doGet(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) { | ||
final AsyncContext asyncContext = httpServletRequest.startAsync(); | ||
asyncContext.setTimeout(0); | ||
getContent(httpServletResponse, asyncContext, httpServletRequest.getServletPath(), true); | ||
} | ||
|
||
private static void getContent(HttpServletResponse httpServletResponse, AsyncContext asyncContext, String path, boolean retry) { | ||
final String content = WebserverResources.get(ServletBase.removeLastSlash(path)); | ||
if (content == null) { | ||
if (retry) { | ||
getContent(httpServletResponse, asyncContext, "index.html", false); | ||
final String requestUri = httpServletRequest.getRequestURI(); | ||
if (requestUri.startsWith(expectedPath)) { | ||
final String path = ServletBase.removeLastSlash(requestUri.replace(expectedPath, "")); | ||
final String newPath = path.isEmpty() ? "index.html" : path; | ||
final String content = contentProvider.apply(newPath); | ||
|
||
if (content == null) { | ||
ServletBase.sendResponse(httpServletResponse, asyncContext, expectedPath, "", HttpResponseStatus.REDIRECT); | ||
} else { | ||
ServletBase.sendResponse(httpServletResponse, asyncContext, "", "", HttpResponseStatus.NOT_FOUND); | ||
ServletBase.sendResponse(httpServletResponse, asyncContext, content, ServletBase.getMimeType(newPath), HttpResponseStatus.OK); | ||
} | ||
} else { | ||
ServletBase.sendResponse(httpServletResponse, asyncContext, content, ServletBase.getMimeType(path), HttpResponseStatus.OK); | ||
ServletBase.sendResponse(httpServletResponse, asyncContext, expectedPath, "", HttpResponseStatus.REDIRECT); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters