Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom context path configuation option #475

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import edu.cornell.mannlib.vitro.webapp.auth.permissions.PermissionSets;
import edu.cornell.mannlib.vitro.webapp.auth.policy.EntityPolicyController;
import edu.cornell.mannlib.vitro.webapp.beans.PermissionSet;
import edu.cornell.mannlib.vitro.webapp.config.ContextPath;
import edu.cornell.mannlib.vitro.webapp.controller.Controllers;
import edu.cornell.mannlib.vitro.webapp.controller.VitroHttpServlet;
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
Expand Down Expand Up @@ -222,7 +223,7 @@ protected WebappDaoFactory getWebappDaoFactory(String userURI) {
}

public String getDefaultLandingPage(HttpServletRequest request) {
return (request.getContextPath() + DEFAULT_LANDING_PAGE);
return (ContextPath.getPath(request) + DEFAULT_LANDING_PAGE);
}

protected static void addAccessAttributes(HttpServletRequest req, String entityURI, AccessObjectType aot) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package edu.cornell.mannlib.vitro.webapp.config;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class ContextPath {
static final Log log = LogFactory.getLog(ContextPath.class);

public static String getPath(HttpServletRequest request) {
String path = ConfigurationProperties.getInstance().getProperty("context.path");
log.debug(String.format("Custom path %s, request path %s", path, request.getContextPath()));
return path == null ? request.getContextPath() : path;
}

public static String getPath(ServletContext ctx) {
String path = ConfigurationProperties.getInstance().getProperty("context.path");
log.debug(String.format("Custom path %s, ctx path %s", path, ctx.getContextPath()));
return path == null ? ctx.getContextPath() : path;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.apache.jena.shared.Lock;

import edu.cornell.mannlib.vitro.webapp.beans.ApplicationBean;
import edu.cornell.mannlib.vitro.webapp.config.ContextPath;
import edu.cornell.mannlib.vitro.webapp.modelaccess.ModelAccess;
import edu.cornell.mannlib.vitro.webapp.utils.jena.JenaOutputUtils;
import edu.cornell.mannlib.vitro.webapp.web.ContentType;
Expand All @@ -45,7 +46,7 @@ public void doGet(HttpServletRequest req, HttpServletResponse res)
super.doGet(req, res);

//get URL without hostname or servlet context
String url = req.getRequestURI().substring(req.getContextPath().length());
String url = req.getRequestURI().substring(ContextPath.getPath(req).length());

String redirectURL = checkForRedirect ( url, req.getHeader("accept") );

Expand Down Expand Up @@ -218,11 +219,11 @@ private void doRedirect(HttpServletRequest req, HttpServletResponse res,
String hn = req.getHeader("Host");
if (req.isSecure()) {
res.setHeader("Location", res.encodeURL("https://" + hn
+ req.getContextPath() + redirectURL));
+ ContextPath.getPath(req) + redirectURL));
log.info("doRedirect by using HTTPS");
} else {
res.setHeader("Location", res.encodeURL("http://" + hn
+ req.getContextPath() + redirectURL));
+ ContextPath.getPath(req) + redirectURL));
log.info("doRedirect by using HTTP");
}
res.setStatus(res.SC_SEE_OTHER);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import edu.cornell.mannlib.vitro.webapp.auth.requestedAction.AuthorizationRequest;
import edu.cornell.mannlib.vitro.webapp.beans.DisplayMessage;
import edu.cornell.mannlib.vitro.webapp.beans.ResourceBean;
import edu.cornell.mannlib.vitro.webapp.config.ContextPath;
import edu.cornell.mannlib.vitro.webapp.controller.authenticate.LogoutRedirector;
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.UrlBuilder;
import edu.cornell.mannlib.vitro.webapp.i18n.I18n;
Expand Down Expand Up @@ -155,7 +156,7 @@ public static void redirectToInsufficientAuthorizationPage(
try {
DisplayMessage.setMessage(request,
I18n.bundle(request).text("insufficient_authorization"));
response.sendRedirect(request.getContextPath());
response.sendRedirect(ContextPath.getPath(request));
} catch (IOException e) {
log.error("Could not redirect to show insufficient authorization.");
}
Expand Down Expand Up @@ -196,7 +197,7 @@ private static String assembleLoginUrlWithReturn(
} catch (UnsupportedEncodingException e) {
log.error("Really? No UTF-8 encoding?", e);
}
return request.getContextPath() + Controllers.AUTHENTICATE
return ContextPath.getPath(request) + Controllers.AUTHENTICATE
+ "?afterLogin=" + encodedAfterLoginUrl;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import edu.cornell.mannlib.vitro.webapp.auth.permissions.SimplePermission;
import edu.cornell.mannlib.vitro.webapp.beans.DisplayMessage;
import edu.cornell.mannlib.vitro.webapp.beans.UserAccount;
import edu.cornell.mannlib.vitro.webapp.config.ContextPath;
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
import edu.cornell.mannlib.vitro.webapp.controller.authenticate.Authenticator;
import edu.cornell.mannlib.vitro.webapp.controller.authenticate.Authenticator.LoginNotPermitted;
Expand Down Expand Up @@ -151,14 +152,15 @@ private ResponseValues showLoginRedirection(VitroRequest vreq,
* Bridge the gap.
*/
private String stripContextPath(VitroRequest vreq, String uri) {
if ((uri == null) || uri.isEmpty() || uri.equals(vreq.getContextPath())) {
String contextPath = ContextPath.getPath(vreq);
if ((uri == null) || uri.isEmpty() || uri.equals(contextPath)) {
return "/";
}
if (uri.contains("://")) {
return uri;
}
if (uri.startsWith(vreq.getContextPath() + '/')) {
return uri.substring(vreq.getContextPath().length());
if (uri.startsWith(contextPath + '/')) {
return uri.substring(contextPath.length());
}
return uri;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import edu.cornell.mannlib.vitro.webapp.auth.permissions.SimplePermission;
import edu.cornell.mannlib.vitro.webapp.auth.requestedAction.AuthorizationRequest;
import edu.cornell.mannlib.vitro.webapp.config.ContextPath;
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.FreemarkerHttpServlet;
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.responsevalues.ResponseValues;
Expand Down Expand Up @@ -40,7 +41,7 @@ protected ResponseValues processRequest(VitroRequest vreq) {
}

private String getContextPath() {
String cp = getServletContext().getContextPath();
String cp = ContextPath.getPath(getServletContext());
if ((cp == null) || cp.isEmpty()) {
return "The application";
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import edu.cornell.mannlib.vitro.webapp.config.ContextPath;
import edu.cornell.mannlib.vitro.webapp.controller.login.LoginProcessBean;
import edu.cornell.mannlib.vitro.webapp.controller.login.LoginProcessBean.MLevel;
import edu.cornell.mannlib.vitro.webapp.controller.login.LoginProcessBean.Message;
Expand Down Expand Up @@ -66,6 +66,6 @@ protected String figureHomePageUrl(HttpServletRequest req) {
String uri = req.getRequestURI();
int authLength = url.length() - uri.length();
String auth = url.substring(0, authLength);
return auth + req.getContextPath();
return auth + ContextPath.getPath(req);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import edu.cornell.mannlib.vitro.webapp.beans.CaptchaServiceBean;
import edu.cornell.mannlib.vitro.webapp.beans.UserAccount;
import edu.cornell.mannlib.vitro.webapp.config.ConfigurationProperties;
import edu.cornell.mannlib.vitro.webapp.config.ContextPath;
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.FreemarkerHttpServlet;
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.UrlBuilder;
Expand Down Expand Up @@ -224,7 +225,7 @@ private void setCommonValues(Map<String, Object> dataContext, VitroRequest vreq)

dataContext.put("forgotPasswordUrl", getForgotPasswordUrl(vreq));
dataContext.put("contactUrl", getContactUrl(vreq));
dataContext.put("contextPath", vreq.getContextPath());
dataContext.put("contextPath", ContextPath.getPath(vreq));
dataContext.put("emailConfigured", FreemarkerEmailFactory.isConfigured(vreq));
dataContext.put("emailValue", "");
dataContext.put("contactEmailConfigured", StringUtils.isNotBlank(appBean.getContactMail()));
Expand Down Expand Up @@ -355,7 +356,7 @@ private Date calculateExpirationDate() {
* @return The URL for the "Forgot Password" page as a string.
*/
private String getForgotPasswordUrl(VitroRequest request) {
String contextPath = request.getContextPath();
String contextPath = ContextPath.getPath(request);
return contextPath + "/forgotPassword";
}

Expand All @@ -366,7 +367,7 @@ private String getForgotPasswordUrl(VitroRequest request) {
* @return The URL for the "Contact" page as a string.
*/
private String getContactUrl(VitroRequest request) {
String contextPath = request.getContextPath();
String contextPath = ContextPath.getPath(request);
return contextPath + "/contact";
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import edu.cornell.mannlib.vitro.webapp.auth.policy.PolicyHelper;
import edu.cornell.mannlib.vitro.webapp.beans.DisplayMessage;
import edu.cornell.mannlib.vitro.webapp.beans.UserAccount;
import edu.cornell.mannlib.vitro.webapp.config.ContextPath;
import edu.cornell.mannlib.vitro.webapp.controller.Controllers;
import edu.cornell.mannlib.vitro.webapp.i18n.I18n;
import edu.cornell.mannlib.vitro.webapp.i18n.I18nBundle;
Expand Down Expand Up @@ -157,12 +158,12 @@ private boolean canSeeSiteAdminPage() {
}

private boolean isLoginPage(String page) {
return ((page != null) && page.endsWith(request.getContextPath()
return ((page != null) && page.endsWith(ContextPath.getPath(request)
+ Controllers.LOGIN));
}

private String getSiteAdminPageUrl() {
String contextPath = request.getContextPath();
String contextPath = ContextPath.getPath(request);
return contextPath + Controllers.SITE_ADMIN;
}

Expand All @@ -172,15 +173,15 @@ private boolean isSelfEditorWithIndividual() {

private String getAssociatedIndividualHomePage() {
try {
return request.getContextPath() + "/individual?uri="
return ContextPath.getPath(request) + "/individual?uri="
+ URLEncoder.encode(uriOfAssociatedIndividual, "UTF-8");
} catch (UnsupportedEncodingException e) {
throw new IllegalStateException("No UTF-8 encoding? Really?", e);
}
}

private String getApplicationHomePageUrl() {
String contextPath = request.getContextPath();
String contextPath = ContextPath.getPath(request);
if (contextPath.equals("")) {
return "/";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import edu.cornell.mannlib.vitro.webapp.config.ContextPath;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

Expand All @@ -34,7 +35,7 @@ public static String getRedirectUrl(HttpServletRequest request,
if ((referringUri == null)
|| (getRestrictedPageUris(request).contains(referringUri))) {
log.debug("Sending to home page.");
return request.getContextPath();
return ContextPath.getPath(request);
} else {
log.debug("Sending back to referring page.");
return referrer;
Expand All @@ -43,7 +44,7 @@ public static String getRedirectUrl(HttpServletRequest request,

private static String figureUriFromUrl(HttpServletRequest request,
String referrer) {
String postContext = breakBeforeContextPath(request.getContextPath(),
String postContext = breakBeforeContextPath(ContextPath.getPath(request),
referrer);
String uri = removeQueryString(postContext);
log.debug("referrer='" + referrer + "', uri='" + uri + "'");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import edu.cornell.mannlib.vitro.webapp.config.ContextPath;
import edu.cornell.mannlib.vitro.webapp.controller.datatools.dumprestore.DumpRestoreController.BadRequestException;
import edu.cornell.mannlib.vitro.webapp.modelaccess.ModelAccess.WhichService;
import edu.cornell.mannlib.vitro.webapp.rdfservice.RDFService;
Expand Down Expand Up @@ -46,7 +46,7 @@ class DumpModelsAction extends AbstractDumpRestoreAction {

void redirectToFilename() throws IOException {
String filename = which + N_QUADS_EXTENSION;
String urlPath = req.getContextPath() + req.getServletPath()
String urlPath = ContextPath.getPath(req) + req.getServletPath()
+ ACTION_DUMP;
resp.sendRedirect(urlPath + "/" + filename + "?" + queryString);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import edu.cornell.mannlib.vedit.beans.LoginStatusBean;
import edu.cornell.mannlib.vedit.beans.LoginStatusBean.AuthenticationSource;
import edu.cornell.mannlib.vitro.webapp.beans.UserAccount;
import edu.cornell.mannlib.vitro.webapp.config.ContextPath;
import edu.cornell.mannlib.vitro.webapp.controller.Controllers;
import edu.cornell.mannlib.vitro.webapp.controller.VitroHttpServlet;
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
Expand Down Expand Up @@ -192,10 +193,10 @@ private void recordLoginProcessPages(HttpServletRequest request) {

if (afterLoginUrl != null) {
bean.setAfterLoginUrl(afterLoginUrl);
bean.setLoginPageUrl(request.getContextPath() + Controllers.LOGIN);
bean.setLoginPageUrl(ContextPath.getPath(request) + Controllers.LOGIN);
} else if (doReturn) {
bean.setAfterLoginUrl(referrer);
bean.setLoginPageUrl(request.getContextPath() + Controllers.LOGIN);
bean.setLoginPageUrl(ContextPath.getPath(request) + Controllers.LOGIN);
} else {
bean.setAfterLoginUrl(referrer);
bean.setLoginPageUrl(referrer);
Expand Down Expand Up @@ -230,7 +231,7 @@ private String whereDidWeComeFrom(HttpServletRequest request) {
if (referrer != null) {
return referrer;
} else {
return request.getContextPath() + Controllers.LOGIN;
return ContextPath.getPath(request) + Controllers.LOGIN;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.apache.jena.shared.Lock;

import edu.cornell.mannlib.vitro.webapp.auth.permissions.SimplePermission;
import edu.cornell.mannlib.vitro.webapp.config.ContextPath;
import edu.cornell.mannlib.vitro.webapp.controller.VitroHttpServlet;
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
import edu.cornell.mannlib.vitro.webapp.dao.DisplayVocabulary;
Expand All @@ -50,7 +51,7 @@ protected void doPost(HttpServletRequest rawRequest, HttpServletResponse resp)
if(pageUri != null) {
doDeletePage(pageUri, vreq, resp);
}
resp.sendRedirect(rawRequest.getContextPath() + REDIRECT_URL);
resp.sendRedirect(ContextPath.getPath(rawRequest) + REDIRECT_URL);
}

protected void doGet(HttpServletRequest rawRequest, HttpServletResponse resp) throws ServletException, IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import edu.cornell.mannlib.vitro.webapp.beans.IndividualImpl;
import edu.cornell.mannlib.vitro.webapp.beans.VClass;
import edu.cornell.mannlib.vitro.webapp.beans.VClassGroup;
import edu.cornell.mannlib.vitro.webapp.config.ContextPath;
import edu.cornell.mannlib.vitro.webapp.controller.Controllers;
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
import edu.cornell.mannlib.vitro.webapp.dao.DataPropertyDao;
Expand All @@ -68,7 +69,7 @@ public void doPost (HttpServletRequest request, HttpServletResponse response) {
}

VitroRequest vreq = new VitroRequest(request);
String siteAdminUrl = vreq.getContextPath() + Controllers.SITE_ADMIN;
String siteAdminUrl = ContextPath.getPath(vreq) + Controllers.SITE_ADMIN;

//create an EditProcessObject for this and put it in the session
EditProcessObject epo = super.createEpo(request);
Expand Down Expand Up @@ -332,7 +333,7 @@ public void doForward(HttpServletRequest request, HttpServletResponse response,
}
} else {
try {
String siteAdminUrl = request.getContextPath() + Controllers.SITE_ADMIN;
String siteAdminUrl = ContextPath.getPath(request) + Controllers.SITE_ADMIN;
response.sendRedirect(siteAdminUrl);
} catch (IOException e) {
log.error("EntityInsertPageForwarder could not redirect to about page.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import org.apache.jena.rdf.model.Resource;
import org.apache.jena.rdf.model.ResourceFactory;
import org.apache.jena.shared.Lock;

import edu.cornell.mannlib.vitro.webapp.config.ContextPath;
import edu.cornell.mannlib.vitro.webapp.controller.VitroHttpServlet;
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
import edu.cornell.mannlib.vitro.webapp.dao.DisplayVocabulary;
Expand Down Expand Up @@ -57,7 +57,7 @@ protected void doPost(HttpServletRequest rawRequest, HttpServletResponse resp)
}
//Need to redirect correctly
if(!isReorder(command)){
resp.sendRedirect(rawRequest.getContextPath() + REDIRECT_URL);
resp.sendRedirect(ContextPath.getPath(rawRequest) + REDIRECT_URL);
} else {

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import edu.cornell.mannlib.vedit.controller.BaseEditController;
import edu.cornell.mannlib.vitro.webapp.auth.permissions.SimplePermission;
import edu.cornell.mannlib.vitro.webapp.beans.Ontology;
import edu.cornell.mannlib.vitro.webapp.config.ContextPath;
import edu.cornell.mannlib.vitro.webapp.controller.Controllers;
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
import edu.cornell.mannlib.vitro.webapp.dao.OntologyDao;
Expand Down Expand Up @@ -84,7 +85,7 @@ public void doPost (HttpServletRequest req, HttpServletResponse response) {
// See OntologyDaoJena.ontologyFromOntologyResource() comments
String realURI = OntologyDaoJena.adjustOntologyURI(o.getURI());
request.setAttribute("realURI", realURI);
request.setAttribute("exportURL", request.getContextPath() + Controllers.EXPORT_RDF);
request.setAttribute("exportURL", ContextPath.getPath(request) + Controllers.EXPORT_RDF);

request.setAttribute("epoKey",epo.getKey());
request.setAttribute("title","Ontology Control Panel");
Expand Down
Loading
Loading