2022-03-24 13:22:29 +01:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
package org.gcube.datatransfer.resolver.services;
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileInputStream;
|
|
|
|
import java.io.InputStream;
|
|
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
|
import javax.ws.rs.GET;
|
|
|
|
import javax.ws.rs.Path;
|
|
|
|
import javax.ws.rs.WebApplicationException;
|
|
|
|
import javax.ws.rs.core.Context;
|
|
|
|
|
|
|
|
import org.gcube.datatransfer.resolver.services.error.ExceptionManager;
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
/**
|
2022-03-31 10:52:52 +02:00
|
|
|
* The UriResolverDocs provides the URI Resolver API Documentation
|
2022-03-24 13:22:29 +01:00
|
|
|
*
|
2022-03-24 17:33:54 +01:00
|
|
|
* @author Francesco Mangiacrapa at ISTI-CNR francesco.mangiacrapa@isti.cnr.it
|
|
|
|
*
|
2022-03-28 15:18:17 +02:00
|
|
|
* Mar 24, 2022
|
2022-03-24 13:22:29 +01:00
|
|
|
*/
|
2022-03-24 15:59:07 +01:00
|
|
|
@Path("docs")
|
2022-03-24 13:22:29 +01:00
|
|
|
public class UriResolverDocs {
|
|
|
|
|
|
|
|
private static Logger logger = LoggerFactory.getLogger(UriResolverDocs.class);
|
|
|
|
|
2022-03-24 17:33:54 +01:00
|
|
|
/**
|
|
|
|
* To doc.
|
|
|
|
*
|
|
|
|
* @param req the req
|
|
|
|
* @return the input stream
|
|
|
|
* @throws WebApplicationException the web application exception
|
|
|
|
*/
|
2022-03-24 13:22:29 +01:00
|
|
|
@GET
|
2022-03-24 16:20:26 +01:00
|
|
|
@Path("/{any: .*}")
|
2022-03-28 15:18:17 +02:00
|
|
|
public InputStream toDoc(@Context HttpServletRequest req) throws WebApplicationException {
|
2022-03-24 16:38:16 +01:00
|
|
|
logger.info(UriResolverDocs.class.getSimpleName() + " toDoc called");
|
2022-03-24 15:59:07 +01:00
|
|
|
|
2022-03-28 15:18:17 +02:00
|
|
|
String pathInfo = req.getPathInfo();
|
|
|
|
logger.info("pathInfo {}", pathInfo);
|
2022-03-24 15:59:07 +01:00
|
|
|
try {
|
|
|
|
|
2022-03-24 16:38:16 +01:00
|
|
|
if (pathInfo.equals("/docs/"))
|
|
|
|
pathInfo += "index.html";
|
2022-03-28 15:18:17 +02:00
|
|
|
|
|
|
|
logger.info("redirecting to {}", pathInfo);
|
|
|
|
|
|
|
|
String realPath = req.getServletContext().getRealPath(pathInfo);
|
|
|
|
return new FileInputStream(new File(realPath));
|
|
|
|
|
2022-03-24 15:59:07 +01:00
|
|
|
} catch (Exception e) {
|
|
|
|
|
|
|
|
if (!(e instanceof WebApplicationException)) {
|
|
|
|
// UNEXPECTED EXCEPTION managing it as WebApplicationException
|
2022-03-28 15:18:17 +02:00
|
|
|
String error = pathInfo + " not found. Please, contact the support!";
|
2022-03-24 13:22:29 +01:00
|
|
|
throw ExceptionManager.internalErrorException(req, error, this.getClass(), null);
|
|
|
|
}
|
2022-03-24 15:59:07 +01:00
|
|
|
// ALREADY MANAGED AS WebApplicationException
|
2022-03-24 13:22:29 +01:00
|
|
|
logger.error("Exception:", e);
|
|
|
|
throw (WebApplicationException) e;
|
|
|
|
}
|
|
|
|
}
|
2022-03-24 16:38:16 +01:00
|
|
|
|
2022-03-24 17:33:54 +01:00
|
|
|
/**
|
|
|
|
* Index.
|
|
|
|
*
|
|
|
|
* @param req the req
|
|
|
|
* @return the input stream
|
|
|
|
* @throws WebApplicationException the web application exception
|
|
|
|
*/
|
2022-03-24 16:38:16 +01:00
|
|
|
@GET
|
2022-03-28 15:59:50 +02:00
|
|
|
// @Produces({ MediaType.TEXT_HTML })
|
2022-03-24 16:38:16 +01:00
|
|
|
@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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-24 15:59:07 +01:00
|
|
|
}
|