/** * Validates that an "update" (add, commit or optimize) results in success. * * :TODO: currently only deals with one add/doc at a time, this will need changed if/when SOLR-2 is resolved * * @param xml The XML of the update * @return null if successful, otherwise the XML response to the update */ public String validateUpdate(String xml) throws SAXException { return checkUpdateStatus(xml, "0"); }
/** * Helper that returns an <optimize> String with * optional key/val pairs. * * @param args 0 and Even numbered args are params, Odd numbered args are values. */ public static String optimize(String... args) { return simpleTag("optimize", args); }
/** * Validates that an "update" (add, commit or optimize) results in success. * * :TODO: currently only deals with one add/doc at a time, this will need changed if/when SOLR-2 is resolved * * @param xml The XML of the update * @return null if successful, otherwise the XML response to the update */ public String checkUpdateStatus(String xml, String code) throws SAXException { try { String res = update(xml); String valid = validateXPath(res, "//int[@name='status']="+code ); return (null == valid) ? null : res; } catch (XPathExpressionException e) { throw new RuntimeException ("?!? static xpath has bug?", e); } } }
public static Object evaluateXPath(String xml, String xpath, QName returnType) throws XPathExpressionException, SAXException { if (null == xpath) return null; Document document = null; try { document = getXmlDocumentBuilder().parse(new ByteArrayInputStream (xml.getBytes(StandardCharsets.UTF_8))); } catch (UnsupportedEncodingException e1) { throw new RuntimeException("Totally weird UTF-8 exception", e1); } catch (IOException e2) { throw new RuntimeException("Totally weird io exception", e2); } xpath = xpath.trim(); return getXpath().evaluate(xpath.trim(), document, returnType); }
/** * Generates a delete by id xml string * @param id ID that has not already been xml escaped * @param args The attributes of the delete tag */ public static String deleteById(String id, String... args) { try { StringWriter r = new StringWriter(); XML.writeXML(r, "id", id); return delete(r.getBuffer().toString(), args); } catch(IOException e) { throw new RuntimeException ("this should never happen with a StringWriter", e); } }
/** * A helper method which validates a String against an array of XPath test * strings. * * @param xml The xml String to validate * @param tests Array of XPath strings to test (in boolean mode) on the xml * @return null if all good, otherwise the first test that fails. */ public static String validateXPath(String xml, String... tests) throws XPathExpressionException, SAXException { if (tests==null || tests.length == 0) return null; Document document = null; try { document = getXmlDocumentBuilder().parse(new ByteArrayInputStream (xml.getBytes(StandardCharsets.UTF_8))); } catch (UnsupportedEncodingException e1) { throw new RuntimeException("Totally weird UTF-8 exception", e1); } catch (IOException e2) { throw new RuntimeException("Totally weird io exception", e2); } for (String xp : tests) { xp=xp.trim(); Boolean bool = (Boolean) getXpath().evaluate(xp, document, XPathConstants.BOOLEAN); if (!bool) { return xp; } } return null; }
/** * Generates a delete by query xml string * @param q Query that has not already been xml escaped * @param args The attributes of the delete tag */ public static String deleteByQuery(String q, String... args) { try { StringWriter r = new StringWriter(); XML.writeXML(r, "query", q); return delete(r.getBuffer().toString(), args); } catch(IOException e) { throw new RuntimeException ("this should never happen with a StringWriter", e); } }
/** * Helper that returns an <commit> String with * optional key/val pairs. * * @param args 0 and Even numbered args are params, Odd numbered args are values. */ public static String commit(String... args) { return simpleTag("commit", args); }
/** * Validates that an "update" (add, commit or optimize) results in success. * * :TODO: currently only deals with one add/doc at a time, this will need changed if/when SOLR-2 is resolved * * @param xml The XML of the update * @return null if successful, otherwise the XML response to the update */ public String validateErrorUpdate(String xml) throws SAXException { try { return checkUpdateStatus(xml, "1"); } catch (SolrException e) { // return ((SolrException)e).getMessage(); return null; // success } }