Example usage for javax.servlet ServletRequest getLocalName

List of usage examples for javax.servlet ServletRequest getLocalName

Introduction

In this page you can find the example usage for javax.servlet ServletRequest getLocalName.

Prototype

public String getLocalName();

Source Link

Document

Returns the host name of the Internet Protocol (IP) interface on which the request was received.

Usage

From source file:com.springsource.hq.plugin.tcserver.serverconfig.web.support.RequestUtils.java

/**
 * Returns the url to access HQ locally, i.e. without routing through any proxy or load balancer which may be in
 * front of HQ./*from  ww  w  .ja v a 2s  .  c  om*/
 * 
 * @param request A request received by the HQ server from which the HQ URL will be determined
 * @return the local URL for the HQ server hosting the web app
 */
public static String getLocalHqUrl(ServletRequest request) {
    StringBuilder serverUrl = new StringBuilder();

    serverUrl.append(request.getScheme());
    serverUrl.append("://");
    String hostName = request.getLocalName();
    if (hostName.contains(":")) {
        hostName = "[" + hostName + "]";
    }
    serverUrl.append(hostName);
    serverUrl.append(":");
    serverUrl.append(request.getLocalPort());
    if (request.isSecure()) {
        LOGGER.debug("Registering protocol.");
        UntrustedSSLProtocolSocketFactory.register();
    }
    return serverUrl.toString();
}

From source file:org.apache.solr.servlet.SolrDispatchFilter.java

private boolean authenticateRequest(ServletRequest request, ServletResponse response,
        final AtomicReference<ServletRequest> wrappedRequest) throws IOException {
    boolean requestContinues = false;
    final AtomicBoolean isAuthenticated = new AtomicBoolean(false);
    AuthenticationPlugin authenticationPlugin = cores.getAuthenticationPlugin();
    if (authenticationPlugin == null) {
        return true;
    } else {//from  ww w  .j ava 2s .c o  m
        // /admin/info/key must be always open. see SOLR-9188
        // tests work only w/ getPathInfo
        //otherwise it's just enough to have getServletPath()
        if (PKIAuthenticationPlugin.PATH.equals(((HttpServletRequest) request).getServletPath())
                || PKIAuthenticationPlugin.PATH.equals(((HttpServletRequest) request).getPathInfo()))
            return true;
        String header = ((HttpServletRequest) request).getHeader(PKIAuthenticationPlugin.HEADER);
        if (header != null && cores.getPkiAuthenticationPlugin() != null)
            authenticationPlugin = cores.getPkiAuthenticationPlugin();
        try {
            log.debug("Request to authenticate: {}, domain: {}, port: {}", request, request.getLocalName(),
                    request.getLocalPort());
            // upon successful authentication, this should call the chain's next filter.
            requestContinues = authenticationPlugin.doAuthenticate(request, response, (req, rsp) -> {
                isAuthenticated.set(true);
                wrappedRequest.set(req);
            });
        } catch (Exception e) {
            log.info("Error authenticating", e);
            throw new SolrException(ErrorCode.SERVER_ERROR, "Error during request authentication, ", e);
        }
    }
    // requestContinues is an optional short circuit, thus we still need to check isAuthenticated.
    // This is because the AuthenticationPlugin doesn't always have enough information to determine if
    // it should short circuit, e.g. the Kerberos Authentication Filter will send an error and not
    // call later filters in chain, but doesn't throw an exception.  We could force each Plugin
    // to implement isAuthenticated to simplify the check here, but that just moves the complexity to
    // multiple code paths.
    if (!requestContinues || !isAuthenticated.get()) {
        response.flushBuffer();
        return false;
    }
    return true;
}

From source file:se.natusoft.osgi.aps.rpchttpextender.servlet.RPCServlet.java

/**
 * Catch our host and port information which as far as I can determine is only possible to get from a request.
 *
 * @param req/*  w w  w  .  j av a  2 s.co  m*/
 * @param resp
 * @throws ServletException
 * @throws IOException
 */
public void service(ServletRequest req, ServletResponse resp) throws ServletException, IOException {
    if (this.rpcBaseUrl == null) {
        String protocol = req.getProtocol().split("/")[0].toLowerCase();
        if (req.getServerName() != null) {
            this.serverHost = req.getServerName();
        } else if (req.getLocalName() != null) {
            this.serverHost = req.getLocalName();
        }
        if (this.serverHost.equals("localhost")) {
            this.serverHost = InetAddress.getLocalHost().getHostName();
        }
        this.serverPort = req.getServerPort();
        this.rpcBaseUrl = protocol + "://" + this.serverHost + ":" + this.serverPort + "/apsrpc/";

        try {
            onServiceAvailable(this.discoveryServiceTracker.allocateService(), null);
            this.discoveryServiceTracker.releaseService();
        } catch (Exception e) {
        }
    }
    super.service(req, resp);
}