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 @Override 038 public void release() { 039 super.release(); 040 value=""; 041 statustext=null; 042 name=null; 043 statuscode=0; 044 hasStatucCode=false; 045 charset=null; 046 } 047 048 049 /** set the value value 050 * A value for the HTTP header. This attribute is used in conjunction with the name attribute. 051 * @param value value to set 052 **/ 053 public void setValue(String value) { 054 this.value=value; 055 } 056 057 /** set the value statustext 058 * Text that explains the status code. This attribute is used in conjunction with the 059 * statusCode attribute. 060 * @param statustext value to set 061 **/ 062 public void setStatustext(String statustext) { 063 this.statustext=statustext; 064 } 065 066 /** set the value name 067 * A name for the header. 068 * @param name value to set 069 **/ 070 public void setName(String name) { 071 this.name=name; 072 } 073 074 /** set the value statuscode 075 * A number that sets the HTTP status code. 076 * @param statuscode value to set 077 **/ 078 public void setStatuscode(double statuscode) { 079 this.statuscode=(int) statuscode; 080 hasStatucCode=true; 081 } 082 083 /** 084 * @param charset The charset to set. 085 */ 086 public void setCharset(String charset) { 087 this.charset = charset; 088 } 089 090 091 @Override 092 public int doStartTag() throws PageException { 093 094 HttpServletResponse rsp = pageContext. getHttpServletResponse(); 095 if(rsp.isCommitted()) 096 throw new TemplateException("can't assign value to header, header is alredy committed"); 097 098 // set name value 099 if(name != null) { 100 if(charset==null && name.equalsIgnoreCase("content-disposition")) { 101 charset=pageContext.getConfig().getWebCharset(); 102 } 103 try { 104 if(charset!=null) { 105 name = new String(name.getBytes(charset), "ISO-8859-1"); 106 value = new String(value.getBytes(charset), "ISO-8859-1"); 107 } 108 else { 109 name = new String(name.getBytes(), "ISO-8859-1"); 110 value = new String(value.getBytes(), "ISO-8859-1"); 111 } 112 } 113 catch (UnsupportedEncodingException e) { 114 throw Caster.toPageException(e); 115 } 116 117 if(name.toLowerCase().equals("content-type") && value.length()>0) { 118 rsp.setContentType(value); 119 } 120 else { 121 rsp.addHeader(name, value); 122 } 123 } 124 // set status 125 if(hasStatucCode) { 126 if(statustext != null) { 127 //try { 128 ///rsp.sendError(statuscode, statustext); 129 rsp.setStatus(statuscode,statustext); 130 /*} 131 catch (IOException e) { 132 throw new TemplateException("can't assign value to header, header is alredy committed",e.getMessage()); 133 } */ 134 } 135 else { 136 rsp.setStatus(statuscode); 137 } 138 } 139 return SKIP_BODY; 140 } 141 142 @Override 143 public int doEndTag() { 144 return EVAL_PAGE; 145 } 146 }