Finding the WEB-INF directory of a Running Grails Application

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Finding the WEB-INF directory of a Running Grails Application

chris8185
This may be a stupid question, but I want to upload some files to a
folder in the WEB-INF directory of a my grails application.
Is there an easy way to save an uploaded file to the WEB-INF directory
using a built-in grails utility?
I have a simple controller that processes the uploaded file like this:
        def fileName = params.get("fileName")
        def f = request.getFile("fileData")
        def file = new File(f.originalFilename)
        f.transferTo(file)
        render "saved the file " + file.getProperties()

Thanks,
Christopher Kwiatkowski

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: Finding the WEB-INF directory of a Running Grails Application

Graeme-Rocher
Use the servlet API.. servletContext.getRealPath("/WEB-INF")

Cheers

On Tue, Feb 26, 2008 at 8:41 PM, Christopher Kwiatkowski <[hidden email]> wrote:

> This may be a stupid question, but I want to upload some files to a
>  folder in the WEB-INF directory of a my grails application.
>  Is there an easy way to save an uploaded file to the WEB-INF directory
>  using a built-in grails utility?
>  I have a simple controller that processes the uploaded file like this:
>         def fileName = params.get("fileName")
>         def f = request.getFile("fileData")
>         def file = new File(f.originalFilename)
>         f.transferTo(file)
>         render "saved the file " + file.getProperties()
>
>  Thanks,
>  Christopher Kwiatkowski
>
>  ---------------------------------------------------------------------
>  To unsubscribe from this list, please visit:
>
>     http://xircles.codehaus.org/manage_email
>
>
>



--
Graeme Rocher
Grails Project Lead
G2One, Inc. Chief Technology Officer
http://www.g2one.com

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: Finding the WEB-INF directory of a Running Grails Application

Jason Morris-5
Any idea how you would do that in a service (i.e. where there is no servletContext available)?

Thanks,

Jason

On Tue, Feb 26, 2008 at 9:10 PM, Graeme Rocher <[hidden email]> wrote:
Use the servlet API.. servletContext.getRealPath("/WEB-INF")

Cheers

On Tue, Feb 26, 2008 at 8:41 PM, Christopher Kwiatkowski <[hidden email]> wrote:
> This may be a stupid question, but I want to upload some files to a
>  folder in the WEB-INF directory of a my grails application.
>  Is there an easy way to save an uploaded file to the WEB-INF directory
>  using a built-in grails utility?
>  I have a simple controller that processes the uploaded file like this:
>         def fileName = params.get("fileName")
>         def f = request.getFile("fileData")
>         def file = new File(f.originalFilename)
>         f.transferTo(file)
>         render "saved the file " + file.getProperties()
>
>  Thanks,
>  Christopher Kwiatkowski
>
>  ---------------------------------------------------------------------
>  To unsubscribe from this list, please visit:
>
>     http://xircles.codehaus.org/manage_email
>
>
>



--
Graeme Rocher
Grails Project Lead
G2One, Inc. Chief Technology Officer
http://www.g2one.com

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

   http://xircles.codehaus.org/manage_email



Reply | Threaded
Open this post in threaded view
|

Re: Finding the WEB-INF directory of a Running Grails Application

ericacm
import org.springframework.context.ApplicationContext
import org.springframework.context.ApplicationContextAware

class myService implements ApplicationContextAware {
  def servletContext
  void setApplicationContext(ApplicationContext context) { this.servletContext = context.getServletContext() }
//...
}

enjoy

On Wed, Feb 27, 2008 at 3:39 AM, Jason Morris <[hidden email]> wrote:
Any idea how you would do that in a service (i.e. where there is no servletContext available)?

Thanks,

Jason

On Tue, Feb 26, 2008 at 9:10 PM, Graeme Rocher <[hidden email]> wrote:
Use the servlet API.. servletContext.getRealPath("/WEB-INF")

Cheers

