001/**
002 *
003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved.
004 *
005 * This library is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU Lesser General Public
007 * License as published by the Free Software Foundation; either 
008 * version 2.1 of the License, or (at your option) any later version.
009 * 
010 * This library is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013 * Lesser General Public License for more details.
014 * 
015 * You should have received a copy of the GNU Lesser General Public 
016 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
017 * 
018 **/
019package lucee.runtime.functions.owasp;
020
021import java.io.PrintStream;
022
023import lucee.commons.io.DevNullOutputStream;
024import lucee.commons.lang.StringUtil;
025import lucee.runtime.PageContext;
026import lucee.runtime.exp.ApplicationException;
027import lucee.runtime.exp.FunctionException;
028import lucee.runtime.exp.PageException;
029import lucee.runtime.ext.function.Function;
030import lucee.runtime.op.Caster;
031
032import org.owasp.esapi.ESAPI;
033import org.owasp.esapi.Encoder;
034import org.owasp.esapi.errors.EncodingException;
035
036public class ESAPIDecode implements Function {
037        
038        private static final long serialVersionUID = 7054200748398531363L;
039        
040        public static final short DEC_BASE64=1;
041        public static final short DEC_URL=2;
042        
043        public static String decode(String item, short decFrom) throws PageException  {
044                
045                PrintStream out = System.out;
046                try {
047                         System.setOut(new PrintStream(DevNullOutputStream.DEV_NULL_OUTPUT_STREAM));
048                         Encoder encoder = ESAPI.encoder();
049                         switch(decFrom){
050                         case DEC_URL:return encoder.decodeFromURL(item);
051                         }
052                         throw new ApplicationException("invalid target decoding defintion");
053                }
054                catch(EncodingException ee){
055                        throw Caster.toPageException(ee);
056                }
057                finally {
058                         System.setOut(out);
059                }
060        }
061        
062        public static String call(PageContext pc , String strDecodeFrom, String value) throws PageException{
063                short decFrom;
064                strDecodeFrom=StringUtil.emptyIfNull(strDecodeFrom).trim().toLowerCase();
065                if("url".equals(strDecodeFrom)) decFrom=DEC_URL;
066                else 
067                        throw new FunctionException(pc, "ESAPIDecode", 1, "decodeFrom", "value ["+strDecodeFrom+"] is invalid, valid values are " +
068                                        "[url]");
069                return decode(value, decFrom);
070        }
071        
072}