KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > verge > mvc > controller > DefaultLongTxnHandler


1 /*
2  * Copyright (c) 2003, Inversoft
3  *
4  * This software is distribuable under the GNU Lesser General Public License.
5  * For more information visit gnu.org.
6  */

7 package com.inversoft.verge.mvc.controller;
8
9
10 import java.io.IOException JavaDoc;
11 import java.io.Writer JavaDoc;
12 import java.net.URISyntaxException JavaDoc;
13
14 import javax.servlet.RequestDispatcher JavaDoc;
15 import javax.servlet.ServletException JavaDoc;
16 import javax.servlet.http.HttpServletRequest JavaDoc;
17 import javax.servlet.http.HttpServletResponse JavaDoc;
18
19 import org.apache.log4j.Logger;
20
21 import com.inversoft.verge.mvc.MVCException;
22
23
24 /**
25  * <p>
26  * This class is the default implementation of the long
27  * transaction handler.
28  * </p>
29  *
30  * @author Brian Pontarelli
31  */

32 public class DefaultLongTxnHandler implements LongTxnHandler {
33
34     /**
35      * This classes logger
36      */

37     private static final Logger logger = Logger.getLogger(DefaultLongTxnHandler.class);
38
39     /**
40      * The key in the request that the result is stored under
41      */

42     public static final String JavaDoc RESULT_PARAMETER =
43         "com.inversoft.verge.mvc.controller.form.Result";
44
45
46     /**
47      * Constructs a new <code>DefaultLongTxnHandler</code>
48      */

49     public DefaultLongTxnHandler() {
50         super();
51     }
52
53     /**
54      * Handles the long transaction by dynamically including the given URL unless
55      * it is null.
56      *
57      * @see LongTxnHandler#handleStartLongTxn(HttpServletRequest,
58      * HttpServletResponse, String)
59      */

60     public void handleStartLongTxn(HttpServletRequest JavaDoc request,
61             HttpServletResponse JavaDoc response, String JavaDoc url)
62     throws MVCException {
63         if (url != null) {
64
65             if (logger.isDebugEnabled()) {
66                 logger.debug("Including long txn URL: " + url);
67             }
68
69             RequestDispatcher JavaDoc rd = request.getRequestDispatcher(url);
70             try {
71                 rd.include(request, response);
72                 response.flushBuffer();
73             } catch (ServletException JavaDoc se) {
74                 throw new MVCException(se);
75             } catch (IOException JavaDoc ioe) {
76                 throw new MVCException(ioe);
77             }
78         } else {
79             logger.error("LongTxnEnabled but URL not specified. Consult the Verge" +
80                 " docs to figure out how to configure the URLs.");
81         }
82     }
83
84     /**
85      * Handles the long transaction end by dynamically including the given URL or
86      * manually outputting a HTML head tag with a meta-refresh tag to the URL in
87      * the given Result.
88      *
89      * @see LongTxnHandler#handleEndLongTxn(HttpServletRequest,
90      * HttpServletResponse, String, Result)
91      */

92     public void handleEndLongTxn(HttpServletRequest JavaDoc request,
93             HttpServletResponse JavaDoc response, String JavaDoc url, Result result)
94     throws MVCException {
95         if (url != null) {
96
97             // Store the result for use
98
request.setAttribute(RESULT_PARAMETER, result);
99
100             if (logger.isDebugEnabled()) {
101                 logger.debug("Including long txn end URL: " + url);
102             }
103
104             RequestDispatcher JavaDoc rd = request.getRequestDispatcher(url);
105             try {
106                 rd.include(request, response);
107             } catch (ServletException JavaDoc se) {
108                 throw new MVCException(se);
109             } catch (IOException JavaDoc ioe) {
110                 throw new MVCException(ioe);
111             }
112         } else {
113             // Write it out by hand
114
try {
115                 url = response.encodeRedirectURL(result.getGeneratedURL(request));
116             } catch (URISyntaxException JavaDoc urie) {
117                 throw new MVCException(urie);
118             }
119
120             if (logger.isDebugEnabled()) {
121                 logger.debug("Manually writing out long txn end meta-refresh " +
122                     "with URL: " + url);
123             }
124
125             try {
126                 Writer JavaDoc writer = response.getWriter();
127                 writer.write("<head><meta http-equiv='REFRESH' content='0;url=" +
128                     url + "'/></head></html>");
129                 response.flushBuffer();
130             } catch (IOException JavaDoc ioe) {
131                 throw new MVCException(ioe);
132             }
133         }
134     }
135 }
Popular Tags