Skip to content

Instantly share code, notes, and snippets.

@xoba
Created November 28, 2011 20:41
Show Gist options
  • Save xoba/1401944 to your computer and use it in GitHub Desktop.
Save xoba/1401944 to your computer and use it in GitHub Desktop.
phantomjs rendering
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.URLDecoder;
import java.util.LinkedList;
import java.util.List;
import com.xoba.util.ILogger;
import com.xoba.util.LogFactory;
import com.xoba.util.MraUtils;
public class ScreenShotMain {
private static final ILogger logger = LogFactory.getDefault().create();
public static void main(String[] args) throws Exception {
File dir = new File(args[0]).getAbsoluteFile();
int port = new Integer(args[1]);
logger.debugf("dir = %s; port = %d", dir, port);
long round = 0;
while (true) {
try {
runRound(round++, dir, port);
} catch (Exception e) {
logger.warnf("exception in round %,d: %s", round, e);
e.printStackTrace();
} finally {
long sleep = 5000;
logger.debugf("sleeping %,d ms after round %,d", sleep, round);
Thread.sleep(sleep);
}
}
}
private static void runRound(final long round, final File dir, int port) throws Exception {
long count = 0;
ServerSocket ss = new ServerSocket(port);
try {
while (true) {
final Socket s = ss.accept();
final long fCount = count++;
new Thread() {
@Override
public void run() {
try {
List<String> lines = new LinkedList<String>();
logger.debugf("got socket #%,d %s in round %,d", fCount, s, round);
BufferedReader reader = new BufferedReader(new InputStreamReader(new BufferedInputStream(
s.getInputStream())));
boolean done = false;
while (!done) {
String line = reader.readLine();
if (line == null) {
done = true;
} else {
logger.debugf("#%,d: %s", fCount, line);
lines.add(line);
if (line.trim().length() == 0) {
done = true;
} else if (line.startsWith("GET")) {
String[] parts = line.split(" ");
if (parts.length > 0) {
String path = parts[1].toLowerCase();
logger.debugf("path = %s", path);
respond(path.substring(1, path.length()), s.getOutputStream(), dir);
}
}
}
}
} catch (Exception e) {
logger.warnf("exception handling socket #,d: %s", fCount, e);
e.printStackTrace();
} finally {
try {
s.close();
} catch (Exception e) {
logger.debugf("can't close socket: %s", e);
}
}
}
}.start();
}
} finally {
ss.close();
}
}
private static void respond(String site, OutputStream out, File dir) throws Exception {
site = URLDecoder.decode(site, "US-ASCII");
logger.debugf("site = %s", site);
File tmp = File.createTempFile("image", ".png");
try {
logger.debugf("creating %s", tmp);
ProcessBuilder pb = new ProcessBuilder(new File(dir, "bin/phantomjs").getAbsolutePath(), new File(dir,
"examples/rasterize.js").getAbsolutePath(), site, tmp.getAbsolutePath());
Process p = pb.start();
logger.debugf("result = %,d", p.waitFor());
emit(out, "image/png", tmp);
} finally {
tmp.delete();
}
}
private static void emit(OutputStream out, String mime, File f) throws Exception {
out.write("HTTP/1.0 200 OK\r\n".getBytes());
out.write(("Content-Length: " + f.length() + "\r\n").getBytes());
out.write(("Content-Type: " + mime + "\r\n").getBytes());
out.write("\r\n".getBytes());
MraUtils.copy(f, out);
out.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment