use of com.sun.net.httpserver.Headers in project eureka by Netflix.
the class SimpleEurekaHttpServer method mapToEurekaHttpRequest.
private EurekaHttpRequest mapToEurekaHttpRequest(HttpExchange httpExchange) {
Headers exchangeHeaders = httpExchange.getRequestHeaders();
Map<String, String> headers = new HashMap<>();
for (String key : exchangeHeaders.keySet()) {
headers.put(key, exchangeHeaders.getFirst(key));
}
return new EurekaHttpRequest(httpExchange.getRequestMethod(), httpExchange.getRequestURI(), headers);
}
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();
}
}
use of com.sun.net.httpserver.Headers in project cloudstack by apache.
the class ConsoleProxyAjaxHandler method handleClientStart.
private void handleClientStart(HttpExchange t, ConsoleProxyClient viewer, String title, String guest) throws IOException {
List<String> languages = t.getRequestHeaders().get("Accept-Language");
String response = viewer.onAjaxClientStart(title, languages, guest);
Headers hds = t.getResponseHeaders();
hds.set("Content-Type", "text/html");
hds.set("Cache-Control", "no-cache");
hds.set("Cache-Control", "no-store");
t.sendResponseHeaders(200, response.length());
OutputStream os = t.getResponseBody();
try {
os.write(response.getBytes());
} finally {
os.close();
}
}
use of com.sun.net.httpserver.Headers in project cloudstack by apache.
the class ConsoleProxyAjaxHandler method sendResponse.
private void sendResponse(HttpExchange t, String contentType, String response) throws IOException {
Headers hds = t.getResponseHeaders();
hds.set("Content-Type", contentType);
t.sendResponseHeaders(200, response.length());
OutputStream os = t.getResponseBody();
try {
os.write(response.getBytes());
} finally {
os.close();
}
}
Aggregations