/** * Determines the servlet's context path. * * This is here to try and deal with the wide variation in servers and what they return. * * getServletContext().getContextPath() is supposed to return the path to the specific servlet we are deployed as but it's not available everywhere. On some servers getServletContext() can return * null (old Jetty seems to suffer from this, see hapi-fhir-base-test-mindeps-server) and on other old servers (Servlet 2.4) getServletContext().getContextPath() doesn't even exist. * * theRequest.getContextPath() returns the context for the specific incoming request. It should be available everywhere, but it's likely to be less predicable if there are multiple servlet mappings * pointing to the same servlet, so we don't favour it. This is possibly not the best strategy (maybe we should just always use theRequest.getContextPath()?) but so far people seem happy with this * behavour across a wide variety of platforms. * * If you are having troubles on a given platform/configuration and want to suggest a change or even report incompatibility here, we'd love to hear about it. */ public static String determineServletContextPath(HttpServletRequest theRequest, RestfulServer server) { String retVal; if (server.getServletContext() != null) { if (server.getServletContext().getMajorVersion() >= 3 || (server.getServletContext().getMajorVersion() > 2 && server.getServletContext().getMinorVersion() >= 5)) { retVal = server.getServletContext().getContextPath(); } else { retVal = theRequest.getContextPath(); } } else { retVal = theRequest.getContextPath(); } retVal = StringUtils.defaultString(retVal); return retVal; }
/** * Returns the server base URL (with no trailing '/') for a given request */ public String getServerBaseForRequest(ServletRequestDetails theRequest) { String fhirServerBase; fhirServerBase = myServerAddressStrategy.determineServerBase(getServletContext(), theRequest.getServletRequest()); if (fhirServerBase.endsWith("/")) { fhirServerBase = fhirServerBase.substring(0, fhirServerBase.length() - 1); } if (myTenantIdentificationStrategy != null) { fhirServerBase = myTenantIdentificationStrategy.massageServerBaseUrl(fhirServerBase, theRequest); } return fhirServerBase; }
requestDetails.setServletResponse(theResponse); theRequest.setAttribute(SERVLET_CONTEXT_ATTRIBUTE, getServletContext());
/** * Determines the servlet's context path. * * This is here to try and deal with the wide variation in servers and what they return. * * getServletContext().getContextPath() is supposed to return the path to the specific servlet we are deployed as but it's not available everywhere. On some servers getServletContext() can return * null (old Jetty seems to suffer from this, see hapi-fhir-base-test-mindeps-server) and on other old servers (Servlet 2.4) getServletContext().getContextPath() doesn't even exist. * * theRequest.getContextPath() returns the context for the specific incoming request. It should be available everywhere, but it's likely to be less predicable if there are multiple servlet mappings * pointing to the same servlet, so we don't favour it. This is possibly not the best strategy (maybe we should just always use theRequest.getContextPath()?) but so far people seem happy with this * behavour across a wide variety of platforms. * * If you are having troubles on a given platform/configuration and want to suggest a change or even report incompatibility here, we'd love to hear about it. */ public static String determineServletContextPath(HttpServletRequest theRequest, RestfulServer server) { String retVal; if (server.getServletContext() != null) { if (server.getServletContext().getMajorVersion() >= 3 || (server.getServletContext().getMajorVersion() > 2 && server.getServletContext().getMinorVersion() >= 5)) { retVal = server.getServletContext().getContextPath(); } else { retVal = theRequest.getContextPath(); } } else { retVal = theRequest.getContextPath(); } retVal = StringUtils.defaultString(retVal); return retVal; }
/** * Returns the server base URL (with no trailing '/') for a given request */ public String getServerBaseForRequest(ServletRequestDetails theRequest) { String fhirServerBase; fhirServerBase = myServerAddressStrategy.determineServerBase(getServletContext(), theRequest.getServletRequest()); if (fhirServerBase.endsWith("/")) { fhirServerBase = fhirServerBase.substring(0, fhirServerBase.length() - 1); } if (myTenantIdentificationStrategy != null) { fhirServerBase = myTenantIdentificationStrategy.massageServerBaseUrl(fhirServerBase, theRequest); } return fhirServerBase; }
requestDetails.setServletResponse(theResponse); theRequest.setAttribute(SERVLET_CONTEXT_ATTRIBUTE, getServletContext());