On Tue, Feb 26, 2008 at 8:41 PM, Christopher Kwiatkowski <[hidden email]> wrote:
> This may be a stupid question, but I want to upload some files to a
>  folder in the WEB-INF directory of a my grails application.
>  Is there an easy way to save an uploaded file to the WEB-INF directory
>  using a built-in grails utility?
>  I have a simple controller that processes the uploaded file like this:
>         def fileName = params.get("fileName")
>         def f = request.getFile("fileData")
>         def file = new File(f.originalFilename)
>         f.transferTo(file)
>         render "saved the file " + file.getProperties()
>
>  Thanks,
>  Christopher Kwiatkowski
>
>  ---------------------------------------------------------------------
>  To unsubscribe from this list, please visit:
>
>     http://xircles.codehaus.org/manage_email
>
>
>



--
Graeme Rocher
Grails Project Lead
G2One, Inc. Chief Technology Officer
http://www.g2one.com

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

   http://xircles.codehaus.org/manage_email






--
Eric
[hidden email]
Reply | Threaded
Open this post in threaded view
|

Re: Finding the WEB-INF directory of a Running Grails Application

Jason Morris-5
Eric you are a legend - consider it enjoyed! :-))))

Thanks so much,

Jason

On Wed, Feb 27, 2008 at 3:02 PM, Eric <[hidden email]> wrote:
import org.springframework.context.ApplicationContext
import org.springframework.context.ApplicationContextAware

class myService implements ApplicationContextAware {
  def servletContext
  void setApplicationContext(ApplicationContext context) { this.servletContext = context.getServletContext() }
//...
}

enjoy


On Wed, Feb 27, 2008 at 3:39 AM, Jason Morris <[hidden email]> wrote:
Any idea how you would do that in a service (i.e. where there is no servletContext available)?

Thanks,

Jason

On Tue, Feb 26, 2008 at 9:10 PM, Graeme Rocher <[hidden email]> wrote:
Use the servlet API.. servletContext.getRealPath("/WEB-INF")

Cheers

On Tue, Feb 26, 2008 at 8:41 PM, Christopher Kwiatkowski <[hidden email]> wrote:
> This may be a stupid question, but I want to upload some files to a
>  folder in the WEB-INF directory of a my grails application.
>  Is there an easy way to save an uploaded file to the WEB-INF directory
>  using a built-in grails utility?
>  I have a simple controller that processes the uploaded file like this:
>         def fileName = params.get("fileName")
>         def f = request.getFile("fileData")
>         def file = new File(f.originalFilename)
>         f.transferTo(file)
>         render "saved the file " + file.getProperties()
>
>  Thanks,
>  Christopher Kwiatkowski
>
>  ---------------------------------------------------------------------
>  To unsubscribe from this list, please visit:
>
>     http://xircles.codehaus.org/manage_email
>
>
>



--
Graeme Rocher
Grails Project Lead
G2One, Inc. Chief Technology Officer
http://www.g2one.com

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

   http://xircles.codehaus.org/manage_email






