Example usage for javax.servlet.http HttpServletRequest getLocalAddr

List of usage examples for javax.servlet.http HttpServletRequest getLocalAddr

Introduction

In this page you can find the example usage for javax.servlet.http HttpServletRequest getLocalAddr.

Prototype

public String getLocalAddr();

Source Link

Document

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

Usage

From source file:org.impalaframework.web.utils.WebPathUtils.java

public static void maybeLogRequest(HttpServletRequest request, Log logger) {

    if (logger.isDebugEnabled()) {
        logger.debug("Request context path: " + request.getContextPath());
        logger.debug("Request local address: " + request.getLocalAddr());
        logger.debug("Request local name: " + request.getLocalName());
        logger.debug("Request path info: " + request.getPathInfo());
        logger.debug("Request path translated: " + request.getPathTranslated());
        logger.debug("Request query string: " + request.getQueryString());
        logger.debug("Request servlet path: " + request.getServletPath());
        logger.debug("Request request URI: " + request.getRequestURI());
        logger.debug("Request request URL: " + request.getRequestURL());
        logger.debug("Request session ID: " + request.getRequestedSessionId());
    }//from w  ww . j  a va 2s  . com
}

From source file:net.jadler.stubbing.server.jetty.RequestUtils.java

public static Request convert(HttpServletRequest source) throws IOException {
    String method = source.getMethod();
    URI requestUri = URI.create(source.getRequestURL() + getQueryString(source));
    InputStream body = source.getInputStream();
    InetSocketAddress localAddress = new InetSocketAddress(source.getLocalAddr(), source.getLocalPort());
    InetSocketAddress remoteAddress = new InetSocketAddress(source.getRemoteAddr(), source.getRemotePort());
    String encoding = source.getCharacterEncoding();
    Map<String, List<String>> headers = converHeaders(source);
    return new Request(method, requestUri, headers, body, localAddress, remoteAddress, encoding);
}

From source file:com.beginner.core.utils.ProjectUtil.java

/**
 * ?IP(?????)/*from  w  w w. j a  va 2 s .com*/
 */
public static String ip() {
    HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
            .getRequest();
    return request.getLocalAddr();
}

From source file:org.keycloak.quickstart.appjee.ServiceClient.java

