1

The situation:

On server A we want to display content from server B in line on server A.

The problem:

Some of the hyperlinks in the content on server B are relative to server B which makes them invalid when displayed on server A.

Given a block of HTML code that contains anchor tags like the following

<a href="/something/somwhere.html">Somewhere</a>

what would be the most efficient way to convert them to

<a href="http://server-b.com/something/somewhere.html">Somewhere</a>

There can be multiple anchor tags in the content, the one catch is that some might be absolute and I want to leave those as they are, I only want to prepend the domain of server B to the relative URLs

2
  • Java? So you're using JSP/Servlet? Do you want to do this during runtime (dynamically) or just during development time (static find & replace in all files)? Commented Jun 23, 2010 at 17:13
  • Runtime, the JSP page makes an ajax call to a local servlet that pulls content from server-b using API calls (kind of like a proxy). We had initially thought it would be best/easiest to have the servlet return the html snippet in "working order" however after reading Vivin's response it may be best to let the view interpret the response from the servlet Commented Jun 23, 2010 at 17:23

3 Answers 3

3

Depending on a lot of things around how your web app is set up, and also on your definition of efficient, this might not be what you need or are looking for. But anyway, if you have your HTML as a String (in some late stage of a Filter for example), you could do something like this:

html = html.replaceAll("href=\"/", "href=\"http://server-b.com/")
Sign up to request clarification or add additional context in comments.

Comments

2

There is my method, whitch i use for convert relative URLs to absolute. I use it for converting some pages to email body.

public String replaceLinks(String address, String content) throws URISyntaxException{
    //absolute URI used for change all relative links
    URI addressUri = new URI(address);
    //finds all link atributes (href, src, etc.)
    Pattern pattern = Pattern.compile("(href|src|action|background)=\"[^\"]*\"", Pattern.CASE_INSENSITIVE);
    Matcher m = pattern.matcher(content);
    //determines if the link is allready absolute
    Pattern absoluteLinkPattern = Pattern.compile("[a-z]+://.+");
    //buffer for result saving
    StringBuffer buffer = new StringBuffer();
    //position from where should next interation take content to append to buffer
    int lastEnd = 0;
    while(m.find()){
        //position of link in quotes
        int startPos = content.indexOf('"',m.start())+1;
        int endPos = m.end()-1;
        String link = content.substring(startPos,endPos);
        Matcher absoluteMatcher = absoluteLinkPattern.matcher(link);
        //is the link relative?
        if(!absoluteMatcher.find())
        {
            //create relative URL
            URI tmpUri = addressUri.resolve(link);
            //append the string between links
            buffer.append(content.substring(lastEnd,startPos-1));
            //append new link
            buffer.append(tmpUri.toString());
            lastEnd =endPos+1;
        }
    }
    //append the end of file
    buffer.append(content.substring(lastEnd));
    return buffer.toString();
}

hope it helps.

Comments

1

I wouldn't do this in Java; I like to handle view-specific logic in the view layer. I'm assuming this block of code is coming from an AJAX call. So what you can do is get the HTML from the AJAX call and then do this:

jQuery(html).find("a[href]").each(function(index, value) {
  var $a = jQuery(value);
  var href = $a.attr("href");

  if(!/^http:/.test(href)) {
     $a.attr("href", "http://server-b.com" + href);
   }
});

Or if you really want to do this in Java, Lauri's answer will work.

1 Comment

Thanks, although this was a Javascript solution it worked well for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.