getting clients ip address in grails

1,762 views
Skip to first unread message

sensex

unread,
Jan 2, 2011, 12:17:12 PM1/2/11
to Indian Groovy & Grails User Group
hello everyone.. i m a newbie in flex working on a project with grails
flex and granite data service combination now i just wonder how we can
get client ip address in grails i m trying request.getRemoteAddr() but
it giving an error not able to find request or request is not a
compile time constant.

Antonio Pagano

unread,
Jan 4, 2011, 5:33:37 PM1/4/11
to Indian Groovy & Grails User Group
¿ Where do you do 'request.getRemoteAddr()', controller or service ?

anand sharma

unread,
Jan 4, 2011, 11:29:01 PM1/4/11
to ig...@googlegroups.com
hi.. i am doing it in service so please can you give me the syntax for the service...

Yagnesh Chawda

unread,
Jan 5, 2011, 7:55:58 AM1/5/11
to Indian Groovy & Grails User Group
Hi,
do following in your service class:


import org.springframework.web.context.request.RequestContextHolder

In an method within the Service you can do following stuffs :

def request = RequestContextHolder.requestAttributes.request
def session = RequestContextHolder.requestAttributes.session

This will give you access to Request and Session object within service
class.



On Jan 5, 9:29 am, anand sharma <anand2sha...@gmail.com> wrote:
> hi.. i am doing it in service so please can you give me the syntax for the
> service...
>
> On Wed, Jan 5, 2011 at 4:03 AM, Antonio Pagano
> <ing.antoniopag...@gmail.com>wrote:> ¿ Where do you do 'request.getRemoteAddr()', controller or service ?

Antonio Pagano

unread,
Jan 5, 2011, 8:56:05 AM1/5/11
to Indian Groovy & Grails User Group
Well, recently what i do is pass the request from the controller to
the service because the request is a controller bounded variable AFK,
what you can to is to call your service method passing the request
from controller as a parameter.

like this ( if you want to do it in a Service ) :

class Controller {
def myService;
def myAction = { def ip = myService.obtainRemoteIp( request );... }
}

class MyService {
public String obtainRemoteIp( def request ){
return request.getRemoteAddr();
}
}

I hope this work for you, anyway you can do this operation in
controller just :

class Controller {
def myService;
def myAction = { def ip = request.getRemoteAddr();... }
}

On 4 ene, 23:29, anand sharma <anand2sha...@gmail.com> wrote:
> hi.. i am doing it in service so please can you give me the syntax for the
> service...
>
> On Wed, Jan 5, 2011 at 4:03 AM, Antonio Pagano
> <ing.antoniopag...@gmail.com>wrote:> ¿ Where do you do 'request.getRemoteAddr()', controller or service ?

anand sharma

unread,
Jan 5, 2011, 11:21:16 AM1/5/11
to ig...@googlegroups.com
thanks for your reply i tried it but giving this request.getRemoteAddr() with the given solution error not even compiling...


"C:\Program Files\Java\jdk1.6.0\bin\java" "-Dgrails.home=C:\Program Files\grails-1.3.5" "-Dbase.dir=C:\Program Files\grails-1.3.5\grails applications\commerce" "-Dtools.jar=C:\Program Files\Java\jdk1.6.0\lib\tools.jar" "-Dgroovy.starter.conf=C:\Program Files\grails-1.3.5/conf/groovy-starter.conf" -Xmx512M -Didea.launcher.port=7537 "-Didea.launcher.bin.path=C:\Program Files\JetBrains\IntelliJ IDEA 10.0\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\grails-1.3.5\lib\groovy-all-1.7.5.jar;C:\Program Files\grails-1.3.5\dist\grails-bootstrap-1.3.5.jar;C:\Program Files\JetBrains\IntelliJ IDEA 10.0\lib\idea_rt.jar" com.intellij.rt.execution.application.AppMain org.codehaus.groovy.grails.cli.support.GrailsStarter --main org.codehaus.groovy.grails.cli.GrailsScriptRunner --conf "C:\Program Files\grails-1.3.5/conf/groovy-starter.conf" run-app
Welcome to Grails 1.3.5 - http://grails.org/
Licensed under Apache Standard License 2.0
Grails home is set to: C:\Program Files\grails-1.3.5

Base Directory: C:\Program Files\grails-1.3.5\grails applications\commerce
Resolving dependencies...
Dependencies resolved in 875ms.
Running script C:\Program Files\grails-1.3.5\scripts\RunApp.groovy
Environment set to development
Using Flex SDK: C:\Program Files\Adobe\Adobe Flash Builder 4 Plug-in\sdks\4.1.0
  [groovyc] Compiling 2 source files to C:\Program Files\grails-1.3.5\grails applications\commerce\target\classes
  [groovyc] org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
  [groovyc] C:\Program Files\grails-1.3.5\grails applications\commerce\grails-app\services\commerce\UserCAService.groovy: 16: unexpected token: println @ line 16, column 15.
  [groovyc]                  println request.getRemoteAddr()
  [groovyc]                  ^
  [groovyc] 
  [groovyc] 1 error
Compilation error: Compilation Failed

Process finished with exit code 1

Yagnesh Chawda

unread,
Jan 6, 2011, 12:49:08 AM1/6/11
to Indian Groovy & Grails User Group
Hi,
Are you referring to my solution or Antonio.
And as far as the compilation issue is concerned, i think there is
issue with line 15 as its expecting some parameter or something and
that is what causing compilation issue. Please confirm by removing
ref. to all request object and compile again

On Jan 5, 9:21 pm, anand sharma <anand2sha...@gmail.com> wrote:
> thanks for your reply i tried it but giving this request.getRemoteAddr()
> with the given solution error not even compiling...
>
> "C:\Program Files\Java\jdk1.6.0\bin\java" "-Dgrails.home=C:\Program
> Files\grails-1.3.5" "-Dbase.dir=C:\Program Files\grails-1.3.5\grails
> applications\commerce" "-Dtools.jar=C:\Program
> Files\Java\jdk1.6.0\lib\tools.jar" "-Dgroovy.starter.conf=C:\Program
> Files\grails-1.3.5/conf/groovy-starter.conf" -Xmx512M
> -Didea.launcher.port=7537 "-Didea.launcher.bin.path=C:\Program
> Files\JetBrains\IntelliJ IDEA 10.0\bin" -Dfile.encoding=UTF-8 -classpath
> "C:\Program Files\grails-1.3.5\lib\groovy-all-1.7.5.jar;C:\Program
> Files\grails-1.3.5\dist\grails-bootstrap-1.3.5.jar;C:\Program
> Files\JetBrains\IntelliJ IDEA 10.0\lib\idea_rt.jar"
> com.intellij.rt.execution.application.AppMain
> org.codehaus.groovy.grails.cli.support.GrailsStarter --main
> org.codehaus.groovy.grails.cli.GrailsScriptRunner --conf "C:\Program
> Files\grails-1.3.5/conf/groovy-starter.conf" run-app
> Welcome to Grails 1.3.5 -http://grails.org/
> Licensed under Apache Standard License 2.0
> Grails home is set to: C:\Program Files\grails-1.3.5
>
> Base Directory: C:\Program Files\grails-1.3.5\grails applications\commerce
> Resolving dependencies...
> Dependencies resolved in 875ms.
> Running script C:\Program Files\grails-1.3.5\scripts\RunApp.groovy
> Environment set to development
> Using Flex SDK: C:\Program Files\Adobe\Adobe Flash Builder 4
> Plug-in\sdks\4.1.0
>   [groovyc] Compiling 2 source files to C:\Program Files\grails-1.3.5\grails
> applications\commerce\target\classes
>   [groovyc] org.codehaus.groovy.control.MultipleCompilationErrorsException:
> startup failed:
>   [groovyc] C:\Program Files\grails-1.3.5\grails
> applications\commerce\grails-app\services\commerce\UserCAService.groovy: 16:
> unexpected token: println @ line 16, column 15.
>   [groovyc]                  println request.getRemoteAddr()
>   [groovyc]                  ^
>   [groovyc]
>   [groovyc] 1 error
> Compilation error: Compilation Failed
>
> Process finished with exit code 1
>

Antonio Pagano

unread,
Jan 6, 2011, 9:09:02 AM1/6/11
to Indian Groovy & Grails User Group
If you could show us more code, maybe we can help you more.
like Yagnesh says there is a compilation issue over there.

anand sharma

unread,
Jan 8, 2011, 1:51:30 AM1/8/11
to ig...@googlegroups.com
hey gyzz.. thanks for your solution ultimstely i am accessiung request object using your solution....
 
keep it touch...
Reply all
Reply to author
Forward
0 new messages