001    /*
002     * 
003     * $Revision: 15202 $ $Date: 2009-05-15 17:00:44 +0200 (Fri, 15 May 2009) $
004     *
005     * This file is part of ***  M y C o R e  ***
006     * See http://www.mycore.de/ for details.
007     *
008     * This program is free software; you can use it, redistribute it
009     * and / or modify it under the terms of the GNU General Public License
010     * (GPL) as published by the Free Software Foundation; either version 2
011     * of the License or (at your option) any later version.
012     *
013     * This program is distributed in the hope that it will be useful, but
014     * WITHOUT ANY WARRANTY; without even the implied warranty of
015     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
016     * GNU General Public License for more details.
017     *
018     * You should have received a copy of the GNU General Public License
019     * along with this program, in a file called gpl.txt or license.txt.
020     * If not, write to the Free Software Foundation Inc.,
021     * 59 Temple Place - Suite 330, Boston, MA  02111-1307 USA
022     */
023    
024    package org.mycore.frontend.wcms;
025    
026    import java.io.File;
027    import java.io.IOException;
028    import java.util.ArrayList;
029    
030    import javax.servlet.ServletException;
031    import javax.servlet.http.HttpServletRequest;
032    import javax.servlet.http.HttpServletResponse;
033    
034    import org.jdom.Document;
035    import org.jdom.Element;
036    import org.jdom.JDOMException;
037    import org.jdom.input.SAXBuilder;
038    import org.mycore.access.MCRAccessInterface;
039    import org.mycore.access.MCRAccessManager;
040    import org.mycore.common.MCRConfiguration;
041    import org.mycore.common.MCRSession;
042    import org.mycore.common.MCRSessionMgr;
043    import org.mycore.frontend.MCRWebsiteWriteProtection;
044    import org.mycore.frontend.servlets.MCRServlet;
045    import org.mycore.frontend.servlets.MCRServletJob;
046    
047    /**
048     * @author Andreas Trappe, Thomas Scheffler
049     * 
050     * Need to insert some things here
051     * 
052     */
053    public abstract class MCRWCMSServlet extends MCRServlet {
054        protected static final String OUTPUT_ENCODING = "UTF-8";
055    
056        protected static final String VALIDATOR = "JTidy";
057    
058    
059        protected void doGetPost(MCRServletJob job) throws Exception {
060            if (MCRWebsiteWriteProtection.printInfoPageIfNoAccess(job.getRequest(), job.getResponse(), getBaseURL()))
061                return;
062            if (accessGeneral()) {
063                // set some global required params
064                MCRSession session = MCRSessionMgr.getCurrentSession();
065                session.put("status", "loggedIn");
066                session.put("userID", session.getCurrentUserID());
067                session.put("userRealName", session.getCurrentUserID());
068                session.put("userClass", "admin");
069                session.put("rootNodes", new ArrayList());
070                // forward
071                processRequest(job.getRequest(), job.getResponse());
072            } else{
073                String LOGINSERVLET_URL = getServletBaseURL() + "MCRLoginServlet";
074                job.getResponse().sendRedirect(LOGINSERVLET_URL);
075            }
076        }
077    
078        protected final boolean accessGeneral() {
079            return (MCRWCMSUtilities.writeAccessGeneral() || hasRight2Manage());
080        }
081    
082        private boolean hasRight2Manage() {
083            return (MCRWCMSUtilities.manageReadAccess() || MCRWCMSUtilities.manageWCMSAccess());
084        }
085    
086        public Element getTemplates() {
087            Element templates = new Element("templates");
088    
089            // master
090            File[] masterTemplates = new File(MCRConfiguration.instance().getString("MCR.templatePath") + "master/".replace('/', File.separatorChar)).listFiles();
091            Element master = new Element("master");
092    
093            for (int i = 0; i < masterTemplates.length; i++) {
094                if (masterTemplates[i].isDirectory() && (masterTemplates[i].getName().compareToIgnoreCase("cvs") != 0)) {
095                    master.addContent(new Element("template").setText(masterTemplates[i].getName()));
096                }
097            }
098    
099            // templates.addContent(content);
100            templates.addContent(master);
101    
102            return templates;
103        }
104    
105        final Document XMLFile2JDOM(String pathOfFile) throws IOException, JDOMException {
106            File XMLFile = new File(pathOfFile);
107            SAXBuilder builder = new SAXBuilder();
108            Document doc = builder.build(XMLFile);
109    
110            return doc;
111        }
112    
113        /*
114         * final void WriteJDOM2XMLFile(Document doc, String pathOfFile) { }
115         */
116        protected abstract void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException;
117    
118        /**
119         * Attaches on the given JDOM-Element an JDOM-Element with the configuration
120         * of available multimedia objects (images, misc. documents).
121         * 
122         * @param root
123         * @return
124         */
125        public Element getMultimediaConfig(Element root) {
126    
127            File[] imageList = null;
128            File[] documentList = null;
129    
130            Element templates;
131            templates = new Element("templates");
132    
133            Element images = new Element("images");
134            root.addContent(images);
135    
136            File imagePath = new File((MCRConfiguration.instance().getString("MCR.WCMS.imagePath").replace('/', File.separatorChar)));
137    
138            if (!imagePath.exists()) {
139                imagePath.mkdirs();
140            }
141    
142            imageList = new File(imagePath.toString()).listFiles();
143    
144            for (int i = 0; i < imageList.length; i++) {
145                if (!imageList[i].isDirectory()) {
146                    images.addContent(new Element("image").setText(imageList[i].getName()));
147                }
148            }
149    
150            root.addContent(new Element("imagePath").setText(MCRConfiguration.instance().getString("MCR.WCMS.imagePath").substring(
151                            MCRConfiguration.instance().getString("MCR.WCMS.imagePath").lastIndexOf("webapps"))));
152    
153            Element documents = new Element("documents");
154            root.addContent(documents);
155    
156            File documentPath = new File((MCRConfiguration.instance().getString("MCR.WCMS.documentPath").replace('/', File.separatorChar)));
157    
158            if (!documentPath.exists()) {
159                documentPath.mkdirs();
160            }
161    
162            documentList = new File(documentPath.toString()).listFiles();
163    
164            for (int i = 0; i < documentList.length; i++) {
165                if (!documentList[i].isDirectory()) {
166                    documents.addContent(new Element("document").setText(documentList[i].getName()));
167                }
168            }
169    
170            root.addContent(new Element("documentPath").setText(MCRConfiguration.instance().getString("MCR.WCMS.documentPath").substring(
171                            MCRConfiguration.instance().getString("MCR.WCMS.documentPath").lastIndexOf("webapps"))));
172            return templates;
173        }
174    }