Source Code Cross Referenced for ServletWorker.java in  » Science » Cougaar12_4 » org » cougaar » planning » servlet » 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 » Science » Cougaar12_4 » org.cougaar.planning.servlet 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * <copyright>
003:         *  
004:         *  Copyright 1997-2004 BBNT Solutions, LLC
005:         *  under sponsorship of the Defense Advanced Research Projects
006:         *  Agency (DARPA).
007:         * 
008:         *  You can redistribute this software and/or modify it under the
009:         *  terms of the Cougaar Open Source License as published on the
010:         *  Cougaar Open Source Website (www.cougaar.org).
011:         * 
012:         *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
013:         *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
014:         *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
015:         *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
016:         *  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
017:         *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
018:         *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
019:         *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
020:         *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
021:         *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
022:         *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
023:         *  
024:         * </copyright>
025:         */
026:
027:        package org.cougaar.planning.servlet;
028:
029:        import java.io.FilterOutputStream;
030:        import java.io.IOException;
031:        import java.io.ObjectOutputStream;
032:        import java.io.OutputStream;
033:        import java.io.OutputStreamWriter;
034:        import java.io.PrintWriter;
035:
036:        import javax.servlet.ServletException;
037:        import javax.servlet.http.HttpServletRequest;
038:        import javax.servlet.http.HttpServletResponse;
039:
040:        import org.cougaar.core.servlet.ServletUtil;
041:        import org.cougaar.core.servlet.SimpleServletSupport;
042:        import org.cougaar.planning.servlet.data.xml.XMLWriter;
043:        import org.cougaar.planning.servlet.data.xml.XMLable;
044:
045:        /**
046:         * <pre>
047:         * ServletWorker is the base class for all servlet workers.
048:         *
049:         * ServletWorkers assume that the result can be returned in any of
050:         * three formats : html, xml, or serialized java object.  These
051:         * formats are specified by the URL format parameter, i.e.
052:         *   http://localhost:8800/$AGENT_NAME/SERVLET_NAME?format=html
053:         *
054:         * The format parameter is set in the getSettings method.  This should
055:         * be called from a subclass if overridden.
056:         *
057:         * Defines the writeResponse method, which returns the type of result
058:         * indicated by the format parameter to the output stream of the servlet 
059:         * response.
060:         *
061:         * Note that if you make a mistake and don't provide a value for a 
062:         * parameter, e.g.
063:         *   http://localhost:8800/$AGENT_NAME/SERVLET_NAME?format
064:         *                                                        ^^^^ - no value
065:         * the ServletUtil object will throw an exception.
066:         *
067:         * </pre>
068:         */
069:        public class ServletWorker {
070:            public static final boolean DEBUG = false;
071:
072:            public static boolean VERBOSE = false;
073:            public static final int FORMAT_DATA = 0;
074:            public static final int FORMAT_XML = 1;
075:            public static final int FORMAT_HTML = 2;
076:
077:            static {
078:                VERBOSE = Boolean
079:                        .getBoolean("org.cougaar.mlm.ui.psp.transit.ServletWorker.verbose");
080:            }
081:
082:            protected int format;
083:
084:            /**
085:             * Main method. <p>
086:             * Most of the work is done in getHierarchy.
087:             * This method mainly checks that the parameters are the right 
088:             * number and sets the format and recurse fields.
089:             * <p>
090:             * Uses the ServletUtil to parse the parameters.
091:             * @see org.cougaar.core.servlet.ServletUtil#parseParams
092:             */
093:            public void execute(HttpServletRequest request,
094:                    HttpServletResponse response, SimpleServletSupport support)
095:                    throws IOException, ServletException {
096:                ServletUtil.ParamVisitor vis = new ServletUtil.ParamVisitor() {
097:                    public void setParam(String name, String value) {
098:                        getSettings(name, value);
099:                    }
100:                };
101:
102:                // visit the URL parameters
103:                ServletUtil.parseParams(vis, request);
104:
105:                // generate our response.
106:            }
107:
108:            /** 
109:             * <pre>
110:             * sets format 
111:             *
112:             * format  is either data, xml, or html
113:             *
114:             * see class description for what these values mean
115:             * </pre>
116:             */
117:            protected void getSettings(String name, String value) {
118:                if (eq("format", name)) {
119:                    if (eq("data", value)) {
120:                        format = FORMAT_DATA;
121:                    } else if (eq("xml", value)) {
122:                        format = FORMAT_XML;
123:                    } else if (eq("html", value)) {
124:                        format = FORMAT_HTML;
125:                    }
126:                    // stay backwards-compatable
127:                } else if (eq("data", name)) {
128:                    format = FORMAT_DATA;
129:                } else if (eq("xml", name)) {
130:                    format = FORMAT_XML;
131:                } else if (eq("html", name)) {
132:                    format = FORMAT_HTML;
133:                }
134:            }
135:
136:            protected boolean isHtmlFormat() {
137:                return (format == FORMAT_HTML);
138:            }
139:
140:            protected boolean isXmlFormat() {
141:                return (format == FORMAT_XML);
142:            }
143:
144:            protected boolean isDataFormat() {
145:                return (format == FORMAT_DATA);
146:            }
147:
148:            protected String getPrefix() {
149:                return "";
150:            }
151:
152:            /**
153:             * Write XMLable result to output. <p>
154:             *
155:             * Output format is either data, xml, or html <p>
156:             *
157:             * For an example of how this is used, see the following references.
158:             * @see org.cougaar.planning.servlet.HierarchyWorker#getHierarchy
159:             * @see org.cougaar.planning.servlet.HierarchyWorker#writeResponse
160:             */
161:            protected void writeResponse(XMLable result, OutputStream out,
162:                    HttpServletRequest request, SimpleServletSupport support,
163:                    int format) {
164:                // write data
165:                try {
166:                    if (format == FORMAT_DATA) {
167:                        // serialize
168:                        ObjectOutputStream oos = new ObjectOutputStream(out);
169:                        oos.writeObject(result);
170:                        oos.flush();
171:                    } else {
172:                        // xml or html-wrapped xml
173:                        XMLWriter w;
174:                        PrintWriter writer = new PrintWriter(out);
175:
176:                        if (format == FORMAT_HTML) {
177:                            // wrapped xml
178:                            writer.println("<HTML><HEAD><TITLE> " + getPrefix()
179:                                    + support.getAgentIdentifier()
180:                                    + "</TITLE></HEAD><BODY>\n"
181:                                    + "<H2><CENTER>" + getPrefix()
182:                                    + support.getAgentIdentifier()
183:                                    + "</CENTER></H2><p><pre>\n");
184:                            writer.flush();
185:                            w = new XMLWriter(new OutputStreamWriter(
186:                                    new XMLtoHTMLOutputStream(out)), true);
187:                        } else {
188:                            // raw xml
189:                            writer.println("<?xml version='1.0' ?>");
190:                            writer.flush();
191:                            w = new XMLWriter(new OutputStreamWriter(out));
192:                        }
193:                        // write as xml
194:                        result.toXML(w);
195:                        w.flush();
196:                        if (format == FORMAT_HTML) {
197:                            writer.println("\n</pre></BODY></HTML>\n");
198:                        }
199:                    }
200:                } catch (IOException e) {
201:                    e.printStackTrace();
202:                }
203:            }
204:
205:            /**
206:             * Convert XML to HTML-friendly output.
207:             *
208:             * Taken from PSP_PlanView.  For Internet Explorer this isn't such
209:             * a big deal...
210:             */
211:            protected static class XMLtoHTMLOutputStream extends
212:                    FilterOutputStream {
213:                protected static final byte[] LESS_THAN;
214:                protected static final byte[] GREATER_THAN;
215:                static {
216:                    LESS_THAN = "<font color=green>&lt;".getBytes();
217:                    GREATER_THAN = "&gt;</font>".getBytes();
218:                }
219:
220:                public XMLtoHTMLOutputStream(OutputStream o) {
221:                    super (o);
222:                }
223:
224:                public void write(int b) throws IOException {
225:                    if (b == '<') {
226:                        out.write(LESS_THAN);
227:                    } else if (b == '>') {
228:                        out.write(GREATER_THAN);
229:                    } else {
230:                        out.write(b);
231:                    }
232:                }
233:            }
234:
235:            public static final boolean eq(String a, String b) {
236:                return a.regionMatches(true, 0, b, 0, a.length());
237:            }
238:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.