001: /* JFox, the OpenSource J2EE Application Server
002: *
003: * Copyright (C) 2002 huihoo.com
004: * Distributable under GNU LGPL license
005: * See the GNU Lesser General Public License for more details.
006: */
007:
008: package org.huihoo.jfox.jmx.adaptor.http;
009:
010: import java.util.HashMap;
011: import java.util.Map;
012: import java.util.Properties;
013: import java.util.Date;
014: import java.io.StringWriter;
015:
016: import org.apache.velocity.app.Velocity;
017: import org.apache.velocity.Template;
018: import org.apache.velocity.VelocityContext;
019:
020: /**
021: *
022: * @author <a href="mailto:young_yy@hotmail.com">Young Yang</a>
023: */
024:
025: public class HttpResponse {
026:
027: private HttpRequest request = null;
028: private String contentType = "text/html";
029: private Map headersMap = new HashMap();
030: private int status = HttpConstants.HTTP_REPLY_OK_ID;
031:
032: public HttpResponse(HttpRequest req) {
033: this .request = req;
034: String templatePath = request.getThread().getServer()
035: .getDocumentRoot();
036: Properties prop = new Properties();
037: // System.out.println(templatePath);
038: prop.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH,
039: templatePath);
040: prop.setProperty(Velocity.RUNTIME_LOG, templatePath
041: + "/velocity.log");
042: try {
043: Velocity.init(prop);
044: } catch (Exception e) {
045: e.printStackTrace();
046: }
047:
048: }
049:
050: public void setHeader(String name, String value) {
051: headersMap.put(name.trim().toUpperCase(), value.trim());
052: }
053:
054: public void addHeader(String name, String value) {
055: setHeader(name, value);
056: }
057:
058: public void setStatus(int status) {
059: this .status = status;
060: }
061:
062: /**
063: * output the web page
064: */
065: public String output() throws Exception {
066: String commandName = request.getCommand();
067: commandName = commandName.substring(0, 1).toUpperCase()
068: + commandName.substring(1); // first letter upper case
069: return output(commandName);
070: }
071:
072: String output(String action) throws Exception {
073: StringWriter out = new StringWriter();
074: out.write(generateHeader());
075:
076: try {
077: Command command = CommandLoader.loadCommand(action);
078: VelocityContext ctx = command.execute(request, this );
079: Template tmpl = Velocity
080: .getTemplate(getTemplateName(command
081: .getTemplateName()));
082: tmpl.merge(ctx, out);
083: out.flush();
084: String page = out.getBuffer().toString();
085: out.close();
086: return page;
087: } catch (HttpRedirectException e) {
088: String url = e.getMessage();
089: return redirect(url);
090: }
091:
092: }
093:
094: private String redirect(String url) throws Exception {
095: request.setURI(url);
096: return output();
097: }
098:
099: public void setContentType(String type) {
100: contentType = type;
101: }
102:
103: private String getTemplateName(String command) {
104: return command.toLowerCase() + ".tmpl";
105: }
106:
107: // in ie, the first three line will done by head
108: private String generateHeader() {
109: StringWriter out = new StringWriter();
110: out.write("HTTP/1.1 200 OK" + HttpConstants.CRLF);
111: out.write("Content-Type: text/html" + HttpConstants.CRLF);
112: out.write("Date: " + new Date() + HttpConstants.CRLF);
113: out.write(HttpConstants.CRLF);
114: return out.getBuffer().toString();
115: }
116:
117: /*
118: HTTP/1.1 200 OK
119: Date: Thu, 26 Sep 2002 13:34:08 GMT
120: Server: Microsoft-IIS/6.0
121: P3P: CP='ALL IND DSP COR ADM CONo CUR CUSo IVAo IVDo PSA PSD TAI TELo OUR SAMo C
122: NT COM INT NAV ONL PHY PRE PUR UNI'
123: Content-Length: 31136
124: Content-Type: text/html
125: Expires: Thu, 26 Sep 2002 13:34:08 GMT
126: Cache-control: private
127: */
128: }
|