| CODENOTIFIER | HelpYou are not signed inSign in |
Project: PEtALS
Revision: 8970
Author: rnaudin
Date: 04 Sep 2008 11:54:11
Changes:Refactor the handling of in messages in XQUARE
Files:| ... | ...@@ -33,7 +33,6 @@ | |
| 33 | 33 | import javax.naming.NamingException; |
| 34 | 34 | import javax.sql.DataSource; |
| 35 | 35 | import javax.xml.parsers.ParserConfigurationException; |
| 36 | import javax.xml.transform.Source; | |
| 37 | 36 | import javax.xml.transform.stream.StreamSource; |
| 38 | 37 | |
| 39 | 38 | import org.ow2.petals.bc.xquare.XQuareBC; |
| ... | ...@@ -44,9 +43,9 @@ | |
| 44 | 43 | import org.ow2.petals.component.framework.api.exception.PEtALSCDKException; |
| 45 | 44 | import org.ow2.petals.component.framework.api.message.Exchange; |
| 46 | 45 | import org.ow2.petals.component.framework.listener.AbstractJBIListener; |
| 47 | import org.ow2.petals.component.framework.util.SourceHelper; | |
| 48 | 46 | import org.ow2.petals.component.framework.util.XMLUtil; |
| 49 | 47 | import org.w3c.dom.Document; |
| 48 | import org.w3c.dom.Element; | |
| 50 | 49 | import org.w3c.dom.Node; |
| 51 | 50 | import org.xml.sax.SAXException; |
| 52 | 51 | import org.xquark.bridge.Mapping; |
| ... | ...@@ -265,10 +264,9 @@ | |
| 265 | 264 | |
| 266 | 265 | // FIXME: Use the document directly when Xquare would be DOM3 |
| 267 | 266 | // compatible |
| 268 | String document = SourceHelper.convertToString(exchange.getInMessageContentAsSource()); | |
| 269 | 267 | getLogger().finest(exchange.getExchangeId() + " - Insert document"); |
| 270 | 268 | Mapping mapping = ((XQuareBC) this.getComponent()).getXQBridgeMappings().get(bridge); |
| 271 | mapping.getMapper().insertDocument(document); | |
| 269 | mapping.getMapper().insertDocument(exchange.getInMessageContentAsString()); | |
| 272 | 270 | |
| 273 | 271 | // Create out "success" message |
| 274 | 272 | if (exchange.isInOutPattern() || exchange.isInOptionalOutPattern()) { |
| ... | ...@@ -300,14 +298,11 @@ | |
| 300 | 298 | throw new MessagingException("The MEP '" + exchange.getExchangePattern() |
| 301 | 299 | + "' is not supported for operation '" + QUERY_OPERATION + "'"); |
| 302 | 300 | } |
| 303 | String query = exchange.getInMessageContentAsString(); | |
| 304 | String queryString = XQuareUtils.removeXMLTopElement(query, QUERY_TAG); | |
| 305 | if (queryString == null) { | |
| 306 | throw new XQuareBCException("Query should be encapsulated in a " + QUERY_TAG | |
| 307 | + " element :" + query); | |
| 308 | } | |
| 309 | XMLConnection xc = null; | |
| 310 | 301 | |
| 302 | Document queryDocument = exchange.getInMessageContentAsDocument(); | |
| 303 | String queryString = XQuareUtils.getRootElementContent(queryDocument, QUERY_TAG); | |
| 304 | ||
| 305 | XMLConnection xc = null; | |
| 311 | 306 | try { |
| 312 | 307 | // Loading the XQuare bridge of the specified service unit |
| 313 | 308 | String ref = XQuareUtils.generateKey(provides); |
| ... | ...@@ -370,9 +365,15 @@ | |
| 370 | 365 | throw new MessagingException("The MEP '" + exchange.getExchangePattern() |
| 371 | 366 | + "' is not supported for operation '" + STORED_QUERY_OPERATION + "'"); |
| 372 | 367 | } |
| 368 | ||
| 373 | 369 | Document contentDocument = exchange.getInMessageContentAsDocument(); |
| 370 | Element queryElement = contentDocument.getDocumentElement(); | |
| 371 | if (!STORED_QUERY_TAG.equals(queryElement.getLocalName())) { | |
| 372 | throw new XQuareBCException("XQuery statement(s) must have a root element named '" | |
| 373 | + STORED_QUERY_TAG + "'"); | |
| 374 | } | |
| 375 | Node contentNode = queryElement.getFirstChild(); | |
| 374 | 376 | |
| 375 | Node contentNode = XQuareUtils.getContentNode(contentDocument, STORED_QUERY_TAG); | |
| 376 | 377 | // load from extensions the stored queries and their query |
| 377 | 378 | // template |
| 378 | 379 | StoredQuery storedQuery = getStoredQuery(contentNode); |
| ... | ...@@ -527,21 +528,21 @@ | |
| 527 | 528 | * @throws ParserConfigurationException |
| 528 | 529 | */ |
| 529 | 530 | protected void doExecuteSql(Exchange exchange) throws MessagingException, PEtALSCDKException, |
| 530 | SQLException, NamingException, ParserConfigurationException { | |
| 531 | SQLException, NamingException, ParserConfigurationException, XQuareBCException { | |
| 531 | 532 | |
| 532 | 533 | getLogger().finest(exchange.getExchangeId() + " - Start the sql operation"); |
| 533 | 534 | |
| 534 | 535 | Connection connection = null; |
| 535 | 536 | Statement statement = null; |
| 536 | 537 | |
| 537 | Source source = exchange.getInMessageContentAsSource(); | |
| 538 | String xmlDocument = SourceHelper.convertToString(source); | |
| 539 | String sql = XQuareUtils.removeXMLTopElement(xmlDocument, SQL_TAG); | |
| 538 | Document queryDocument = exchange.getInMessageContentAsDocument(); | |
| 539 | String sqlString = XQuareUtils.getRootElementContent(queryDocument, SQL_TAG); | |
| 540 | ||
| 540 | 541 | String[] sqls; |
| 541 | if (sql.contains(";")) { | |
| 542 | sqls = sql.split(";"); // TODO bug with sql like | |
| 542 | if (sqlString.contains(";")) { | |
| 543 | sqls = sqlString.split(";"); // TODO bug with sql like | |
| 543 | 544 | } else { |
| 544 | sqls = new String[] { sql }; | |
| 545 | sqls = new String[] { sqlString }; | |
| 545 | 546 | } |
| 546 | 547 | //' my;string' |
| 547 | 548 | DataSource ds = ((XQuareBC) this.getComponent()).getDataSources().getDataSource( |
| ... | ...@@ -33,13 +33,8 @@ | |
| 33 | 33 | import org.ow2.petals.component.framework.api.configuration.ConfigurationExtensions; |
| 34 | 34 | import org.ow2.petals.component.framework.jbidescriptor.generated.Consumes; |
| 35 | 35 | import org.ow2.petals.component.framework.jbidescriptor.generated.Provides; |
| 36 | import org.ow2.petals.component.framework.util.XMLUtil; | |
| 37 | import org.w3c.dom.CDATASection; | |
| 38 | 36 | import org.w3c.dom.Document; |
| 39 | 37 | import org.w3c.dom.Element; |
| 40 | import org.w3c.dom.Node; | |
| 41 | import org.w3c.dom.NodeList; | |
| 42 | import org.w3c.dom.Text; | |
| 43 | 38 | import org.xquark.xml.xdbc.XMLDBCException; |
| 44 | 39 | import org.xquark.xml.xdbc.XMLDocument; |
| 45 | 40 | import org.xquark.xml.xdbc.XMLResultSet; |
| ... | ...@@ -50,7 +45,7 @@ | |
| 50 | 45 | * @author Marc Dutoo - Open Wide |
| 51 | 46 | * @author Guillaume Decarnin - Open Wide |
| 52 | 47 | * @author Roland NAUDIN - EBM Websourcing |
| 53 | * @author alouis | |
| 48 | * @author alouis - EBM Websourcing | |
| 54 | 49 | */ |
| 55 | 50 | public class XQuareUtils { |
| 56 | 51 | |
| ... | ...@@ -66,87 +61,20 @@ | |
| 66 | 61 | } |
| 67 | 62 | } |
| 68 | 63 | }; |
| 69 | ||
| 64 | ||
| 70 | 65 | /** |
| 71 | * TODO MAY GO IN COMMONS Returns the given content as a String that must be | |
| 72 | * wrapped under a given top tag (checked). Helper method to get everything | |
| 73 | * there is under a given top tag as a String. XML nodes will be formatted | |
| 74 | * and included within. NB. For XQuare BC, the point is to allow users to | |
| 75 | * write results formatting in XQuery code without having to XML-encode the | |
| 76 | * tags. | |
| 66 | * Get the content of the root element and control its local name. | |
| 77 | 67 | * |
| 78 | * @param contentDocument | |
| 79 | * the content of an incoming JBI message | |
| 80 | * @param contentTag | |
| 81 | * the root tag expected | |
| 82 | * @return a String. NB. For XQuare BC, an XQuery. | |
| 83 | * @throws XQuareBCException | |
| 84 | * if bad top content tag | |
| 68 | * @param document | |
| 69 | * @throws XQuareBCException The root element is not correct | |
| 85 | 70 | */ |
| 86 | public static String getContentString(Document contentDocument, String contentTag) | |
| 87 | throws XQuareBCException { | |
| 88 | try { | |
| 89 | Node contentNode = XMLUtil.getFirstChild(contentDocument); | |
| 90 | // Checking that node element tag is the required one | |
| 91 | if (!contentTag.equals(contentNode.getLocalName())) { | |
| 92 | throw new XQuareBCException("XML XQuare queries should be " | |
| 93 | + "encapsulated in a top <\"" + contentTag + "\"> tag !"); | |
| 94 | } | |
| 95 | // Now reading the XQuery code in the most "nice" way | |
| 96 | NodeList queryNodes = contentNode.getChildNodes(); | |
| 97 | int queryNodeNb = queryNodes.getLength(); | |
| 98 | if (queryNodeNb == 1) { | |
| 99 | Node singleQueryNode = queryNodes.item(0); | |
| 100 | if (singleQueryNode instanceof CDATASection) { | |
| 101 | return ((CDATASection) singleQueryNode).getData(); | |
| 102 | } | |
| 103 | if (singleQueryNode instanceof Text) { | |
| 104 | return ((Text) singleQueryNode).getTextContent(); | |
| 105 | } | |
| 106 | } | |
| 107 | StringBuilder contentBuf = new StringBuilder(); | |
| 108 | for (int i = 0; i < queryNodeNb; i++) { | |
| 109 | Node queryNode = queryNodes.item(i); | |
| 110 | // getting the full XML of the node in order to help the user | |
| 111 | // pass | |
| 112 | // along Results XML formatting | |
| 113 | contentBuf.append(XMLUtil.createStringFromDOMNode(queryNode)); | |
| 114 | contentBuf.append("\n"); | |
| 115 | } | |
| 116 | return contentBuf.toString(); | |
| 117 | } catch (XQuareBCException e) { | |
| 118 | throw e; | |
| 119 | } catch (Exception e) { | |
| 120 | throw new XQuareBCException("Error while building content String from IN message : ", e); | |
| 121 | } | |
| 122 | } | |
| 123 | ||
| 124 | /** | |
| 125 | * TODO MAY GO IN COMMONS Returns the given content as a DOM node that must | |
| 126 | * be wrapped under a given top tag (checked). | |
| 127 | * | |
| 128 | * @param contentDocument | |
| 129 | * the content of an incoming JBI message | |
| 130 | * @param contentTag | |
| 131 | * the root tag to match | |
| 132 | * @return the corresponding DOM node tree | |
| 133 | * @throws XQuareBCException | |
| 134 | * if bad top tag | |
| 135 | */ | |
| 136 | public static Node getContentNode(Document contentDocument, String contentTag) | |
| 137 | throws XQuareBCException { | |
| 138 | try { | |
| 139 | Node rootNode = XMLUtil.getFirstChild(contentDocument); | |
| 140 | // Checking that node element tag is the required one | |
| 141 | if (!contentTag.equals(rootNode.getLocalName())) { | |
| 142 | throw new XQuareBCException("XML XQuare queries should be " | |
| 143 | + "encapsulated in a top <\"" + contentTag + "\"> tag !"); | |
| 144 | } | |
| 145 | return rootNode; | |
| 146 | } catch (Exception e) { | |
| 147 | throw new XQuareBCException("XML XQuare queries should be " | |
| 148 | + "encapsulated in a top \"" + contentTag + "\"> tag !" + e.getMessage(), e); | |
| 71 | public static final String getRootElementContent(Document document, String rootElementName) throws XQuareBCException { | |
| 72 | Element rootElement = document.getDocumentElement(); | |
| 73 | if (!rootElementName.equals(rootElement.getLocalName())) { | |
| 74 | throw new XQuareBCException("XQuery statement(s) must have a root element named '" | |
| 75 | + rootElementName + "'"); | |
| 149 | 76 | } |
| 77 | return rootElement.getTextContent(); | |
| 150 | 78 | } |
| 151 | 79 | |
| 152 | 80 | /** |
| ... | ...@@ -213,43 +141,6 @@ | |
| 213 | 141 | } |
| 214 | 142 | |
| 215 | 143 | /** |
| 216 | * Remove the xml top element of the given xml document. String methods | |
| 217 | * usage, may not be good for large documents | |
| 218 | * | |
| 219 | * @param xmlDocument | |
| 220 | * @param topElementName | |
| 221 | * the top element, namespace is not needed | |
| 222 | * @return | |
| 223 | */ | |
| 224 | public static String removeXMLTopElement(String xmlDocument, String topElementName) { | |
| 225 | String result = null; | |
| 226 | if (xmlDocument != null && topElementName != null) { | |
| 227 | // 1 find the beginning of the sub document (assume that namespace | |
| 228 | // can appear) | |
| 229 | int topElementStartIndex = xmlDocument.indexOf(topElementName); | |
| 230 | if (topElementStartIndex >= 0) { | |
| 231 | String next = xmlDocument.substring(topElementStartIndex, xmlDocument.length()); | |
| 232 | int startIndex = next.indexOf('>'); | |
| 233 | // 2 check the top element | |
| 234 | if (startIndex >= 0) { | |
| 235 | next = next.substring(startIndex + 1, next.length()); | |
| 236 | // 3 find the end of the sub document(assume that namespace | |
| 237 | // can appear) | |
| 238 | int topElementEndIndex = next.lastIndexOf(topElementName); | |
| 239 | if (topElementEndIndex > 0) { | |
| 240 | next = next.substring(0, topElementEndIndex); | |
| 241 | int endIndex = next.lastIndexOf('<'); | |
| 242 | if (endIndex > 0) { | |
| 243 | result = next.substring(0, endIndex); | |
| 244 | } | |
| 245 | } | |
| 246 | } | |
| 247 | } | |
| 248 | } | |
| 249 | return result; | |
| 250 | } | |
| 251 | ||
| 252 | /** | |
| 253 | 144 | * Return a string that is used for bridge and mapping hashMap storage. |
| 254 | 145 | * Based on itf, srv, and ep |
| 255 | 146 | * |