feature_23113 #4

Merged
francesco.mangiacrapa merged 58 commits from feature_23113 into master 2022-04-06 09:59:06 +02:00
1 changed files with 12 additions and 38 deletions
Showing only changes of commit 827e69486c - Show all commits

View File

@ -6,9 +6,7 @@ package org.gcube.datatransfer.resolver.services;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.InputStream; import java.io.InputStream;
import java.net.URI;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.GET; import javax.ws.rs.GET;
import javax.ws.rs.Path; import javax.ws.rs.Path;
@ -16,8 +14,6 @@ import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException; import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Context; import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType; 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.gcube.datatransfer.resolver.services.error.ExceptionManager;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -28,7 +24,7 @@ import org.slf4j.LoggerFactory;
* *
* @author Francesco Mangiacrapa at ISTI-CNR francesco.mangiacrapa@isti.cnr.it * @author Francesco Mangiacrapa at ISTI-CNR francesco.mangiacrapa@isti.cnr.it
* *
* Mar 24, 2022 * Mar 24, 2022
*/ */
@Path("docs") @Path("docs")
public class UriResolverDocs { public class UriResolverDocs {
@ -43,50 +39,28 @@ public class UriResolverDocs {
* @throws WebApplicationException the web application exception * @throws WebApplicationException the web application exception
*/ */
@GET @GET
//@Produces({ MediaType.TEXT_HTML }) @Produces({ MediaType.TEXT_HTML })
@Path("/{any: .*}") @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"); logger.info(UriResolverDocs.class.getSimpleName() + " toDoc called");
StringBuilder newURL = null; String pathInfo = req.getPathInfo();
logger.info("pathInfo {}", pathInfo);
try { 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/")) if (pathInfo.equals("/docs/"))
pathInfo += "index.html"; pathInfo += "index.html";
if (pathInfo != null) { logger.info("redirecting to {}", pathInfo);
newURL.append(pathInfo);
} String realPath = req.getServletContext().getRealPath(pathInfo);
if (queryString != null) { return new FileInputStream(new File(realPath));
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();
} catch (Exception e) { } catch (Exception e) {
if (!(e instanceof WebApplicationException)) { if (!(e instanceof WebApplicationException)) {
// UNEXPECTED EXCEPTION managing it as 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); throw ExceptionManager.internalErrorException(req, error, this.getClass(), null);
} }
// ALREADY MANAGED AS WebApplicationException // ALREADY MANAGED AS WebApplicationException