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.rest;
020
021import java.util.List;
022
023import javax.servlet.http.HttpServletRequest;
024
025import lucee.commons.lang.mimetype.MimeType;
026import lucee.runtime.PageContext;
027import lucee.runtime.PageSource;
028import lucee.runtime.exp.PageException;
029import lucee.runtime.listener.RequestListener;
030import lucee.runtime.type.Struct;
031
032public class RestRequestListener implements RequestListener {
033
034        private final Mapping mapping;
035        private final String path;
036        private final int format;
037        private final Struct matrix;
038        private final Result defaultValue;
039        private Result result;
040        private final List<MimeType> accept;
041        private final MimeType contentType;
042        private final boolean hasFormatExtension;
043
044        public RestRequestListener(Mapping mapping,String path,Struct matrix,int format,boolean hasFormatExtension,List<MimeType> accept,MimeType contentType, Result defaultValue) {
045                this.mapping=mapping;
046                this.path=path;
047                this.format=format;
048                this.hasFormatExtension=hasFormatExtension;
049                this.matrix=matrix;
050                this.defaultValue=defaultValue;
051                this.accept=accept;
052                this.contentType=contentType;
053        }
054
055        @Override
056        public PageSource execute(PageContext pc, PageSource requestedPage) throws PageException {
057                result = mapping.getResult(pc, path, matrix,format,hasFormatExtension,accept,contentType, defaultValue);
058                HttpServletRequest req = pc.getHttpServletRequest();
059                req.setAttribute("client", "lucee-rest-1-0");
060                req.setAttribute("rest-path", path);
061                req.setAttribute("rest-result", result); 
062                
063                if(result==null) {
064                        RestUtil.setStatus(pc,404,"no rest service for ["+path+"] found in mapping ["+mapping.getVirtual()+"]");
065                        return null;
066                }
067                
068                return result.getSource().getPageSource();
069        }
070
071        /**
072         * @return the result
073         */
074        public Result getResult() {
075                return result;
076        }
077}