All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cat.inspiracio.orange.WebXml Maven / Gradle / Ivy

/*    Copyright 2019 Alexander Bunkenburg

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
package cat.inspiracio.orange;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServlet;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import java.io.InputStream;

/** Read my own web.xml */
class WebXml {

    private HttpServlet servlet;

    WebXml(HttpServlet s){
        servlet=s;
    }

    private String welcome;
    private boolean welcomeSet=false;

    // business methods --------------------------

    /*** Looks for the first welcome file in WEB-INF/web.xml
     * and returns it, starting with "/", or null.
     * Called from OrangeServlet. */
     String getWelcomeFile(){
         if(welcomeSet)
             return welcome;

         //cache miss
         try {
             ServletContext context=servlet.getServletContext();
            InputStream in=context.getResourceAsStream("/WEB-INF/web.xml");//null
            if(in!=null) {
                DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
                DocumentBuilder builder=factory.newDocumentBuilder();
                Document webXml=builder.parse(in);
                //
                //	
                //		index.html
                //	
                Element webApp=webXml.getDocumentElement();
                Element welcomeFileList=getChild(webApp, "welcome-file-list");//null
                if(welcomeFileList!=null) {
                    Element welcomeFile=getChild(welcomeFileList, "welcome-file");//null
                    if(welcomeFile!=null) {
                        String content=welcomeFile.getTextContent();//	index.html
                        welcome = "/" + content;
                    }
                }
            }
            welcomeSet = true;
            return welcome;

        } catch (ParserConfigurationException | SAXException | IOException e) {
            return null;
        }
    }

    // helpers -------------------------------------------

    /** Returns the first child element with certain tag, or null. */
    private Element getChild(Element element, String tag){
        NodeList children=element.getChildNodes();
        for(int i=0; i




© 2015 - 2025 Weber Informatics LLC | Privacy Policy