DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Seasoned technology professional with strong experience in developing desktop applications, web applications, mobile web applications, web services platform based on multi-tiered distributed architecture, legacy modernization, and SOA. Senthamizh Chelvi has posted 8 posts at DZone. You can read more from them at their website. View Full User Profile

How to get the assets like images on the server behind the firewall?

11.21.2012
| 1284 views |
  • submit to reddit
public static String getAssetUrl(HttpServletRequest request, String fileName) {
    String scheme = request.getScheme();               
    String localServerName= request.getLocalName();
    int localServerPort = request.getLocalPort();
    String contextPath = request.getContextPath();
    String pathInfo = request.getPathInfo();      

    // Reconstruct original request URL
    String url = scheme + "://" + localServerName+":" + localServerPort + contextPath + "/asset?fileImage=/" + fileName;

    if (pathInfo != null) {
        url += pathInfo;
    }

    return url;
}    
To retrieve the asset in the web server behind the firewall, the getLocalName() and getLocalPort() methods must be used to reconstruct the URL.

The request.getServerName() method returns the name of the server that the HTTP request was sent to.  If this method is used, it would return the name of the public server name configured in the firewall.

The request.getLocalName() method returns the name of the server that actually received the request. So this must be used to reconstruct the URL and get the desired asset in the web server.

Note: Asset could be a template (XSLT), image etc.

    Tags: