Skip to content

Commit

Permalink
replaced getContextPath with ContextPath.getPath
Browse files Browse the repository at this point in the history
  • Loading branch information
litvinovg committed Sep 26, 2024
1 parent 073d547 commit b690134
Show file tree
Hide file tree
Showing 15 changed files with 65 additions and 454 deletions.
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 @@ -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 @@ -26,6 +26,7 @@
import edu.cornell.mannlib.vitro.webapp.auth.requestedAction.AuthorizationRequest;
import edu.cornell.mannlib.vitro.webapp.beans.ApplicationBean;
import edu.cornell.mannlib.vitro.webapp.beans.DisplayMessage;
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.controller.freemarker.TemplateProcessingHelper.TemplateProcessingException;
Expand Down Expand Up @@ -485,7 +486,7 @@ private String normalizeServletName(String name) {
}

protected MainMenu getDisplayModelMenu(VitroRequest vreq){
String url = vreq.getRequestURI().substring(vreq.getContextPath().length());
String url = vreq.getRequestURI().substring(ContextPath.getPath(vreq).length());
return vreq.getWebappDaoFactory().getMenuDao().getMainMenu(url);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.apache.commons.logging.LogFactory;

import edu.cornell.mannlib.vitro.webapp.beans.Individual;
import edu.cornell.mannlib.vitro.webapp.config.ContextPath;
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
import edu.cornell.mannlib.vitro.webapp.web.ContentType;

Expand All @@ -42,8 +43,7 @@ public IndividualRequestAnalyzer(VitroRequest vreq,
this.vreq = vreq;

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

this.analysisContext = analysisContext;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.apache.commons.lang3.StringUtils;

import edu.cornell.mannlib.vitro.webapp.beans.ApplicationBean;
import edu.cornell.mannlib.vitro.webapp.config.ContextPath;
import edu.cornell.mannlib.vitro.webapp.dao.WebappDaoFactory;
import edu.cornell.mannlib.vitro.webapp.modelaccess.ModelAccess;
import edu.cornell.mannlib.vitro.webapp.startup.StartupStatus;
Expand Down Expand Up @@ -107,7 +108,7 @@ private void displayStartupStatus(ServletRequest req, ServletResponse resp)
}

private String getContextPath() {
String cp = ctx.getContextPath();
String cp = ContextPath.getPath(ctx);
if ((cp == null) || cp.isEmpty()) {
return "The application";
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,19 @@
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Pattern;

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

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

import edu.cornell.mannlib.vitro.webapp.beans.IndividualImpl;
import edu.cornell.mannlib.vitro.webapp.dao.WebappDaoFactory;
import edu.cornell.mannlib.vitro.webapp.modelaccess.ModelAccess;
import edu.cornell.mannlib.vitro.webapp.utils.NamespaceMapper;
import edu.cornell.mannlib.vitro.webapp.utils.NamespaceMapperFactory;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class URLRewritingHttpServletResponse extends HttpServletResponseWrapper/*implements HttpServletResponse */{

Expand All @@ -28,15 +26,12 @@ public class URLRewritingHttpServletResponse extends HttpServletResponseWrapper/
private HttpServletResponse _response;
private ServletContext _context;
private WebappDaoFactory wadf;
private int contextPathDepth;
private Pattern slashPattern = Pattern.compile("/");

public URLRewritingHttpServletResponse(HttpServletResponse response, HttpServletRequest request, ServletContext context) {
super(response);
this._response = response;
this._context = context;
this.wadf = ModelAccess.on(context).getWebappDaoFactory();
this.contextPathDepth = slashPattern.split(request.getContextPath()).length-1;
this.wadf = ModelAccess.getInstance().getWebappDaoFactory();
}

/** for testing. */
Expand Down Expand Up @@ -69,7 +64,6 @@ public String encodeURL(String inUrl) {
if( log.isDebugEnabled() ){
log.debug("START");
log.debug("charEncoding: " + this.getCharacterEncoding() );
log.debug("contextPathDepth," + contextPathDepth);
log.debug("nsMap," + nsMap);
log.debug("wadf.getDefaultNamespace(), " + wadf.getDefaultNamespace());
log.debug("externallyLinkedNamespaces " + externallyLinkedNamespaces);
Expand All @@ -80,7 +74,6 @@ public String encodeURL(String inUrl) {
inUrl,
this.getCharacterEncoding(),
/*wadf.getPortalDao().isSinglePortal()*/ true,
contextPathDepth,
nsMap,
wadf.getDefaultNamespace(),
externallyLinkedNamespaces
Expand All @@ -100,7 +93,6 @@ protected String encodeForVitro(
String inUrl,
String characterEncoding,
Boolean isSInglePortal,
int contextPathDepth,
NamespaceMapper nsMap,
String defaultNamespace,
List<String> externalNamespaces) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ private String getCancelUrl(HttpServletRequest request) {
* What's the password recovery URL for this servlet?
*/
private String getForgotPasswordUrl(HttpServletRequest request) {
String contextPath = request.getContextPath();
String contextPath = ContextPath.getPath(request);
return contextPath + "/forgotPassword";
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.linkeddatafragments.servlet;

import com.fasterxml.jackson.databind.JsonNode;
import edu.cornell.mannlib.vitro.webapp.config.ContextPath;
import org.apache.jena.riot.Lang;
import org.linkeddatafragments.config.ConfigReader;
import org.linkeddatafragments.datasource.DataSourceFactory;
Expand Down Expand Up @@ -128,7 +129,7 @@ public void destroy()
* @throws IOException
*/
private IDataSource getDataSource(HttpServletRequest request) throws DataSourceNotFoundException {
String contextPath = request.getContextPath();
String contextPath = ContextPath.getPath(request);
String requestURI = request.getRequestURI();

String path = contextPath == null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@

import edu.cornell.mannlib.vitro.webapp.beans.Ontology;
import edu.cornell.mannlib.vitro.webapp.config.ConfigurationProperties;
import edu.cornell.mannlib.vitro.webapp.config.ContextPath;
import edu.cornell.mannlib.vitro.webapp.controller.VitroHttpServlet;
import edu.cornell.mannlib.vitro.webapp.dao.OntologyDao;
import edu.cornell.mannlib.vitro.webapp.modelaccess.ModelAccess;
Expand Down Expand Up @@ -103,7 +104,7 @@ public void init(ServletConfig servletConfig) throws ServletException {
MIMEParse.register(Lang.NTRIPLES.getHeaderString());
MIMEParse.register(Lang.RDFXML.getHeaderString());

HtmlTriplePatternFragmentWriterImpl.setContextPath(servletConfig.getServletContext().getContextPath());
HtmlTriplePatternFragmentWriterImpl.setContextPath(ContextPath.getPath(ctx));
} catch (Exception e) {
throw new ServletException(e);
}
Expand Down Expand Up @@ -133,7 +134,7 @@ private boolean configurationPresent() {
}

private IDataSource getDataSource(HttpServletRequest request) throws DataSourceNotFoundException {
String contextPath = request.getContextPath();
String contextPath = ContextPath.getPath(request);
String requestURI = request.getRequestURI();

String path = contextPath == null
Expand Down
Loading

0 comments on commit b690134

Please sign in to comment.