001 package railo.runtime.tag; 002 003 import java.io.UnsupportedEncodingException; 004 005 import javax.servlet.http.HttpServletResponse; 006 007 import railo.runtime.exp.PageException; 008 import railo.runtime.exp.TemplateException; 009 import railo.runtime.ext.tag.TagImpl; 010 import railo.runtime.op.Caster; 011 012 /** 013 * Generates custom HTTP response headers to return to the client. 014 * 015 * 016 * 017 **/ 018 public final class Header extends TagImpl { 019 020 /** A value for the HTTP header. This attribute is used in conjunction with the name attribute. */ 021 private String value=""; 022 023 /** Text that explains the status code. This attribute is used in conjunction with the 024 ** statusCode attribute. */ 025 private String statustext; 026 027 /** A name for the header. */ 028 private String name; 029 030 /** A number that sets the HTTP status code. */ 031 private int statuscode; 032 033 private boolean hasStatucCode; 034 035 private String charset; 036 037 /** 038 * @see javax.servlet.jsp.tagext.Tag#release() 039 */ 040 public void release() { 041 super.release(); 042 value=""; 043 statustext=null; 044 name=null; 045 statuscode=0; 046 hasStatucCode=false; 047 charset=null; 048 } 049 050 051 /** set the value value 052 * A value for the HTTP header. This attribute is used in conjunction with the name attribute. 053 * @param value value to set 054 **/ 055 public void setValue(String value) { 056 this.value=value; 057 } 058 059 /** set the value statustext 060 * Text that explains the status code. This attribute is used in conjunction with the 061 * statusCode attribute. 062 * @param statustext value to set 063 **/ 064 public void setStatustext(String statustext) { 065 this.statustext=statustext; 066 } 067 068 /** set the value name 069 * A name for the header. 070 * @param name value to set 071 **/ 072 public void setName(String name) { 073 this.name=name; 074 } 075 076 /** set the value statuscode 077 * A number that sets the HTTP status code. 078 * @param statuscode value to set 079 **/ 080 public void setStatuscode(double statuscode) { 081 this.statuscode=(int) statuscode; 082 hasStatucCode=true; 083 } 084 085 /** 086 * @param charset The charset to set. 087 */ 088 public void setCharset(String charset) { 089 this.charset = charset; 090 } 091 092 093 /** 094 * @see javax.servlet.jsp.tagext.Tag#doStartTag() 095 */ 096 public int doStartTag() throws PageException { 097 098 HttpServletResponse rsp = pageContext. getHttpServletResponse(); 099 if(rsp.isCommitted()) 100 throw new TemplateException("can't assign value to header, header is alredy committed"); 101 102 // set name value 103 if(name != null) { 104 if(charset==null && name.equalsIgnoreCase("content-disposition")) { 105 charset=pageContext.getConfig().getWebCharset(); 106 } 107 try { 108 if(charset!=null) { 109 name = new String(name.getBytes(charset), "ISO-8859-1"); 110 value = new String(value.getBytes(charset), "ISO-8859-1"); 111 } 112 else { 113 name = new String(name.getBytes(), "ISO-8859-1"); 114 value = new String(value.getBytes(), "ISO-8859-1"); 115 } 116 } 117 catch (UnsupportedEncodingException e) { 118 throw Caster.toPageException(e); 119 } 120 121 if(name.toLowerCase().equals("content-type") && value.length()>0) { 122 rsp.setContentType(value); 123 } 124 else { 125 rsp.addHeader(name, value); 126 } 127 } 128 // set status 129 if(hasStatucCode) { 130 if(statustext != null) { 131 //try { 132 ///rsp.sendError(statuscode, statustext); 133 rsp.setStatus(statuscode,statustext); 134 /*} 135 catch (IOException e) { 136 throw new TemplateException("can't assign value to header, header is alredy committed",e.getMessage()); 137 } */ 138 } 139 else { 140 rsp.setStatus(statuscode); 141 } 142 } 143 return SKIP_BODY; 144 } 145 146 /** 147 * @see javax.servlet.jsp.tagext.Tag#doEndTag() 148 */ 149 public int doEndTag() { 150 return EVAL_PAGE; 151 } 152 }