Get a URI relative to the webapp root.

@param request the current HttpServletRequest.
@param uri the URI which should be made relative.
431373
public static final String getRelativeURI( HttpServletRequest request, String uri )
    {
        return getRelativeURI( request.getContextPath(), uri );
    }
244414
private String convertRelativeURIToURL(String relativeURI)
    {

        if (relativeURI == null)
        {
            throw new IllegalStateException();
        }
        else
        {
            relativeURI = relativeURI.trim();
        }

        // If the passed URI contains "://" before the query string then it is either an
        // absolute URL or invalid and cannot be converted anyway
        int indexOfSchemeDelimiter = relativeURI.indexOf("://");
        if (indexOfSchemeDelimiter != -1)
        {
            int indexOfQueryString = relativeURI.indexOf('?');

            // If "://" occurs after '?' then it's part of the query string.
            // Otherwise return this absolute (or invalid) URL
            if ((indexOfQueryString == -1) || (indexOfSchemeDelimiter < indexOfQueryString))
            {
                return relativeURI;
            }
        }

        String urlScheme = _request.getScheme();		
        if (com.ibm.ws.webcontainer.osgi.WebContainer.getServletContainerSpecLevel() >= 31) {
            if (relativeURI.startsWith("//")) {
                StringBuffer url = new StringBuffer(urlScheme);
                url.append(":"); // slash slash will be added from the relativeURI
                url.append(relativeURI);
                return url.toString();
            }
        }

        String webAppRootURI = null;
        boolean relativeToRootURI = false;
        try
        {
            webAppRootURI = getWebApp().getContextPath().trim();
            if (webAppRootURI.startsWith("/") == false)
            {
                webAppRootURI = ("/" + webAppRootURI);
            }
        }
        catch (Exception ex)
        {
            com.ibm.wsspi.webcontainer.util.FFDCWrapper.processException(ex, "com.ibm.ws.webcontainer.webapp.WebAppDispatcherResponse.convertRelativeURIToURL", "184", this);
            webAppRootURI = "/";
        }

        try
        {
            relativeToRootURI = relativeURI.startsWith("/");

            // If relative URI begins with "./" (current level in URL hierarchy) then remove it
            if (relativeURI.startsWith("./"))
                relativeURI = relativeURI.substring(2);

            IExtendedRequest request = _request;

            // 112000 - begin - there was no reason to have two schemes for handling relative to root vs.
            // not relative to root so redid the section below

            // add the server info to the front of the url
            int urlPort = request.getServerPort();

            StringBuffer url = new StringBuffer(urlScheme);
            url.append("://");
            url.append(request.getServerName());

            // Add the port to the URL if it is not the default one for the scheme
            if (((urlScheme.equals("http") == true) && (urlPort != 80)) || ((urlScheme.equals("https") == true) && (urlPort != 443)))
            {
                url.append(":");
                url.append(urlPort);
            }

            String redirectURL = null;
            if (relativeToRootURI)
            {
                // 115010 - begin
                // if the relative URI begins with '/' and we're in sendRedirect compatibility mode, then make uri 
                // relative to the context root...otherwise, make it relative to the server (no context root)
                if (com.ibm.ws.webcontainer.util.WebContainerSystemProps.getSendRedirectCompatibilty())
                {
                    // append webapp context and relative URI
                    url.append(webAppRootURI);
                    url.append(relativeURI);
                }
                else
                {
                    url.append(relativeURI);
                }
                // 115010 - end
                redirectURL = url.toString();
            }
            else
            {
                // not relative to webapp context root, but relative to the presently
                // invoked URL

                String requestString = HttpUtils.getRequestURL((HttpServletRequest) request).toString();
                String pathInfo = request.getPathInfo();
                // start PI22830
                if(!webAppRootURI.equals("/") && request.getRequestURI().equals(webAppRootURI)){
                    requestString = requestString + "/";
                    if(com.ibm.ejs.ras.TraceComponent.isAnyTracingEnabled()&&logger.isLoggable (Level.FINE))
                        logger.logp(Level.FINE, CLASS_NAME,"convertRelativeURIToURL", "appended / to requestString --> " + requestString);
                } // end PI22830

                if ((pathInfo != null) && (pathInfo.length() > 0))
                {
                    if (!pathInfo.startsWith("/"))
                    {
                        pathInfo = "/" + pathInfo;
                    }
                    //start PK23779

                    if(redirectWithPathInfo || (com.ibm.ws.webcontainer.osgi.WebContainer.getServletContainerSpecLevel() >= 31)){
                        requestString = requestString.substring(0, requestString.lastIndexOf("/"));
                    }
                    else{
                        // Remove the PathInfo from the request
                        requestString = requestString.substring(0, requestString.lastIndexOf(pathInfo));
                    }
                    //end PK23779
                    redirectURL = requestString + "/" + relativeURI;
                }
                else
                {
                    redirectURL = requestString.substring(0, requestString.lastIndexOf('/') + 1) + relativeURI;
                }
            }
            if (redirectURL.indexOf("..") > -1)
            { // only normalize where required.. this is expensive.
                int skip = new String(urlScheme + "://").length();
                int split = redirectURL.indexOf("/", skip);
                String serverString = redirectURL.substring(0, split);
                String uri = redirectURL.substring(split);
                uri = getWebApp().normalize(uri);
                if (uri != null)
                {
                    return serverString + uri;
                }
            }
            else
            { // return url since it is already normalized.
                return redirectURL;
            }
            // 112000 - end
        }
        catch (Exception ex)
        {
            com.ibm.wsspi.webcontainer.util.FFDCWrapper.processException(ex, "com.ibm.ws.webcontainer.webapp.WebAppDispatcherResponse.convertRelativeURIToURL", "256", this);
        }

        // Could not convert
        return relativeURI;
    }
