
cat.inspiracio.servlet.WebXml Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of dummy-servlet Show documentation
Show all versions of dummy-servlet Show documentation
Dummy Servlet provides some implementations for the interfaces and classes
of the Java Servlet spec that are useful for testing and simulation.
Version 5.0.0 is for Servlet 5.0.0.
The newest version!
/*
Copyright 2016 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.servlet;
import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;
import jakarta.servlet.ServletException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
/** Represents the information in web.xml. */
class WebXml {
private final ListerrorPages=new ArrayList<>();
public WebXml(Reader r){
Document d=parse(r);
// elements
for(Element e : getElementsByTagName(d, "error-page")){
String location=getText(e, "location");
String exceptionType=getText(e, "exception-type");//null
int errorCode=getInt(e, "error-code");//0
ErrorPage errorPage=new ErrorPage();
errorPage.setLocation(location);
errorPage.setExceptionType(exceptionType);
errorPage.setErrorCode(errorCode);
errorPages.add(errorPage);
}
}
// XML helpers --------------------------------------------------------
/** Parses XML. */
private Document parse(Reader reader){
try {
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
InputSource source=new InputSource(reader);
return builder.parse(source);
} catch (ParserConfigurationException | SAXException | IOException e) {
throw new RuntimeException(e);
}
}
/** Returns the text content of the first child element with this tag, or null. */
private String getText(Element e, String tag){
NodeList children=e.getElementsByTagName(tag);
if(children.getLength()==0)
return null;
Node child=children.item(0);
return child.getTextContent();
}
/** Returns the int content of the first child element with this tag, or 0. */
private int getInt(Element e, String tag){
NodeList children=e.getElementsByTagName(tag);
if(children.getLength()==0)
return 0;
Node child=children.item(0);
String s=child.getTextContent();
try{
return Integer.parseInt(s);
}
catch(NumberFormatException ex){
return 0;
}
}
private ListgetElementsByTagName(Document d, String tag){
NodeList nodes=d.getElementsByTagName(tag);
Listlist=new ArrayList<>();
int length=nodes.getLength();
for(int i=0; i c=t.getClass();
while(c!=Object.class){
String name=c.getName();
for(ErrorPage p : errorPages){
String type=p.getExceptionType();
if(name.equals(type))
return p.getLocation();
}
c=c.getSuperclass();
}
//Try exception wrapped in ServletException
if(t instanceof ServletException){
ServletException se=(ServletException)t;
t=se.getRootCause();
return getErrorPage(t);
}
return null;
}
/** Reads web.xml to determine an error page for this status code.
* @return URI of the error page, or null if there is none defined */
public String getErrorPage(int status) {
for(ErrorPage p : errorPages){
int code=p.getErrorCode();
if(status==code)
return p.getLocation();
}
return null;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy