juergen 2003/09/05 05:15:20
Modified: src/webdav/server/org/apache/slide/webdav/method
AbstractWebdavMethod.java
Log:
fixed a bug, if the encoding an xml body is different to the encoding stated in the
contetType header. Now pass the req.stream directly to the xml parser and let the
parser choose the right encoding.
Revision Changes Path
1.12 +175 -179
jakarta-slide/src/webdav/server/org/apache/slide/webdav/method/AbstractWebdavMethod.java
Index: AbstractWebdavMethod.java
===================================================================
RCS file:
/home/cvs/jakarta-slide/src/webdav/server/org/apache/slide/webdav/method/AbstractWebdavMethod.java,v
retrieving revision 1.11
retrieving revision 1.12
diff -u -r1.11 -r1.12
--- AbstractWebdavMethod.java 18 Aug 2003 06:55:09 -0000 1.11
+++ AbstractWebdavMethod.java 5 Sep 2003 12:15:20 -0000 1.12
@@ -126,156 +126,156 @@
*/
public abstract class AbstractWebdavMethod
implements WebdavMethod, WebdavConstants, BindConstants {
-
-
+
+
// -------------------------------------------------------------- Constants
-
+
/**
* String constant for <code>no-cache</code>.
*/
protected static final String NO_CACHE = "no-cache";
-
+
/**
* String constant for <code>http://</code>.
*/
public static final String HTTP_PROTOCOL = "http://";
-
+
/**
* String constant for <code>HTTP/1.1</code>.
*/
public static final String HTTP_VERSION = "HTTP/1.1";
-
+
/**
* String constant for <code>text/xml</code>.
*/
public static final String TEXT_XML = "text/xml";
-
+
/**
* String constant for <code>text/xml; charset="UTF-8"</code>.
*/
public static final String TEXT_XML_UTF_8 = "text/xml; charset=\"UTF-8\"";
-
+
/**
* The indent to use in the XML response.
*/
public static final String XML_REPONSE_INDENT = " ";
-
-
+
+
private static final String LOG_CHANNEL =
AbstractWebdavMethod.class.getName();
-
+
// public static final String PRINCIPAL =
// "org.apache.slide.webdav.method.principal";
-
-
+
+
// ----------------------------------------------------- Instance Variables
-
-
+
+
/**
* Requested Uri.
*/
protected String requestUri;
-
-
+
+
/**
* Servlet request.
*/
protected HttpServletRequest req;
-
-
+
+
/**
* Servlet response.
*/
protected HttpServletResponse resp;
-
-
+
+
/**
* Configuration.
*/
protected WebdavServletConfig config;
-
-
+
+
/**
* Request body.
*/
protected String requestBody;
-
-
+
+
/**
* Namespace access token.
*/
protected NamespaceAccessToken token;
-
-
+
+
/**
* Structure helper.
*/
protected Structure structure;
-
-
+
+
/**
* Content helper.
*/
protected Content content;
-
-
+
+
/**
* Security helper.
*/
protected Security security;
-
-
+
+
/**
* Lock helper.
*/
protected Lock lock;
-
-
+
+
/** wam
* Search helper.
*/
protected Search search;
-
-
+
+
/**
* Macro helper.
*/
protected Macro macro;
-
-
+
+
/**
* Slide token.
*/
protected SlideToken slideToken;
-
-
+
+
/**
* MD5 message digest provider.
*/
protected static MessageDigest md5Helper;
-
-
+
+
/**
* The MD5 helper object for this class.
*/
protected static final MD5Encoder md5Encoder = new MD5Encoder();
-
+
/**
* The request content (XML) Document.
*/
private Document requestContentDocument = null;
-
+
/**
* Indicates if the request content has already been parsed.
*/
private boolean isRequestContentParsed = false;
-
-
+
+
// -------------------------------------------------- Static Initialization
-
-
+
+
static {
-
+
// Load the MD5 helper used to calculate signatures.
try {
md5Helper = MessageDigest.getInstance("MD5");
@@ -284,11 +284,11 @@
throw new IllegalStateException();
}
}
-
-
+
+
// ----------------------------------------------------------- Constructors
-
-
+
+
/**
* Constructor.
*
@@ -297,10 +297,10 @@
*/
public AbstractWebdavMethod(NamespaceAccessToken token,
WebdavServletConfig config) {
-
+
this.config = config;
this.token = token;
-
+
// initialize helpers
structure = token.getStructureHelper();
content = token.getContentHelper();
@@ -308,11 +308,11 @@
lock = token.getLockHelper();
macro = token.getMacroHelper();
}
-
-
+
+
// -------------------------------------------- WebdavMethod Implementation
-
-
+
+
/**
* Exceute method.
*
@@ -320,13 +320,13 @@
*/
public void run(HttpServletRequest req, HttpServletResponse resp)
throws WebdavException {
-
+
this.req = req;
this.resp = resp;
this.slideToken = WebdavUtils.getSlideToken(req);
this.requestUri = WebdavUtils.getRelativePath(req, config);
parseHeaders();
-
+
boolean transactionIsStarted = false;
try {
parseRequest();
@@ -334,7 +334,7 @@
token.begin();
transactionIsStarted = true;
}
-
+
// clear expired lock-tokens
try {
UnlockListener listener =
@@ -342,7 +342,7 @@
lock.clearExpiredLocks( slideToken, requestUri, listener );
}
catch (SlideException e) {}
-
+
executeRequest();
if (methodNeedsTransactionSupport()) {
token.commit();
@@ -365,37 +365,37 @@
}
}
}
-
+
}
-
-
+
+
// --------------------------------------------------------- Public Methods
-
-
+
+
/**
* Returns the configuration of the WebdavServlet.
*
* @return WebdavServletConfig
*/
public WebdavServletConfig getConfig() {
-
+
return config;
}
-
-
+
+
/**
* Return an absolute URL (absolute in the HTTP sense) based on a Slide
* path.
*/
public String getFullPath(String path) {
-
+
if (path.startsWith("/"))
return WebdavUtils.encodeURL(req.getContextPath() + path);
else
return WebdavUtils.encodeURL(req.getContextPath() + "/" + path);
}
-
-
+
+
/**
* Returns a Slide path based on an absolute URL
* (absolute in the HTTP sense)
@@ -406,7 +406,7 @@
fullpath=fullpath.substring(fullpath.indexOf("://")+3);
fullpath=fullpath.substring(fullpath.indexOf("/"));
}
-
+
// strip off the servlet context path
String contextPath=req.getContextPath();
if (fullpath.startsWith(contextPath)) {
@@ -414,11 +414,11 @@
}
return fullpath;
}
-
-
+
+
// ------------------------------------------------------ Protected Methods
-
-
+
+
/**
* Return true if the method needs transaction support.
* Return true if the method needs transaction support, e.g.
@@ -431,8 +431,8 @@
// overwrite it to true, but PropFind creates a RevisionDescriptors
// on the fly
}
-
-
+
+
/**
* Read request contents.
*
@@ -440,12 +440,12 @@
* @return char[] Array of char which contains the body of the request
*/
protected void readRequestContent() {
-
+
if (req.getContentLength() == 0)
return;
-
+
// TODO : Modify this and make it chunking aware
-
+
try {
requestBody = new String(NodeRevisionContent.read(req.getInputStream()),
getEncodingString(req.getCharacterEncoding()));
@@ -454,9 +454,9 @@
token.getLogger().log(e,LOG_CHANNEL,Logger.ERROR);
}
}
-
-
-
+
+
+
/**
* Translate the encoding string into a proper Java encoding String.
*/
@@ -467,28 +467,28 @@
if (result.endsWith("\"")) result = result.substring(0,
result.length()-1);
return result;
}
-
-
+
+
/**
* Parse lock token headers.
*/
protected void parseHeaders() {
-
+
// Retrieve if header
String ifHeader = req.getHeader("If");
//System.out.println("If header : " + ifHeader);
-
+
if (ifHeader == null)
return;
-
+
// Parse if header and extract the lock tokens
int pos = ifHeader.indexOf(S_LOCK_TOKEN);
int endPos = -1;
int offset = S_LOCK_TOKEN.length();
String lockToken = null;
-
+
while (pos != -1) {
-
+
endPos = ifHeader.indexOf('>', pos + offset);
if (endPos == -1) {
lockToken = ifHeader;
@@ -496,17 +496,17 @@
} else {
lockToken = ifHeader.substring(pos + offset, endPos);
}
-
+
//System.out.println("Lock Token found :-" + lockToken + "-");
slideToken.addLockToken(lockToken);
-
+
pos = ifHeader.indexOf(S_LOCK_TOKEN, endPos);
-
+
}
-
+
}
-
-
+
+
/**
* Returns the lock token value from the lock token header.
*
@@ -523,14 +523,14 @@
}
return result;
}
-
+
/**
* Test if a resource given by a path is a collection
*/
protected boolean isCollection(String path) {
return WebdavUtils.isCollection(token, slideToken, path);
}
-
+
/**
* Test whether the resource given by lowerNode is a descendant of the
* resource given by upperNode
@@ -548,13 +548,13 @@
if (upperNode.hasBinding(lowerNode)) {
return true;
}
-
+
NodeRevisionDescriptors lowerNrds = null;
NodeRevisionDescriptor lowerNrd = null;
try {
lowerNrds = content.retrieve(slideToken, lowerNode.getUri());
lowerNrd = content.retrieve(slideToken, lowerNrds);
-
+
NodeProperty psProp = lowerNrd.getProperty(P_PARENT_SET);
XMLValue xv = new XMLValue( String.valueOf(psProp.getValue()) );
Iterator i = xv.getList().iterator();
@@ -566,11 +566,11 @@
return isDescendant( hrNode, upperNode );
}
} catch (Exception e) {}
-
+
return false;
}
-
-
+
+
/**
* Parse WebDAV XML query.
*
@@ -578,9 +578,9 @@
*/
protected abstract void parseRequest()
throws WebdavException;
-
-
-
+
+
+
/**
* Returns the request content (XML) Document.
*
@@ -589,9 +589,9 @@
protected Document getRequestContent() {
return requestContentDocument;
}
-
+
//--
-
+
/**
* precondition: sourceUri != null
*/
@@ -612,12 +612,12 @@
if ((hostName != null) && (uri.startsWith(hostName))) {
uri = uri.substring(hostName.length());
}
-
+
int portIndex = uri.indexOf(":");
if (portIndex >= 0) {
uri = uri.substring(portIndex);
}
-
+
if (uri.startsWith(":")) {
int firstSeparator = uri.indexOf("/");
if (firstSeparator < 0) {
@@ -627,16 +627,16 @@
}
}
}
-
+
// headers are "ISO-8859-1" encoded [not any more with TC 4.1.18
// destinationUri =
WebdavUtils.decodeURL(WebdavUtils.fixTomcatURL(destinationUri, "ISO-8859-1"));
uri = WebdavUtils.decodeURL(uri);
-
+
String contextPath = req.getContextPath();
if ((contextPath != null) && (uri.startsWith(contextPath))) {
uri = uri.substring(contextPath.length());
}
-
+
String pathInfo = req.getPathInfo();
if (pathInfo != null) {
String servletPath = req.getServletPath();
@@ -645,14 +645,14 @@
}
}
uri = getConfig().getScope() + uri;
-
+
return uri;
}
-
+
protected Element parseRequestContent(String rootName) throws JDOMException,
IOException {
Document document;
Element root;
-
+
document = parseRequestContent();
if (document == null) {
throw new JDOMException("Request content missing");
@@ -664,7 +664,7 @@
}
return root;
}
-
+
/**
* Parses the request content (XML) Document.
*
@@ -674,21 +674,17 @@
* @throws JDOMException if parsing the document failed.
*/
protected Document parseRequestContent() throws JDOMException, IOException {
-
+
if (isRequestContentParsed) {
return requestContentDocument;
}
-
- if (requestBody == null) {
- readRequestContent();
- if (requestBody == null) {
- // there is no request content body, so there's nothing to
- // parse
- return requestContentDocument;
- }
+
+ if (req.getContentLength() == 0) {
+ return requestContentDocument;
}
+
try {
- requestContentDocument = new SAXBuilder().build(new
StringReader(requestBody));
+ requestContentDocument = new SAXBuilder().build(req.getInputStream());
isRequestContentParsed = true;
}
catch (JDOMException e) {
@@ -699,11 +695,11 @@
throw e;
}
}
-
+
return requestContentDocument;
}
-
-
+
+
/**
* Generate XML response.
*
@@ -711,8 +707,8 @@
*/
protected abstract void executeRequest()
throws WebdavException, IOException;
-
-
+
+
/**
* Simulate MS IIS5 ?
*
@@ -721,7 +717,7 @@
protected boolean isMsProprietarySupport() {
return (token.getNamespaceConfig().getParameter("ms") != null);
}
-
+
/**
* Sends a precondition vilolation response.
*
@@ -729,21 +725,21 @@
* precondition.
*/
protected void sendPreconditionViolation(PreconditionViolationException pve)
throws IOException {
-
+
if (pve != null) {
-
+
ViolatedPrecondition violatedPrecondition =
pve.getViolatedPrecondition();
-
+
int statusCode = violatedPrecondition.getStatusCode();
printStackTrace( pve, statusCode );
resp.setStatus( statusCode );
resp.setContentType(TEXT_XML_UTF_8);
-
+
new XMLOutputter(XML_REPONSE_INDENT, true).
output(new
Document(MethodUtil.getPreconditionViolationError(pve.getViolatedPrecondition())),
resp.getWriter());
}
}
-
+
/**
* Returns the absolute URL of the given <code>uri</code>, e.g.
* if the server is <code>aloha.com:80</code>, context path is
@@ -755,18 +751,18 @@
* @return the absolute URL.
*/
protected String getAbsoluteURL(String uri) {
-
+
StringBuffer buffer = new StringBuffer();
buffer.append(HTTP_PROTOCOL);
buffer.append(req.getServerName());
buffer.append(":");
buffer.append(req.getServerPort());
-
+
if ( ! req.getContextPath().startsWith("/") ) {
buffer.append("/");
}
buffer.append(req.getContextPath());
-
+
if (uri != null) {
if ( !req.getContextPath().endsWith("/") && !uri.startsWith("/") ) {
buffer.append("/");
@@ -775,10 +771,10 @@
}
return buffer.toString();
}
-
+
// -------------------------------------------------------- Private Methods
-
-
+
+
/**
* Get return status based on exception type.
*/
@@ -790,8 +786,8 @@
return getErrorCode((SlideException)ex);
}
}
-
-
+
+
/**
* Get return status based on exception type.
*/
@@ -821,9 +817,9 @@
return WebdavStatus.SC_INTERNAL_SERVER_ERROR;
}
}
-
-
-
+
+
+
/**
* Get return status based on exception type.
*/
@@ -837,8 +833,8 @@
return getErrorCode((SlideException)cause);
}
}
-
-
+
+
/**
* Returns the value of a boolean init parameter of the servlet.
* Default value: false.
@@ -846,8 +842,8 @@
protected boolean getBooleanInitParameter( String name ) {
return "true".equalsIgnoreCase( getConfig().getInitParameter(name) );
}
-
-
+
+
/**
* Returns the value of an integer init parameter of the servlet.
* Default value: -1.
@@ -860,14 +856,14 @@
catch( NumberFormatException x ) {};
return result;
}
-
+
/**
* Error handling routine
*/
protected void sendError( int statusCode ) {
try { resp.sendError( statusCode ); } catch( Throwable x ) {};
}
-
+
/**
* Error handling routine
*/
@@ -877,7 +873,7 @@
Messages.format( message, (Object)null );
try { resp.sendError( statusCode, statusText ); } catch( Throwable x ) {};
}
-
+
/**
* Error handling routine
*/
@@ -887,7 +883,7 @@
Messages.format( message, args );
try { resp.sendError( statusCode, statusText ); } catch( Throwable x ) {};
}
-
+
/**
* Error handling routine
*/
@@ -901,7 +897,7 @@
WebdavStatus.getStatusText(statusCode)+": "+explanation;
try { resp.sendError( statusCode, statusText ); } catch( Throwable x ) {};
}
-
+
/**
* Prints the stack trace of the given exception if the specified status code
* is greater-or-equal the value of the servlet init-parameter printStackTrace.
@@ -915,8 +911,8 @@
if( statusCode >= printStackTraceFrom )
x.printStackTrace();
}
-
-
+
+
/**
* Generate status text.
*
@@ -925,7 +921,7 @@
* @param statusCode HTTP status code of the error
*/
protected void generateStatusText(Element parentElement, String href, int
statusCode) {
-
+
Element hrefElement = new Element(E_HREF, NamespaceCache.DEFAULT_NAMESPACE);
parentElement.addContent(hrefElement);
hrefElement.setText(getFullPath(href));
@@ -934,7 +930,7 @@
statusElement.setText("HTTP/1.1 " + statusCode + " "
+ WebdavStatus.getStatusText(statusCode));
}
-
+
/**
* Generate an XML error message.
*
@@ -943,13 +939,13 @@
*/
protected String generateErrorMessage
(NestedSlideException nestedException) {
-
+
Element multistatus = new Element(E_MULTISTATUS,
NamespaceCache.DEFAULT_NAMESPACE);
-
+
Enumeration nestedExceptionsList =
nestedException.enumerateExceptions();
while (nestedExceptionsList.hasMoreElements()) {
-
+
Element response = new Element(E_RESPONSE,
NamespaceCache.DEFAULT_NAMESPACE);
multistatus.addContent(response);
SlideException ex =
@@ -959,9 +955,9 @@
if (ex instanceof PreconditionViolationException) {
response.addContent(MethodUtil.getPreconditionViolationResponseDescription((PreconditionViolationException)ex));
}
-
+
}
-
+
StringWriter stringWriter = new StringWriter();
try {
new XMLOutputter().output(multistatus, stringWriter);
@@ -970,9 +966,9 @@
Domain.log(e);
}
return stringWriter.toString();
-
+
}
-
+
protected boolean exists( String uriStr ) throws SlideException {
boolean destinationExists = true;
try {
@@ -983,7 +979,7 @@
}
return destinationExists;
}
-
+
protected boolean isLockNull( String uriStr ) throws SlideException {
boolean isLockNull = false;
try {
@@ -996,7 +992,7 @@
}
return isLockNull;
}
-
+
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]