1616// under the License.
1717package com .cloud .consoleproxy ;
1818
19+ import java .awt .Graphics2D ;
20+ import java .awt .Image ;
21+ import java .awt .image .BufferedImage ;
22+ import java .io .ByteArrayOutputStream ;
1923import java .io .IOException ;
2024import java .io .OutputStream ;
2125import java .util .Map ;
@@ -65,7 +69,14 @@ private void doHandle(HttpExchange t) throws Exception, IllegalArgumentException
6569 String tag = queryMap .get ("tag" );
6670 String ticket = queryMap .get ("ticket" );
6771 String keyStr = queryMap .get ("key" );
68- int key = 0 ;
72+ String console_url = queryMap .get ("consoleurl" );
73+ String console_host_session = queryMap .get ("sessionref" );
74+ String w = queryMap .get ("w" );
75+ String h = queryMap .get ("h" );
76+
77+ int key = 0 ;
78+ int width = 144 ;
79+ int height = 110 ;
6980
7081 if (tag == null )
7182 tag = "" ;
@@ -81,9 +92,16 @@ private void doHandle(HttpExchange t) throws Exception, IllegalArgumentException
8192 throw new IllegalArgumentException (e );
8293 }
8394
84- try {
85- key = Integer .parseInt (keyStr );
86- } catch (NumberFormatException e ) {
95+ try {
96+ if (keyStr != null )
97+ key = Integer .parseInt (keyStr );
98+ if (null != w )
99+ width = Integer .parseInt (w );
100+
101+ if (null != h )
102+ height = Integer .parseInt (h );
103+
104+ } catch (NumberFormatException e ) {
87105 s_logger .warn ("Invalid numeric parameter in query string: " + keyStr );
88106 throw new IllegalArgumentException (e );
89107 }
@@ -94,23 +112,48 @@ private void doHandle(HttpExchange t) throws Exception, IllegalArgumentException
94112 param .setClientHostPassword (sid );
95113 param .setClientTag (tag );
96114 param .setTicket (ticket );
97- ConsoleProxyClient viewer = ConsoleProxy .getVncViewer (param );
98- byte [] img = viewer .getAjaxImageCache ().getImage (key );
99- if (img != null ) {
100- Headers hds = t .getResponseHeaders ();
101- hds .set ("Content-Type" , "image/jpeg" );
102- t .sendResponseHeaders (200 , img .length );
103-
104- OutputStream os = t .getResponseBody ();
105- try {
106- os .write (img , 0 , img .length );
107- } finally {
108- os .close ();
109- }
110- } else {
111- if (s_logger .isInfoEnabled ())
112- s_logger .info ("Image has already been swept out, key: " + key );
113- t .sendResponseHeaders (404 , -1 );
115+ param .setClientTunnelUrl (console_url );
116+ param .setClientTunnelSession (console_host_session );
117+
118+ ConsoleProxyClient viewer = ConsoleProxy .getVncViewer (param );
119+
120+ if (key == 0 ) {
121+ Image scaledImage = viewer .getClientScaledImage (width , height );
122+ BufferedImage bufferedImage = new BufferedImage (width , height ,
123+ BufferedImage .TYPE_3BYTE_BGR );
124+ Graphics2D bufImageGraphics = bufferedImage .createGraphics ();
125+ bufImageGraphics .drawImage (scaledImage , 0 , 0 , null );
126+ ByteArrayOutputStream bos = new ByteArrayOutputStream (8196 );
127+ javax .imageio .ImageIO .write (bufferedImage , "jpg" , bos );
128+ byte [] bs = bos .toByteArray ();
129+ Headers hds = t .getResponseHeaders ();
130+ hds .set ("Content-Type" , "image/jpeg" );
131+ hds .set ("Cache-Control" , "no-cache" );
132+ hds .set ("Cache-Control" , "no-store" );
133+ t .sendResponseHeaders (200 , bs .length );
134+ OutputStream os = t .getResponseBody ();
135+ os .write (bs );
136+ os .close ();
137+ } else {
138+ AjaxFIFOImageCache imageCache = viewer .getAjaxImageCache ();
139+ byte [] img = imageCache .getImage (key );
140+
141+ if (img != null ) {
142+ Headers hds = t .getResponseHeaders ();
143+ hds .set ("Content-Type" , "image/jpeg" );
144+ t .sendResponseHeaders (200 , img .length );
145+
146+ OutputStream os = t .getResponseBody ();
147+ try {
148+ os .write (img , 0 , img .length );
149+ } finally {
150+ os .close ();
151+ }
152+ } else {
153+ if (s_logger .isInfoEnabled ())
154+ s_logger .info ("Image has already been swept out, key: " + key );
155+ t .sendResponseHeaders (404 , -1 );
156+ }
114157 }
115158 }
116159}
0 commit comments