get IP of client

1
+1
-1

Hello team, I want to get the IP client, so what I did was put in the login.jsp :

String ip = request.getRemoteHost();

session.setAttribute("ipClient", ip);

This work ok, now i want to get the IP Attribute and set this value in a variable in my Process i.e., how can I do this?

3 answers

1
+1
-1

I'm not sure that you can have access to the Session from a Groovy connector

I have tried this code in a groovy script in a connector in the start of a process in V6:

String myIp = java.net.InetAddress.getLocalHost().getHostAddress();
apiAccessor.processAPI.updateProcessDataInstance("ip", processInstanceId, myIp)

Note: your host can have multiple network interfaces (wired, wireless ...) and hence multiple IP addresses. So getLocalHost() will select (almost) random one of them if you don't specify the interface explicitly.

Comments

Submitted by galbrend on Mon, 04/28/2014 - 14:10

the java.net.InetAddress.getLocalHost() returns the ip address of my local server that runs the bonita-server, i'm trying to obtain the ip address of the loggedUser, the person who initiated the process, with the code that i've supplied in the login.jsp file it actually stores the initiator's ip address prior to login/authentication. I haven't found a code to be used in the groovy editor that can obtain the initiator's ip address. Perhaps your example returns your localhost ip?

Submitted by jordi.anguela_1 on Tue, 06/03/2014 - 12:05

Hi galbrend, I'm sorry I have been dad for the second time in the last weeks :)

I think you are right, I was testing it in my localhost so the previous solution that I have proposed was not correct :(

As I said in my previous comment I don't how you can access to the Session from a Groovy script (may be it is not even possible).

I would propose you another way to do it, may be it works for you requirements. I will explain you the steps but if it is not clear I can send you the .bos file by email. * I create a global process variable call 'ip' * I create a form in order to start a new process case. * in this form I add a Hidden field that will store the client's IP * I select the Hidden field -> Options -> HTML attributes ->I add id="hip" * In the Data tab I assign the field_hiddenIp to my process variable 'ip' * Then I edit the Form html code and I add the following JS code to retrieve and set the IP before the end < /body> tag :

  <script type="application/javascript">
    function getip(json){
      alert(json.ip); // alerts the ip address
      var elem = document.getElementById("hip");
      console.log(json.ip);
      elem.value = json.ip;
    }
  </script>
  <script type="application/javascript" src="http://jsonip.appspot.com/?callback=getip"></script>

With this simple example I have retrieved my clients IP (using an external service) and store it in a process variable that can be used all along the process.

I hope it helps Jordi

1
0
-1

Host IP ---> java.net.InetAddress.getLocalHost().getHostName()
Host Name ---> java.net.InetAddress.getLocalHost().getHostAddress();

1
0
-1

Hello, you can easily set your process and task variables through the processApi.

you can you use for example: processApi.updateProcessDataInstance("processVarName", processInstanceId, you_client_ip)

remember that in order to call the Bonita Execution Engine APIs you need to have a Session open.

I hope it helps. Jordi

Comments

Submitted by galbrend on Mon, 04/28/2014 - 10:37

i'm using BOS 5.10, it seems like it can't find org.bonitasoft.engine.api library and therefor can't find the class ProcessAPI, what do i have to do in order to include it in the libraries?

Submitted by jordi.anguela_1 on Mon, 04/28/2014 - 11:52

The APIs have been refactored in v6. In BOS 5.x you have another different set of APIs.

you may use in V5 the RuntimeAPI and the method: [setProcessInstanceVariable](http://documentation.bonitasoft.com/javadoc/bpm_engine/5.9/org/ow2/bonit...(org.ow2.bonita.facade.uuid.ProcessInstanceUUID, java.lang.String, java.lang.Object))

Submitted by galbrend on Mon, 04/28/2014 - 12:46

Hi

I thank you for your help but it is still not clear to me how to use this.

I have added in the login.jsp file the lines:

String ip = request.getRemoteHost(); session.setAttribute("ipClient", ip);

what do i need to write in the groovy editor in a script-connector to be able to store the data of the "ipClient" variable in a process-variable?

do i need to add anything in addition in the login.jsp file?

Notifications