Skip to content

Commit

Permalink
Limit wellknown servlet to serve single file
Browse files Browse the repository at this point in the history
  • Loading branch information
davidjgraph committed May 15, 2022
1 parent cf5c78a commit 01ccb27
Showing 1 changed file with 30 additions and 20 deletions.
50 changes: 30 additions & 20 deletions src/main/java/com/mxgraph/online/WellKnownServlet.java
Expand Up @@ -14,7 +14,7 @@
import javax.servlet.http.HttpServletResponse;

/**
* Servlet to fake a .well-known directory
* Servlet to fake a .well-known directory, GAE does not directly support . prefixed directories
*/
@SuppressWarnings("serial")
public class WellKnownServlet extends HttpServlet
Expand All @@ -38,31 +38,41 @@ protected void doGet(HttpServletRequest request,
{
// GAE can't serve dot prefixed folders
String uri = request.getRequestURI().replace("/.", "/");

if (uri.toLowerCase().contains(".json"))
{
response.setContentType("application/json");
}

// Serve whatever was requested from .well-known
try (InputStream in = getServletContext().getResourceAsStream(uri))

// Currently, there is only one file that this servlet serves. This is only
// needed if you want OneDrive integration.
if (uri != null && uri.equals("/well-known/microsoft-identity-association.json"))
{
if (in == null)
if (uri.toLowerCase().contains(".json"))
{
response.sendError(404);
return;
response.setContentType("application/json");
}

byte[] buffer = new byte[8192];
int count;

while ((count = in.read(buffer)) > 0)
// Serve whatever was requested from .well-known
try (InputStream in = getServletContext().getResourceAsStream(uri))
{
response.getOutputStream().write(buffer, 0, count);
if (in == null)
{
response.sendError(404);
return;
}

byte[] buffer = new byte[8192];
int count;

while ((count = in.read(buffer)) > 0)
{
response.getOutputStream().write(buffer, 0, count);
}

response.getOutputStream().flush();
response.getOutputStream().close();
}

response.getOutputStream().flush();
response.getOutputStream().close();
}
else
{
response.sendError(404);
return;
}
}
}

0 comments on commit 01ccb27

Please sign in to comment.