/*
 *  eXist Open Source Native XML Database
 *  Copyright (C) 2001-04 Wolfgang M. Meier
 *  wolfgang@exist-db.org
 *  http://exist-db.org
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public License
 *  as published by the Free Software Foundation; either version 2
 *  of the License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 * 
 *  $Id$
 */
package org.exist.xquery.util;


import org.apache.log4j.Logger;
import org.exist.dom.DocumentImpl;
import org.exist.dom.DocumentSet;
import org.exist.http.servlets.ResponseWrapper;
import org.exist.xquery.Variable;
import org.exist.xquery.XQuery;
import org.exist.xquery.XQueryContext;
import org.exist.xquery.functions.request.RequestModule;
import org.exist.xquery.value.JavaObjectValue;
import org.exist.xquery.value.Sequence;

/** A place holder for static utility functions related to HTTP. 
 * @author jmv */
public class HTTPUtils {
    private final static Logger LOG = Logger.getLogger(XQuery.class);

    /** Feature "Guess last modification time for an XQuery result"; 
     *  the HTTP header Last-Modified is filled with most recent time stamp among all 
     *  XQuery documents appearing in the actual response.
     *  Note however, that the actual response can be influenced, through tests in the query,
     *  by documents more recent. */
    public static void addLastModifiedHeader(Sequence result,
			XQueryContext context) {
		try {
			DocumentSet documentSet = result.getDocumentSet();
			long mostRecentDocumentTime = 0;
			for (int i = 0; i < documentSet.getLength(); i++) {
				DocumentImpl doc = (DocumentImpl) documentSet.item(i);
				if (doc != null) {
					mostRecentDocumentTime = Math.max(doc.getLastModified(),
							mostRecentDocumentTime);
//					LOG.debug("getFileName: " + doc.getFileName() + ", "
//							+ doc.getLastModified());
				}
			}
			LOG.debug("mostRecentDocumentTime: " + mostRecentDocumentTime);

			if (mostRecentDocumentTime > 0) {
				RequestModule myModule = (RequestModule) context
						.getModule(RequestModule.NAMESPACE_URI);
				
				// response servlet object is read from global variable $response
				Variable var = myModule
						.resolveVariable(RequestModule.RESPONSE_VAR);
				if (var != null) {
					JavaObjectValue value = (JavaObjectValue) var.getValue()
							.itemAt(0);
					if (value != null
							&& value.getObject() instanceof ResponseWrapper) {
						// have to take in account that if the header has allready been explicitely set 
						// by the XQuery script, we should not modify it .
						ResponseWrapper responseWrapper = ((ResponseWrapper) value.getObject());
						if ( responseWrapper.getDateHeader("Last-Modified") == 0 )
							responseWrapper.setDateHeader(
								"Last-Modified", mostRecentDocumentTime);
					}
				}
			}
		} catch (Exception e) {
			LOG.debug(e);
		}
	}
}

@@ -3354,10 +3354,1119 @@
 %0A%09%09%7D%0A%09%7D%0A
+    %0A    public static String printStackTraceHTML(Throwable e) %7B%0A        StringBuffer buf = new StringBuffer();%0A        StackTraceElement%5B%5D trace = e.getStackTrace();%0A        buf.append(%22%3Ctable id=%5C%22javatrace%5C%22%3E%22);%0A        buf.append(%22%3Ccaption%3EJava Stack Trace:%3C/caption%3E%22);%0A        buf.append(%22%3Ctr%3E%3Cth%3EClass Name%3C/th%3E%3Cth%3EMethod Name%3C/th%3E%3Cth%3EFile Name%3C/th%3E%3Cth%3ELine%3C/th%3E%3C/tr%3E%22);%0A        for (int i = 0; i %3C trace.length && i %3C 20; i++) %7B%0A            buf.append(%22%3Ctr%3E%22);%0A            buf.append(%22%3Ctd class=%5C%22class%5C%22%3E%22).append(trace%5Bi%5D.getClassName()).append(%22%3C/td%3E%22);%0A            buf.append(%22%3Ctd class=%5C%22method%5C%22%3E%22).append(trace%5Bi%5D.getMethodName()).append(%22%3C/td%3E%22);%0A            buf.append(%22%3Ctd class=%5C%22file%5C%22%3E%22).append(trace%5Bi%5D.getFileName() == null ? %22Unknown%22 : trace%5Bi%5D.getFileName()).append(%22%3C/td%3E%22);%0A            buf.append(%22%3Ctd class=%5C%22line%5C%22%3E%22);%0A            buf.append(trace%5Bi%5D.getLineNumber() %3C 0 ? %22Unavailable%22 : Integer.toString(trace%5Bi%5D.getLineNumber()));%0A            buf.append(%22%3C/td%3E%22);%0A            buf.append(%22%3C/tr%3E%22);%0A        %7D%0A        buf.append(%22%3C/table%3E%22);%0A        return buf.toString();%0A    %7D%0A
 %7D%0A

Improve error messages displayed by XQueryServlet.