private String getFirstExistNodeAncestor(XcapResource resource, String nodeSelector) throws XcapException { XmlResource xmlResource = _dao.getNode(resource, nodeSelector); if (xmlResource != null) return nodeSelector; else { int index = nodeSelector.lastIndexOf('/'); if (index != -1) { String parent = nodeSelector.substring(0, index); return getFirstExistNodeAncestor(resource, parent); } else return null; } }
private void checkIfSameNodeSelected(XcapResourceImpl resource) throws XcapException { if (!resource.getXcapUri().hasNodeSeparator() || resource.isCreation()) return; try { // boolean match = // resource.getSelectedNode().matches(resource.getNodeSelector()); XmlResource xmlResource = _dao.getNode(resource); boolean valid = false; if (xmlResource == null) { valid = resource.getAction().equals(DELETE); } else { Node node = xmlResource.getDom(); valid = node.equals(resource.getSelectedResource().getDom()) && resource.getAction().equals(PUT); } if (!valid) { XcapException e1 = new XcapException( "Request no more select the same node", HttpServletResponse.SC_CONFLICT); StringBuffer sb = new StringBuffer(); sb.append(XcapException.XCAP_ERROR_HEADER); sb.append("<cannot-insert/>"); sb.append(XcapException.XCAP_ERROR_FOOTER); e1.setContent(XcapException.XCAP_ERROR_CONTENT_TYPE, sb.toString().getBytes()); throw e1; } } catch (XcapException e) { throw e; } catch (Throwable e) { XcapException e1 = new XcapException( "Cannot check if select the same node", HttpServletResponse.SC_CONFLICT, e); StringBuffer sb = new StringBuffer(); sb.append(XcapException.XCAP_ERROR_HEADER); sb.append("<cannot-insert/>"); sb.append(XcapException.XCAP_ERROR_FOOTER); e1.setContent(XcapException.XCAP_ERROR_CONTENT_TYPE, sb.toString().getBytes()); throw e1; } }
public XcapResourceImpl getResource( XcapUri xcapUri, boolean isPut, String requestUrlHead, Map<String, String> requestNamespaceContext) throws XcapException { XcapResourceProcessor processor = _processors.get(xcapUri.getAuid()); if (processor == null) throw new XcapException( "Not supported auid: " + xcapUri.getAuid() + " in URI: " + xcapUri, HttpServletResponse.SC_NOT_FOUND); Document document = null; XcapResourceImpl resource = new XcapResourceImpl(); resource.setXcapUri(xcapUri); resource.setProcessor(processor); XmlResource xmlResource = _dao.getDocument(xcapUri, isPut && !xcapUri.hasNodeSeparator()); if (xmlResource == null) { if (isPut) { locatingParent(xcapUri, requestUrlHead); resource.setCreation(true); return resource; } else { XcapException e = new XcapException( "Resource: " + xcapUri.getDocumentSelector() + " not found", HttpServletResponse.SC_NOT_FOUND); e.setLevel(Level.INFO); throw e; } } resource.setDocument(xmlResource); // TODO check rootDirectory is in parent dir. // TODO authenticate & authorization if (_validateOnGet) { try { XcapUtil.validate(document, processor.getXsdSchema()); } catch (XcapException e) { _log.warn("Unable to validated document:" + e.getMessage(), e); } } if (resource.isAllDocument()) return resource; String nodeSelector = XcapUtil.insertDefaultNamespace( xcapUri.getNodeSelector(), processor.getDefaultNamespacePrefix()); if (_log.isDebugEnabled()) _log.debug("select node " + nodeSelector + " in " + xcapUri.getDocumentSelector()); if (requestNamespaceContext == null) requestNamespaceContext = new HashMap<String, String>(); requestNamespaceContext.putAll(processor.getNamespaceContext()); resource.setNamespaceContext(requestNamespaceContext); XmlResource xmlResource2 = _dao.getNode(resource, nodeSelector); if (xmlResource2 == null) { if (isPut) { // XCAP 8.2.1. Locating the Parent String parent = locatingParent(resource, nodeSelector, xcapUri.getDocumentSelector(), requestUrlHead); resource.setCreation(true); NodeType nodeType; String nodeName = nodeSelector.substring(parent.length()); // /@id is an attribute and /service[@id="1"] is an // element if (nodeName.indexOf('@') != -1 && nodeName.indexOf('[') == -1) { nodeType = NodeType.ATTRIBUTE; nodeName = nodeName.substring(nodeName.indexOf('@') + 1); } else nodeType = NodeType.ELEMENT; resource.setParent(parent, nodeType, nodeName); } else { XcapException e = new XcapException( "Resource: " + xcapUri + " not found (no node selected)", HttpServletResponse.SC_NOT_FOUND); e.setLevel(Level.INFO); throw e; } } else { resource.setCreation(false); resource.setSelectedResource(xmlResource2); } return resource; }