summaryrefslogtreecommitdiffstats
path: root/src/com/itmill/toolkit/terminal/web/AjaxApplicationManager.java
blob: 861ececa1d9a869666ec3d8fd5b9943eaefb2e52 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
/* *************************************************************************
 
                               IT Mill Toolkit 

               Development of Browser User Intarfaces Made Easy

                    Copyright (C) 2000-2006 IT Mill Ltd
                     
   *************************************************************************

   This product is distributed under commercial license that can be found
   from the product package on license/license.txt. Use of this product might 
   require purchasing a commercial license from IT Mill Ltd. For guidelines 
   on usage, see license/licensing-guidelines.html

   *************************************************************************
   
   For more information, contact:
   
   IT Mill Ltd                           phone: +358 2 4802 7180
   Ruukinkatu 2-4                        fax:   +358 2 4802 7181
   20540, Turku                          email:  info@itmill.com
   Finland                               company www: www.itmill.com
   
   Primary source for information and releases: www.itmill.com

   ********************************************************************** */

package com.itmill.toolkit.terminal.web;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.WeakHashMap;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.itmill.toolkit.Application;
import com.itmill.toolkit.Application.WindowAttachEvent;
import com.itmill.toolkit.Application.WindowDetachEvent;
import com.itmill.toolkit.terminal.DownloadStream;
import com.itmill.toolkit.terminal.Paintable;
import com.itmill.toolkit.terminal.URIHandler;
import com.itmill.toolkit.terminal.Paintable.RepaintRequestEvent;
import com.itmill.toolkit.ui.Component;
import com.itmill.toolkit.ui.FrameWindow;
import com.itmill.toolkit.ui.Window;

/** Application manager processes changes and paints for single
 *  application instance.
 * 
 * @author IT Mill Ltd, Joonas Lehtinen, Sami Ekblad
 * @version @VERSION@
 * @since 3.1
 */
public class AjaxApplicationManager implements Paintable.RepaintRequestListener,
        Application.WindowAttachListener, Application.WindowDetachListener {

    private static String GET_PARAM_REPAINT_ALL = "repaintAll";

    private static int DEFAULT_BUFFER_SIZE = 32 * 1024;

    private static int MAX_BUFFER_SIZE = 64 * 1024;

    private WeakHashMap applicationToVariableMapMap = new WeakHashMap();

    private HashSet dirtyPaintabletSet = new HashSet();

    private WeakHashMap paintableIdMap = new WeakHashMap();

    private int idSequence = 0;

    private Application application;

    private Set removedWindows = new HashSet();

    private AjaxPaintTarget paintTarget;

    public AjaxApplicationManager(Application application) {
        this.application = application;
    }

    private AjaxVariableMap getVariableMap() {
        AjaxVariableMap vm = (AjaxVariableMap) applicationToVariableMapMap
                .get(application);
        if (vm == null) {
            vm = new AjaxVariableMap();
            applicationToVariableMapMap.put(application, vm);
        }
        return vm;
    }

    public void takeControl() {
        application.addListener((Application.WindowAttachListener) this);
        application.addListener((Application.WindowDetachListener) this);

    }

    public void releaseControl() {
        application.removeListener((Application.WindowAttachListener) this);
        application.removeListener((Application.WindowDetachListener) this);
    }

    public void handleUidlRequest(HttpServletRequest request,
            HttpServletResponse response) throws IOException {

        boolean repaintAll = request.getParameter(GET_PARAM_REPAINT_ALL) != null;

        OutputStream out = response.getOutputStream();
        try {

            // Is this a download request from application
            DownloadStream download = null;

            // The rest of the process is synchronized with the application
            // in order to guarantee that no parallel variable handling is
            // made
            synchronized (application) {

                // Change all variables based on request parameters
                Map unhandledParameters = getVariableMap().handleVariables(
                        request, application);

                // Handle the URI if the application is still running
                if (application.isRunning())
                    download = handleURI(application, request, response);

                // If this is not a download request
                if (download == null) {
                    
                    // Find the window within the application
                    Window window = null;
                    if (application.isRunning())
                        window = getApplicationWindow(request, application);

                    // Handle the unhandled parameters if the application is
                    // still running
                    if (window != null && unhandledParameters != null
                            && !unhandledParameters.isEmpty())
                        window.handleParameters(unhandledParameters);

                    // Remove application if it has stopped
                    if (!application.isRunning()) {
                        endApplication(request, response, application);
                        return;
                    }

                    // Return if no window found
                    if (window == null)
                        return;

                    // Set the response type
                    response.setContentType("application/xml; charset=UTF-8");

                    paintTarget = new AjaxPaintTarget(
                                                getVariableMap(), this, out);
                    
                    // Render the removed windows
                    Set removed = new HashSet(getRemovedWindows());
                    if (removed.size() > 0) {
                        for (Iterator i = removed.iterator(); i.hasNext();) {
                            Window w = (Window) i.next();
                            paintTarget.startTag("change");
                            paintTarget.addAttribute("format", "uidl");
                            String pid = getPaintableId(w);
                            paintTarget.addAttribute("pid", pid);
                            paintTarget.addAttribute("windowname", w
                                    .getName());
                            paintTarget.addAttribute("visible", false);
                            paintTarget.endTag("change");
                            removedWindowNotified(w);

                        }
                    }

                    // Paint components
                    Set paintables;
                    if (repaintAll) {
                        paintables = new LinkedHashSet();
                        paintables.add(window);
                    } else
                        paintables = getDirtyComponents();
                    if (paintables != null) {

                        // Create "working copy" of the current state.
                        List currentPaintables = new ArrayList(paintables);

                        // Sort the paintable so that parent windows
                        // are always painted before child windows
                        Collections.sort(currentPaintables, new Comparator() {

                            public int compare(Object o1, Object o2) {

                                // If first argumement is now window
                                // the second is "smaller" if it is.
                                if (!(o1 instanceof Window)) {
                                    return (o2 instanceof Window) ? 1 : 0;
                                }

                                // Now, if second is not window the
                                // first is smaller.
                                if (!(o2 instanceof Window)) {
                                    return -1;
                                }

                                // Both are windows.
                                String n1 = ((Window) o1).getName();
                                String n2 = ((Window) o2).getName();
                                if (o1 instanceof FrameWindow) {
                                    if (((FrameWindow) o1).getFrameset()
                                            .getFrame(n2) != null) {
                                        return -1;
                                    } else if (!(o2 instanceof FrameWindow)) {
                                        return -1;
                                    }
                                }
                                if (o2 instanceof FrameWindow) {
                                    if (((FrameWindow) o2).getFrameset()
                                            .getFrame(n1) != null) {
                                        return 1;
                                    } else if (!(o1 instanceof FrameWindow)) {
                                        return 1;
                                    }
                                }

                                return 0;
                            }
                        });

                        for (Iterator i = currentPaintables.iterator(); i
                                .hasNext();) {
                            Paintable p = (Paintable) i.next();
                            paintTarget.startTag("change");
                            paintTarget.addAttribute("format", "uidl");
                            String pid = getPaintableId(p);
                            paintTarget.addAttribute("pid", pid);
                            
                            // Track paints to identify empty paints
                            paintTarget.setTrackPaints(true);
                            p.paint(paintTarget);

                            // If no paints add attribute empty
                            if (paintTarget.getNumberOfPaints() <= 0) {
                                paintTarget.addAttribute("visible", false);
                            }
                            paintTarget.endTag("change");
                            paintablePainted(p);
                        }
                    }
                    

                    paintTarget.close();
                    out.flush();
                } else {

                    // For download request, transfer the downloaded data
                    handleDownload(download, request, response);
                }
            }

            out.flush();
            out.close();

        } catch (Throwable e) {
            // Write the error report to client
            OutputStreamWriter w = new OutputStreamWriter(out);
            PrintWriter err = new PrintWriter(w);
            err
                    .write("<html><head><title>Application Internal Error</title></head><body>");
            err.write("<h1>" + e.toString() + "</h1><pre>\n");
            e.printStackTrace(new PrintWriter(err));
            err.write("\n</pre></body></html>");
            err.close();
        } finally {

        }

    }

    /**
     * Get the existing application or create a new one. Get a window within an
     * application based on the requested URI.
     * 
     * @param request
     *            HTTP Request.
     * @param application
     *            Application to query for window.
     * @return Window mathing the given URI or null if not found.
     */
    private Window getApplicationWindow(HttpServletRequest request,
            Application application) throws ServletException {

        Window window = null;

        // Find the window where the request is handled
        String path = request.getPathInfo();

        // Main window as the URI is empty
        if (path == null || path.length() == 0 || path.equals("/"))
            window = application.getMainWindow();

        // Try to search by window name
        else {
            String windowName = null;
            if (path.charAt(0) == '/')
                path = path.substring(1);
            int index = path.indexOf('/');
            if (index < 0) {
                windowName = path;
                path = "";
            } else {
                windowName = path.substring(0, index);
                path = path.substring(index + 1);
            }
            window = application.getWindow(windowName);

            // By default, we use main window
            if (window == null)
                window = application.getMainWindow();
        }

        return window;
    }

    /**
     * Handle the requested URI. An application can add handlers to do special
     * processing, when a certain URI is requested. The handlers are invoked
     * before any windows URIs are processed and if a DownloadStream is returned
     * it is sent to the client.
     * 
     * @see com.itmill.toolkit.terminal.URIHandler
     * 
     * @param application
     *            Application owning the URI
     * @param request
     *            HTTP request instance
     * @param response
     *            HTTP response to write to.
     * @return boolean True if the request was handled and further processing
     *         should be suppressed, false otherwise.
     */
    private DownloadStream handleURI(Application application,
            HttpServletRequest request, HttpServletResponse response) {

        String uri = request.getPathInfo();

        // If no URI is available
        if (uri == null || uri.length() == 0 || uri.equals("/"))
            return null;

        // Remove the leading /
        while (uri.startsWith("/") && uri.length() > 0)
            uri = uri.substring(1);

        // Handle the uri
        DownloadStream stream = null;
        try {
            stream = application.handleURI(application.getURL(), uri);
        } catch (Throwable t) {
            application.terminalError(new URIHandlerErrorImpl(application, t));
        }

        return stream;
    }

    /**
     * Handle the requested URI. An application can add handlers to do special
     * processing, when a certain URI is requested. The handlers are invoked
     * before any windows URIs are processed and if a DownloadStream is returned
     * it is sent to the client.
     * 
     * @see com.itmill.toolkit.terminal.URIHandler
     * 
     * @param application
     *            Application owning the URI
     * @param request
     *            HTTP request instance
     * @param response
     *            HTTP response to write to.
     * @return boolean True if the request was handled and further processing
     *         should be suppressed, false otherwise.
     */
    private void handleDownload(DownloadStream stream,
            HttpServletRequest request, HttpServletResponse response) {

        // Download from given stream
        InputStream data = stream.getStream();
        if (data != null) {

            // Set content type
            response.setContentType(stream.getContentType());

            // Set cache headers
            long cacheTime = stream.getCacheTime();
            if (cacheTime <= 0) {
                response.setHeader("Cache-Control", "no-cache");
                response.setHeader("Pragma", "no-cache");
                response.setDateHeader("Expires", 0);
            } else {
                response.setHeader("Cache-Control", "max-age=" + cacheTime
                        / 1000);
                response.setDateHeader("Expires", System.currentTimeMillis()
                        + cacheTime);
                response.setHeader("Pragma", "cache"); // Required to apply
                // caching in some
                // Tomcats
            }

            // Copy download stream parameters directly
            // to HTTP headers.
            Iterator i = stream.getParameterNames();
            if (i != null) {
                while (i.hasNext()) {
                    String param = (String) i.next();
                    response.setHeader((String) param, stream
                            .getParameter(param));
                }
            }

            int bufferSize = stream.getBufferSize();
            if (bufferSize <= 0 || bufferSize > MAX_BUFFER_SIZE)
                bufferSize = DEFAULT_BUFFER_SIZE;
            byte[] buffer = new byte[bufferSize];
            int bytesRead = 0;

            try {
                OutputStream out = response.getOutputStream();

                while ((bytesRead = data.read(buffer)) > 0) {
                    out.write(buffer, 0, bytesRead);
                    out.flush();
                }
                out.close();
            } catch (IOException ignored) {
            }

        }

    }

    /** End application */
    private void endApplication(HttpServletRequest request,
            HttpServletResponse response, Application application)
            throws IOException {

        String logoutUrl = application.getLogoutURL();
        if (logoutUrl == null)
            logoutUrl = application.getURL().toString();

        response.sendRedirect(response.encodeRedirectURL(logoutUrl));
    }

    /**
     * @param window
     * @return
     */
    public synchronized String getPaintableId(Paintable paintable) {

        String id = (String) paintableIdMap.get(paintable);
        if (id == null) {
            id = "PID" + Integer.toString(idSequence++);
            paintableIdMap.put(paintable, id);
        }

        return id;
    }

    public synchronized Set getDirtyComponents() {
        
        // Remove unnecessary repaints from the list
        Object[] paintables = dirtyPaintabletSet.toArray();
        for (int i=0; i<paintables.length; i++) {
            if (paintables[i] instanceof Component) {
                Component c = (Component) paintables[i];
                
                // Check if any of the parents of c already exist in the list
                Component p = c.getParent();
                while (p != null) {
                    if (dirtyPaintabletSet.contains(p)) {
                        
                        // Remove component c from the dirty paintables as its parent is also dirty
                        dirtyPaintabletSet.remove(c);
                        p = null;
                    } else
                        p = p.getParent();
                }
            }
        }
        
        return Collections.unmodifiableSet(dirtyPaintabletSet);
    }

    public synchronized void clearDirtyComponents() {
        dirtyPaintabletSet.clear();
    }

    public void repaintRequested(RepaintRequestEvent event) {
        Paintable p = event.getPaintable();
        dirtyPaintabletSet.add(p);
        
        // For FrameWindows we mark all frames (windows) dirty
        if (p instanceof FrameWindow) {
            FrameWindow fw = (FrameWindow)p;
            repaintFrameset(fw.getFrameset());
        }
    }

    /** Recursively request repaint for all frames in frameset.
     * 
     * @param fs Framewindow.Frameset
     */
    private void repaintFrameset(FrameWindow.Frameset fs) {
        List frames = fs.getFrames();
        for (Iterator i = frames.iterator(); i.hasNext();) {
            FrameWindow.Frame f = (FrameWindow.Frame) i.next();
            if (f instanceof FrameWindow.Frameset) {
                repaintFrameset((FrameWindow.Frameset) f);
            } else {
                Window w = f.getWindow();
                if (w != null) {
                    w.requestRepaint();
                }
            }
        }               
    }

    public void paintablePainted(Paintable p) {
        dirtyPaintabletSet.remove(p);
        p.requestRepaintRequests();
    }

    public boolean isDirty(Paintable paintable) {
        return (dirtyPaintabletSet.contains(paintable));
    }

    public void windowAttached(WindowAttachEvent event) {
        event.getWindow().addListener(this);
        dirtyPaintabletSet.add(event.getWindow());
    }

    public void windowDetached(WindowDetachEvent event) {
        event.getWindow().removeListener(this);
        // Notify client of the close operation
        removedWindows.add(event.getWindow());
    }

    public synchronized Set getRemovedWindows() {
        return Collections.unmodifiableSet(removedWindows);

    }

    private void removedWindowNotified(Window w) {
        this.removedWindows.remove(w);
    }

    /** Implementation of URIHandler.ErrorEvent interface. */
    public class URIHandlerErrorImpl implements URIHandler.ErrorEvent {

        private URIHandler owner;

        private Throwable throwable;

        private URIHandlerErrorImpl(URIHandler owner, Throwable throwable) {
            this.owner = owner;
            this.throwable = throwable;
        }

        /**
         * @see com.itmill.toolkit.terminal.Terminal.ErrorEvent#getThrowable()
         */
        public Throwable getThrowable() {
            return this.throwable;
        }

        /**
         * @see com.itmill.toolkit.terminal.URIHandler.ErrorEvent#getURIHandler()
         */
        public URIHandler getURIHandler() {
            return this.owner;
        }
    }
}