Announcement Announcement Module
Collapse
No announcement yet.
how to get the server's url from a spring bean? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • how to get the server's url from a spring bean?

    Hi,

    One of the requirements for a lot of my applications is to send out emails with full links to some other page in the app.

    My current approach to this has been to hard code the url into the bean.

    Code:
    public class Emailer  {
         public static final String LINK = "http://www.myserver/myapp/";
    }
    this sucks for a lot of obvious reasons. what i'm wondering is if spring has a nice way of pulling that for me. I looked at ServletContextAware, but the servlet context doesn't offer a method to get that... I think i need an HttpRequest in there, it has the methods i need.

    Any ideas?? I'm sure other's have done this.

    TIA

  • #2
    UrlPathHelper has a lot of quick and easy utilities for finding that stuff out.

    Good luck,

    Steve O

    Comment


    • #3
      looking at the docs for that, it seems like it's nothing more than a nicer api for a given HttpServletRequest. i guess i'd rather use than that the raw servlet api, but it won't be of much use since i don't have one to work with.

      so i guess the real question is, how do i get an instance of a HttpServletRequest that points to my webapp app injected into a bean in my application context?

      once i have the request in the Emailer class, i can pull the full url from there via that util or the servlet api itself. that kind of sucks, isn't there something out there like WebAppUtils.getLinkToWebapp(); ?

      Comment


      • #4
        bump for a friday

        Comment


        • #5
          I guess it depends. What initiates your E-Mailer class? Is it a timed process or is it indeed a request from the web which starts it. In the first case it would be quite impossible to get a request object.

          In the second one it shouldn't be to hard, you could even consider pulling of the request, and pass the url as a parameter.

          Comment


          • #6
            the emailer is injected into a stripes action bean. what i ended up doing is pretty much what you suggested, determine the url in the action bean, and pass it on down. here is an example

            Code:
            public class MyActionBean  {
            
            	private Emailer emailer;
            
            	@SpringBean("emailer")
            	public void injectEmailer(Emailer emailer)  {
            		emailer.setWebAppUrl(getWebAppUrl());
            		this.emailer= emailer;
            	}
            }
            
            public class Emailer {
            	private String webAppUrl;
            
            	public String setWebAppUrl(String webAppUrl)  {
            		this.webAppUrl = webAppUrl;
            	}
            
            	public void sendTrackingEmail(Order order)  {
            		String link = webAppUrl + ActionUrls.TRACK_ORDER + queryString(order);
            	}
            }
            works fine, no more hardcoding the server name. this was a pain having to remember switch it from localhost to our website everytime we pushed it out and vice versa.

            Comment


            • #7
              I use UrlPathHelper getContextPath(HttpServletRequest) and works great to get the web application context.

              For server and port, just using getServerName and getServerPort from HttpServletRequest

              Comment


              • #8
                Originally posted by fragus View Post
                I use UrlPathHelper getContextPath(HttpServletRequest) and works great to get the web application context.

                For server and port, just using getServerName and getServerPort from HttpServletRequest
                Unfortunately server and port not always reflect the ones you need.
                Think for a moment if you have apache in front of tomcat. The request will contain a port like 8080 while "externally" the request is on port 80.
                The only way I managed to solve this problem is using injected values together with a PropertyPlaceHolderConfigurer. In this way you can store the path in a config file which may also be located outside of the war.
                This approach works perfectly for test and production environments

                Comment


                • #9
                  Originally posted by robcos View Post
                  This approach works perfectly for test and production environments
                  unless you forget to change it, which is the whole problem i'm having. having to edit a config file is not that much different that editing a class everytime you switch from dev to production. the issue here was to not have to ever type localhost or www.oursite.com but rather have the app infer this dynamically at runtime.

                  i just leave getPort() alone and all is well.

                  Comment


                  • #10
                    Originally posted by lloyd.mcclendon View Post
                    unless you forget to change it, which is the whole problem i'm having. having to edit a config file is not that much different that editing a class everytime you switch from dev to production.
                    Not if you keep your configuration file outside of the war.
                    One configuration file for your production and one for your test.

                    Comment

                    Working...
                    X