001    /*
002     * ----------------------------------------------------------------------
003     * Copyright (C) 2009 Enrique Lara (k957@68k.org)
004     *
005     * TinLizard is free software; you can redistribute it and/or
006     * modify it under the terms of the GNU Lesser General Public License
007     * as published by the Free Software Foundation; either version 3.0
008     * of the License, or (at your option) any later version.
009     *
010     * TinLizard 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
013     * GNU Lesser General Public License for more details.
014     *
015     * You should have received a copy of the GNU Lesser General Public License
016     * along with TinLizard. If not, see http://www.gnu.org/licenses/.
017     * ----------------------------------------------------------------------
018     */
019    package tinlizard.web;
020    
021    import tinlizard.util.Messages;
022    
023    import java.io.IOException;
024    import java.util.Date;
025    
026    import javax.servlet.RequestDispatcher;
027    import javax.servlet.ServletException;
028    
029    import org.apache.commons.lang.StringUtils;
030    
031    import org.kohsuke.stapler.StaplerRequest;
032    import org.kohsuke.stapler.StaplerResponse;
033    import org.kohsuke.stapler.export.Exported;
034    import org.kohsuke.stapler.export.ExportedBean;
035    import org.kohsuke.stapler.export.Flavor;
036    
037    /**
038     * Base class for beans exposed via Stapler.
039     * @param <E> the object being exposed.
040     */
041    @ExportedBean(defaultVisibility = 1)
042    public abstract class ObjectWeb<E extends Object> {
043        //XXX make max depth configurable.
044        private static final int MAX_DEPTH = 5;
045        private static final int MAX_DEPTH_ERROR = 451;
046    
047        @Exported
048        public abstract String getName();
049    
050        protected abstract E getSubject();
051    
052        public abstract Date getCreated();
053    
054        public abstract String getCreatedBy();
055    
056        public abstract Date getLastModified();
057    
058        public abstract String getLastModifiedBy();
059    
060        public void doDelete(final StaplerRequest request, final StaplerResponse response) throws Exception {
061            throw new UnsupportedOperationException();
062        }
063    
064        public final void doJson(final StaplerRequest request, final StaplerResponse response) throws Exception {
065            if (checkDepth(request, response)) {
066                response.serveExposedBean(request, this, Flavor.JSON);
067            }
068        }
069    
070        public final void doXml(final StaplerRequest request, final StaplerResponse response) throws Exception {
071            if (checkDepth(request, response)) {
072                response.serveExposedBean(request, this, Flavor.XML);
073            }
074        }
075    
076        public final void doPython(final StaplerRequest request, final StaplerResponse response) throws Exception {
077            if (checkDepth(request, response)) {
078                response.serveExposedBean(request, this, Flavor.PYTHON);
079            }
080        }
081    
082        private boolean checkDepth(final StaplerRequest request, final StaplerResponse response) throws IOException {
083            if (StringUtils.isNumeric(request.getParameter("depth"))) {
084                int depth = Integer.parseInt(request.getParameter("depth"));
085    
086                if (depth > MAX_DEPTH) {
087                    response.sendError(MAX_DEPTH_ERROR, Messages.Error451());
088                }
089            }
090    
091            return true;
092        }
093    
094        protected void gotoMyIndex(final StaplerRequest request, final StaplerResponse response) throws IOException, ServletException {
095            RequestDispatcher view = request.getView(this, "index.jsp");
096            view.include(request, response);
097        }
098    }