1010// limitations under the License.
1111//
1212// Automatically generated by addcopyright.py at 04/03/2012
13- package com .cloud .servlet ;
14-
13+ package com .cloud .servlet ;
14+
1515import java .io .IOException ;
16+ import java .io .UnsupportedEncodingException ;
17+ import java .net .URLDecoder ;
1618import java .net .URLEncoder ;
1719import java .util .ArrayList ;
1820import java .util .Collections ;
4850import com .cloud .vm .VMInstanceVO ;
4951import com .cloud .vm .VirtualMachine ;
5052import com .cloud .vm .VirtualMachineManager ;
51-
52- /**
53- * Thumbnail access : /console?cmd=thumbnail&vm=xxx&w=xxx&h=xxx
54- * Console access : /conosole?cmd=access&vm=xxx
55- * Authentication : /console?cmd=auth&vm=xxx&sid=xxx
56- */
57- public class ConsoleProxyServlet extends HttpServlet {
58- private static final long serialVersionUID = -5515382620323808168L ;
59- public static final Logger s_logger = Logger .getLogger (ConsoleProxyServlet .class .getName ());
60- private static final int DEFAULT_THUMBNAIL_WIDTH = 144 ;
61- private static final int DEFAULT_THUMBNAIL_HEIGHT = 110 ;
62-
53+
54+ /**
55+ * Thumbnail access : /console?cmd=thumbnail&vm=xxx&w=xxx&h=xxx
56+ * Console access : /conosole?cmd=access&vm=xxx
57+ * Authentication : /console?cmd=auth&vm=xxx&sid=xxx
58+ */
59+ public class ConsoleProxyServlet extends HttpServlet {
60+ private static final long serialVersionUID = -5515382620323808168L ;
61+ public static final Logger s_logger = Logger .getLogger (ConsoleProxyServlet .class .getName ());
62+ private static final int DEFAULT_THUMBNAIL_WIDTH = 144 ;
63+ private static final int DEFAULT_THUMBNAIL_HEIGHT = 110 ;
64+
6365 private final static AccountManager _accountMgr = ComponentLocator .getLocator (ManagementServer .Name ).getManager (AccountManager .class );
6466 private final static VirtualMachineManager _vmMgr = ComponentLocator .getLocator (ManagementServer .Name ).getManager (VirtualMachineManager .class );
6567 private final static DomainManager _domainMgr = ComponentLocator .getLocator (ManagementServer .Name ).getManager (DomainManager .class );
6668 private final static ManagementServer _ms = (ManagementServer )ComponentLocator .getComponent (ManagementServer .Name );
6769 private final static IdentityService _identityService = (IdentityService )ComponentLocator .getLocator (ManagementServer .Name ).getManager (IdentityService .class );
6870
69- @ Override
70- protected void doPost (HttpServletRequest req , HttpServletResponse resp ) {
71- doGet (req , resp );
72- }
73-
74- @ Override
71+ @ Override
72+ protected void doPost (HttpServletRequest req , HttpServletResponse resp ) {
73+ doGet (req , resp );
74+ }
75+
76+ @ Override
7577 protected void doGet (HttpServletRequest req , HttpServletResponse resp ) {
7678
7779 try {
@@ -122,117 +124,117 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp) {
122124 sendResponse (resp , "Access denied. Invalid or inconsistent account is found" );
123125 return ;
124126 }
125-
126- String cmd = req .getParameter ("cmd" );
127- if (cmd == null || !isValidCmd (cmd )) {
128- s_logger .debug ("invalid console servlet command: " + cmd );
129- sendResponse (resp , "" );
130- return ;
131- }
132-
127+
128+ String cmd = req .getParameter ("cmd" );
129+ if (cmd == null || !isValidCmd (cmd )) {
130+ s_logger .debug ("invalid console servlet command: " + cmd );
131+ sendResponse (resp , "" );
132+ return ;
133+ }
134+
133135 String vmIdString = req .getParameter ("vm" );
134136 Long vmId = _identityService .getIdentityId ("vm_instance" , vmIdString );
135137 if (vmId == null ) {
136138 s_logger .info ("invalid console servlet command parameter: " + vmIdString );
137139 sendResponse (resp , "" );
138140 return ;
139141 }
140-
141- if (!checkSessionPermision (req , vmId , accountObj )) {
142- sendResponse (resp , "Permission denied" );
143- return ;
144- }
145-
142+
143+ if (!checkSessionPermision (req , vmId , accountObj )) {
144+ sendResponse (resp , "Permission denied" );
145+ return ;
146+ }
147+
146148 if (cmd .equalsIgnoreCase ("thumbnail" )) {
147149 handleThumbnailRequest (req , resp , vmId );
148150 } else if (cmd .equalsIgnoreCase ("access" )) {
149151 handleAccessRequest (req , resp , vmId );
150152 } else {
151153 handleAuthRequest (req , resp , vmId );
152- }
153- } catch (Throwable e ) {
154- s_logger .error ("Unexepected exception in ConsoleProxyServlet" , e );
155- sendResponse (resp , "Server Internal Error" );
156- }
157- }
158-
154+ }
155+ } catch (Throwable e ) {
156+ s_logger .error ("Unexepected exception in ConsoleProxyServlet" , e );
157+ sendResponse (resp , "Server Internal Error" );
158+ }
159+ }
160+
159161 private void handleThumbnailRequest (HttpServletRequest req , HttpServletResponse resp , long vmId ) {
160- VMInstanceVO vm = _vmMgr .findById (vmId );
161- if (vm == null ) {
162- s_logger .warn ("VM " + vmId + " does not exist, sending blank response for thumbnail request" );
163- sendResponse (resp , "" );
164- return ;
165- }
166-
167- if (vm .getHostId () == null ) {
168- s_logger .warn ("VM " + vmId + " lost host info, sending blank response for thumbnail request" );
169- sendResponse (resp , "" );
170- return ;
171- }
172-
173- HostVO host = _ms .getHostBy (vm .getHostId ());
174- if (host == null ) {
175- s_logger .warn ("VM " + vmId + "'s host does not exist, sending blank response for thumbnail request" );
176- sendResponse (resp , "" );
177- return ;
178- }
179-
180- String rootUrl = _ms .getConsoleAccessUrlRoot (vmId );
181- if (rootUrl == null ) {
182- sendResponse (resp , "" );
183- return ;
184- }
185-
186- int w = DEFAULT_THUMBNAIL_WIDTH ;
187- int h = DEFAULT_THUMBNAIL_HEIGHT ;
188-
189- String value = req .getParameter ("w" );
190- try {
191- w = Integer .parseInt (value );
192- } catch (NumberFormatException e ) {
193- }
194-
195- value = req .getParameter ("h" );
196- try {
197- h = Integer .parseInt (value );
198- } catch (NumberFormatException e ) {
199- }
200-
201- try {
202- resp .sendRedirect (composeThumbnailUrl (rootUrl , vm , host , w , h ));
203- } catch (IOException e ) {
162+ VMInstanceVO vm = _vmMgr .findById (vmId );
163+ if (vm == null ) {
164+ s_logger .warn ("VM " + vmId + " does not exist, sending blank response for thumbnail request" );
165+ sendResponse (resp , "" );
166+ return ;
167+ }
168+
169+ if (vm .getHostId () == null ) {
170+ s_logger .warn ("VM " + vmId + " lost host info, sending blank response for thumbnail request" );
171+ sendResponse (resp , "" );
172+ return ;
173+ }
174+
175+ HostVO host = _ms .getHostBy (vm .getHostId ());
176+ if (host == null ) {
177+ s_logger .warn ("VM " + vmId + "'s host does not exist, sending blank response for thumbnail request" );
178+ sendResponse (resp , "" );
179+ return ;
180+ }
181+
182+ String rootUrl = _ms .getConsoleAccessUrlRoot (vmId );
183+ if (rootUrl == null ) {
184+ sendResponse (resp , "" );
185+ return ;
186+ }
187+
188+ int w = DEFAULT_THUMBNAIL_WIDTH ;
189+ int h = DEFAULT_THUMBNAIL_HEIGHT ;
190+
191+ String value = req .getParameter ("w" );
192+ try {
193+ w = Integer .parseInt (value );
194+ } catch (NumberFormatException e ) {
195+ }
196+
197+ value = req .getParameter ("h" );
198+ try {
199+ h = Integer .parseInt (value );
200+ } catch (NumberFormatException e ) {
201+ }
202+
203+ try {
204+ resp .sendRedirect (composeThumbnailUrl (rootUrl , vm , host , w , h ));
205+ } catch (IOException e ) {
204206 if (s_logger .isInfoEnabled ()) {
205207 s_logger .info ("Client may already close the connection" );
206- }
207- }
208- }
209-
210- private void handleAccessRequest (HttpServletRequest req , HttpServletResponse resp , long vmId ) {
211- VMInstanceVO vm = _vmMgr .findById (vmId );
212- if (vm == null ) {
213- s_logger .warn ("VM " + vmId + " does not exist, sending blank response for console access request" );
214- sendResponse (resp , "" );
215- return ;
216- }
217-
218- if (vm .getHostId () == null ) {
219- s_logger .warn ("VM " + vmId + " lost host info, sending blank response for console access request" );
220- sendResponse (resp , "" );
221- return ;
222- }
223-
224- HostVO host = _ms .getHostBy (vm .getHostId ());
225- if (host == null ) {
226- s_logger .warn ("VM " + vmId + "'s host does not exist, sending blank response for console access request" );
227- sendResponse (resp , "" );
228- return ;
229- }
230-
231- String rootUrl = _ms .getConsoleAccessUrlRoot (vmId );
232- if (rootUrl == null ) {
233- sendResponse (resp , "<html><body><p>Console access will be ready in a few minutes. Please try it again later.</p></body></html>" );
234- return ;
235- }
208+ }
209+ }
210+ }
211+
212+ private void handleAccessRequest (HttpServletRequest req , HttpServletResponse resp , long vmId ) {
213+ VMInstanceVO vm = _vmMgr .findById (vmId );
214+ if (vm == null ) {
215+ s_logger .warn ("VM " + vmId + " does not exist, sending blank response for console access request" );
216+ sendResponse (resp , "" );
217+ return ;
218+ }
219+
220+ if (vm .getHostId () == null ) {
221+ s_logger .warn ("VM " + vmId + " lost host info, sending blank response for console access request" );
222+ sendResponse (resp , "" );
223+ return ;
224+ }
225+
226+ HostVO host = _ms .getHostBy (vm .getHostId ());
227+ if (host == null ) {
228+ s_logger .warn ("VM " + vmId + "'s host does not exist, sending blank response for console access request" );
229+ sendResponse (resp , "" );
230+ return ;
231+ }
232+
233+ String rootUrl = _ms .getConsoleAccessUrlRoot (vmId );
234+ if (rootUrl == null ) {
235+ sendResponse (resp , "<html><body><p>Console access will be ready in a few minutes. Please try it again later.</p></body></html>" );
236+ return ;
237+ }
236238
237239 String vmName = vm .getHostName ();
238240 if (vm .getType () == VirtualMachine .Type .User ) {
@@ -315,17 +317,29 @@ private String composeThumbnailUrl(String rootUrl, VMInstanceVO vm, HostVO hostV
315317 String tag = String .valueOf (vm .getId ());
316318 tag = _identityService .getIdentityUuid ("vm_instance" , tag );
317319 String ticket = genAccessTicket (host , String .valueOf (portInfo .second ()), sid , tag );
320+ String consoleurl = null ;
321+ String sessionref = null ;
318322
319323 sb .append ("/getscreen?host=" ).append (parsedHostInfo .first ());
320324 sb .append ("&port=" ).append (portInfo .second ());
321325 sb .append ("&sid=" ).append (sid );
322326 sb .append ("&w=" ).append (w ).append ("&h=" ).append (h );
323327 sb .append ("&tag=" ).append (tag );
324328 sb .append ("&ticket=" ).append (ticket );
325-
329+
326330 if (parsedHostInfo .second () != null && parsedHostInfo .third () != null ) {
327- sb .append ("&" ).append ("consoleurl=" ).append (URLEncoder .encode (parsedHostInfo .second ()));
328- sb .append ("&" ).append ("sessionref=" ).append (URLEncoder .encode (parsedHostInfo .third ()));
331+
332+ try {
333+
334+ consoleurl = URLEncoder .encode (parsedHostInfo .second (), "UTF-8" );
335+ sessionref = URLEncoder .encode (parsedHostInfo .third (), "UTF-8" );
336+ sb .append ("&" ).append ("consoleurl=" ).append (URLDecoder .decode (consoleurl , "UTF-8" ));
337+ sb .append ("&" ).append ("sessionref=" ).append (URLDecoder .decode (sessionref , "UTF-8" ));
338+
339+ } catch (UnsupportedEncodingException e ) {
340+ s_logger .error ("Unexpected exception " , e );
341+ }
342+
329343 }
330344
331345 if (s_logger .isDebugEnabled ()) {
@@ -348,6 +362,8 @@ private String composeConsoleAccessUrl(String rootUrl, VMInstanceVO vm, HostVO h
348362 String tag = String .valueOf (vm .getId ());
349363 tag = _identityService .getIdentityUuid ("vm_instance" , tag );
350364 String ticket = genAccessTicket (host , String .valueOf (portInfo .second ()), sid , tag );
365+ String consoleurl = null ;
366+ String sessionref = null ;
351367
352368 sb .append ("/ajax?host=" ).append (parsedHostInfo .first ());
353369 sb .append ("&port=" ).append (portInfo .second ());
@@ -356,8 +372,18 @@ private String composeConsoleAccessUrl(String rootUrl, VMInstanceVO vm, HostVO h
356372 sb .append ("&ticket=" ).append (ticket );
357373
358374 if (parsedHostInfo .second () != null && parsedHostInfo .third () != null ) {
359- sb .append ("&" ).append ("consoleurl=" ).append (URLEncoder .encode (parsedHostInfo .second ()));
360- sb .append ("&" ).append ("sessionref=" ).append (URLEncoder .encode (parsedHostInfo .third ()));
375+
376+ try {
377+
378+ consoleurl = URLEncoder .encode (parsedHostInfo .second (), "UTF-8" );
379+ sessionref = URLEncoder .encode (parsedHostInfo .third (), "UTF-8" );
380+ sb .append ("&" ).append ("consoleurl=" ).append (URLDecoder .decode (consoleurl , "UTF-8" ));
381+ sb .append ("&" ).append ("sessionref=" ).append (URLDecoder .decode (sessionref , "UTF-8" ));
382+
383+ } catch (UnsupportedEncodingException e ) {
384+ s_logger .error ("Unexpected exception " , e );
385+ }
386+
361387 }
362388
363389 // for console access, we need guest OS type to help implement keyboard
@@ -403,14 +429,14 @@ public static String genAccessTicket(String host, String port, String sid, Strin
403429 private void sendResponse (HttpServletResponse resp , String content ) {
404430 try {
405431 resp .setContentType ("text/html" );
406- resp .getWriter ().print (content );
407- } catch (IOException e ) {
432+ resp .getWriter ().print (content );
433+ } catch (IOException e ) {
408434 if (s_logger .isInfoEnabled ()) {
409435 s_logger .info ("Client may already close the connection" );
410- }
411- }
412- }
413-
436+ }
437+ }
438+ }
439+
414440 private boolean checkSessionPermision (HttpServletRequest req , long vmId , Account accountObj ) {
415441
416442 VMInstanceVO vm = _vmMgr .findById (vmId );
@@ -454,15 +480,15 @@ private boolean checkSessionPermision(HttpServletRequest req, long vmId, Account
454480 return false ;
455481 }
456482
457- return true ;
458- }
459-
460- private boolean isValidCmd (String cmd ) {
483+ return true ;
484+ }
485+
486+ private boolean isValidCmd (String cmd ) {
461487 if (cmd .equalsIgnoreCase ("thumbnail" ) || cmd .equalsIgnoreCase ("access" ) || cmd .equalsIgnoreCase ("auth" )) {
462488 return true ;
463- }
464-
465- return false ;
489+ }
490+
491+ return false ;
466492 }
467493
468494 public boolean verifyUser (Long userId ) {
@@ -596,4 +622,4 @@ public static final String escapeHTML(String content){
596622 }
597623 return sb .toString ();
598624 }
599- }
625+ }
0 commit comments