001    package railo.runtime.tag;
002    
003    import java.util.Iterator;
004    import java.util.Map.Entry;
005    
006    import railo.commons.lang.StringUtil;
007    import railo.runtime.exp.ApplicationException;
008    import railo.runtime.exp.CatchBlock;
009    import railo.runtime.exp.CustomTypeException;
010    import railo.runtime.exp.ExpressionException;
011    import railo.runtime.exp.PageException;
012    import railo.runtime.exp.PageExceptionImpl;
013    import railo.runtime.ext.tag.TagImpl;
014    import railo.runtime.op.Caster;
015    import railo.runtime.type.Collection.Key;
016    import railo.runtime.type.ObjectWrap;
017    import railo.runtime.type.Struct;
018    import railo.runtime.type.util.KeyConstants;
019    
020    /**
021    * The cfthrow tag raises a developer-specified exception that can be caught with cfcatch tag 
022    *   having any of the following type specifications - cfcatch type = 'custom_type', cfcatch type = 'Application'
023    *   'cfcatch' type = 'Any'
024    *
025    *
026    *
027    **/
028    public final class Throw extends TagImpl {
029    
030            /** A custom error code that you supply. */
031            private String extendedinfo=null;
032    
033            private String type="application";
034    
035            private String detail="";
036    
037            /** A message that describes the exceptional event. */
038            private Object message;
039    
040            /** A custom error code that you supply. */
041            private String errorcode="";
042    
043            private Object object;
044    
045            @Override
046            public void release()   {
047                    super.release();
048                    extendedinfo=null;
049                    type="application";
050                    detail="";
051                    message=null;
052                    errorcode="";
053                    object=null;
054            }
055    
056    
057    
058            /** set the value extendedinfo
059            *  A custom error code that you supply.
060            * @param extendedinfo value to set
061            **/
062            public void setExtendedinfo(String extendedinfo)        {
063                    this.extendedinfo=extendedinfo;
064            }
065    
066            /** set the value type
067            * @param type value to set
068            **/
069            public void setType(String type)        {
070                    this.type=type;
071            }
072    
073            /** set the value detail
074            * @param detail value to set
075            **/
076            public void setDetail(String detail)    {
077                    this.detail=detail;
078            }
079    
080            /** set the value message
081            *  A message that describes the exceptional event.
082            * @param message value to set
083            **/
084            public void setMessage(Object message)  {
085                    this.message=message;
086            }
087            
088            /**
089             * @deprecated this method should no longer be used.
090             * */
091            public void setMessage(String message)  {
092                    this.message=message;
093            }
094    
095            /** set the value errorcode
096            *  A custom error code that you supply.
097            * @param errorcode value to set
098            **/
099            public void setErrorcode(String errorcode)      {
100                    this.errorcode=errorcode;
101            }
102    
103            /** set the value object
104            *  a native java exception Object, if this attribute is defined all other will be ignored.
105            * @param object object to set
106             * @throws PageException
107            **/
108            public void setObject(Object object) throws PageException       {
109                    this.object=object;     
110            }
111    
112    
113            private PageException toPageException(Object object, PageException defaultValue) throws PageException {
114                    if((object instanceof ObjectWrap))
115                            return toPageException(((ObjectWrap)object).getEmbededObject(),defaultValue);
116                    
117                    
118                    if(object instanceof CatchBlock) {
119                            CatchBlock cb = (CatchBlock)object;
120                            return cb.getPageException();
121                    }
122                    if(object instanceof PageException) return (PageException)object;
123                    if(object instanceof Throwable) {
124                            Throwable t=(Throwable)object;
125                            return new CustomTypeException(t.getMessage(),"","",t.getClass().getName(),"");
126                    }
127                    if(object instanceof Struct){
128                            Struct sct=(Struct) object;
129                            String type=Caster.toString(sct.get(KeyConstants._type,""),"").trim();
130                            String msg=Caster.toString(sct.get(KeyConstants._message,null),null);
131                            if(!StringUtil.isEmpty(msg, true)) {
132                                    String detail=Caster.toString(sct.get(KeyConstants._detail,null),null);
133                                    String errCode=Caster.toString(sct.get("ErrorCode",null),null);
134                                    String extInfo=Caster.toString(sct.get("ExtendedInfo",null),null);
135                                    
136                                    PageException pe=null;
137                                    if("application".equalsIgnoreCase(type)) 
138                                            pe = new ApplicationException(msg, detail);
139                                    else if("expression".equalsIgnoreCase(type)) 
140                                            pe = new ExpressionException(msg, detail);
141                                    else 
142                                            pe=new CustomTypeException(msg, detail, errCode, type, extInfo);
143                                    
144                                    // Extended Info
145                                    if(!StringUtil.isEmpty(extInfo,true))pe.setExtendedInfo(extInfo);
146            
147                                    // Error Code
148                                    if(!StringUtil.isEmpty(errCode,true))pe.setErrorCode(errCode);
149                                    
150                                    // Additional
151                                    if(pe instanceof PageExceptionImpl) {
152                                            PageExceptionImpl pei=(PageExceptionImpl) pe;
153                                            sct=Caster.toStruct(sct.get("additional",null),null);
154                                            Iterator<Entry<Key, Object>> it = sct.entryIterator();
155                                            Entry<Key, Object> e;
156                                            while(it.hasNext()){
157                                                    e = it.next();
158                                                    pei.setAdditional(e.getKey(), e.getValue());
159                                            }
160                                    }
161                                    return pe;
162                            }
163                    }
164                    
165                    return defaultValue;
166                    
167            }
168    
169    
170    
171            @Override
172            public int doStartTag() throws PageException    {
173                    
174                    _doStartTag(message);
175                    _doStartTag(object);
176                    
177                    throw new CustomTypeException( "",detail,errorcode,type,extendedinfo);
178            }
179    
180            private void _doStartTag(Object obj) throws PageException {
181                    if(!StringUtil.isEmpty(obj)){
182                            PageException pe = toPageException(obj,null);
183                            if(pe!=null) throw pe;
184                            
185                            CustomTypeException exception = new CustomTypeException(Caster.toString(obj),detail,errorcode,type,extendedinfo);
186                            throw exception;
187                    }
188            }
189    
190    
191    
192            @Override
193            public int doEndTag()   {
194                    return EVAL_PAGE;
195            }
196    }