@Override protected void doStart() throws Exception { setRootName("xcap"); _validate = _validateOnGet = false; _processors = new HashMap<String, XcapResourceProcessor>(); createIetfProcessors(); createOmaProcessors(); _dao.init(_processors.values()); initXcapCaps(); }
private void doDelete(HttpServletRequest request, XcapResourceImpl resource) throws XcapException, SAXException, VerifierConfigurationException { throwExceptionIfNamespace(resource); _dao.delete(resource); if (!resource.isAllDocument()) { if (_validate) { XcapUtil.validate(resource.getDocument().getDom(), resource.getProcessor().getXsdSchema()); } validateSpecificAppResource(resource); } }
private void doPut(HttpServletRequest request, XcapResourceImpl resource) throws IOException, XcapException, SAXException, VerifierConfigurationException { throwExceptionIfNamespace(resource); String content = getContent(request, resource); _dao.update(resource, content); if (_validate) XcapUtil.validate(resource.getDocument().getDom(), resource.getProcessor().getXsdSchema()); validateSpecificAppResource(resource); }
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 initXcapCaps() throws XcapException, IOException { StringBuilder sb = new StringBuilder(); sb.append("<?xml version=\"1.0\"?>\n"); sb.append("<xcap-caps xmlns=\"urn:ietf:params:xml:ns:xcap-caps\"\n"); sb.append(" xmlns:xsi=\"htt//www.w3.org/2001/XMLSchema-instance\"\n"); sb.append(" xsi:schemaLocation=\"urn:ietf:params:xml:ns:xcap-caps xcap-caps.xsd\">\n"); sb.append("<auids>\n"); for (XcapResourceProcessor processor : _processors.values()) sb.append("\t<auid>").append(processor.getAuid()).append("</auid>\n"); sb.append("</auids>\n"); sb.append("<namespaces>\n"); for (XcapResourceProcessor processor : _processors.values()) { for (String namespace : processor.getNamespaceContext().values()) sb.append("\t<namespace>").append(namespace).append("</namespace>\n"); } sb.append("</namespaces>\n"); sb.append("</xcap-caps>"); XcapResourceImpl resource = getResource(new XcapUri("xcap-caps/global/index", ""), true, "", null); _dao.update(resource, sb.toString()); _dao.save(resource); }
public void service(HttpServletRequest request, HttpServletResponse response) throws IOException, SAXException, VerifierConfigurationException { String method = request.getMethod(); String requestUri = request.getRequestURI(); StringBuffer requestUrl = request.getRequestURL(); String head = requestUrl.substring(0, requestUrl.indexOf(requestUri)); try { if ("POST".equals(method)) { throw new XcapException(HttpServletResponse.SC_METHOD_NOT_ALLOWED); } Map<String, String> context = getXpointer(request); XcapUri xcapUri = new XcapUri(requestUri, _rootName); XcapResourceImpl resource = getResource(xcapUri, PUT.equals(method), head, context); ifMatchConditionalProcessing(request, resource); ifNoneMatchConditionalProcessing(request, resource); resource.setAction(method); if (method.equals(GET)) doGet(response, resource); else if (method.equals(PUT)) doPut(request, resource); else if (method.equals(DELETE)) doDelete(request, resource); if (method.equals(PUT) || method.equals(DELETE)) { checkIfSameNodeSelected(resource); resource.getProcessor().processResource(resource); _dao.save(resource); notifyResourceChanged(resource); } String newEtag = getEtag(resource); resource.setNewEtag(newEtag); response.setHeader(Constants.ETAG, newEtag); response.setStatus(HttpServletResponse.SC_OK); if (_log.isDebugEnabled()) _log.debug(method + " " + requestUri + " sucessful"); } catch (XcapException e) { if (e.shouldShowStackTrace()) _log.info("Unable to process " + method + " " + requestUri, e); else _log.info("Unable to process " + method + " " + requestUri + ": " + e.getMessage()); e.sendResponse(response); } }
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; } }
private void locatingParent(XcapUri uri, String requestUrlHead) throws XcapException { // See XCAP 8.2.1. Locating the Parent String ancestor = _dao.getFirstExistAncestor(uri); if (uri.hasNodeSeparator() || uri.getDocumentSelector().substring(ancestor.length() + 1).indexOf('/') != -1) { XcapException e = new XcapException("parent does not exist", HttpServletResponse.SC_CONFLICT); StringBuffer sb = new StringBuffer(); sb.append(XcapException.XCAP_ERROR_HEADER); sb.append("<no-parent><ancestor>"); String url = requestUrlHead + _rootName + ancestor; sb.append(RequestUtil.filter(url)); sb.append("</ancestor></no-parent>"); sb.append(XcapException.XCAP_ERROR_FOOTER); e.setContent(XcapException.XCAP_ERROR_CONTENT_TYPE, sb.toString().getBytes()); throw e; } }
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; }