/*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
* [Additional notices, if required by prior licensing conditions]
*
*/
package org.apache.slide.webdav.logger;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Vector;
import java.io.IOException;
import org.apache.util.WebdavStatus;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.output.XMLOutputter;
import org.jdom.Document;
/**
* This class writes the header, the command and the body of an HTML request
* or response in an XML structure.
*
* @author Hardy Kiefer, Software AG, Germany
*/
public class XMLTestCaseGenerator {
/** The XML outputter */
private XMLOutputter xmlOut = new XMLOutputter( " ", true ); // indent: 2 spaces, newlines=true
private XHttpServletRequestFacade request;
private XHttpServletResponseFacade response;
private Element root = new Element( "test" );
private Document doc = new Document( root );
/*
* Name of the thread
*/
private String threadName;
public void setThreadName( String threadName ) {
this.threadName = threadName;
}
public String getThreadName() {
return threadName;
}
/*
* Constructs an XMLTestCaseGenerator object.
*/
public XMLTestCaseGenerator( XHttpServletRequestFacade request, XHttpServletResponseFacade response){
this.request = request;
this.response = response;
}
/*
* this method writes the data as XML.
*/
public String toString() {
String result = "";
root.addContent( printXMLstep() );
try {
result = xmlOut.outputString( doc.getRootElement().getChild("step") );
} catch ( Exception e ) {
e.printStackTrace();
}
return result;
}
/*
* this method prints the XML step attribute.
*/
private Element printXMLstep() {
Element stepElem = new Element( "step" );
stepElem.addAttribute( "executeIn", getThreadName() );
stepElem.addContent( printXMLrequest() );
stepElem.addContent( printXMLresponse() );
return stepElem;
}
/*
* this method prints the XML request attribute.
*/
private Element printXMLrequest() {
Element stepElem = new Element( "request" );
stepElem.addContent( printXMLrequestCommand() );
Iterator it = printXMLrequestHeaders();
while ( it.hasNext() ) {
stepElem.addContent( (Element)it.next() );
}
stepElem.addContent( printXMLrequestBody() );
return stepElem;
}
/*
* this method prints the XML request attribute.
*/
private Element printXMLresponse() {
Element respElem = new Element( "response" );
respElem.addContent( printXMLresponseCommand() );
Iterator it = printXMLresponseHeaders();
while ( it.hasNext() ) {
respElem.addContent( (Element)it.next() );
}
respElem.addContent( printXMLresponseBody() );
return respElem;
}
/*
* this method prints the XML request command attribute.
*/
private Element printXMLrequestCommand() {
Element reqComElem = new Element( "command" );
reqComElem.addContent( request.getMethod() + " " + request.getRequestURI() + " " + getProtocol() );
return reqComElem;
}
/*
* this method prints the XML request header attributes.
*/
private Iterator printXMLrequestHeaders() {
Vector vector = new Vector();
Enumeration e = request.getHeaderNames();
if ( e != null ) {
while ( e.hasMoreElements() ) {
String headerName = (String)e.nextElement();
String headerValue = request.getHeader(headerName);
Element elem = new Element( "header" );
elem.addContent( headerName + ": " + headerValue );
vector.add( elem );
}
}
return vector.iterator();
}
/*
* this method prints the XML request body attribute.
*/
private Element printXMLrequestBody(){
Element bodyElem = new Element( "body" );
try {
bodyElem.addContent( request.getRequestBody() );
}
catch (IOException e) {
e.printStackTrace();
}
return bodyElem;
}
/*
* this method prints the XML response command attribute.
*/
private Element printXMLresponseCommand() {
Element respComElem = new Element( "command" );
respComElem.addContent(
getProtocol() + " " +
response.getStatus() + " " +
WebdavStatus.getStatusText(response.getStatus()));
return respComElem;
}
/*
* Returns the protocol without a trailing CRLF
*/
private String getProtocol() {
String result = request.getProtocol();
while (result.endsWith("\n")) {
result = result.substring(0, result.length()-"\n".length()-1);
}
return result;
}
/*
* this method prints the XML response header attributes.
*/
private Iterator printXMLresponseHeaders() {
Vector vector = new Vector();
XResponseHeader respHeader;
Enumeration e = response.getResponseHeaders();
if ( e != null ) {
while ( e.hasMoreElements() ) {
Element elem = new Element( "header" );
elem.addContent( ((XResponseHeader)e.nextElement()).toString() );
vector.add( elem );
}
}
return vector.iterator();
}
/*
* this method prints the XML response body attribute.
*/
private Element printXMLresponseBody() {
Element bodyElem = new Element( "body" );
try {
bodyElem.addContent( response.getResponseBody() );
} catch ( IOException e ) {
e.printStackTrace();
}
return bodyElem;
}
}