Source Code Cross Referenced for PageContextImpl.java in  » EJB-Server-resin-3.1.5 » resin » com » caucho » jsp » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » EJB Server resin 3.1.5 » resin » com.caucho.jsp 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


0001:        /*
0002:         * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
0003:         *
0004:         * This file is part of Resin(R) Open Source
0005:         *
0006:         * Each copy or derived work must preserve the copyright notice and this
0007:         * notice unmodified.
0008:         *
0009:         * Resin Open Source is free software; you can redistribute it and/or modify
0010:         * it under the terms of the GNU General Public License as published by
0011:         * the Free Software Foundation; either version 2 of the License, or
0012:         * (at your option) any later version.
0013:         *
0014:         * Resin Open Source is distributed in the hope that it will be useful,
0015:         * but WITHOUT ANY WARRANTY; without even the implied warranty of
0016:         * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
0017:         * of NON-INFRINGEMENT.  See the GNU General Public License for more
0018:         * details.
0019:         *
0020:         * You should have received a copy of the GNU General Public License
0021:         * along with Resin Open Source; if not, write to the
0022:         *
0023:         *   Free Software Foundation, Inc.
0024:         *   59 Temple Place, Suite 330
0025:         *   Boston, MA 02111-1307  USA
0026:         *
0027:         * @author Scott Ferguson
0028:         */
0029:
0030:        package com.caucho.jsp;
0031:
0032:        import com.caucho.el.EL;
0033:        import com.caucho.el.ExprEnv;
0034:        import com.caucho.jsp.cfg.JspPropertyGroup;
0035:        import com.caucho.jsp.el.*;
0036:        import com.caucho.jstl.JstlPageContext;
0037:        import com.caucho.log.Log;
0038:        import com.caucho.server.connection.AbstractHttpRequest;
0039:        import com.caucho.server.connection.AbstractResponseStream;
0040:        import com.caucho.server.connection.CauchoRequest;
0041:        import com.caucho.server.connection.CauchoResponse;
0042:        import com.caucho.server.connection.RequestAdapter;
0043:        import com.caucho.server.connection.ToCharResponseAdapter;
0044:        import com.caucho.server.webapp.RequestDispatcherImpl;
0045:        import com.caucho.server.webapp.WebApp;
0046:        import com.caucho.util.CharBuffer;
0047:        import com.caucho.util.HashMapImpl;
0048:        import com.caucho.util.L10N;
0049:        import com.caucho.util.NullEnumeration;
0050:        import com.caucho.vfs.ClientDisconnectException;
0051:        import com.caucho.vfs.FlushBuffer;
0052:        import com.caucho.vfs.Path;
0053:        import com.caucho.vfs.TempCharBuffer;
0054:        import com.caucho.xpath.VarEnv;
0055:
0056:        import org.w3c.dom.Node;
0057:
0058:        import javax.el.ELContext;
0059:        import javax.el.ELContextEvent;
0060:        import javax.el.ELContextListener;
0061:        import javax.el.ELResolver;
0062:        import javax.el.ValueExpression;
0063:        import javax.servlet.*;
0064:        import javax.servlet.http.Cookie;
0065:        import javax.servlet.http.HttpServletRequest;
0066:        import javax.servlet.http.HttpServletResponse;
0067:        import javax.servlet.http.HttpSession;
0068:        import javax.servlet.jsp.ErrorData;
0069:        import javax.servlet.jsp.JspContext;
0070:        import javax.servlet.jsp.JspException;
0071:        import javax.servlet.jsp.JspWriter;
0072:        import javax.servlet.jsp.PageContext;
0073:        import javax.servlet.jsp.SkipPageException;
0074:        import javax.servlet.jsp.el.ExpressionEvaluator;
0075:        import javax.servlet.jsp.el.VariableResolver;
0076:        import javax.servlet.jsp.jstl.core.Config;
0077:        import javax.servlet.jsp.jstl.fmt.LocalizationContext;
0078:        import javax.servlet.jsp.tagext.BodyContent;
0079:        import javax.servlet.jsp.tagext.JspFragment;
0080:        import java.io.FileNotFoundException;
0081:        import java.io.IOException;
0082:        import java.io.PrintWriter;
0083:        import java.io.Reader;
0084:        import java.io.Writer;
0085:        import java.lang.reflect.Method;
0086:        import java.text.MessageFormat;
0087:        import java.util.Collections;
0088:        import java.util.Enumeration;
0089:        import java.util.HashMap;
0090:        import java.util.Locale;
0091:        import java.util.Map;
0092:        import java.util.ResourceBundle;
0093:        import java.util.logging.Level;
0094:        import java.util.logging.Logger;
0095:
0096:        public class PageContextImpl extends PageContext implements  ExprEnv,
0097:                JstlPageContext, VariableResolver {
0098:            private static final Logger log = Log.open(PageContextImpl.class);
0099:            static final L10N L = new L10N(PageContextImpl.class);
0100:
0101:            private JspWriterAdapter _jspAdapter = new JspWriterAdapter();
0102:            private JspServletOutputStream _jspOutputStream = new JspServletOutputStream(
0103:                    this );
0104:
0105:            private Map<String, Object> _attributes;
0106:            private Servlet _servlet;
0107:            private HttpServletRequest _request;
0108:
0109:            private CauchoResponse _response;
0110:            private ToCharResponseAdapter _responseAdapter;
0111:
0112:            private WebApp _webApp;
0113:            private HttpSession _session;
0114:            private JspWriter _topOut;
0115:            private JspWriter _out;
0116:            private String _errorPage;
0117:            protected boolean _isFilled;
0118:
0119:            private AbstractResponseStream _responseStream;
0120:
0121:            private BodyResponseStream _bodyResponseStream;
0122:
0123:            private JspPrintWriter _jspPrintWriter;
0124:
0125:            private int _bufferSize = 8192;
0126:            private boolean autoFlush;
0127:            private BodyContentImpl _bodyOut;
0128:
0129:            private Locale _locale;
0130:            private BundleManager _bundleManager;
0131:
0132:            private VarEnv _varEnv;
0133:            private Node _nodeEnv;
0134:
0135:            private final CharBuffer _cb = new CharBuffer();
0136:
0137:            private VariableResolver _varResolver;
0138:            private ELContext _elContext;
0139:            private ELResolver _elResolver;
0140:            private javax.el.FunctionMapper _functionMapper;
0141:            private PageVariableMapper _variableMapper;
0142:            private boolean _hasException;
0143:
0144:            private HashMap<String, Method> _functionMap;
0145:
0146:            private ExpressionEvaluatorImpl _expressionEvaluator;
0147:
0148:            PageContextImpl() {
0149:                _attributes = new HashMapImpl<String, Object>();
0150:
0151:                _bodyResponseStream = new BodyResponseStream();
0152:                _bodyResponseStream.start();
0153:
0154:                _jspPrintWriter = new JspPrintWriter();
0155:            }
0156:
0157:            public PageContextImpl(WebApp webApp, Servlet servlet) {
0158:                this ();
0159:
0160:                if (webApp == null)
0161:                    throw new NullPointerException();
0162:
0163:                _webApp = webApp;
0164:                _servlet = servlet;
0165:
0166:                if (servlet instanceof  Page) {
0167:                    Page page = (Page) servlet;
0168:
0169:                    _functionMap = page._caucho_getFunctionMap();
0170:                } else
0171:                    _functionMap = null;
0172:
0173:            }
0174:
0175:            public PageContextImpl(WebApp webApp,
0176:                    HashMap<String, Method> functionMap) {
0177:                this ();
0178:
0179:                if (webApp == null)
0180:                    throw new NullPointerException();
0181:
0182:                _webApp = webApp;
0183:
0184:                _functionMap = functionMap;
0185:            }
0186:
0187:            public void initialize(Servlet servlet, ServletRequest request,
0188:                    ServletResponse response, String errorPage,
0189:                    boolean needsSession, int bufferSize, boolean autoFlush) {
0190:                HttpSession session = null;
0191:
0192:                if (needsSession)
0193:                    session = ((HttpServletRequest) request).getSession(true);
0194:
0195:                ServletConfig config = servlet.getServletConfig();
0196:                WebApp app = (WebApp) config.getServletContext();
0197:
0198:                initialize(servlet, app, request, response, errorPage, session,
0199:                        bufferSize, autoFlush, false);
0200:            }
0201:
0202:            public void initialize(Servlet servlet, WebApp app,
0203:                    ServletRequest request, ServletResponse response,
0204:                    String errorPage, HttpSession session, int bufferSize,
0205:                    boolean autoFlush, boolean isPrintNullAsBlank) {
0206:                _servlet = servlet;
0207:                _request = (HttpServletRequest) request;
0208:
0209:                if (response instanceof  CauchoResponse
0210:                        && bufferSize <= TempCharBuffer.SIZE) {
0211:                    _response = (CauchoResponse) response;
0212:                    _responseAdapter = null;
0213:                } else {
0214:                    // JSP.12.2.3 - JSP must use PrintWriter
0215:                    _responseAdapter = ToCharResponseAdapter
0216:                            .create((HttpServletResponse) response);
0217:                    _response = _responseAdapter;
0218:
0219:                    try {
0220:                        // jsp/017m
0221:                        response.setBufferSize(bufferSize);
0222:                    } catch (Exception e) {
0223:                        log.log(Level.FINE, e.toString(), e);
0224:                    }
0225:                }
0226:
0227:                _responseStream = _response.getResponseStream();
0228:                _topOut = _jspAdapter;
0229:                _responseStream.setAutoFlush(autoFlush);
0230:                _jspAdapter.init(null, _responseStream);
0231:                _jspAdapter.setPrintNullAsBlank(isPrintNullAsBlank);
0232:
0233:                if (bufferSize != TempCharBuffer.SIZE) {
0234:                    try {
0235:                        _responseStream.setBufferSize(bufferSize);
0236:                    } catch (Exception e) {
0237:                        log.log(Level.FINE, e.toString(), e);
0238:                    }
0239:                }
0240:
0241:                // needed for includes from static pages
0242:
0243:                _bufferSize = bufferSize;
0244:                this .autoFlush = autoFlush;
0245:                _session = session;
0246:
0247:                _out = _topOut;
0248:
0249:                _errorPage = errorPage;
0250:                _webApp = app;
0251:                _locale = null;
0252:
0253:                //_topOut.init(this, bufferSize, autoFlush);
0254:
0255:                // jsp/17ee
0256:                //_jspPrintWriter.init(_topOut);
0257:                //_response.setPrintWriter(_jspPrintWriter);
0258:
0259:                // not quite correct
0260:                //  _writerStreamImpl.setWriter(_topOut);
0261:                // _response.setPrintWriter(body.getWriter());
0262:
0263:                // jsp/3145
0264:                _elContext = null;
0265:                /*
0266:                _elResolver = null;
0267:                 */
0268:
0269:                _hasException = false;
0270:                //if (_attributes.size() > 0)
0271:                //  _attributes.clear();
0272:                _isFilled = false;
0273:                _bundleManager = null;
0274:                _varResolver = null;
0275:                _nodeEnv = null;
0276:
0277:                if (servlet instanceof  Page) {
0278:                    Page page = (Page) servlet;
0279:
0280:                    _functionMap = page._caucho_getFunctionMap();
0281:                } else
0282:                    _functionMap = null;
0283:            }
0284:
0285:            protected void init() {
0286:                _elContext = null;
0287:            }
0288:
0289:            protected void setOut(JspWriter out) {
0290:                _out = out;
0291:            }
0292:
0293:            protected void clearAttributes() {
0294:                _attributes.clear();
0295:            }
0296:
0297:            /**
0298:             * Returns the page attribute with the given name.
0299:             *
0300:             * @param name the attribute name.
0301:             *
0302:             * @return the attribute's value.
0303:             */
0304:            public Object getAttribute(String name) {
0305:                if (name == null)
0306:                    throw new NullPointerException(L
0307:                            .l("getAttribute must have a non-null name"));
0308:
0309:                Object value = _attributes.get(name);
0310:                if (value != null)
0311:                    return value;
0312:                else if (!_isFilled) {
0313:                    fillAttribute();
0314:                    value = _attributes.get(name);
0315:                }
0316:
0317:                if (value != null) {
0318:                } else if (name.equals(OUT)) {
0319:                    // jsp/162d
0320:                    return _out;
0321:                }
0322:
0323:                return value;
0324:            }
0325:
0326:            /**
0327:             * Sets the page attribute with the given name.
0328:             *
0329:             * @param name the attribute name.
0330:             * @param value the new value
0331:             */
0332:            public void setAttribute(String name, Object attribute) {
0333:                if (name == null)
0334:                    throw new NullPointerException(L
0335:                            .l("setAttribute must have a non-null name"));
0336:
0337:                if (attribute != null)
0338:                    _attributes.put(name, attribute);
0339:                else
0340:                    _attributes.remove(name);
0341:            }
0342:
0343:            /**
0344:             * Sets the page attribute with the given name.
0345:             *
0346:             * @param name the attribute name.
0347:             * @param value the new value
0348:             */
0349:            public Object putAttribute(String name, Object attribute) {
0350:                if (name == null)
0351:                    throw new NullPointerException(L
0352:                            .l("putAttribute must have a non-null name"));
0353:
0354:                if (attribute != null)
0355:                    return _attributes.put(name, attribute);
0356:                else
0357:                    return _attributes.remove(name);
0358:            }
0359:
0360:            /**
0361:             * Removes a named attribute from the page context.
0362:             *
0363:             * @param name the name of the attribute to remove
0364:             */
0365:            public void removeAttribute(String name) {
0366:                if (name == null)
0367:                    throw new NullPointerException(L
0368:                            .l("removeAttribute must have a non-null name"));
0369:
0370:                _attributes.remove(name);
0371:                // jsp/162b
0372:                if (_request != null)
0373:                    _request.removeAttribute(name);
0374:
0375:                if (_session != null) {
0376:                    try {
0377:                        _session.removeAttribute(name);
0378:                    } catch (IllegalStateException e) {
0379:                        // jsp/162f
0380:                        log.log(Level.FINE, e.toString(), e);
0381:                    }
0382:                }
0383:
0384:                if (_webApp != null)
0385:                    _webApp.removeAttribute(name);
0386:            }
0387:
0388:            public Enumeration<String> getAttributeNames() {
0389:                if (!_isFilled)
0390:                    fillAttribute();
0391:
0392:                return Collections.enumeration(_attributes.keySet());
0393:            }
0394:
0395:            /**
0396:             * Fills the predefined page content _attributes with their values.
0397:             */
0398:            protected void fillAttribute() {
0399:                _isFilled = true;
0400:                _attributes.put(PAGE, _servlet);
0401:                _attributes.put(PAGECONTEXT, this );
0402:                _attributes.put(REQUEST, getCauchoRequest());
0403:                _attributes.put(RESPONSE, getCauchoResponse());
0404:                if (_servlet != null)
0405:                    _attributes.put(CONFIG, _servlet.getServletConfig());
0406:                if (getSession() != null)
0407:                    _attributes.put(SESSION, getSession());
0408:                _attributes.put(APPLICATION, getApplication());
0409:            }
0410:
0411:            public Object getAttribute(String name, int scope) {
0412:                switch (scope) {
0413:                case PAGE_SCOPE:
0414:                    return getAttribute(name);
0415:                case REQUEST_SCOPE:
0416:                    return getCauchoRequest().getAttribute(name);
0417:                case SESSION_SCOPE: {
0418:                    HttpSession session = getSession();
0419:                    return session != null ? session.getValue(name) : null;
0420:                }
0421:                case APPLICATION_SCOPE:
0422:                    return getApplication().getAttribute(name);
0423:
0424:                default:
0425:                    throw new IllegalArgumentException();
0426:                }
0427:            }
0428:
0429:            public void setAttribute(String name, Object value, int scope) {
0430:                switch (scope) {
0431:                case PAGE_SCOPE:
0432:                    setAttribute(name, value);
0433:                    break;
0434:
0435:                case REQUEST_SCOPE:
0436:                    getCauchoRequest().setAttribute(name, value);
0437:                    break;
0438:
0439:                case SESSION_SCOPE:
0440:                    if (getSession() != null)
0441:                        getSession().putValue(name, value);
0442:                    break;
0443:
0444:                case APPLICATION_SCOPE:
0445:                    getApplication().setAttribute(name, value);
0446:                    break;
0447:
0448:                default:
0449:                    throw new IllegalArgumentException();
0450:                }
0451:            }
0452:
0453:            public void removeAttribute(String name, int scope) {
0454:                if (name == null)
0455:                    throw new NullPointerException(L
0456:                            .l("removeAttribute must have a non-null name"));
0457:
0458:                switch (scope) {
0459:                case PAGE_SCOPE:
0460:                    if (name != null)
0461:                        _attributes.remove(name);
0462:                    break;
0463:
0464:                case REQUEST_SCOPE:
0465:                    getCauchoRequest().removeAttribute(name);
0466:                    break;
0467:
0468:                case SESSION_SCOPE:
0469:                    if (getSession() != null)
0470:                        getSession().removeValue(name);
0471:                    break;
0472:
0473:                case APPLICATION_SCOPE:
0474:                    getApplication().removeAttribute(name);
0475:                    break;
0476:
0477:                default:
0478:                    throw new IllegalArgumentException();
0479:                }
0480:            }
0481:
0482:            public Enumeration getAttributeNames(int scope) {
0483:                switch (scope) {
0484:                case PAGE_SCOPE:
0485:                    return getAttributeNames();
0486:
0487:                case REQUEST_SCOPE:
0488:                    return getCauchoRequest().getAttributeNames();
0489:
0490:                case SESSION_SCOPE:
0491:                    if (getSession() != null)
0492:                        return new StringArrayEnum(getSession().getValueNames());
0493:                    else
0494:                        return NullEnumeration.create();
0495:
0496:                case APPLICATION_SCOPE:
0497:                    return getApplication().getAttributeNames();
0498:
0499:                default:
0500:                    throw new IllegalArgumentException();
0501:                }
0502:            }
0503:
0504:            public Enumeration getAttributeNamesInScope(int scope) {
0505:                return getAttributeNames(scope);
0506:            }
0507:
0508:            /**
0509:             * Finds an attribute in any of the scopes from page to webApp.
0510:             *
0511:             * @param name the attribute name.
0512:             *
0513:             * @return the attribute value
0514:             */
0515:            public Object findAttribute(String name) {
0516:                Object value;
0517:
0518:                if ((value = getAttribute(name)) != null)
0519:                    return value;
0520:
0521:                if ((value = getCauchoRequest().getAttribute(name)) != null)
0522:                    return value;
0523:
0524:                HttpSession session = getSession();
0525:                if (session != null) {
0526:                    try {
0527:                        value = session.getAttribute(name);
0528:                    } catch (IllegalStateException e) {
0529:                        // jsp/162e
0530:                        log.log(Level.FINE, e.toString(), e);
0531:                    }
0532:
0533:                    if (value != null)
0534:                        return value;
0535:                }
0536:
0537:                return getServletContext().getAttribute(name);
0538:            }
0539:
0540:            /**
0541:             * Return the scope of the named attribute.
0542:             *
0543:             * @param name the name of the attribute.
0544:             *
0545:             * @return the scope of the attribute
0546:             */
0547:            public int getAttributesScope(String name) {
0548:                if (getAttribute(name) != null)
0549:                    return PAGE_SCOPE;
0550:
0551:                if (getCauchoRequest().getAttribute(name) != null)
0552:                    return REQUEST_SCOPE;
0553:
0554:                HttpSession session = getSession();
0555:                if (session != null && session.getValue(name) != null)
0556:                    return SESSION_SCOPE;
0557:
0558:                if (getApplication().getAttribute(name) != null)
0559:                    return APPLICATION_SCOPE;
0560:
0561:                return 0;
0562:            }
0563:
0564:            /**
0565:             * Sets the attribute map.
0566:             */
0567:            public Map<String, Object> setMap(Map<String, Object> map) {
0568:                Map<String, Object> oldMap = _attributes;
0569:                _attributes = map;
0570:                return oldMap;
0571:            }
0572:
0573:            /**
0574:             * Returns the current writer.
0575:             */
0576:            public JspWriter getOut() {
0577:                return _out;
0578:            }
0579:
0580:            /**
0581:             * Pushes a new BodyContent onto the JspWriter stack.
0582:             */
0583:            public BodyContent pushBody() {
0584:                BodyContentImpl body;
0585:                if (_bodyOut != null) {
0586:                    body = _bodyOut;
0587:                    _bodyOut = null;
0588:                } else
0589:                    body = BodyContentImpl.allocate();
0590:
0591:                CauchoResponse response = getCauchoResponse();
0592:
0593:                body.init(_out);
0594:
0595:                _out = body;
0596:
0597:                response.setForbidForward(true);
0598:                try {
0599:                    _bodyResponseStream.flushBuffer();
0600:                } catch (IOException e) {
0601:                }
0602:                _bodyResponseStream.start();
0603:                _bodyResponseStream.setWriter(body);
0604:                _bodyResponseStream
0605:                        .setEncoding(response.getCharacterEncoding());
0606:                response.setResponseStream(_bodyResponseStream);
0607:
0608:                return body;
0609:            }
0610:
0611:            /**
0612:             * Pushes a new writer onto the JspWriter stack.
0613:             */
0614:            public JspWriter pushBody(Writer writer) {
0615:                if (writer == _out)
0616:                    return null;
0617:
0618:                JspWriter oldWriter = _out;
0619:
0620:                StreamJspWriter jspWriter;
0621:
0622:                jspWriter = new StreamJspWriter();
0623:                jspWriter.init(_out, writer);
0624:
0625:                _out = jspWriter;
0626:
0627:                getCauchoResponse().setForbidForward(true);
0628:
0629:                _bodyResponseStream.setWriter(writer);
0630:                getCauchoResponse().setResponseStream(_bodyResponseStream);
0631:
0632:                return oldWriter;
0633:            }
0634:
0635:            /**
0636:             * Pops the BodyContent from the JspWriter stack.
0637:             *
0638:             * @return the enclosing writer
0639:             */
0640:            public JspWriter popBody() {
0641:                BodyContentImpl bodyOut = (BodyContentImpl) _out;
0642:                _out = bodyOut.getEnclosingWriter();
0643:
0644:                try {
0645:                    _bodyResponseStream.flushBuffer();
0646:                    //if (_writeStream != null)
0647:                    // _writeStream.flushBuffer();
0648:                } catch (IOException e) {
0649:                    log.log(Level.WARNING, e.toString(), e);
0650:                }
0651:
0652:                if (_out instanceof  StreamJspWriter) {
0653:                    StreamJspWriter writer = (StreamJspWriter) _out;
0654:
0655:                    _bodyResponseStream.setWriter(writer.getWriter());
0656:                } else if (_out instanceof  JspWriterAdapter) {
0657:                    if (getCauchoResponse() != null) {
0658:                        getCauchoResponse().setResponseStream(_responseStream);
0659:                        getCauchoResponse().setForbidForward(false);
0660:                    }
0661:                } else if (_out instanceof  BodyContentImpl) {
0662:                    BodyContentImpl body = (BodyContentImpl) _out;
0663:
0664:                    _bodyResponseStream.setWriter(body.getWriter());
0665:                }
0666:
0667:                return _out;
0668:            }
0669:
0670:            /**
0671:             * Pops the BodyContent from the JspWriter stack.
0672:             *
0673:             * @return the enclosing writer
0674:             */
0675:            public JspWriter popAndReleaseBody() throws IOException {
0676:                BodyContentImpl body = (BodyContentImpl) getOut();
0677:
0678:                JspWriter out = popBody();
0679:
0680:                releaseBody(body);
0681:
0682:                return out;
0683:            }
0684:
0685:            public void releaseBody(BodyContentImpl out) throws IOException {
0686:                if (_bodyOut == null) {
0687:                    out.releaseNoFree();
0688:                    _bodyOut = out;
0689:                } else
0690:                    out.release();
0691:            }
0692:
0693:            /**
0694:             * Pops the BodyContent from the JspWriter stack.
0695:             *
0696:             * @param oldWriter the old writer
0697:             */
0698:            public JspWriter setWriter(JspWriter oldWriter) {
0699:                if (_out == oldWriter)
0700:                    return oldWriter;
0701:
0702:                /*
0703:                if (_out instanceof FlushBuffer) {
0704:                  try {
0705:                ((FlushBuffer) _out).flushBuffer();
0706:                  } catch (IOException e) {
0707:                  }
0708:                }
0709:                 */
0710:                try {
0711:                    if (_out instanceof  FlushBuffer)
0712:                        ((FlushBuffer) _out).flushBuffer();
0713:                } catch (IOException e) {
0714:                }
0715:
0716:                _out = oldWriter;
0717:
0718:                // jsp/18eg
0719:                if (_out instanceof  StreamJspWriter) {
0720:                    StreamJspWriter writer = (StreamJspWriter) _out;
0721:
0722:                    _bodyResponseStream.setWriter(writer.getWriter());
0723:                } else if (_out instanceof  JspWriterAdapter) {
0724:                    if (getCauchoResponse() != null) {
0725:                        getCauchoResponse().setResponseStream(_responseStream);
0726:                        getCauchoResponse().setForbidForward(false);
0727:                    }
0728:                } else if (_out instanceof  BodyContentImpl) {
0729:                    BodyContentImpl body = (BodyContentImpl) _out;
0730:
0731:                    _bodyResponseStream.setWriter(body.getWriter());
0732:                }
0733:
0734:                return oldWriter;
0735:
0736:                // getCauchoResponse().setWriter(_os);
0737:            }
0738:
0739:            /**
0740:             * Returns the top writer.
0741:             */
0742:            public PrintWriter getTopWriter() throws IOException {
0743:                CauchoResponse response = getCauchoResponse();
0744:
0745:                AbstractResponseStream currentStream = response
0746:                        .getResponseStream();
0747:
0748:                response.setResponseStream(_responseStream);
0749:
0750:                try {
0751:                    return response.getWriter();
0752:                } finally {
0753:                    response.setResponseStream(currentStream);
0754:                }
0755:            }
0756:
0757:            /**
0758:             * Returns the response output stream.
0759:             */
0760:            ServletOutputStream getOutputStream() {
0761:                try {
0762:                    return getCauchoResponse().getOutputStream();
0763:                } catch (IOException e) {
0764:                    throw new RuntimeException(e);
0765:                }
0766:            }
0767:
0768:            /**
0769:             * Returns the underlying servlet for the page.
0770:             */
0771:            public Object getPage() {
0772:                return _servlet;
0773:            }
0774:
0775:            /**
0776:             * Returns the servlet request for the page.
0777:             */
0778:            public ServletRequest getRequest() {
0779:                return _request;
0780:            }
0781:
0782:            /**
0783:             * Returns the servlet response for the page.
0784:             */
0785:            public ServletResponse getResponse() {
0786:                return getCauchoResponse();
0787:            }
0788:
0789:            /**
0790:             * Returns the servlet response for the page.
0791:             */
0792:            public CauchoResponse getCauchoResponse() {
0793:                return _response;
0794:            }
0795:
0796:            /**
0797:             * Returns the servlet response for the page.
0798:             */
0799:            public HttpServletRequest getCauchoRequest() {
0800:                return _request;
0801:            }
0802:
0803:            public HttpSession getSession() {
0804:                if (_session == null)
0805:                    _session = getCauchoRequest().getSession(false);
0806:
0807:                return _session;
0808:            }
0809:
0810:            /**
0811:             * Returns the session, throwing an IllegalStateException if it's
0812:             * not available.
0813:             */
0814:            public HttpSession getSessionScope() {
0815:                if (_session == null)
0816:                    _session = getCauchoRequest().getSession(false);
0817:
0818:                if (_session == null)
0819:                    throw new IllegalStateException(L
0820:                            .l("session is not available"));
0821:
0822:                return _session;
0823:            }
0824:
0825:            public ServletConfig getServletConfig() {
0826:                return _servlet.getServletConfig();
0827:            }
0828:
0829:            /**
0830:             * Returns the page's servlet context.
0831:             */
0832:            public ServletContext getServletContext() {
0833:                return _webApp;
0834:            }
0835:
0836:            /**
0837:             * Returns the page's webApp.
0838:             */
0839:            public WebApp getApplication() {
0840:                return _webApp;
0841:            }
0842:
0843:            /**
0844:             * Returns the page's error page.
0845:             */
0846:            public String getErrorPage() {
0847:                return _errorPage;
0848:            }
0849:
0850:            /**
0851:             * Sets the page's error page.
0852:             */
0853:            public void setErrorPage(String errorPage) {
0854:                _errorPage = errorPage;
0855:            }
0856:
0857:            public Exception getException() {
0858:                return (Exception) getThrowable();
0859:            }
0860:
0861:            /**
0862:             * Returns the Throwable stored by the error page.
0863:             */
0864:            public Throwable getThrowable() {
0865:                Throwable exn = (Throwable) getCauchoRequest().getAttribute(
0866:                        EXCEPTION);
0867:
0868:                if (exn == null)
0869:                    exn = (Throwable) getCauchoRequest().getAttribute(
0870:                            "javax.servlet.error.exception_type");
0871:                if (exn == null)
0872:                    exn = (Throwable) getCauchoRequest().getAttribute(
0873:                            "javax.servlet.jsp:jspException");
0874:
0875:                return exn;
0876:            }
0877:
0878:            public void include(String relativeUrl) throws ServletException,
0879:                    IOException {
0880:                include(relativeUrl, false);
0881:            }
0882:
0883:            /**
0884:             * Include another servlet into the current output stream.
0885:             *
0886:             * @param relativeUrl url relative to the current request.
0887:             */
0888:            public void include(String relativeUrl, String query, boolean flush)
0889:                    throws ServletException, IOException {
0890:                if ("".equals(query)) {
0891:                } else if (relativeUrl.indexOf('?') > 0)
0892:                    relativeUrl = relativeUrl + '&' + query;
0893:                else
0894:                    relativeUrl = relativeUrl + '?' + query;
0895:
0896:                include(relativeUrl, flush);
0897:            }
0898:
0899:            /**
0900:             * Include another servlet into the current output stream.
0901:             *
0902:             * @param relativeUrl url relative to the current request.
0903:             */
0904:            public void include(String relativeUrl, boolean flush)
0905:                    throws ServletException, IOException {
0906:                RequestDispatcher rd = null;
0907:
0908:                HttpServletRequest req = (HttpServletRequest) getCauchoRequest();
0909:                HttpServletResponse res = (HttpServletResponse) getResponse();
0910:
0911:                if (relativeUrl != null && !relativeUrl.startsWith("/")) {
0912:                    String path = RequestAdapter.getPageServletPath(req);
0913:                    if (path == null)
0914:                        path = RequestAdapter.getPagePathInfo(req);
0915:                    if (path == null)
0916:                        path = "/";
0917:                    int p = path.lastIndexOf('/');
0918:                    if (p >= 0) {
0919:                        _cb.clear();
0920:                        _cb.append(path, 0, p + 1);
0921:                        _cb.append(relativeUrl);
0922:                        rd = getServletContext().getRequestDispatcher(
0923:                                _cb.toString());
0924:                    }
0925:                }
0926:
0927:                if (rd == null)
0928:                    rd = req.getRequestDispatcher(relativeUrl);
0929:
0930:                if (rd == null)
0931:                    throw new ServletException(L.l(
0932:                            "unknown including page `{0}'.", relativeUrl));
0933:
0934:                // the FlushBuffer needs to happen to deal with OpenSymphony (Bug#1710)
0935:                // jsp/17e9, 15lc, 15m4
0936:                if (!flush) {
0937:                } else if (_out instanceof  FlushBuffer)
0938:                    ((FlushBuffer) _out).flushBuffer();
0939:                else if (flush)
0940:                    _out.flush();
0941:
0942:                rd.include(req, res);
0943:            }
0944:
0945:            /**
0946:             * Include another servlet into the current output stream.
0947:             *
0948:             * @param relativeUrl url relative to the current request.
0949:             */
0950:            public void forward(String relativeUrl, String query)
0951:                    throws ServletException, IOException {
0952:                if ("".equals(query)) {
0953:                } else if (relativeUrl.indexOf('?') > 0)
0954:                    relativeUrl = relativeUrl + '&' + query;
0955:                else
0956:                    relativeUrl = relativeUrl + '?' + query;
0957:
0958:                forward(relativeUrl);
0959:            }
0960:
0961:            /**
0962:             * Forward a subrequest relative to the current url.  Absolute URLs
0963:             * are relative to the context root.
0964:             *
0965:             * @param relativeUrl url relative to the current file
0966:             */
0967:            public void forward(String relativeUrl) throws ServletException,
0968:                    IOException {
0969:                if (_bufferSize == 0) {
0970:                    // jsp/15m3, tck
0971:                    if (_out instanceof  FlushBuffer)
0972:                        ((FlushBuffer) _out).flushBuffer();
0973:                    else
0974:                        _out.flush();
0975:                }
0976:
0977:                RequestDispatcher rd = null;
0978:
0979:                HttpServletRequest req = (HttpServletRequest) getCauchoRequest();
0980:                HttpServletResponse res = (HttpServletResponse) getResponse();
0981:
0982:                if (res.isCommitted())
0983:                    throw new IOException(L
0984:                            .l("can't forward after writing HTTP headers"));
0985:
0986:                _out.clear();
0987:
0988:                if (relativeUrl != null && !relativeUrl.startsWith("/")) {
0989:                    String servletPath = RequestAdapter.getPageServletPath(req);
0990:                    int p = servletPath.lastIndexOf('/');
0991:                    if (p >= 0) {
0992:                        _cb.clear();
0993:                        _cb.append(servletPath, 0, p + 1);
0994:                        _cb.append(relativeUrl);
0995:                        rd = getServletContext().getRequestDispatcher(
0996:                                _cb.toString());
0997:                    }
0998:                }
0999:
1000:                if (rd == null)
1001:                    rd = req.getRequestDispatcher(relativeUrl);
1002:
1003:                if (rd == null)
1004:                    throw new ServletException(L.l(
1005:                            "unknown forwarding page: `{0}'", relativeUrl));
1006:                rd.forward(req, res);
1007:
1008:                _out.close();
1009:                _responseStream.close();
1010:            }
1011:
1012:            /**
1013:             * Handles an exception caught in the JSP page.
1014:             *
1015:             * @param e the caught exception
1016:             */
1017:            public void handlePageException(Exception e)
1018:                    throws ServletException, IOException {
1019:                handlePageException((Throwable) e);
1020:            }
1021:
1022:            /**
1023:             * Handles an exception caught in the JSP page.
1024:             *
1025:             * @param e the caught exception
1026:             */
1027:            public void handlePageException(Throwable e)
1028:                    throws ServletException, IOException {
1029:                if (e instanceof  SkipPageException)
1030:                    return;
1031:
1032:                HttpServletRequest request = getCauchoRequest();
1033:
1034:                request.setAttribute("javax.servlet.jsp.jspException", e);
1035:
1036:                CauchoResponse response = getCauchoResponse();
1037:
1038:                response.setForbidForward(false);
1039:                response.setResponseStream(_responseStream);
1040:                response.killCache();
1041:                response.setNoCache(true);
1042:
1043:                _hasException = true;
1044:
1045:                if (e instanceof  ClientDisconnectException)
1046:                    throw (ClientDisconnectException) e;
1047:
1048:                if (!(_servlet instanceof  Page)) {
1049:                } else if (getApplication() == null
1050:                        || getApplication().getJsp() == null
1051:                        || !getApplication().getJsp().isRecompileOnError()) {
1052:                } else if (e instanceof  OutOfMemoryError) {
1053:                } else if (e instanceof  Error) {
1054:                    try {
1055:                        Path workDir = getApplication().getAppDir().lookup(
1056:                                "WEB-INF/work");
1057:                        String className = _servlet.getClass().getName();
1058:                        Path path = workDir.lookup(className.replace('.', '/')
1059:                                + ".class");
1060:
1061:                        log.warning("Removing " + path + " due to " + e);
1062:
1063:                        path.remove();
1064:                    } catch (Exception e1) {
1065:                    }
1066:                    Page page = (Page) _servlet;
1067:
1068:                    page._caucho_unload();
1069:                    if (!page.isDead()) {
1070:                        page.setDead();
1071:                        page.destroy();
1072:                    }
1073:                }
1074:
1075:                _topOut.clearBuffer();
1076:
1077:                if (_errorPage != null) {
1078:                    getApplication().log(e.toString(), e);
1079:
1080:                    getCauchoRequest().setAttribute(EXCEPTION, e);
1081:                    getCauchoRequest().setAttribute(
1082:                            "javax.servlet.error.exception", e);
1083:                    getCauchoRequest().setAttribute(
1084:                            "javax.servlet.error.exception_type", e);
1085:                    getCauchoRequest().setAttribute(
1086:                            "javax.servlet.error.request_uri",
1087:                            getCauchoRequest().getRequestURI());
1088:
1089:                    try {
1090:                        RequestDispatcher rd = getCauchoRequest()
1091:                                .getRequestDispatcher(_errorPage);
1092:
1093:                        if (rd instanceof  RequestDispatcherImpl) {
1094:                            getCauchoResponse().setHasError(true);
1095:
1096:                            ((RequestDispatcherImpl) rd).error(
1097:                                    getCauchoRequest(), getCauchoResponse());
1098:                        } else {
1099:                            if (rd != null) {
1100:                                getCauchoResponse().killCache();
1101:                                getCauchoResponse().setNoCache(true);
1102:                                rd.forward(getCauchoRequest(),
1103:                                        getCauchoResponse());
1104:                            } else {
1105:                                log.log(Level.FINE, e.toString(), e);
1106:                                throw new ServletException(
1107:                                        L
1108:                                                .l(
1109:                                                        "`{0}' is an unknown error page.  The JSP errorPage directive must refer to a valid URL relative to the current web-app.",
1110:                                                        _errorPage));
1111:                            }
1112:                        }
1113:
1114:                    } catch (FileNotFoundException e2) {
1115:                        log.log(Level.WARNING, e.toString(), e2);
1116:                        throw new ServletException(
1117:                                L
1118:                                        .l(
1119:                                                "`{0}' is an unknown error page.  The JSP errorPage directive must refer to a valid URL relative to the current web-app.",
1120:                                                _errorPage));
1121:                    } catch (IOException e2) {
1122:                        log.log(Level.FINE, e.toString(), e2);
1123:                    }
1124:
1125:                    return;
1126:                }
1127:
1128:                /*
1129:                if (_servlet instanceof Page && ! (e instanceof LineMapException)) {
1130:                  LineMap lineMap = ((Page) _servlet)._caucho_getLineMap();
1131:                  
1132:                  if (lineMap != null)
1133:                    e = new JspLineException(e, lineMap);        
1134:                }
1135:                 */
1136:
1137:                if (e instanceof  ServletException) {
1138:                    throw (ServletException) e;
1139:                } else if (e instanceof  IOException) {
1140:                    throw (IOException) e;
1141:                } else if (e instanceof  RuntimeException) {
1142:                    throw (RuntimeException) e;
1143:                } else if (e instanceof  Error) {
1144:                    throw (Error) e;
1145:                } else {
1146:                    throw new ServletException(e);
1147:                }
1148:            }
1149:
1150:            /**
1151:             * Returns the error data
1152:             */
1153:            public ErrorData getErrorData() {
1154:                String uri = (String) getCauchoRequest().getAttribute(
1155:                        AbstractHttpRequest.ERROR_URI);
1156:
1157:                if (uri == null)
1158:                    return null;
1159:
1160:                Integer status = (Integer) getCauchoRequest().getAttribute(
1161:                        AbstractHttpRequest.STATUS_CODE);
1162:
1163:                return new ErrorData(getThrowable(), status == null ? 0
1164:                        : status.intValue(), (String) getCauchoRequest()
1165:                        .getAttribute(AbstractHttpRequest.ERROR_URI),
1166:                        (String) getCauchoRequest().getAttribute(
1167:                                AbstractHttpRequest.SERVLET_NAME));
1168:            }
1169:
1170:            /**
1171:             * Returns the variable resolver
1172:             */
1173:            public javax.servlet.jsp.el.VariableResolver getVariableResolver() {
1174:                return this ;
1175:            }
1176:
1177:            /**
1178:             * Returns the expression evaluator
1179:             */
1180:            public ExpressionEvaluator getExpressionEvaluator() {
1181:                if (_expressionEvaluator == null)
1182:                    _expressionEvaluator = new ExpressionEvaluatorImpl(
1183:                            getELContext());
1184:
1185:                return _expressionEvaluator;
1186:            }
1187:
1188:            /**
1189:             * Returns the expression evaluator
1190:             */
1191:            public ELContext getELContext() {
1192:                if (_elContext == null) {
1193:                    _elContext = new PageELContext();
1194:
1195:                    WebApp webApp = getApplication();
1196:
1197:                    JspApplicationContextImpl jspContext = (JspApplicationContextImpl) webApp
1198:                            .getJspApplicationContext();
1199:
1200:                    ELResolver[] resolverArray = jspContext
1201:                            .getELResolverArray();
1202:
1203:                    _elResolver = new PageContextELResolver(this , resolverArray);
1204:
1205:                    ELContextListener[] listenerArray = jspContext
1206:                            .getELListenerArray();
1207:
1208:                    if (listenerArray.length > 0) {
1209:                        ELContextEvent event = new ELContextEvent(_elContext);
1210:
1211:                        for (int i = 0; i < listenerArray.length; i++) {
1212:                            listenerArray[i].contextCreated(event);
1213:                        }
1214:                    }
1215:
1216:                    _functionMapper = new PageFunctionMapper();
1217:                    _variableMapper = new PageVariableMapper();
1218:                }
1219:
1220:                return _elContext;
1221:            }
1222:
1223:            /**
1224:             * Given a relative url, return the absolute url.
1225:             *
1226:             * @param value the relative url
1227:             *
1228:             * @return the absolute url.
1229:             */
1230:            private String getRelativeUrl(String value) {
1231:                if (value.length() > 0 && value.charAt(0) == '/')
1232:                    return value;
1233:
1234:                ServletContext context = getServletContext();
1235:                String contextPath = RequestAdapter
1236:                        .getPageContextPath(getCauchoRequest());
1237:                String uri = RequestAdapter.getPageURI(getCauchoRequest());
1238:                String relPath = uri.substring(contextPath.length());
1239:
1240:                int p = relPath.lastIndexOf('/');
1241:                String urlPwd = p <= 0 ? "/" : relPath.substring(0, p + 1);
1242:
1243:                return urlPwd + value;
1244:            }
1245:
1246:            /**
1247:             * Releases the context.
1248:             */
1249:            public void release() {
1250:                try {
1251:                    _servlet = null;
1252:
1253:                    if (_attributes.size() > 0)
1254:                        _attributes.clear();
1255:
1256:                    /* XXX:
1257:                    if (! autoFlush && response instanceof Response)
1258:                      ((Response) response).setDisableAutoFlush(false);
1259:                     */
1260:
1261:                    getCauchoResponse().setResponseStream(_responseStream);
1262:                    getCauchoResponse().setFlushBuffer(null);
1263:
1264:                    _request = null;
1265:                    _webApp = null;
1266:                    _session = null;
1267:                    while (_out instanceof  AbstractJspWriter) {
1268:                        if (_out instanceof  AbstractJspWriter)
1269:                            _out = ((AbstractJspWriter) _out).popWriter();
1270:                    }
1271:
1272:                    JspWriter out = _out;
1273:                    _out = null;
1274:                    _topOut = null;
1275:                    _nodeEnv = null;
1276:                    _jspOutputStream.release();
1277:                    AbstractResponseStream responseStream = _responseStream;
1278:                    _responseStream = null;
1279:
1280:                    if (_responseAdapter != null) {
1281:                        // jsp/15l3
1282:                        _responseAdapter.finish();
1283:                        //_responseAdapter.close();
1284:                        ToCharResponseAdapter resAdapt = _responseAdapter;
1285:                        ToCharResponseAdapter.free(resAdapt);
1286:                    }
1287:
1288:                    /*
1289:                    // server/137q
1290:                    if (! _hasException && responseStream != null)
1291:                    	responseStream.close();
1292:                     */
1293:
1294:                    _response = null;
1295:                } catch (IOException e) {
1296:                    _out = null;
1297:                }
1298:            }
1299:
1300:            /**
1301:             * Returns the localized message appropriate for the current context.
1302:             */
1303:            public String getLocalizedMessage(String key, Object[] args,
1304:                    String basename) {
1305:                Object lc = basename;
1306:
1307:                if (lc == null)
1308:                    lc = getAttribute("caucho.bundle");
1309:
1310:                if (lc == null)
1311:                    lc = Config.find(this , Config.FMT_LOCALIZATION_CONTEXT);
1312:
1313:                return getLocalizedMessage(lc, key, args, basename);
1314:            }
1315:
1316:            /**
1317:             * Returns the localized message appropriate for the current context.
1318:             */
1319:            public String getLocalizedMessage(Object lc, String key,
1320:                    Object[] args, String basename) {
1321:                String bundleString = null;
1322:
1323:                // jsp/1c51, jsp/1c54
1324:                String prefix = (String) getAttribute("caucho.bundle.prefix");
1325:
1326:                // jsp/1c3x
1327:                if (key == null)
1328:                    key = "";
1329:
1330:                if (prefix != null)
1331:                    key = prefix + key;
1332:
1333:                if (lc == null) {
1334:                    lc = basename;
1335:
1336:                    if (lc == null || "".equals(lc))
1337:                        lc = getAttribute("caucho.bundle");
1338:                    if (lc == null)
1339:                        lc = Config.find(this , Config.FMT_LOCALIZATION_CONTEXT);
1340:                }
1341:
1342:                Locale locale = null;
1343:
1344:                if (lc instanceof  LocalizationContext) {
1345:                    ResourceBundle bundle = ((LocalizationContext) lc)
1346:                            .getResourceBundle();
1347:                    locale = ((LocalizationContext) lc).getLocale();
1348:
1349:                    try {
1350:                        if (bundle != null)
1351:                            bundleString = bundle.getString(key);
1352:                    } catch (Exception e) {
1353:                    }
1354:                } else if (lc instanceof  String) {
1355:                    LocalizationContext loc = getBundle((String) lc);
1356:                    locale = loc.getLocale();
1357:
1358:                    ResourceBundle bundle = loc.getResourceBundle();
1359:
1360:                    try {
1361:                        if (bundle != null)
1362:                            bundleString = bundle.getString(key);
1363:                    } catch (Exception e) {
1364:                    }
1365:                }
1366:
1367:                if (bundleString == null)
1368:                    return "???" + key + "???";
1369:                else if (args == null || args.length == 0)
1370:                    return bundleString;
1371:
1372:                if (locale == null)
1373:                    locale = getLocale();
1374:
1375:                if (locale != null)
1376:                    return new MessageFormat(bundleString, locale).format(args);
1377:                else
1378:                    return new MessageFormat(bundleString).format(args);
1379:            }
1380:
1381:            /**
1382:             * Returns the localized message appropriate for the current context.
1383:             */
1384:            public LocalizationContext getBundle(String name) {
1385:                Object localeObj = Config.find(this , Config.FMT_LOCALE);
1386:                LocalizationContext bundle = null;
1387:                BundleManager manager = getBundleManager();
1388:
1389:                if (localeObj instanceof  Locale) {
1390:                    Locale locale = (Locale) localeObj;
1391:
1392:                    bundle = manager.getBundle(name, locale);
1393:                } else if (localeObj instanceof  String) {
1394:                    Locale locale = getLocale((String) localeObj, null);
1395:
1396:                    bundle = manager.getBundle(name, locale);
1397:                } else {
1398:                    String acceptLanguage = getCauchoRequest().getHeader(
1399:                            "Accept-Language");
1400:
1401:                    if (acceptLanguage != null) {
1402:                        String cacheKey = name + acceptLanguage;
1403:                        bundle = manager.getBundle(name, cacheKey,
1404:                                getCauchoRequest().getLocales());
1405:                    }
1406:                }
1407:
1408:                if (bundle != null)
1409:                    return bundle;
1410:
1411:                Object fallback = Config.find(this , Config.FMT_FALLBACK_LOCALE);
1412:
1413:                if (fallback instanceof  Locale) {
1414:                    Locale locale = (Locale) fallback;
1415:
1416:                    bundle = manager.getBundle(name, locale);
1417:                } else if (fallback instanceof  String) {
1418:                    String localeName = (String) fallback;
1419:                    Locale locale = getLocale(localeName, null);
1420:
1421:                    bundle = manager.getBundle(name, locale);
1422:                }
1423:
1424:                if (bundle != null)
1425:                    return bundle;
1426:
1427:                bundle = manager.getBundle(name);
1428:
1429:                if (bundle != null)
1430:                    return bundle;
1431:                else {
1432:                    return BundleManager.NULL_BUNDLE;
1433:                }
1434:            }
1435:
1436:            /**
1437:             * Returns the currently active locale.
1438:             */
1439:            public Locale getLocale() {
1440:                if (_locale != null)
1441:                    return _locale;
1442:
1443:                _locale = getLocaleImpl();
1444:
1445:                if (_locale != null)
1446:                    getResponse().setLocale(_locale);
1447:
1448:                return _locale;
1449:            }
1450:
1451:            /**
1452:             * Returns the currently active locale.
1453:             */
1454:            private Locale getLocaleImpl() {
1455:                Object localeObj = Config.find(this ,
1456:                        Config.FMT_LOCALIZATION_CONTEXT);
1457:
1458:                LocalizationContext lc;
1459:                Locale locale = null;
1460:
1461:                if (localeObj instanceof  LocalizationContext) {
1462:                    lc = (LocalizationContext) localeObj;
1463:
1464:                    locale = lc.getLocale();
1465:
1466:                    if (locale != null)
1467:                        return locale;
1468:                }
1469:
1470:                localeObj = Config.find(this , Config.FMT_LOCALE);
1471:
1472:                if (localeObj instanceof  Locale)
1473:                    return (Locale) localeObj;
1474:                else if (localeObj instanceof  String)
1475:                    return getLocale((String) localeObj, null);
1476:
1477:                lc = (LocalizationContext) getAttribute("caucho.bundle");
1478:
1479:                if (lc != null)
1480:                    locale = lc.getLocale();
1481:
1482:                if (locale != null)
1483:                    return locale;
1484:
1485:                String acceptLanguage = getCauchoRequest().getHeader(
1486:                        "Accept-Language");
1487:
1488:                if (acceptLanguage != null) {
1489:                    Enumeration e = getCauchoRequest().getLocales();
1490:
1491:                    if (e != null && e.hasMoreElements())
1492:                        locale = (Locale) e.nextElement();
1493:                }
1494:
1495:                localeObj = Config.find(this , Config.FMT_FALLBACK_LOCALE);
1496:
1497:                if (localeObj instanceof  Locale)
1498:                    return (Locale) localeObj;
1499:                else if (localeObj instanceof  String)
1500:                    return getLocale((String) localeObj, null);
1501:                else
1502:                    return null;
1503:            }
1504:
1505:            public static Locale getLocale(String value, String variant) {
1506:                Locale locale = null;
1507:                int len = value.length();
1508:
1509:                CharBuffer cb = new CharBuffer();
1510:                int i = 0;
1511:                char ch = 0;
1512:                for (; i < len && (ch = value.charAt(i)) != '_' && ch != '-'; i++)
1513:                    cb.append(ch);
1514:
1515:                String language = cb.toString();
1516:
1517:                if (ch == '_' || ch == '-') {
1518:                    cb.clear();
1519:
1520:                    for (i++; i < len && (ch = value.charAt(i)) != '_'
1521:                            && ch != '-'; i++)
1522:                        cb.append(ch);
1523:
1524:                    String country = cb.toString();
1525:
1526:                    if (variant != null && !variant.equals(""))
1527:                        return new Locale(language, country, variant);
1528:                    else
1529:                        return new Locale(language, country);
1530:                } else if (variant != null && !variant.equals(""))
1531:                    return new Locale(language, "", variant);
1532:                else
1533:                    return new Locale(language, "");
1534:            }
1535:
1536:            public static void printBody(BodyContentImpl body, boolean isEscaped)
1537:                    throws IOException {
1538:                JspWriter out = body.getEnclosingWriter();
1539:                CharBuffer string = body.getCharBuffer();
1540:                char[] cBuf = string.getBuffer();
1541:
1542:                int length = string.length() - 1;
1543:
1544:                for (; length >= 0; length--) {
1545:                    char ch = cBuf[length];
1546:                    if (ch != ' ' && ch != '\n' && ch != '\t' && ch != '\r')
1547:                        break;
1548:                }
1549:                length++;
1550:
1551:                int i;
1552:
1553:                for (i = 0; i < length; i++) {
1554:                    char ch = cBuf[i];
1555:                    if (ch != ' ' && ch != '\n' && ch != '\t' && ch != '\r')
1556:                        break;
1557:                }
1558:
1559:                if (!isEscaped) {
1560:                    out.write(cBuf, i, length - i);
1561:                    return;
1562:                }
1563:
1564:                for (; i < length; i++) {
1565:                    char ch = cBuf[i];
1566:
1567:                    switch (ch) {
1568:                    case '<':
1569:                        out.write("&lt;");
1570:                        break;
1571:                    case '>':
1572:                        out.write("&gt;");
1573:                        break;
1574:                    case '&':
1575:                        out.write("&amp;");
1576:                        break;
1577:                    case '\'':
1578:                        out.write("&#039;");
1579:                        break;
1580:                    case '"':
1581:                        out.write("&#034;");
1582:                        break;
1583:                    default:
1584:                        out.write((char) ch);
1585:                        break;
1586:                    }
1587:                }
1588:            }
1589:
1590:            /**
1591:             * Evaluates the fragment, returing the string value.
1592:             */
1593:            public String invoke(JspFragment fragment) throws JspException,
1594:                    IOException {
1595:                if (fragment == null)
1596:                    return "";
1597:
1598:                BodyContentImpl body = (BodyContentImpl) pushBody();
1599:
1600:                try {
1601:                    fragment.invoke(body);
1602:                    return body.getString();
1603:                } finally {
1604:                    popBody();
1605:                    body.release();
1606:                }
1607:            }
1608:
1609:            /**
1610:             * Evaluates the fragment, returing the string value.
1611:             */
1612:            public String invokeTrim(JspFragment fragment) throws JspException,
1613:                    IOException {
1614:                if (fragment == null)
1615:                    return "";
1616:
1617:                BodyContentImpl body = (BodyContentImpl) pushBody();
1618:
1619:                try {
1620:                    fragment.invoke(body);
1621:                    return body.getTrimString();
1622:                } finally {
1623:                    popBody();
1624:                    body.release();
1625:                }
1626:            }
1627:
1628:            /**
1629:             * Evaluates the fragment, returing a reader
1630:             */
1631:            public Reader invokeReader(JspFragment fragment)
1632:                    throws JspException, IOException {
1633:                if (fragment == null)
1634:                    return null;
1635:
1636:                BodyContentImpl body = (BodyContentImpl) pushBody();
1637:
1638:                try {
1639:                    fragment.invoke(body);
1640:
1641:                    return body.getReader();
1642:                } finally {
1643:                    popBody();
1644:                    body.release();
1645:                }
1646:            }
1647:
1648:            /**
1649:             * Parses a boolean value.
1650:             */
1651:            static public boolean toBoolean(String value) {
1652:                if (value == null)
1653:                    return false;
1654:                else if (value.equalsIgnoreCase("true"))
1655:                    return true;
1656:                else if (value.equalsIgnoreCase("false"))
1657:                    return false;
1658:                else
1659:                    return value.trim().equalsIgnoreCase("true");
1660:            }
1661:
1662:            /**
1663:             * Set/Remove a page attribute.
1664:             */
1665:            public void defaultSetOrRemove(String var, Object value) {
1666:                if (value != null)
1667:                    putAttribute(var, value);
1668:                else
1669:                    removeAttribute(var);
1670:            }
1671:
1672:            /**
1673:             * Set/Remove a page attribute.
1674:             */
1675:            public void pageSetOrRemove(String var, Object value) {
1676:                if (value != null)
1677:                    _attributes.put(var, value);
1678:                else
1679:                    _attributes.remove(var);
1680:            }
1681:
1682:            /**
1683:             * Set/Remove a request attribute.
1684:             */
1685:            public void requestSetOrRemove(String var, Object value) {
1686:                if (value != null)
1687:                    getCauchoRequest().setAttribute(var, value);
1688:                else
1689:                    getCauchoRequest().removeAttribute(var);
1690:            }
1691:
1692:            /**
1693:             * Set/Remove a session attribute.
1694:             */
1695:            public void sessionSetOrRemove(String var, Object value) {
1696:                if (value != null)
1697:                    getSession().setAttribute(var, value);
1698:                else
1699:                    getSession().removeAttribute(var);
1700:            }
1701:
1702:            /**
1703:             * Set/Remove an webApp attribute.
1704:             */
1705:            public void applicationSetOrRemove(String var, Object value) {
1706:                if (value != null)
1707:                    getApplication().setAttribute(var, value);
1708:                else
1709:                    getApplication().removeAttribute(var);
1710:            }
1711:
1712:            /**
1713:             * Returns true if the EL ignores exceptions
1714:             */
1715:            public boolean isIgnoreException() {
1716:                JspPropertyGroup jsp = getApplication().getJsp();
1717:
1718:                return (jsp == null || jsp.isIgnoreELException());
1719:            }
1720:
1721:            /**
1722:             * Returns the XPath variable environment corresponding to this page
1723:             */
1724:            public VarEnv getVarEnv() {
1725:                if (_varEnv == null)
1726:                    _varEnv = new PageVarEnv();
1727:
1728:                return _varEnv;
1729:            }
1730:
1731:            /**
1732:             * Returns the XPath node environment corresponding to this page
1733:             */
1734:            public Node getNodeEnv() {
1735:                return _nodeEnv;
1736:            }
1737:
1738:            /**
1739:             * Returns the XPath node environment corresponding to this page
1740:             */
1741:            public void setNodeEnv(Node node) {
1742:                _nodeEnv = node;
1743:            }
1744:
1745:            /**
1746:             * Creates an expression.
1747:             */
1748:            public ValueExpression createExpr(ValueExpression expr,
1749:                    String exprString, Class type) {
1750:                if (_variableMapper == null || _variableMapper.isConstant())
1751:                    return expr;
1752:
1753:                return JspUtil.createValueExpression(getELContext(), type,
1754:                        exprString);
1755:            }
1756:
1757:            /**
1758:             * Finds an attribute in any of the scopes from page to webApp.
1759:             *
1760:             * @param name the attribute name.
1761:             *
1762:             * @return the attribute value
1763:             */
1764:            public Object resolveVariable(String name)
1765:                    throws javax.el.ELException {
1766:                Object value = findAttribute(name);
1767:
1768:                if (value != null)
1769:                    return value;
1770:
1771:                if (_elContext == null)
1772:                    _elContext = EL.getEnvironment();
1773:
1774:                return _elContext.getELResolver().getValue(_elContext, name,
1775:                        null);
1776:            }
1777:
1778:            /**
1779:             * Returns the bundle manager.
1780:             */
1781:            private BundleManager getBundleManager() {
1782:                if (_bundleManager == null)
1783:                    _bundleManager = BundleManager.create();
1784:
1785:                return _bundleManager;
1786:            }
1787:
1788:            /**
1789:             * Represents the XPath environment for this page.
1790:             */
1791:            public class PageVarEnv extends VarEnv {
1792:                /**
1793:                 * Returns the value corresponding to the name.
1794:                 */
1795:                public Object getValue(String name) {
1796:                    Object value = findAttribute(name);
1797:
1798:                    if (value != null)
1799:                        return value;
1800:
1801:                    int p = name.indexOf(':');
1802:                    if (p < 0) {
1803:                        // jsp/1m43, TCK
1804:                        throw new javax.el.ELException(L.l(
1805:                                "'{0}' is an unknown variable", name));
1806:                    }
1807:
1808:                    String prefix = name.substring(0, p);
1809:                    String suffix = name.substring(p + 1);
1810:
1811:                    if (prefix.equals("param"))
1812:                        return getCauchoRequest().getParameter(suffix);
1813:                    else if (prefix.equals("header"))
1814:                        return ((HttpServletRequest) getCauchoRequest())
1815:                                .getHeader(suffix);
1816:                    else if (prefix.equals("cookie")) {
1817:                        Cookie cookie;
1818:                        HttpServletRequest request = (HttpServletRequest) getCauchoRequest();
1819:
1820:                        if (request instanceof  CauchoRequest)
1821:                            cookie = ((CauchoRequest) request)
1822:                                    .getCookie(suffix);
1823:                        else
1824:                            cookie = null;
1825:
1826:                        if (cookie != null)
1827:                            return cookie.getValue();
1828:                        else
1829:                            return null;
1830:                    } else if (prefix.equals("initParam"))
1831:                        return getApplication().getInitParameter(suffix);
1832:                    else if (prefix.equals("pageScope"))
1833:                        return getAttribute(suffix);
1834:                    else if (prefix.equals("requestScope"))
1835:                        return getCauchoRequest().getAttribute(suffix);
1836:                    else if (prefix.equals("sessionScope"))
1837:                        return getSession().getAttribute(suffix);
1838:                    else if (prefix.equals("applicationScope"))
1839:                        return getApplication().getAttribute(suffix);
1840:                    else
1841:                        return null;
1842:
1843:                }
1844:            }
1845:
1846:            static class StringArrayEnum implements  Enumeration {
1847:                private int _index;
1848:                private String[] _values;
1849:
1850:                StringArrayEnum(String[] values) {
1851:                    _values = values;
1852:                }
1853:
1854:                public boolean hasMoreElements() {
1855:                    return _index < _values.length;
1856:                }
1857:
1858:                public Object nextElement() {
1859:                    return _values[_index++];
1860:                }
1861:            }
1862:
1863:            public class PageELContext extends ServletELContext {
1864:                public PageELContext() {
1865:                    putContext(JspContext.class, PageContextImpl.this );
1866:                }
1867:
1868:                public PageContextImpl getPageContext() {
1869:                    return PageContextImpl.this ;
1870:                }
1871:
1872:                @Override
1873:                public ServletContext getApplication() {
1874:                    return PageContextImpl.this .getApplication();
1875:                }
1876:
1877:                @Override
1878:                public Object getApplicationScope() {
1879:                    return new PageContextAttributeMap(PageContextImpl.this ,
1880:                            APPLICATION_SCOPE);
1881:                }
1882:
1883:                @Override
1884:                public HttpServletRequest getRequest() {
1885:                    return (HttpServletRequest) PageContextImpl.this 
1886:                            .getRequest();
1887:                }
1888:
1889:                @Override
1890:                public Object getRequestScope() {
1891:                    return new PageContextAttributeMap(PageContextImpl.this ,
1892:                            REQUEST_SCOPE);
1893:                }
1894:
1895:                @Override
1896:                public Object getSessionScope() {
1897:                    return new PageContextAttributeMap(PageContextImpl.this ,
1898:                            SESSION_SCOPE);
1899:                }
1900:
1901:                public ELResolver getELResolver() {
1902:                    return _elResolver;
1903:                }
1904:
1905:                public javax.el.FunctionMapper getFunctionMapper() {
1906:                    return _functionMapper;
1907:                }
1908:
1909:                public javax.el.VariableMapper getVariableMapper() {
1910:                    return _variableMapper;
1911:                }
1912:            }
1913:
1914:            public class PageFunctionMapper extends javax.el.FunctionMapper {
1915:                public Method resolveFunction(String prefix, String localName) {
1916:                    if (_functionMap != null)
1917:                        return _functionMap.get(prefix + ":" + localName);
1918:                    else
1919:                        return null;
1920:                }
1921:            }
1922:
1923:            public class PageVariableMapper extends ImplicitVariableMapper {
1924:                private HashMap<String, ValueExpression> _map;
1925:
1926:                final boolean isConstant() {
1927:                    return _map == null || _map.size() == 0;
1928:                }
1929:
1930:                public ValueExpression resolveVariable(String var) {
1931:                    if (_map != null) {
1932:                        ValueExpression expr = _map.get(var);
1933:
1934:                        if (expr != null)
1935:                            return expr;
1936:                    }
1937:
1938:                    return super .resolveVariable(var);
1939:                }
1940:
1941:                public ValueExpression setVariable(String var,
1942:                        ValueExpression expr) {
1943:                    // ValueExpression oldValue = getVariable(var);
1944:
1945:                    if (_map == null) {
1946:                        _map = new HashMap<String, ValueExpression>();
1947:                    }
1948:
1949:                    _map.put(var, expr);
1950:
1951:                    return expr;
1952:                }
1953:            }
1954:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.