Java Code Examples for javax.servlet.ServletRequest#getServerName()

The following examples show how to use javax.servlet.ServletRequest#getServerName() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CorsFilter.java    From Explorer with Apache License 2.0 6 votes vote down vote up
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain)
        throws IOException, ServletException {
    String sourceHost = request.getServerName();
    String currentHost = java.net.InetAddress.getLocalHost().getHostName();
    String origin = "";
    if (currentHost.equals(sourceHost) || "localhost".equals(sourceHost)) {
        origin = ((HttpServletRequest) request).getHeader("Origin");
    }

    if (((HttpServletRequest) request).getMethod().equals("OPTIONS")) {
        HttpServletResponse resp = ((HttpServletResponse) response);
        addCorsHeaders(resp, origin);
        return;
    }

    if (response instanceof HttpServletResponse) {
        HttpServletResponse alteredResponse = ((HttpServletResponse) response);
        addCorsHeaders(alteredResponse, origin);
    }
    filterChain.doFilter(request, response);
}
 
Example 2
Source File: UrlServletHelper.java    From scipio-erp with Apache License 2.0 5 votes vote down vote up
public static void setRequestAttributes(ServletRequest request, Delegator delegator, ServletContext servletContext) {
    HttpServletRequest httpRequest = (HttpServletRequest) request;
    // check if multi tenant is enabled
    boolean useMultitenant = EntityUtil.isMultiTenantEnabled();
    if (useMultitenant) {
        // get tenant delegator by domain name
        String serverName = request.getServerName();
        try {
            // if tenant was specified, replace delegator with the new per-tenant delegator and set tenantId to session attribute
            delegator = getDelegator(servletContext);

            //Use base delegator for fetching data from entity of entityGroup org.ofbiz.tenant
            Delegator baseDelegator = DelegatorFactory.getDelegator(delegator.getDelegatorBaseName());
            GenericValue tenantDomainName = EntityQuery.use(baseDelegator).from("TenantDomainName").where("domainName", serverName).queryOne();

            if (UtilValidate.isNotEmpty(tenantDomainName)) {
                String tenantId = tenantDomainName.getString("tenantId");
                // make that tenant active, setup a new delegator and a new dispatcher
                String tenantDelegatorName = delegator.getDelegatorBaseName() + "#" + tenantId;
                httpRequest.getSession().setAttribute("delegatorName", tenantDelegatorName);

                // after this line the delegator is replaced with the new per-tenant delegator
                delegator = DelegatorFactory.getDelegator(tenantDelegatorName);
                servletContext.setAttribute("delegator", delegator);
            }

        } catch (GenericEntityException e) {
            Debug.logWarning(e, "Unable to get Tenant", module);
        }
    }

    // set the web context in the request for future use
    request.setAttribute("servletContext", httpRequest.getServletContext()); // SCIPIO: NOTE: no longer need getSession() for getServletContext(), since servlet API 3.0
    request.setAttribute("delegator", delegator);

    // set the webSiteId in the session
    if (UtilValidate.isEmpty(httpRequest.getSession().getAttribute("webSiteId"))){
        httpRequest.getSession().setAttribute("webSiteId", httpRequest.getServletContext().getAttribute("webSiteId")); // SCIPIO: NOTE: no longer need getSession() for getServletContext(), since servlet API 3.0
    }
}
 
Example 3
Source File: MCRFrontendUtil.java    From mycore with GNU General Public License v3.0 5 votes vote down vote up
/**
 * returns the base URL of the mycore system. This method uses the request to 'calculate' the right baseURL.
 * Generally it is sufficent to use {@link #getBaseURL()} instead.
 */
public static String getBaseURL(ServletRequest req) {
    HttpServletRequest request = (HttpServletRequest) req;
    String scheme = req.getScheme();
    String host = req.getServerName();
    int serverPort = req.getServerPort();
    String path = request.getContextPath() + "/";

    if (TRUSTED_PROXIES.contains(req.getRemoteAddr())) {
        scheme = Optional.ofNullable(request.getHeader(PROXY_HEADER_SCHEME)).orElse(scheme);
        host = Optional.ofNullable(request.getHeader(PROXY_HEADER_HOST)).orElse(host);
        serverPort = Optional.ofNullable(request.getHeader(PROXY_HEADER_PORT))
            .map(Integer::parseInt)
            .orElse(serverPort);
        path = Optional.ofNullable(request.getHeader(PROXY_HEADER_PATH)).orElse(path);
        if (!path.endsWith("/")) {
            path += "/";
        }
    }
    StringBuilder webappBase = new StringBuilder(scheme);
    webappBase.append("://");
    webappBase.append(host);
    if (!("http".equals(scheme) && serverPort == 80 || "https".equals(scheme) && serverPort == 443)) {
        webappBase.append(':').append(serverPort);
    }
    webappBase.append(path);
    return webappBase.toString();
}
 
Example 4
Source File: RewriteFilter.java    From development with Apache License 2.0 5 votes vote down vote up
/**
 * Rewrites the outgoing stream to make sure URLs and headers
 * are correct. The incoming request is first processed to 
 * identify what resource we want to proxy.
 * 
 * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)
 */
public void doFilter(ServletRequest request, ServletResponse response,
        FilterChain filterChain) throws IOException, ServletException {
    
    if (response.isCommitted()) {
        log.info("Not proxying, already committed.");
        return;
    } else if (!(request instanceof HttpServletRequest)) {
        log.info("Request is not HttpRequest, will only handle HttpRequests.");
        return;
    } else if (!(response instanceof HttpServletResponse)) {
        log.info("Request is not HttpResponse, will only handle HttpResponses.");
        return;
    } else {
        HttpServletResponse httpResponse = (HttpServletResponse) response;
        HttpServletRequest httpRequest = (HttpServletRequest) request;
        
        Server server = serverChain.evaluate(httpRequest);
        if (server == null) {
            log.info("Could not find a rule for this request, will not do anything.");
            filterChain.doFilter(request, response);
        } else {
            httpRequest.setAttribute("proxyServer", server);
            
            String ownHostName = request.getServerName() + ":" + request.getServerPort();
            UrlRewritingResponseWrapper wrappedResponse;
            wrappedResponse = new UrlRewritingResponseWrapper(httpResponse, server, ownHostName, httpRequest.getContextPath(), serverChain);
            
            filterChain.doFilter(httpRequest, wrappedResponse);

            wrappedResponse.processStream();
        }
    }
}
 
Example 5
Source File: ExtractDeploymentAddressFilter.java    From training with MIT License 5 votes vote down vote up
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,
		ServletException {
	if (DEPLOY_BASE_URL == null) {
		DEPLOY_BASE_URL = "http://" + request.getServerName() + ":" + request.getServerPort()
				+ request.getServletContext().getContextPath();
	}
	chain.doFilter(request, response);
}
 
Example 6
Source File: AppUrlBaseFilter.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
    throws IOException, ServletException {
    String appUrlBase =
        request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort();
    System.setProperty(APP_URL_BASE, appUrlBase);
    chain.doFilter(request, response);
}
 
Example 7
Source File: ServletUtil.java    From webauthn4j-spring-security with Apache License 2.0 2 votes vote down vote up
/**
 * Returns {@link Origin} corresponding {@link ServletRequest} url
 *
 * @param request http servlet request
 * @return the {@link Origin}
 */
public static Origin getOrigin(ServletRequest request) {
    return new Origin(request.getScheme(), request.getServerName(), request.getServerPort());
}