private static String getServiceUrl(HttpServletRequest req, KeycloakSecurityContext session) {
    String uri = req.getServletContext().getInitParameter(SERVICE_URI_INIT_PARAM_NAME);
    if (uri != null && !uri.contains("localhost"))
        return uri;

    String ip = req.getLocalAddr();

    if (ip != null) {
        ip = "localhost";
        try {/*from w w w.  jav a 2 s .c  o  m*/
            ip = java.net.InetAddress.getLocalHost().getHostAddress();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    return "http://" + ip + ":8080/service";
}

From source file:com.evolveum.midpoint.web.security.MidPointAuthenticationProvider.java

public static String getRemoteHost() {
    WebRequest req = (WebRequest) RequestCycle.get().getRequest();
    HttpServletRequest httpReq = (HttpServletRequest) req.getContainerRequest();
    String remoteIp = httpReq.getRemoteHost();

    String localIp = httpReq.getLocalAddr();

    if (remoteIp.equals(localIp)) {
        try {/*w  w  w  .  j  a  v  a2  s . c  om*/
            InetAddress inetAddress = InetAddress.getLocalHost();
            remoteIp = inetAddress.getHostAddress();
        } catch (UnknownHostException ex) {
            LOGGER.error("Can't get local host: " + ex.getMessage());
        }
    }
    return remoteIp;
}

From source file:de.adorsys.oauth.authdispatcher.FixedServletUtils.java

/**
 * Reconstructs the request URL string for the specified servlet
 * request. The host part is always the local IP address. The query
 * string and fragment is always omitted.
 *
 * @param request The servlet request. Must not be {@code null}.
 *
 * @return The reconstructed request URL string.
 */// w w  w .  j  a  v a  2 s .  c o  m
private static String reconstructRequestURLString(final HttpServletRequest request) {

    StringBuilder sb = new StringBuilder("http");

    if (request.isSecure())
        sb.append('s');

    sb.append("://");

    String localAddress = request.getLocalAddr();

    if (localAddress.contains(".")) {
        // IPv3 address
        sb.append(localAddress);
    } else if (localAddress.contains(":")) {
        // IPv6 address, see RFC 2732
        sb.append('[');
        sb.append(localAddress);
        sb.append(']');
    } else {
        // Don't know what to do
    }

    if (!request.isSecure() && request.getLocalPort() != 80) {
        // HTTP plain at port other than 80
        sb.append(':');
        sb.append(request.getLocalPort());
    }

    if (request.isSecure() && request.getLocalPort() != 443) {
        // HTTPS at port other than 443 (default TLS)
        sb.append(':');
        sb.append(request.getLocalPort());
    }

    String path = request.getRequestURI();

    if (path != null)
        sb.append(path);

    return sb.toString();
}

From source file:org.easyrec.util.core.Web.java

/**
 * This function returns a relative servlet url into a complete one. e.g.
 * /peppi?id=43 --> http://localhost:8080/sat-xxx/peppi?id=43
 *
 * @param request HttpServletRequest/*from   w w  w.  j a  v  a2s .c o  m*/
 * @param servlet String
 * @return String
 */
@SuppressWarnings({ "UnusedDeclaration" })
public static String createRequestFromServlet(HttpServletRequest request, String servlet) {
    return request.getScheme() + "://" + request.getLocalAddr() + ":" + request.getLocalPort()
            + request.getContextPath() + servlet;
}

From source file:org.hx.rainbow.common.web.session.RainbowSession.java

public static void web2Service(HttpServletRequest request) {
    if (request == null) {
        return;//  w  ww .  ja va  2 s  .  com
    }
    setProperty(ThreadConstants.CONSTMER_IPADDRESS, request.getRemoteAddr());
    setProperty(ThreadConstants.CONSTMER_HOST, request.getRemoteHost());
    setProperty(ThreadConstants.CONSTMER_PORT, request.getRemotePort());
    setProperty(ThreadConstants.SERVICE_IPADDRESS, request.getLocalAddr());
    setProperty(ThreadConstants.SERVICE_HOST, request.getLocalName());
    setProperty(ThreadConstants.RAINBOW_REQUEST, request);

    HttpSession session = request.getSession();

    RainbowUser rainbowUser = (RainbowUser) session.getAttribute(ThreadConstants.RAINBOW_USER);
    if (rainbowUser != null) {
        setProperty(ThreadConstants.RAINBOW_SESSION, rainbowUser);
        setProperty(ThreadConstants.RAINBOW_LOGINID, rainbowUser.getUsername());
        setProperty(ThreadConstants.RAINBOW_USERNAME, rainbowUser.getSessionData().get("name"));

        String sessionKeys = (String) PropertiesUtil.getInstance().read(THREAD_LOACL_FILE)
                .get(ThreadConstants.SESSION_KEYS);
        if (sessionKeys != null) {
            if (sessionKeys.equals("*")) {
                Enumeration<String> attrNames = session.getAttributeNames();
                while (attrNames.hasMoreElements()) {
                    String attrName = (String) attrNames.nextElement();
                    if (SESSION_KEYS.contains(attrName)) {
                        continue;
                    }
                    if (attrName != null) {
                        Object session_attr = session.getAttribute(attrName);
                        if (session_attr != null) {
                            rainbowUser.getSessionData().put(attrName, session_attr);
                        }
                    }
                }
            } else {
                String[] s_sessionkey = StringUtils.split(sessionKeys, ",");
                for (int i = 0; i < s_sessionkey.length; i++) {
                    if (s_sessionkey[i] != null) {
                        Object session_attr = session.getAttribute(s_sessionkey[i]);
                        if (session_attr != null) {
                            rainbowUser.getSessionData().put(s_sessionkey[i], session_attr);
                        }
                    }
                }
            }
        }
    }
    //   
    //
    //
    //
    //      Map<String, Object> inCookie = new ConcurrentHashMap<String, Object>();
    //      String cookieKeys = (String)PropertiesUtil.get(ThreadConstants.resource_cookieKeys);
    //
    //      if (cookieKeys != null) {
    //         Cookie[] cookies = request.getCookies();
    //         if (cookies != null) {
    //            if (cookieKeys.equals("*")) {
    //               for (int i = 0; i < cookies.length; i++) {
    //                  Cookie cookie = cookies[i];
    //                  String cookieName = cookie.getName();
    //                  String cookieValue = cookie.getValue();
    //                  if(cookieName != null && cookieValue != null){
    //                     inCookie.put(cookieName, cookieValue);
    //                  }
    //               }
    //            } else {
    //               cookieKeys = cookieKeys + ",";
    //               for (int i = 0; i < cookies.length; i++) {
    //                  Cookie cookie = cookies[i];
    //                  String cookieName = cookie.getName();
    //                  if (cookieKeys.indexOf(cookieName + ",") > -1) {
    //                     String cookieValue = cookie.getValue();
    //                     if(cookieName != null && cookieValue != null){
    //                        inCookie.put(cookieName, cookieValue);
    //                     }
    //                  }
    //               }
    //            }
    //         }
    //         setProperty(ThreadConstants.IN_COOKIE, inCookie);
    //      }

}

From source file:org.ambraproject.wombat.controller.FeedbackController.java

private static Map<String, String> getUserSessionAttributes(HttpServletRequest request) {
    Map<String, String> headers = new LinkedHashMap<>();

    for (String headerName : Collections.list(request.getHeaderNames())) {
        List<String> headerValues = Collections.list(request.getHeaders(headerName));
        headers.put(headerName, headerValues.stream().collect(joinWithComma()));
    }// w  ww .ja  v a  2 s  .  c  o m

    headers.put("server-name", request.getServerName() + ":" + request.getServerPort());
    headers.put("remote-addr", request.getRemoteAddr());
    headers.put("local-addr", request.getLocalAddr() + ":" + request.getLocalPort());

    /*
     * Keeping this in case more values get passed from the client other than just the visible form
     * fields
     */
    for (String paramName : Collections.list(request.getParameterNames())) {
        String[] paramValues = request.getParameterValues(paramName);
        headers.put(paramName, Stream.of(paramValues).collect(joinWithComma()));
    }

    return headers;
}

From source file:fr.paris.lutece.util.http.SecurityUtil.java

/**
 * Write request variables into the dump stringbuffer
 * @param sb The dump stringbuffer/* w w w.ja  v  a 2  s. c o m*/
 * @param request The HTTP request
 */
private static void dumpVariables(StringBuffer sb, HttpServletRequest request) {
    dumpVariable(sb, "AUTH_TYPE", request.getAuthType());
    dumpVariable(sb, "REQUEST_METHOD", request.getMethod());
    dumpVariable(sb, "PATH_INFO", request.getPathInfo());
    dumpVariable(sb, "PATH_TRANSLATED", request.getPathTranslated());
    dumpVariable(sb, "QUERY_STRING", request.getQueryString());
    dumpVariable(sb, "REQUEST_URI", request.getRequestURI());
    dumpVariable(sb, "SCRIPT_NAME", request.getServletPath());
    dumpVariable(sb, "LOCAL_ADDR", request.getLocalAddr());
    dumpVariable(sb, "SERVER_PROTOCOL", request.getProtocol());
    dumpVariable(sb, "REMOTE_ADDR", request.getRemoteAddr());
    dumpVariable(sb, "REMOTE_HOST", request.getRemoteHost());
    dumpVariable(sb, "HTTPS", request.getScheme());
    dumpVariable(sb, "SERVER_NAME", request.getServerName());
    dumpVariable(sb, "SERVER_PORT", String.valueOf(request.getServerPort()));
}