Created
November 23, 2013 17:50
-
-
Save Cher80/7617717 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.indexisto.front.adminpanel.server.rpc; | |
import static org.slf4j.LoggerFactory.getLogger; | |
import java.io.IOException; | |
import java.net.URI; | |
import java.net.URISyntaxException; | |
import java.util.regex.Matcher; | |
import java.util.regex.Pattern; | |
import javax.servlet.ServletException; | |
import javax.servlet.http.HttpServletRequest; | |
import javax.servlet.http.HttpServletResponse; | |
import org.apache.commons.io.IOUtils; | |
import org.apache.http.HttpEntity; | |
import org.apache.http.HttpResponse; | |
import org.apache.http.client.HttpClient; | |
import org.apache.http.client.methods.HttpGet; | |
import org.apache.http.impl.client.DefaultHttpClient; | |
import org.apache.http.util.EntityUtils; | |
import org.slf4j.Logger; | |
import com.indexisto.front.adminpanel.client.rpc.FilesRPCService; | |
import com.indexisto.front.commons.gwt.service.GwtServiceServletBase; | |
@SuppressWarnings("serial") | |
public class MarkerRPCServiceImpl extends GwtServiceServletBase implements FilesRPCService { | |
private final static Logger LOG = getLogger(MarkerRPCServiceImpl.class); | |
@Override | |
protected void service(final HttpServletRequest request, | |
HttpServletResponse response) throws ServletException, IOException | |
{ | |
final String url = request.getParameter("url"); | |
final HttpGet requestApache = new HttpGet(url); | |
final HttpClient httpClient = new DefaultHttpClient(); | |
final HttpResponse responseApache = httpClient.execute(requestApache); | |
final HttpEntity entity = responseApache.getEntity(); | |
final String encoding = EntityUtils.getContentCharSet( entity ); | |
final String mime = EntityUtils.getContentMimeType(entity); | |
System.out.println("encoding " + encoding); | |
System.out.println("mime " + mime); | |
String responseText = IOUtils.toString(entity.getContent(), encoding); | |
String host = ""; | |
try { | |
final URI uri = new URI(url); | |
host = "http://" + uri.getHost(); | |
} catch (final URISyntaxException e1) { | |
e1.printStackTrace(); | |
} | |
System.out.println("host " + host); | |
try { | |
responseText = replaceLinks(host,responseText); | |
System.out.println("perlacedUels"); | |
} catch (final URISyntaxException e) { | |
e.printStackTrace(); | |
} | |
System.out.println(responseText); | |
sendResponse(response,responseText, encoding, mime); | |
return; | |
} | |
public String replaceLinks(String address, String content) throws URISyntaxException{ | |
//absolute URI used for change all relative links | |
final URI addressUri = new URI(address); | |
//finds all link atributes (href, src, etc.) | |
final Pattern pattern = Pattern.compile("(href|src|action|background)=\"[^\"]*\"", Pattern.CASE_INSENSITIVE); | |
final Matcher m = pattern.matcher(content); | |
//determines if the link is allready absolute | |
final Pattern absoluteLinkPattern = Pattern.compile("[a-z]+://.+"); | |
//buffer for result saving | |
final 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 | |
final int startPos = content.indexOf('"',m.start())+1; | |
final int endPos = m.end()-1; | |
final String link = content.substring(startPos,endPos); | |
final Matcher absoluteMatcher = absoluteLinkPattern.matcher(link); | |
//is the link relative? | |
if(!absoluteMatcher.find()) | |
{ | |
//create relative URL | |
final 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(); | |
} | |
protected void sendResponse(HttpServletResponse response, String responseText, String encoding, String mime) throws ServletException, IOException { | |
response.setContentType(mime); | |
response.setCharacterEncoding(encoding); | |
response.setStatus(HttpServletResponse.SC_OK); | |
response.getWriter().print(responseText ); | |
response.flushBuffer(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment