3

I would like to capture the user's IP address when downloading a bitstream from dspace. I have successfully captured the user's "Full name" by using this code:

        EPerson loggedin = context.getCurrentUser();
        String eperson = null;
        if (loggedin != null)
        {
            eperson = loggedin.getFullName();
        }
        else eperson = "Anonymous";

Now, I would also like to capture the IP address like those seen from the Control panel > Current Activity. Please advise on how can I achieve this and what methods should I use?

EDIT: I will implement this in the cover page feature (available in DSpace 5, xmlui), where the user's IP address will be displayed if the user did not log in or is an anonymous user (eg Download by [User's IP address]) when downloading the PDF. Example of PDF with cover page from Taylor and Francis. Note the first line in the articles' cover page: This article was downloaded by: [your IP address].

Please note that I have very limited knowledge in java and will gladly appreciate any suggestions and help.

Thanks in advance.

UPDATE

I tried using the answer from this post but when I do an mvn build, I'm having this error:

method getClientIpAddr in class CoverPage cannot be applied to given types;
[ERROR] required: javax.servlet.http.HttpServletRequest
[ERROR] found: no arguments

Would really appreciate if you can help me with this, and I deleted my answer to this post because it will return the external IP of the server and not the client's IP address.

Community
  • 1
  • 1
euler
  • 1,401
  • 2
  • 18
  • 39
  • Before looking at the java methods to return the ip address, I wanted to clarify something. Are you writing code that will run as a download takes place or are you writing code to report on past downloads?The ip addresses for each download are captured in the solr statistics records. You could construct a solr query to find these records and facet the results by ip address. Do you have the ability to query your solr statistics repo? – terrywb Feb 11 '15 at 04:44
  • @terrywb, the code I am writing is for the cover page feature, so that means as the download takes place. I would like to display in the citation cover page the IP address of the user who is downloading that bitstream. I have successfully captured the user's name if loggedin. My goal is if that user is anonymous, I would just display his/her IP address, like those from [JSTOR](http://www.maths.ed.ac.uk/~aar/papers/thurstonsimple.pdf) (please see footer of cover page). Thanks – euler Feb 11 '15 at 08:36

1 Answers1

2

Get it from the HttpServletRequest request.getRemoteAddr() example: https://github.com/DSpace/DSpace/blob/master/dspace-api/src/main/java/org/dspace/statistics/SolrLogger.java#L298

In xmlui, if you have an objectmodel map (E.g. AbstractDSpaceTransformer or Action have it) you can get the request from the ObjectModelHelper.getRequest(objectmodel). ObjectModelHelper gives you a org.apache.cocoon.environment.Request object which has the getRemoteAddr() method because it extends javax.servlet.http.HttpServletRequest.

Antoine Snyers
  • 682
  • 3
  • 7
  • I'm sorry but I don't know how to apply these, particularly which files should I import. I'm encountering the `cannot find symbol` errors because I might be using the wrong import. I have the `import javax.servlet.http.HttpServletRequest;`, I don't know what value should I put in `HttpServletRequest request =` so that I can use the `String ip = request.getRemoteAddr();` statement. Thanks. – euler Feb 13 '15 at 07:08
  • I added the exact references in my answer (the links) – Antoine Snyers Feb 13 '15 at 08:13
  • Thanks for these Antoine, but I'm afraid that it's beyond my knowledge right now. – euler Feb 13 '15 at 09:02