public Tuple service(String method, String path, Map<String, String> headers, byte[] data) { WebDAVStorage webDAVStorage = new DLWebDAVStorageImpl(); webDAVStorage.setToken("document_library"); WebDAVUtil.addStorage(webDAVStorage); WebDAVServlet webDAVServlet = new WebDAVServlet(); String requestURI = _CONTEXT_PATH + _SERVLET_PATH + _PATH_INFO_PREFACE + path; MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest(method, requestURI); mockHttpServletRequest.setContextPath(_CONTEXT_PATH); mockHttpServletRequest.setServletPath(_SERVLET_PATH); mockHttpServletRequest.setPathInfo(_PATH_INFO_PREFACE + path); try { mockHttpServletRequest.setRemoteUser(String.valueOf(TestPropsValues.getUserId())); } catch (Exception e) { Assert.fail("User ID cannot be initialized"); } if (headers == null) { headers = new HashMap<String, String>(); } headers.put(HttpHeaders.USER_AGENT, getUserAgent()); try { throw new Exception(); } catch (Exception e) { StackTraceElement[] stackTraceElements = e.getStackTrace(); for (StackTraceElement stackTraceElement : stackTraceElements) { String methodName = stackTraceElement.getMethodName(); if (methodName.equals("setUp") || methodName.equals("tearDown") || methodName.startsWith("test")) { String testName = StringUtil.extractLast(stackTraceElement.getClassName(), CharPool.PERIOD); testName = StringUtil.replace(testName, new String[] {"WebDAV", "Test"}, new String[] {"", ""}); headers.put( "X-Litmus", testName + ": (" + stackTraceElement.getMethodName() + ":" + stackTraceElement.getLineNumber() + ")"); break; } } } if (data != null) { mockHttpServletRequest.setContent(data); String contentType = headers.remove(HttpHeaders.CONTENT_TYPE); if (contentType != null) { mockHttpServletRequest.setContentType(contentType); } else { mockHttpServletRequest.setContentType(ContentTypes.TEXT_PLAIN); } } for (Map.Entry<String, String> entry : headers.entrySet()) { String key = entry.getKey(); String value = entry.getValue(); mockHttpServletRequest.addHeader(key, value); } try { MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse(); webDAVServlet.service(mockHttpServletRequest, mockHttpServletResponse); int statusCode = mockHttpServletResponse.getStatus(); byte[] responseBody = mockHttpServletResponse.getContentAsByteArray(); Map<String, String> responseHeaders = new HashMap<String, String>(); for (String name : mockHttpServletResponse.getHeaderNames()) { responseHeaders.put(name, (String) mockHttpServletResponse.getHeader(name)); } return new Tuple(statusCode, responseBody, responseHeaders); } catch (Exception e) { e.printStackTrace(); } return null; }
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(); }