@Override public String formatXML(String xml) { // This is only supposed to format your xml, however, it will also // unwantingly change © and other characters like it into their // respective readable versions try { xml = StringUtil.replace(xml, "&#", "[$SPECIAL_CHARACTER$]"); xml = XMLFormatter.toString(xml, _XML_INDENT); xml = StringUtil.replace(xml, "[$SPECIAL_CHARACTER$]", "&#"); return xml; } catch (IOException ioe) { throw new SystemException(ioe); } catch (org.dom4j.DocumentException de) { throw new SystemException(de); } }
protected String getResponseXML(Lock lock, long depth) throws Exception { StringBundler sb = new StringBundler(20); long timeoutSecs = lock.getExpirationTime() / Time.SECOND; sb.append("<?xml version=\"1.0\" encoding=\"utf-8\" ?>"); sb.append("<D:prop xmlns:D=\"DAV:\">"); sb.append("<D:lockdiscovery>"); sb.append("<D:activelock>"); sb.append("<D:locktype><D:write/></D:locktype>"); sb.append("<D:lockscope><D:exclusive/></D:lockscope>"); if (depth < 0) { sb.append("<D:depth>Infinity</D:depth>"); } sb.append("<D:owner>"); sb.append(lock.getOwner()); sb.append("</D:owner>"); sb.append("<D:timeout>"); if (timeoutSecs > 0) { sb.append("Second-" + timeoutSecs); } else { sb.append("Infinite"); } sb.append("</D:timeout>"); sb.append("<D:locktoken><D:href>"); sb.append(WebDAVUtil.TOKEN_PREFIX); sb.append(lock.getUuid()); sb.append("</D:href></D:locktoken>"); sb.append("</D:activelock>"); sb.append("</D:lockdiscovery>"); sb.append("</D:prop>"); return XMLFormatter.toString(sb.toString()); }
@Override public String formattedString(String indent, boolean expandEmptyElements, boolean trimText) throws IOException { return XMLFormatter.toString(_node, indent, expandEmptyElements, trimText); }
@Override public String formattedString(String indent) throws IOException { return XMLFormatter.toString(_node, indent); }
protected int doProcess(WebDAVRequest webDAVRequest) throws Exception { WebDAVStorage storage = webDAVRequest.getWebDAVStorage(); if (!storage.isSupportsClassTwo()) { return HttpServletResponse.SC_METHOD_NOT_ALLOWED; } HttpServletRequest request = webDAVRequest.getHttpServletRequest(); HttpServletResponse response = webDAVRequest.getHttpServletResponse(); Lock lock = null; Status status = null; String lockUuid = webDAVRequest.getLockUuid(); long timeout = WebDAVUtil.getTimeout(request); if (Validator.isNull(lockUuid)) { // Create new lock String owner = null; String xml = new String(FileUtil.getBytes(request.getInputStream())); if (Validator.isNotNull(xml)) { if (_log.isDebugEnabled()) { _log.debug("Request XML\n" + XMLFormatter.toString(xml)); } Document document = SAXReaderUtil.read(xml); Element rootElement = document.getRootElement(); boolean exclusive = false; Element lockscopeElement = rootElement.element("lockscope"); for (Element element : lockscopeElement.elements()) { String name = GetterUtil.getString(element.getName()); if (name.equals("exclusive")) { exclusive = true; } } if (!exclusive) { return HttpServletResponse.SC_BAD_REQUEST; } Element ownerElement = rootElement.element("owner"); owner = ownerElement.getTextTrim(); if (Validator.isNull(owner)) { List<Element> hrefElements = ownerElement.elements("href"); for (Element hrefElement : hrefElements) { owner = "<D:href>" + hrefElement.getTextTrim() + "</D:href>"; } } } else { _log.error("Empty request XML"); return HttpServletResponse.SC_PRECONDITION_FAILED; } status = storage.lockResource(webDAVRequest, owner, timeout); lock = (Lock) status.getObject(); } else { try { // Refresh existing lock lock = storage.refreshResourceLock(webDAVRequest, lockUuid, timeout); status = new Status(HttpServletResponse.SC_OK); } catch (WebDAVException wde) { if (wde.getCause() instanceof NoSuchLockException) { return HttpServletResponse.SC_PRECONDITION_FAILED; } else { throw wde; } } } // Return lock details if (lock == null) { return status.getCode(); } long depth = WebDAVUtil.getDepth(request); String xml = getResponseXML(lock, depth); if (_log.isDebugEnabled()) { _log.debug("Response XML\n" + xml); } String lockToken = "<" + WebDAVUtil.TOKEN_PREFIX + lock.getUuid() + ">"; response.setContentType(ContentTypes.TEXT_XML_UTF8); response.setHeader("Lock-Token", lockToken); response.setStatus(status.getCode()); if (_log.isDebugEnabled()) { _log.debug("Returning lock token " + lockToken); } try { ServletResponseUtil.write(response, xml); } catch (Exception e) { if (_log.isWarnEnabled()) { _log.warn(e); } } return status.getCode(); }