restored previous solution

This commit is contained in:
Francesco Mangiacrapa 2022-03-28 15:18:17 +02:00
parent b0dcc5f0a8
commit 827e69486c
1 changed files with 12 additions and 38 deletions

View File

@ -6,9 +6,7 @@ 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;
@ -16,8 +14,6 @@ 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;
@ -28,7 +24,7 @@ import org.slf4j.LoggerFactory;
*
* @author Francesco Mangiacrapa at ISTI-CNR francesco.mangiacrapa@isti.cnr.it
*
* Mar 24, 2022
* Mar 24, 2022
*/
@Path("docs")
public class UriResolverDocs {
@ -43,50 +39,28 @@ public class UriResolverDocs {
* @throws WebApplicationException the web application exception
*/
@GET
//@Produces({ MediaType.TEXT_HTML })
@Produces({ MediaType.TEXT_HTML })
@Path("/{any: .*}")
public Response toDoc(@Context HttpServletRequest req) throws WebApplicationException {
public InputStream toDoc(@Context HttpServletRequest req) throws WebApplicationException {
logger.info(UriResolverDocs.class.getSimpleName() + " toDoc called");
StringBuilder newURL = null;
String pathInfo = req.getPathInfo();
logger.info("pathInfo {}", pathInfo);
try {
String scheme = req.getScheme(); // http
String serverName = req.getServerName(); // hostname.com
int serverPort = req.getServerPort();
String pathInfo = req.getPathInfo();
String queryString = req.getQueryString();
// Reconstruct original requesting URL
newURL = new StringBuilder();
newURL.append(scheme).append("://").append(serverName);
// if (serverPort != 80 && serverPort != 443) {
// url.append(":").append(serverPort);
// }
logger.info("pathInfo {}", pathInfo);
if (pathInfo.equals("/docs/"))
pathInfo += "index.html";
if (pathInfo != null) {
newURL.append(pathInfo);
}
if (queryString != null) {
newURL.append("?").append(queryString);
}
UriBuilder builder = UriBuilder.fromPath(newURL.toString());
// builder.path(pathInfo);
URI redirectTo = builder.build();
logger.info("redirecting to {}", redirectTo);
return Response.seeOther(redirectTo).build();
logger.info("redirecting to {}", pathInfo);
String realPath = req.getServletContext().getRealPath(pathInfo);
return new FileInputStream(new File(realPath));
} catch (Exception e) {
if (!(e instanceof WebApplicationException)) {
// UNEXPECTED EXCEPTION managing it as WebApplicationException
String error = newURL + " not found. Please, contact the support!";
String error = pathInfo + " not found. Please, contact the support!";
throw ExceptionManager.internalErrorException(req, error, this.getClass(), null);
}
// ALREADY MANAGED AS WebApplicationException