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

The following examples show how to use javax.servlet.ServletRequest#getServerPort() . 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: HstsFilter.java    From browserprint with MIT License 6 votes vote down vote up
/**
 * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
 */
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
	if(request.getServerPort() == 443){
		Matcher m = domainPattern.matcher(request.getServerName());

		//Enable HSTS for domains other than ones starting with hsts\d+.
		if(m.matches() == false){
			((HttpServletResponse)response).setHeader("Strict-Transport-Security", "max-age=31622400");
		}
	}

	// pass the request along the filter chain
	if(chain != null){
		chain.doFilter(request, response);
	}
	else{
		return;
	}
}
 
Example 2
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 3
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 4
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 5
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 6
Source File: HttpFilter.java    From MultimediaDesktop with Apache License 2.0 4 votes vote down vote up
protected boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue) throws Exception {
    int requiredPort = toPort(mappedValue);
    int requestPort = request.getServerPort();
    return requiredPort == requestPort;
}
 
Example 7
Source File: PortFilter.java    From tapestry-security with Apache License 2.0 4 votes vote down vote up
protected boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue) throws Exception {
	int requiredPort = toPort(mappedValue);
	int requestPort = request.getServerPort();
	return requiredPort == requestPort;
}
 
Example 8
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());
}