Capture local ip address and store it in a custom field at the time of issue creation

Ritesh Garg January 29, 2012

I need to store the local ip address in a custom field, whenever an issue is created. Any idea how that can be possible?? Thanks for help in advance

4 answers

1 accepted

1 vote
Answer accepted
Ritesh Garg January 30, 2012

I was able to get it done by modifying the setCustomValue fucntion in issueimpl class as follows

public void setCustomFieldValue(CustomField customField, Object value)

{

HttpServletRequest request = ExecutingHttpRequest.get();

if(customField.getName().equalsIgnoreCase("IP") && value == null || value.toString().trim().length()==0)

value = (String)request.getRemoteAddr(); ModifiedValue modifiedValue = new ModifiedValue(getCustomFieldValue(customField), value);

customFieldValues.put(customField, value);

modifiedFields.put(customField.getId(), modifiedValue);

}

Thanks everyone :)
JamieA
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 30, 2012

You don't want to be modifying IssueImpl...

Ritesh Garg January 31, 2012

Any other way we can do that?

1 vote
Colin Goudie
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 29, 2012

You should be able to get it from the HttpServletRequest.

You can get access to that object here - https://developer.atlassian.com/pages/viewpage.action?pageId=4227147

The method getRemoteAddr should return it, although it may also return the last proxy address instead. Someone with more knowledge of servlets might have a better way

Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 29, 2012

I can vouch for the point about proxies - the Jira here only records two ip addresses - the corporate proxy and the internet gateway.

I'd also point out that unless you're capturing inside a single network, you are almost certainly going to catch the wrong IP address. Example - even if there weren't an internet gateway here, I have three machines at home that I use to connect to Jira. According to the requests, they all have the same IP address - that of my router.

Ritesh Garg January 29, 2012

Thanks a lot for your reply Collin.. but m still not able to get it worked. Do u know where i need to make changes to use this method to set the value of custom field?? i really appreciate your response.

Ritesh Garg January 29, 2012

@Nic Brough- I agree.. But i am not able to find any way to store even the router ip in custom field and i am kind of stuck now. Any help will be really appreciated

Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 29, 2012

You're not telling us what you're doing. On the assumption that you are writing a post-function, then you need to grab a handle on to the target custom field, then poke the value you extract from the request header into it. But I don't know what you've got so far or exactly where you might be stuck

Ritesh Garg January 29, 2012

I have added a custom field named IP. and i want to store the local ip address in this field, whenever a new issue is created. So, that there will be a record of reporter ip address corresponding to each issue. I tried using javascript to provide this vaue but nothing works appropriately. Now, i am thinking if it might be possible to provide this value to custom field by changes in source code or any other better approach

Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 31, 2012

Ah, ok new issue, sorry, I didn't see that before.

I see you solved trapping and setting, although I 'd agree with Jamie in that you really don't want to mess with the code in that way - you really do want a post-function (that can use the code you've got to extract and set the field without messing with the main code)

0 votes
atlast last December 15, 2013

Use the following code snippet to find out ip address :

public class IPTest {


public static void main(String args[]) throws UnknownHostException {

InetAddress addr = InetAddress.getLocalHost();

//Getting IPAddress of localhost - getHostAddress return IP Address
// in textual format
String ipAddress = addr.getHostAddress();

System.out.println("IP address of localhost from Java Program: " + ipAddress);

//Hostname
String hostname = addr.getHostName();
System.out.println("Name of hostname : " + hostname);

after that , you can store it by using any new variable . You can implement any of the ip-address look up sites like Ip-details.com to find out ip address and store it as directly .

thank you !

0 votes
atlast last December 15, 2013

Use the following code snippet to find out ip address :

public class IPTest {
 
 
    public static void main(String args[]) throws UnknownHostException {
   
        InetAddress addr = InetAddress.getLocalHost();
     
        //Getting IPAddress of localhost - getHostAddress return IP Address
        // in textual format
        String ipAddress = addr.getHostAddress();
     
        System.out.println("IP address of localhost from Java Program: " + ipAddress);
     
        //Hostname
        String hostname = addr.getHostName();
        System.out.println("Name of hostname : " + hostname);

after that , you can store it by using any new variable . You can implement any of the ip-address look up sites like Ip-details.com to find out ip address and store it as directly .

thank you !

Suggest an answer

Log in or Sign up to answer