📄 multistatusresponse.java
字号:
for (int i = 0; iter.hasNext(); i++) { Integer statusKey = (Integer) iter.next(); sts[i] = new Status(statusKey.intValue()); } } else { sts = new Status[] {status}; } return sts; } /** * @param document * @see org.apache.jackrabbit.webdav.xml.XmlSerializable#toXml(org.w3c.dom.Document) */ public Element toXml(Document document) { Element response = DomUtil.createElement(document, XML_RESPONSE, NAMESPACE); // add '<href>' response.appendChild(DomUtil.hrefToXml(getHref(), document)); if (type == TYPE_PROPSTAT) { // add '<propstat>' elements Iterator iter = statusMap.keySet().iterator(); while (iter.hasNext()) { Integer statusKey = (Integer) iter.next(); Status st = new Status(statusKey.intValue()); PropContainer propCont = (PropContainer) statusMap.get(statusKey); Element propstat = DomUtil.createElement(document, XML_PROPSTAT, NAMESPACE); propstat.appendChild(propCont.toXml(document)); propstat.appendChild(st.toXml(document)); response.appendChild(propstat); } } else { // add a single '<status>' element // NOTE: a href+status response cannot be created with 'null' status response.appendChild(status.toXml(document)); } // add the optional '<responsedescription>' element String description = getResponseDescription(); if (description != null) { Element desc = DomUtil.createElement(document, XML_RESPONSEDESCRIPTION, NAMESPACE); DomUtil.setText(desc, description); response.appendChild(desc); } return response; } //----------------------------------------------< type specific methods >--- /** * Adds a property to this response '200' propstat set. * * @param property the property to add */ public void add(DavProperty property) { checkType(TYPE_PROPSTAT); PropContainer status200 = getPropContainer(DavServletResponse.SC_OK, false); status200.addContent(property); } /** * Adds a property name to this response '200' propstat set. * * @param propertyName the property name to add */ public void add(DavPropertyName propertyName) { checkType(TYPE_PROPSTAT); PropContainer status200 = getPropContainer(DavServletResponse.SC_OK, true); status200.addContent(propertyName); } /** * Adds a property to this response * * @param property the property to add * @param status the status of the response set to select */ public void add(DavProperty property, int status) { checkType(TYPE_PROPSTAT); PropContainer propCont = getPropContainer(status, false); propCont.addContent(property); } /** * Adds a property name to this response * * @param propertyName the property name to add * @param status the status of the response set to select */ public void add(DavPropertyName propertyName, int status) { checkType(TYPE_PROPSTAT); PropContainer propCont = getPropContainer(status, true); propCont.addContent(propertyName); } /** * @param status * @return */ private PropContainer getPropContainer(int status, boolean forNames) { Integer statusKey = new Integer(status); PropContainer propContainer; Object entry = statusMap.get(statusKey); if (entry == null) { if (forNames) { propContainer = new DavPropertyNameSet(); } else { propContainer = new DavPropertySet(); } statusMap.put(statusKey, propContainer); } else { propContainer = (PropContainer) entry; } return propContainer; } private void checkType(int type) { if (this.type != type) { throw new IllegalStateException("The given MultiStatusResponse is not of the required type."); } } /** * Get properties present in this response for the given status code. In * case this MultiStatusResponse does not represent a 'propstat' response, * always an empty {@link DavPropertySet} will be returned. * * @param status * @return property set */ public DavPropertySet getProperties(int status) { Integer key = new Integer(status); if (statusMap.containsKey(key)) { Object mapEntry = statusMap.get(key); if (mapEntry != null && mapEntry instanceof DavPropertySet) { return (DavPropertySet) mapEntry; } } return new DavPropertySet(); } /** * Get property names present in this response for the given status code. In * case this MultiStatusResponse does not represent a 'propstat' response, * always an empty {@link DavPropertyNameSet} will be returned. * * @param status * @return property names */ public DavPropertyNameSet getPropertyNames(int status) { Integer key = new Integer(status); if (statusMap.containsKey(key)) { Object mapEntry = statusMap.get(key); if (mapEntry != null) { if (mapEntry instanceof DavPropertySet) { DavPropertyNameSet set = new DavPropertyNameSet(); DavPropertyName[] names = ((DavPropertySet) mapEntry).getPropertyNames(); for (int i = 0; i < names.length; i++) { set.add(names[i]); } return set; } else { // is alread a DavPropertyNameSet return (DavPropertyNameSet) mapEntry; } } } return new DavPropertyNameSet(); } /** * Build a new response object from the given xml element. * * @param responseElement * @return new <code>MultiStatusResponse</code> instance * @throws IllegalArgumentException if the specified element is * <code>null</code> or not a DAV:response element or if the mandatory * DAV:href child is missing. */ public static MultiStatusResponse createFromXml(Element responseElement) { if (!DomUtil.matches(responseElement, XML_RESPONSE, NAMESPACE)) { throw new IllegalArgumentException("DAV:response element required."); } String href = DomUtil.getChildTextTrim(responseElement, XML_HREF, NAMESPACE); if (href == null) { throw new IllegalArgumentException("DAV:response element must contain a DAV:href element expected."); } String statusLine = DomUtil.getChildText(responseElement, XML_STATUS, NAMESPACE); String responseDescription = DomUtil.getChildText(responseElement, XML_RESPONSEDESCRIPTION, NAMESPACE); MultiStatusResponse response; if (statusLine != null) { Status status = Status.parse(statusLine); response = new MultiStatusResponse(href, status, responseDescription); } else { response = new MultiStatusResponse(href, responseDescription, TYPE_PROPSTAT); // read propstat elements ElementIterator it = DomUtil.getChildren(responseElement, XML_PROPSTAT, NAMESPACE); while (it.hasNext()) { Element propstat = it.nextElement(); String propstatus = DomUtil.getChildText(propstat, XML_STATUS, NAMESPACE); Element prop = DomUtil.getChildElement(propstat, XML_PROP, NAMESPACE); if (propstatus != null && prop != null) { int statusCode = Status.parse(propstatus).getStatusCode(); ElementIterator propIt = DomUtil.getChildren(prop); while (propIt.hasNext()) { Element el = propIt.nextElement(); /* always build dav property from the given element, since destinction between prop-names and properties not having a value is not possible. retrieval of the set of 'property names' is possible from the given prop-set by calling DavPropertySet#getPropertyNameSet() */ DavProperty property = DefaultDavProperty.createFromXml(el); response.add(property, statusCode); } } } } return response; } /** * @param href * @return false if the given href is <code>null</code> or empty string. */ private static boolean isValidHref(String href) { return href != null && !"".equals(href); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -