Трябва да манипулирате HTML по този начин, така че всички URL адреси на ресурси в интранет домейна също да бъдат прокси. напр. всички от следните препратки към ресурси в HTML
<base href="http://intranet.com/" />
<script src="http://intranet.com/script.js"></script>
<link href="http://intranet.com/style.css" />
<img src="http://intranet.com/image.png" />
<a href="http://intranet.com/page.html">link</a>
трябва да се променят в HTML по този начин, така че вместо това да преминават през вашия прокси сървлет, напр.
<base href="http://example.com/proxy/" />
<script src="http://example.com/proxy/script.js"></script>
<link href="http://example.com/proxy/style.css" />
<img src="http://example.com/proxy/image.png" />
<a href="http://example.com/proxy/page.html">link</a>
HTML анализатор като Jsoup е изключително полезен за това. Можете да направите следното във вашия прокси сървлет, който, предполагам, е картографиран върху URL модел на /proxy/*
.
String intranetURL = "http://intranet.com";
String internetURL = "http://example.com/proxy";
if (request.getRequestURI().endsWith(".html")) { // A HTML page is requested.
Document document = Jsoup.connect(intranetURL + request.getPathInfo()).get();
for (Element element : document.select("[href]")) {
element.attr("href", element.absUrl("href").replaceFirst(intranetURL, internetURL));
}
for (Element element : document.select("[src]")) {
element.attr("src", element.absUrl("src").replaceFirst(intranetURL, internetURL));
}
response.setContentType("text/html;charset=UTF-8");
response.setCharacterEncoding("UTF-8");
resposne.getWriter().write(document.html());
}
else { // Other resources like images, etc.
URLConnection connection = new URL(intranetURL + request.getPathInfo()).openConnection();
for (Map.Entry<String, List<String>> header : connection.getHeaderFields().entrySet()) {
for (String value : header.getValue()) {
response.addHeader(header.getKey(), value);
}
}
InputStream input = connection.getInputStream();
OutputStream output = response.getOutputStream();
// Now just copy input to output.
}
person
BalusC
schedule
08.06.2011