28772
public static final String getRequestUriWithoutContextPath(HttpServletRequest request) {
        String uri = request.getRequestURI().substring(request.getContextPath().length());
        if (!uri.isEmpty()) {
            return uri;
        } else {
            return "/";
        }
    }
1092710
public static String getHistoryResourceURI(String uri, ServletRequest req) {

        String histUri = uri;
        if (CmsHistoryResourceHandler.isHistoryRequest(req)) {
            String version = req.getParameter(CmsHistoryResourceHandler.PARAM_VERSION);
            histUri = CmsRequestUtil.appendParameter(uri, CmsHistoryResourceHandler.PARAM_VERSION, version);
        }
        return histUri;
    }
995152
public static String getFullRequestUrl(HttpServletRequest request) {
        StringBuilder buff = new StringBuilder(
                request.getRequestURL().toString());
        String queryString = request.getQueryString();
        if (queryString != null) {
            buff.append("?").append(queryString);
        }

        return buff.toString();
    }
929493
public static URI getRequestURI(HttpRequestMessage request, IoSession session) {
        URI requestURI = request.getRequestURI();
        String host = request.getHeader("Host");
        return getRequestURI(requestURI, host, session);
    }
1058852
public static boolean isHttpGetRequest(HttpServletRequest request) {
        boolean isSane = isSaneRequest(request);
        boolean isGet = isSane && request.getMethod().equals(HttpMethod.GET.toString());
        return isSane && isGet;
    }
500213
private URI redirectUriUsing(HttpServletRequest request) {
        URI redirect;

        URI redirectUri = oAuth2ServiceProperties.getRedirectUri();
        if (!redirectUri.isAbsolute()) {
            redirect = UriBuilder.fromPath(request.getContextPath())
                    .path(redirectUri.toString())
                    .scheme(request.getScheme())
                    .host(request.getServerName())
                    .port(request.getServerPort())
                    .build();
        } else {
            redirect = redirectUri;
        }

            return redirect;
    }
1237001
public static Tag uri(ClientRequest request) {
		String uri = (String) request.attribute(URI_TEMPLATE_ATTRIBUTE)
				.orElseGet(() -> request.url().getPath());
		return Tag.of("uri", extractPath(uri));
	}
