fixing UriResolverDocs

This commit is contained in:
Francesco Mangiacrapa 2022-03-24 15:59:07 +01:00
parent 645709a701
commit 50c822904e
1 changed files with 40 additions and 27 deletions

View File

@ -21,44 +21,57 @@ import org.slf4j.LoggerFactory;
/**
*
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
* Oct 22, 2018
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it Oct 22, 2018
*/
@Path("docs/*")
@Path("docs")
public class UriResolverDocs {
private static Logger logger = LoggerFactory.getLogger(UriResolverDocs.class);
@GET
@Produces({MediaType.TEXT_HTML})
@Path("")
public InputStream index(@Context HttpServletRequest req) throws WebApplicationException{
logger.info(UriResolverDocs.class.getSimpleName() +" called");
String requestURI = req.getRequestURI();
try{
if(requestURI == null)
requestURI = "/index.html";
String pathInfo = req.getPathInfo();
logger.info("pathInfo {}",pathInfo);
logger.info("request URI {}", requestURI);
String realPath = req.getServletContext().getRealPath("/docs/"+requestURI);
return new FileInputStream(new File(realPath));
}catch (Exception e) {
@Produces({ MediaType.TEXT_HTML })
@Path("/")
public InputStream index(@Context HttpServletRequest req) throws WebApplicationException {
logger.info(UriResolverDocs.class.getSimpleName() + " called");
if(!(e instanceof WebApplicationException)){
//UNEXPECTED EXCEPTION managing it as WebApplicationException
String error = requestURI+" not found. Please, contact the support!";
String requestURI = req.getRequestURI();
try {
if (requestURI == null)
requestURI = "/index.html";
String pathInfo = req.getPathInfo();
logger.info("pathInfo {}", pathInfo);
logger.info("request URI {}", requestURI);
String realPath = req.getServletContext().getRealPath("/docs/" + requestURI);
return new FileInputStream(new File(realPath));
} catch (Exception e) {
if (!(e instanceof WebApplicationException)) {
// UNEXPECTED EXCEPTION managing it as WebApplicationException
String error = requestURI + " not found. Please, contact the support!";
throw ExceptionManager.internalErrorException(req, error, this.getClass(), null);
}
//ALREADY MANAGED AS WebApplicationException
// ALREADY MANAGED AS WebApplicationException
logger.error("Exception:", e);
throw (WebApplicationException) e;
}
}
}
@GET
@Produces({ MediaType.TEXT_HTML })
@Path("*")
public InputStream toDoc1(@Context HttpServletRequest req) throws WebApplicationException {
logger.info(UriResolverDocs.class.getSimpleName() + " toDoc1 called");
return index(req);
}
@GET
@Produces({ MediaType.TEXT_HTML })
@Path("/*")
public InputStream toDoc2(@Context HttpServletRequest req) throws WebApplicationException {
logger.info(UriResolverDocs.class.getSimpleName() + " toDoc2 called");
return index(req);
}
}