use of com.sun.net.httpserver.Headers in project cloudstack by apache.
the class ConsoleProxyCmdHandler method doHandle.
public void doHandle(HttpExchange t) throws Exception {
String path = t.getRequestURI().getPath();
int i = path.indexOf("/", 1);
String cmd = path.substring(i + 1);
s_logger.info("Get CMD request for " + cmd);
if (cmd.equals("getstatus")) {
ConsoleProxyClientStatsCollector statsCollector = ConsoleProxy.getStatsCollector();
Headers hds = t.getResponseHeaders();
hds.set("Content-Type", "text/plain");
t.sendResponseHeaders(200, 0);
OutputStreamWriter os = new OutputStreamWriter(t.getResponseBody(), "UTF-8");
statsCollector.getStatsReport(os);
os.close();
}
}
use of com.sun.net.httpserver.Headers in project cloudstack by apache.
the class ConsoleProxyThumbnailHandler method handle.
@Override
@SuppressWarnings("access")
public void handle(HttpExchange t) throws IOException {
try {
Thread.currentThread().setName("JPG Thread " + Thread.currentThread().getId() + " " + t.getRemoteAddress());
if (s_logger.isDebugEnabled())
s_logger.debug("ScreenHandler " + t.getRequestURI());
long startTick = System.currentTimeMillis();
doHandle(t);
if (s_logger.isDebugEnabled())
s_logger.debug(t.getRequestURI() + "Process time " + (System.currentTimeMillis() - startTick) + " ms");
} catch (IllegalArgumentException e) {
String response = "Bad query string";
s_logger.error(response + ", request URI : " + t.getRequestURI());
t.sendResponseHeaders(200, response.length());
OutputStream os = t.getResponseBody();
os.write(response.getBytes());
os.close();
} catch (OutOfMemoryError e) {
s_logger.error("Unrecoverable OutOfMemory Error, exit and let it be re-launched");
System.exit(1);
} catch (Throwable e) {
s_logger.error("Unexpected exception while handing thumbnail request, ", e);
String queries = t.getRequestURI().getQuery();
Map<String, String> queryMap = getQueryMap(queries);
int width = 0;
int height = 0;
String ws = queryMap.get("w");
String hs = queryMap.get("h");
try {
width = Integer.parseInt(ws);
height = Integer.parseInt(hs);
} catch (NumberFormatException ex) {
s_logger.debug("Cannot parse width: " + ws + " or height: " + hs, ex);
}
width = Math.min(width, 800);
height = Math.min(height, 600);
BufferedImage img = generateTextImage(width, height, "Cannot Connect");
ByteArrayOutputStream bos = new ByteArrayOutputStream(8196);
javax.imageio.ImageIO.write(img, "jpg", bos);
byte[] bs = bos.toByteArray();
Headers hds = t.getResponseHeaders();
hds.set("Content-Type", "image/jpeg");
hds.set("Cache-Control", "no-cache");
hds.set("Cache-Control", "no-store");
t.sendResponseHeaders(200, bs.length);
OutputStream os = t.getResponseBody();
os.write(bs);
os.close();
s_logger.error("Cannot get console, sent error JPG response for " + t.getRequestURI());
return;
} finally {
t.close();
}
}
use of com.sun.net.httpserver.Headers in project cloudstack by apache.
the class ConsoleProxyThumbnailHandler method doHandle.
private void doHandle(HttpExchange t) throws Exception, IllegalArgumentException {
String queries = t.getRequestURI().getQuery();
Map<String, String> queryMap = getQueryMap(queries);
int width = 0;
int height = 0;
int port = 0;
String ws = queryMap.get("w");
String hs = queryMap.get("h");
String host = queryMap.get("host");
String portStr = queryMap.get("port");
String sid = queryMap.get("sid");
String tag = queryMap.get("tag");
String ticket = queryMap.get("ticket");
String console_url = queryMap.get("consoleurl");
String console_host_session = queryMap.get("sessionref");
if (tag == null)
tag = "";
if (ws == null || hs == null || host == null || portStr == null || sid == null) {
throw new IllegalArgumentException();
}
try {
width = Integer.parseInt(ws);
height = Integer.parseInt(hs);
port = Integer.parseInt(portStr);
} catch (NumberFormatException e) {
throw new IllegalArgumentException(e);
}
ConsoleProxyClientParam param = new ConsoleProxyClientParam();
param.setClientHostAddress(host);
param.setClientHostPort(port);
param.setClientHostPassword(sid);
param.setClientTag(tag);
param.setTicket(ticket);
param.setClientTunnelUrl(console_url);
param.setClientTunnelSession(console_host_session);
ConsoleProxyClient viewer = ConsoleProxy.getVncViewer(param);
if (!viewer.isHostConnected()) {
// use generated image instead of static
BufferedImage img = generateTextImage(width, height, "Connecting");
ByteArrayOutputStream bos = new ByteArrayOutputStream(8196);
javax.imageio.ImageIO.write(img, "jpg", bos);
byte[] bs = bos.toByteArray();
Headers hds = t.getResponseHeaders();
hds.set("Content-Type", "image/jpeg");
hds.set("Cache-Control", "no-cache");
hds.set("Cache-Control", "no-store");
t.sendResponseHeaders(200, bs.length);
OutputStream os = t.getResponseBody();
os.write(bs);
os.close();
if (s_logger.isInfoEnabled())
s_logger.info("Console not ready, sent dummy JPG response");
return;
}
{
Image scaledImage = viewer.getClientScaledImage(width, height);
BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
Graphics2D bufImageGraphics = bufferedImage.createGraphics();
bufImageGraphics.drawImage(scaledImage, 0, 0, null);
ByteArrayOutputStream bos = new ByteArrayOutputStream(8196);
javax.imageio.ImageIO.write(bufferedImage, "jpg", bos);
byte[] bs = bos.toByteArray();
Headers hds = t.getResponseHeaders();
hds.set("Content-Type", "image/jpeg");
hds.set("Cache-Control", "no-cache");
hds.set("Cache-Control", "no-store");
t.sendResponseHeaders(200, bs.length);
OutputStream os = t.getResponseBody();
os.write(bs);
os.close();
}
}
Aggregations