Search in sources :

Example 11 with LogbookException

use of org.phoebus.logbook.LogbookException in project phoebus by ControlSystemStudio.

the class ElogApi method getTypes.

/**
 * Get list of available types
 *
 * @return Collection<String>
 */
public Collection<String> getTypes() throws LogbookException {
    Map<String, Object> cookies = new HashMap<>();
    cookies.put("unm", this.username);
    cookies.put("upwd", this.password);
    Response<String> resp = Requests.get(this.url + "?cmd=new").cookies(cookies).followRedirect(false).verify(false).send().toTextResponse();
    // validateResponse( resp );
    // validateResponse does not work here, as the response body contains
    // "form name=form1"....
    int statuscode = resp.statusCode();
    if (statuscode != 200 && statuscode != 302) {
        final Pattern pattern = Pattern.compile("<td.*?class=\"errormsg\".*?>(.*?)</td>");
        Matcher m = pattern.matcher(resp.body());
        if (m.matches()) {
            String err = m.group();
            throw new LogbookException("Rejected because of: " + err);
        } else {
            throw new LogbookException("Rejected because of unknown error.");
        }
    } else {
        String location = resp.getHeader("Location");
        if (location != null && !location.isEmpty()) {
            if (location.contains("has moved")) {
                throw new LogbookException("Logbook server has moved to another location.");
            } else if (location.contains("fail")) {
                throw new LogbookException("Failed to submit log entry, invalid credentials.");
            }
        }
    }
    List<String> types = new ArrayList<>();
    try {
        TagNode tagNode = new HtmlCleaner().clean(resp.body());
        org.w3c.dom.Document doc = new DomSerializer(new CleanerProperties()).createDOM(tagNode);
        XPath xpath = XPathFactory.newInstance().newXPath();
        NodeList typenodes = (NodeList) xpath.evaluate("//tr/td[@class=\"attribvalue\"]/select[@name=\"Type\"]/option/@value", doc, XPathConstants.NODESET);
        for (int i = 0; i < typenodes.getLength(); i++) {
            String t = typenodes.item(i).getNodeValue();
            if (!t.isEmpty()) {
                types.add(t);
            }
        }
    } catch (XPathExpressionException | ParserConfigurationException e) {
        throw new LogbookException("could not parse the elog response", e);
    }
    return types;
}
Also used : XPath(javax.xml.xpath.XPath) Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) XPathExpressionException(javax.xml.xpath.XPathExpressionException) NodeList(org.w3c.dom.NodeList) HtmlCleaner(org.htmlcleaner.HtmlCleaner) DomSerializer(org.htmlcleaner.DomSerializer) CleanerProperties(org.htmlcleaner.CleanerProperties) LogbookException(org.phoebus.logbook.LogbookException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) TagNode(org.htmlcleaner.TagNode)

Aggregations

LogbookException (org.phoebus.logbook.LogbookException)11 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)4 XPath (javax.xml.xpath.XPath)4 XPathExpressionException (javax.xml.xpath.XPathExpressionException)4 CleanerProperties (org.htmlcleaner.CleanerProperties)4 DomSerializer (org.htmlcleaner.DomSerializer)4 HtmlCleaner (org.htmlcleaner.HtmlCleaner)4 TagNode (org.htmlcleaner.TagNode)4 NodeList (org.w3c.dom.NodeList)4 Matcher (java.util.regex.Matcher)3 Pattern (java.util.regex.Pattern)3 Property (org.phoebus.logbook.Property)3 OlogLog (org.phoebus.olog.es.api.model.OlogLog)3 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 FXML (javafx.fxml.FXML)2 Test (org.junit.Test)2 LogClient (org.phoebus.logbook.LogClient)2 LogEntry (org.phoebus.logbook.LogEntry)2 ClientHandlerException (com.sun.jersey.api.client.ClientHandlerException)1