0

Hi I am currently developing a spring (3.2.x) application where I have to insert my content into a given page at a certain point specified by an id.

This is what I am currently doing:

@RequestMapping(value = "/{part}", method = RequestMethod.POST, produces="text/html")
@ResponseBody
public String enterModul(HttpServletRequest request, @PathVariable String part, @ModelAttribute Body body){
    //body handling omitted
    //getting the external html
    String frame = restTemplate.getForObject("...externalUrl", String.class);

    //getting my content
    String uri = request.getRequestURL().toString();
    String content = restTemplate.getForObject(uri, String.class);

    // merge frame and content
    String completeView = this.mergeFrameAndContent(frame, content);
    return completeView;
}

@RequestMapping(value = "/{part}", method = RequestMethod.GET, produces="text/html")
@ResponseBody
public ModelAndView getInitialContentForPart(@PathVariable String part) {
    //irrelevant code/model creation ommited
    //just using InternalResourceViewResolver so nothing fancy here
    ModelAndView view = new ModelAndView(part, "model", model);
    return view;
}

private String mergeFrameAndContent(String frame, String content) {
            //id identifies position
    String view = frame.replace("id", content);
    return view;
}

But doing it like this somehow does not feel right. Are there better solutions? I tried doing it with tiles 3 but that did not work.

Tarken
  • 2,112
  • 2
  • 23
  • 42
  • Do you just want to insert text or change the code? – SSC Jun 04 '13 at 08:40
  • I want to insert the content html into the external html – Tarken Jun 04 '13 at 08:44
  • Okay so why don't you go with jQuery? – SSC Jun 04 '13 at 08:58
  • See if this helps you? http://stackoverflow.com/questions/4967629/insert-external-page-html-into-a-page-html – SSC Jun 04 '13 at 09:01
  • I have no control over the external page where I want to insert my html into. How would you do it with jquery? Using jquery inside my content html is no prob but I would have to inject it somehow into the external html wouldn't I? – Tarken Jun 04 '13 at 09:02
  • the link is the other way around from what I want to do^^ That I know how to do ;-) – Tarken Jun 04 '13 at 09:03
  • Sorry then may be I didn't understand it earlier. What are you trying to achieve by this? – SSC Jun 04 '13 at 09:08
  • Its a distributed architecture where many applications have to use that external html (generated by its own complex logic). Getting it should be better than putting it and its logic into every application. – Tarken Jun 04 '13 at 09:14
  • I do not know the solution but I hope there are others who will be able to. Thanks for your replies though @Tarken – SSC Jun 04 '13 at 09:20
  • It may be more robust to use some sort of DOM or SAX parser to do the replacement instead of straight String.replace(). But, outside of getting the external page and replacing a chunk of it, I don't know of any better overall process to do what you are trying to do. – CodeChimp Jun 04 '13 at 11:14

2 Answers2

0

Since you talk about a distributed architecture with many apps, Edge Side Includes (ESI, see http://en.wikipedia.org/wiki/Edge_Side_Includes) might be what you are looking for. You could use e.g. a Varnish reverse proxy (see https://www.varnish-cache.org/trac/wiki/ESIfeatures) to handle them.

Jukka
  • 4,583
  • 18
  • 14
0

I found a better solution by using a Filter to manipulate the Response. By doing this I am saving one internal Request and it is a reusable solution:

public class FrameFilter extends GenericFilterBean { 
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        ServletResponse newResponse = response;

        if (request instanceof HttpServletRequest) {
            newResponse = new CharResponseWrapper((HttpServletResponse) response);
        }

        chain.doFilter(request, newResponse);

        if (newResponse instanceof CharResponseWrapper) {
           String modulContent = newResponse.toString();
           if (modulContent != null) {
                RestTemplate restTemplate = new RestTemplate();
                String frame = restTemplate.getForObject("FRAMEURL", String.class);
                String completeView = this.mergeFrameAndContent(frame, modulContent);
                response.getWriter().write(completeView);
            }
        }
    }
}

The CharResponseWrapper is taken form this example: Example

Tarken
  • 2,112
  • 2
  • 23
  • 42