Skip to content

Commit

Permalink
returning 404 for bad sub-api urls
Browse files Browse the repository at this point in the history
  • Loading branch information
emanueldima committed Jul 22, 2019
1 parent b57e2af commit c495004
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,24 @@
* the default index.html HTML code.
*/
public class HttpErrorHandler extends org.eclipse.jetty.server.handler.ErrorHandler {

private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(HttpErrorHandler.class);

public static final String REDIRECT_ROUTE = "/index.html";
private String rootPath;

public HttpErrorHandler(String appContextPath, String urlPattern) {
String rootPattern = urlPattern.endsWith("/*") ? urlPattern.substring(0, urlPattern.length() - 1) : urlPattern;
String normalizedContextPath = !appContextPath.isEmpty() && !appContextPath.equals("/")
? (appContextPath.startsWith("/") ? appContextPath : "/" + appContextPath) : "";
rootPath = normalizedContextPath + rootPattern;
}


@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException {
// On 404 page we need to show index.html and let JS router do the work, otherwise show error page
if (response.getStatus() == HttpServletResponse.SC_NOT_FOUND) {
if (response.getStatus() == HttpServletResponse.SC_NOT_FOUND &&
!request.getRequestURI().startsWith(rootPath)) {
forward(REDIRECT_ROUTE, baseRequest, response);
} else {
super.handle(target, baseRequest, request, response);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import java.util.Map;

public class SwitchboardApp extends Application<Config> {
public static final String URL_PATTERN = "/api/*";

public static void main(String[] args) throws Exception {
new SwitchboardApp().run(args);
}
Expand Down Expand Up @@ -72,16 +74,16 @@ public void run(Config configuration, Environment environment) throws IOExceptio
DataResource dataResource = new DataResource(mediaLibrary);
ToolsResource toolsResource = new ToolsResource(toolRegistry);

environment.getApplicationContext().setErrorHandler(new HttpErrorHandler());
environment.jersey().setUrlPattern(URL_PATTERN);
HttpErrorHandler httpErrorHandler = new HttpErrorHandler(appContextPath, URL_PATTERN);
environment.getApplicationContext().setErrorHandler(httpErrorHandler);

environment.jersey().register(MultiPartFeature.class);
environment.jersey().register(StoragePolicyExceptionMapper.class);
environment.jersey().register(infoResource);
environment.jersey().register(dataResource);
environment.jersey().register(toolsResource);

environment.jersey().setUrlPattern("/api/*");

environment.healthChecks().register("switchboard", new AppHealthCheck());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,17 @@ public DataResource(MediaLibrary mediaLibrary) {
@GET
@Path("/{id}")
public Response getFile(@PathParam("id") String idString) {
UUID id = UUID.fromString(idString);
UUID id;
try {
id = UUID.fromString(idString);
} catch (IllegalArgumentException xc) {
return Response.status(Response.Status.NOT_FOUND).build();
}
MediaLibrary.FileInfo fi = mediaLibrary.getFileInfo(id);
if (fi == null) {
return Response.status(Response.Status.NOT_FOUND).build();
}

StreamingOutput fileStream = output -> {
byte[] data = Files.readAllBytes(fi.getPath());
output.write(data);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import javax.ws.rs.core.Response;
import java.util.List;

@Path("tools")
@Path("")
public class ToolsResource {
private static final ch.qos.logback.classic.Logger LOGGER = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(ToolsResource.class);

Expand All @@ -23,7 +23,7 @@ public ToolsResource(ToolRegistry toolRegistry) {
}

@GET
@Path("")
@Path("tools")
@Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
public Response getTools(@QueryParam("includeWS") String includeWS,
@QueryParam("deployment") String deployment,
Expand Down

0 comments on commit c495004

Please sign in to comment.