--
Eric
[hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: Finding the WEB-INF directory of a Running Grails Application

burtbeckwith
In reply to this post by ericacm
A little cleaner way to get the ServletContext via DI:

import org.springframework.web.context.ServletContextAware
import javax.servlet.ServletContext

class myService implements ServletContextAware {
   ServletContext servletContext
   void setServletContext(ServletContext context) {
     servletContext = context
  }
...
}

but if you just want to access the WEB-INF dir during a web request, you can
use RequestContextHolder.getRequestAttributes(), which gives you access to
the current request, response, servlet context, etc.:

import org.springframework.web.context.request.RequestContextHolder as RCH
...
File webinf = new
File(RCH.requestAttributes.servletContext.getRealPath('/WEB-INF'))
...

See org.codehaus.groovy.grails.web.servlet.mvc.GrailsWebRequest for the other
attributes that are available.

Burt

On Wednesday 27 February 2008 10:02:56 am Eric wrote:

> import org.springframework.context.ApplicationContext
> import org.springframework.context.ApplicationContextAware
>
> class myService implements ApplicationContextAware {
>   def servletContext
>   void setApplicationContext(ApplicationContext context) {
> this.servletContext = context.getServletContext() }
> //...
> }
>
> enjoy
>
> On Wed, Feb 27, 2008 at 3:39 AM, Jason Morris <[hidden email]>
>
> wrote:
> > Any idea how you would do that in a service (i.e. where there is no
> > servletContext available)?
> >
> > Thanks,
> >
> > Jason
> >
> > On Tue, Feb 26, 2008 at 9:10 PM, Graeme Rocher <[hidden email]> wrote:
> > > Use the servlet API.. servletContext.getRealPath("/WEB-INF")
> > >
> > > Cheers
> > >
> > > On Tue, Feb 26, 2008 at 8:41 PM, Christopher Kwiatkowski
> > > <[hidden email]>
> > >
> > > wrote:
> > > > This may be a stupid question, but I want to upload some files to a
> > > >  folder in the WEB-INF directory of a my grails application.
> > > >  Is there an easy way to save an uploaded file to the WEB-INF
> > >
> > > directory
> > >
> > > >  using a built-in grails utility?
> > > >  I have a simple controller that processes the uploaded file like
> > >
> > > this:
> > > >         def fileName = params.get("fileName")
> > > >         def f = request.getFile("fileData")
> > > >         def file = new File(f.originalFilename)
> > > >         f.transferTo(file)
> > > >         render "saved the file " + file.getProperties()
> > > >
> > > >  Thanks,
> > > >  Christopher Kwiatkowski
> > > >
> > > >
> > > > ---------------------------------------------------------------------
> > > > To unsubscribe from this list, please visit:
> > > >
> > > >     http://xircles.codehaus.org/manage_email
> > >
> > > --
> > > Graeme Rocher
> > > Grails Project Lead
> > > G2One, Inc. Chief Technology Officer
> > > http://www.g2one.com
> > >
> > > ---------------------------------------------------------------------
> > > To unsubscribe from this list, please visit:
> > >
> > >    http://xircles.codehaus.org/manage_email



---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: Finding the WEB-INF directory of a Running Grails Application

ericacm
In reply to this post by Jason Morris-5
no problem

there's a simpler alternative too (thanks to others on the list for pointing out ApplicationHolder)

import org.codehaus.groovy.grails.commons.ApplicationHolder

def servletContext = ApplicationHolder.getApplication().getParentContext().getServletContext()


On Wed, Feb 27, 2008 at 10:43 AM, Jason Morris <[hidden email]> wrote:
Eric you are a legend - consider it enjoyed! :-))))

Thanks so much,

Jason


On Wed, Feb 27, 2008 at 3:02 PM, Eric <[hidden email]> wrote:
import org.springframework.context.ApplicationContext
import org.springframework.context.ApplicationContextAware

class myService implements ApplicationContextAware {
  def servletContext
  void setApplicationContext(ApplicationContext context) { this.servletContext = context.getServletContext() }
//...
}

enjoy


On Wed, Feb 27, 2008 at 3:39 AM, Jason Morris <[hidden email]> wrote:
Any idea how you would do that in a service (i.e. where there is no servletContext available)?

Thanks,

Jason

On Tue, Feb 26, 2008 at 9:10 PM, Graeme Rocher <[hidden email]> wrote:
Use the servlet API.. servletContext.getRealPath("/WEB-INF")

Cheers

On Tue, Feb 26, 2008 at 8:41 PM, Christopher Kwiatkowski <[hidden email]> wrote:
> This may be a stupid question, but I want to upload some files to a
>  folder in the WEB-INF directory of a my grails application.
>  Is there an easy way to save an uploaded file to the WEB-INF directory
>  using a built-in grails utility?
>  I have a simple controller that processes the uploaded file like this:
>         def fileName = params.get("fileName")
>         def f = request.getFile("fileData")
>         def file = new File(f.originalFilename)
>         f.transferTo(file)
>         render "saved the file " + file.getProperties()
>
>  Thanks,
>  Christopher Kwiatkowski
>
>  ---------------------------------------------------------------------
>  To unsubscribe from this list, please visit:
>
>     http://xircles.codehaus.org/manage_email
>
>
>



--
Graeme Rocher
Grails Project Lead
G2One, Inc. Chief Technology Officer
http://www.g2one.com

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

   http://xircles.codehaus.org/manage_email






--
Eric
[hidden email]




--
Eric
[hidden email]