399047
public static String getModulePathForRelativeURI( String uri )
    {
        if ( uri == null ) return null;
        assert uri.length() > 0;
        assert uri.charAt( 0 ) == '/' : uri;

        // Strip off the actual page name (e.g., some_page.jsp)
        int slash = uri.lastIndexOf( '/' );
        uri = uri.substring( 0, slash );

        return uri;
    }
359366
public static ActionResolver getCurrentActionResolver( HttpServletRequest request )
    {
        ServletContext servletContext = InternalUtils.getServletContext( request );
        return getCurrentActionResolver( request, servletContext );
    }
958187
protected URI createUri(String file,
      DatanodeID[] candidates,  UnixUserGroupInformation ugi,
      HttpServletRequest request) throws URISyntaxException {
    String scheme = request.getScheme();
    final DatanodeID host = candidates[0];
    final String hostname;
    if (host instanceof DatanodeInfo) {
      hostname = ((DatanodeInfo)host).getHostName();
    } else {
      hostname = host.getHost();
    }
    
    // Construct query.
    StringBuilder builder = new StringBuilder();
    builder.append("ugi=" + ugi);
    
    // Populate the rest of parameters.
    Enumeration<?> it = request.getParameterNames();
    while (it.hasMoreElements()) {
      String key = it.nextElement().toString();
      String value = request.getParameter(key);
      builder.append("&" + key + "=" + value);
    }

    // Construct the possible candidates for retry
    if (candidates.length > 1) {
      builder.append("&candidates=");
      appendDatanodeID(builder, candidates[1]);
      for (int j=2; j<candidates.length; j++) {
        builder.append(" ");
        appendDatanodeID(builder, candidates[j]);
      }
    }

    // Add namenode address to the url params
    NameNode nn = (NameNode)getServletContext().getAttribute("name.node");
    String addr = NetUtils.toIpPort(nn.getNameNodeAddress());
    builder.append(JspHelper.getUrlParam(JspHelper.NAMENODE_ADDRESS, addr));

    return new URI(scheme, null, hostname,
        "https".equals(scheme)
          ? (Integer)getServletContext().getAttribute("datanode.https.port")
          : host.getInfoPort(),
        "/streamFile" + file, builder.toString(), null);
  }
1074226
@Nullable
  public static URL uriToUrl(@Nullable Uri uri) {
    if (uri == null) {
      return null;
    }

    try {
      return new URL(uri.toString());
    } catch (java.net.MalformedURLException e) {
      // This should never happen since we got a valid uri
      throw new RuntimeException(e);
    }
  }
270594
@Override
    public String getURI() {
        ServletRESTRequestImpl ret = castRequest();
        if (ret != null)
            return ret.getURI();
        return null;
    }
731433
public void setRequest(HttpServletRequest request) {
		WaybackRequest wbRequest = new WaybackRequest();
		wbRequest.extractHttpRequestInfo(request);
		this.wbRequest = wbRequest;
	}
949804
private String getRequestPath(HttpServletRequest request) {
		String url = request.getServletPath();

		if (request.getPathInfo() != null) {
			url += request.getPathInfo();
		}

		return url;
	}
343322
public String getEndUserAuthorizationRequestUri(final HttpServletRequest request) {

		OAuthClientRequest oauthClientRequest;

		try {

			oauthClientRequest = OAuthClientRequest
				.authorizationLocation(authorizationLocation)
				.setClientId(clientId)
				.setRedirectURI(getAbsoluteUrl(request, redirectUri))
				.setScope(getScope())
				.setResponseType(getResponseType())
				.setState(getState())
				.buildQueryMessage();

			logger.info("Authorization request location URI: {}", oauthClientRequest.getLocationUri());

			return oauthClientRequest.getLocationUri();

		} catch (OAuthSystemException ex) {
			logger.error("", ex);
		}

		return null;

	}