1 13 package com.tonbeller.wcf.statusline; 14 15 import java.io.PrintWriter ; 16 import java.io.StringWriter ; 17 18 import javax.servlet.http.HttpSession ; 19 20 import com.tonbeller.tbutils.res.Resources; 21 import com.tonbeller.wcf.controller.RequestContext; 22 import com.tonbeller.wcf.utils.JDK13Utils; 23 import com.tonbeller.wcf.utils.XmlUtils; 24 25 29 public class StatusLine { 30 31 public static final String WEBKEY = "wcfStatusLine"; 32 private String message; 33 private Exception exception; 34 private String exceptionType; 35 36 private Resources resources; 37 38 private StatusLine() { 39 resources = Resources.instance(StatusLine.class); 40 } 41 42 public static synchronized StatusLine instance() { 43 return instance(RequestContext.instance().getSession()); 44 } 45 46 public static synchronized StatusLine instance(HttpSession session) { 47 StatusLine sl = (StatusLine) session.getAttribute(WEBKEY); 48 if (sl == null) { 49 sl = new StatusLine(); 50 session.setAttribute(WEBKEY, sl); 51 } 52 return sl; 53 } 54 55 public void setMessage(String message) { 56 this.message = message; 57 this.exception = null; 58 } 59 60 public void setError(String exceptionType, Exception exception) { 61 this.exceptionType = exceptionType; 62 this.exception = exception; 63 } 64 65 public String getStatusHTML() { 66 if (exception != null) 67 return getErrorHTML(); 68 if (message != null) 69 return getMessageHTML(); 70 return ""; 71 } 72 73 public String getStatusText() { 74 if (exception != null) 75 return getErrorText(); 76 if (message != null) 77 return getMessageText(); 78 return ""; 79 } 80 81 public boolean isEmpty() { 82 return exception == null && message == null; 83 } 84 85 public void clear() { 86 exception = null; 87 message = null; 88 } 89 90 private String getMessageHTML() { 91 String s = XmlUtils.escapeXml(message); 92 s = resources.getString("statusLine.message.format.html", s); 93 return s; 94 } 95 96 private String getMessageText() { 97 return resources.getString("statusLine.message.format.text", message); 98 } 99 100 private String getErrorHTML() { 101 return getErrorFmt("statusLine.error.format.html"); 102 } 103 104 private String getErrorText() { 105 return getErrorFmt("statusLine.error.format.text"); 106 } 107 108 private String getErrorFmt(String resKey) { 109 Object [] args = new Object [3]; 110 Throwable t = unwind(exception); 111 if (exceptionType != null) 112 args[0] = exceptionType; 113 else 114 args[0] = t.getClass().getName(); 115 args[1] = XmlUtils.escapeXml(t.getMessage()); 116 args[2] = getStackTrace(t); 117 return resources.getString(resKey, args); 118 } 119 120 private String getStackTrace(Throwable t) { 121 StringWriter sw = new StringWriter (); 122 PrintWriter pw = new PrintWriter (sw); 123 t.printStackTrace(pw); 124 pw.close(); 125 return sw.toString(); 126 } 127 128 private Throwable unwind(Throwable t) { 129 Throwable x = JDK13Utils.getCause(t); 130 while (x != null) { 131 t = x; 132 x = JDK13Utils.getCause(t); 133 } 134 return t; 135 } 136 137 } | Popular Tags |