From 48e2cb69a08870ab8c95baaa052e6a06edbf534f Mon Sep 17 00:00:00 2001 From: charitha Date: Thu, 18 Jun 2020 11:02:57 +0530 Subject: [PATCH] Add IoT-Node-IP response header --- .../framework/WebappAuthenticationValve.java | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/main/java/org/wso2/carbon/webapp/authenticator/framework/WebappAuthenticationValve.java b/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/main/java/org/wso2/carbon/webapp/authenticator/framework/WebappAuthenticationValve.java index 1370482cd2..b987c14311 100644 --- a/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/main/java/org/wso2/carbon/webapp/authenticator/framework/WebappAuthenticationValve.java +++ b/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/main/java/org/wso2/carbon/webapp/authenticator/framework/WebappAuthenticationValve.java @@ -36,6 +36,11 @@ import org.wso2.carbon.webapp.authenticator.framework.authenticator.WebappAuthen import org.wso2.carbon.webapp.authenticator.framework.authorizer.WebappTenantAuthorizer; import javax.servlet.http.HttpServletResponse; +import java.net.InetAddress; +import java.net.NetworkInterface; +import java.net.SocketException; +import java.util.Enumeration; +import java.util.HashMap; import java.util.StringTokenizer; import java.util.TreeMap; import java.util.regex.Pattern; @@ -49,6 +54,31 @@ public class WebappAuthenticationValve extends CarbonTomcatValve { @Override public void invoke(Request request, Response response, CompositeValve compositeValve) { + if (response != null) { + if (inetAddress == null) { + try { + Enumeration ifaces = NetworkInterface.getNetworkInterfaces(); + while (ifaces.hasMoreElements()) { + NetworkInterface iface = ifaces.nextElement(); + if (!iface.isLoopback() && iface.isUp()) { + Enumeration addresses = iface.getInetAddresses(); + while (addresses.hasMoreElements()) { + inetAddress = addresses.nextElement(); + break; + } + } + break; + } + } catch (SocketException e) { + if (log.isDebugEnabled()) { + log.debug("Unable to get IP address of the node.", e); + } + } + } + if (inetAddress != null) { + response.setHeader("IoT-Node-IP", inetAddress.getHostAddress()); + } + } if ((this.isContextSkipped(request) || this.skipAuthentication(request)) && (StringUtils.isEmpty(request.getHeader(AUTHORIZE_PERMISSION)))) {