You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
uri-resolver/src/main/java/org/gcube/datatransfer/resolver/services/UriResolverDocs.java

112 lines
3.2 KiB
Java

/**
*
*/
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;
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;
import org.slf4j.LoggerFactory;
/**
* The Class UriResolverDocs.
*
* @author Francesco Mangiacrapa at ISTI-CNR francesco.mangiacrapa@isti.cnr.it
*
* Mar 24, 2022
*/
@Path("docs")
public class UriResolverDocs {
private static Logger logger = LoggerFactory.getLogger(UriResolverDocs.class);
/**
* To doc.
*
* @param req the req
* @return the input stream
* @throws WebApplicationException the web application exception
*/
@GET
//@Produces({ MediaType.TEXT_HTML })
@Path("/{any: .*}")
public Response toDoc(@Context HttpServletRequest req) throws WebApplicationException {
logger.info(UriResolverDocs.class.getSimpleName() + " toDoc called");
String pathInfo = req.getPathInfo();
logger.info("pathInfo {}", pathInfo);
try {
if (pathInfo.equals("/docs/"))
pathInfo += "index.html";
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)) {
// UNEXPECTED EXCEPTION managing it as WebApplicationException
String error = pathInfo + " not found. Please, contact the support!";
throw ExceptionManager.internalErrorException(req, error, this.getClass(), null);
}
// ALREADY MANAGED AS WebApplicationException
logger.error("Exception:", e);
throw (WebApplicationException) e;
}
}
/**
* Index.
*
* @param req the req
* @return the input stream
* @throws WebApplicationException the web application exception
*/
@GET
@Produces({ MediaType.TEXT_HTML })
@Path("")
public InputStream index(@Context HttpServletRequest req) throws WebApplicationException {
logger.info(UriResolverDocs.class.getSimpleName() + " index called");
String pathInfo = "/docs/index.html";
try {
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 = pathInfo + " not found. Please, contact the support!";
throw ExceptionManager.internalErrorException(req, error, this.getClass(), null);
}
// ALREADY MANAGED AS WebApplicationException
logger.error("Exception:", e);
throw (WebApplicationException) e;
}
}
}