From 7a25614be03c18abbc89b4a680a554d246f9c20d Mon Sep 17 00:00:00 2001 From: "francesco.mangiacrapa" Date: Mon, 28 Mar 2022 13:24:58 +0200 Subject: [PATCH] updated toDoc method --- .../resolver/services/UriResolverDocs.java | 21 +++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/src/main/java/org/gcube/datatransfer/resolver/services/UriResolverDocs.java b/src/main/java/org/gcube/datatransfer/resolver/services/UriResolverDocs.java index 5a4819e..042ceef 100644 --- a/src/main/java/org/gcube/datatransfer/resolver/services/UriResolverDocs.java +++ b/src/main/java/org/gcube/datatransfer/resolver/services/UriResolverDocs.java @@ -6,7 +6,9 @@ package org.gcube.datatransfer.resolver.services; import java.io.File; import java.io.FileInputStream; import java.io.InputStream; +import java.net.URI; +import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; import javax.ws.rs.GET; import javax.ws.rs.Path; @@ -14,6 +16,8 @@ import javax.ws.rs.Produces; import javax.ws.rs.WebApplicationException; import javax.ws.rs.core.Context; import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; +import javax.ws.rs.core.UriBuilder; import org.gcube.datatransfer.resolver.services.error.ExceptionManager; import org.slf4j.Logger; @@ -39,9 +43,9 @@ public class UriResolverDocs { * @throws WebApplicationException the web application exception */ @GET - @Produces({ MediaType.TEXT_HTML }) + //@Produces({ MediaType.TEXT_HTML }) @Path("/{any: .*}") - public InputStream toDoc(@Context HttpServletRequest req) throws WebApplicationException { + public Response toDoc(@Context HttpServletRequest req) throws WebApplicationException { logger.info(UriResolverDocs.class.getSimpleName() + " toDoc called"); String pathInfo = req.getPathInfo(); @@ -51,10 +55,15 @@ public class UriResolverDocs { if (pathInfo.equals("/docs/")) pathInfo += "index.html"; - logger.info("redirecting to {}", pathInfo); - - String realPath = req.getServletContext().getRealPath(pathInfo); - return new FileInputStream(new File(realPath)); + ServletContext context = req.getServletContext(); + //String realPath = context.getRealPath(pathInfo); + UriBuilder builder = UriBuilder.fromPath(context.getContextPath()); + pathInfo=pathInfo.startsWith("/")?pathInfo:"/"+pathInfo; + builder.path(pathInfo); + + URI redirectTo = builder.build(); + logger.info("redirecting to {}", redirectTo); + return Response.seeOther(redirectTo).build(); } catch (Exception e) { if (!(e instanceof WebApplicationException)) {