Search in sources :

Example 1 with LogbookException

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

the class OlogClient method save.

/**
 * Calls the back-end service to persist the log entry.
 *
 * @param log       The log entry to save.
 * @param inReplyTo If non-null, this save operation will treat the <code>log</code> parameter as a reply to
 *                  the log entry represented by <code>inReplyTo</code>.
 * @return The saved log entry.
 * @throws LogbookException E.g. due to invalid log entry data.
 */
private LogEntry save(LogEntry log, LogEntry inReplyTo) throws LogbookException {
    ClientResponse clientResponse;
    try {
        MultivaluedMap<String, String> queryParams = new MultivaluedMapImpl();
        queryParams.putSingle("markup", "commonmark");
        if (inReplyTo != null) {
            queryParams.putSingle("inReplyTo", Long.toString(inReplyTo.getId()));
        }
        clientResponse = service.path("logs").queryParams(queryParams).type(MediaType.APPLICATION_JSON).header(OLOG_CLIENT_INFO_HEADER, CLIENT_INFO).accept(MediaType.APPLICATION_XML).accept(MediaType.APPLICATION_JSON).put(ClientResponse.class, OlogObjectMappers.logEntrySerializer.writeValueAsString(log));
        if (clientResponse.getStatus() < 300) {
            OlogLog createdLog = OlogObjectMappers.logEntryDeserializer.readValue(clientResponse.getEntityInputStream(), OlogLog.class);
            log.getAttachments().stream().forEach(attachment -> {
                FormDataMultiPart form = new FormDataMultiPart();
                // Add id only if it is set, otherwise Jersey will complain and cause the submission to fail.
                if (attachment.getId() != null && !attachment.getId().isEmpty()) {
                    form.bodyPart(new FormDataBodyPart("id", attachment.getId()));
                }
                form.bodyPart(new FileDataBodyPart("file", attachment.getFile()));
                form.bodyPart(new FormDataBodyPart("filename", attachment.getName()));
                form.bodyPart(new FormDataBodyPart("fileMetadataDescription", attachment.getContentType()));
                ClientResponse attachmentResponse = service.path("logs").path("attachments").path(String.valueOf(createdLog.getId())).type(MediaType.MULTIPART_FORM_DATA).accept(MediaType.APPLICATION_XML).accept(MediaType.APPLICATION_JSON).post(ClientResponse.class, form);
                if (attachmentResponse.getStatus() > 300) {
                    // TODO failed to add attachments
                    logger.log(Level.SEVERE, "Failed to submit attachment(s), HTTP status: " + attachmentResponse.getStatus());
                }
            });
            clientResponse = service.path("logs").path(String.valueOf(createdLog.getId())).type(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
            return OlogObjectMappers.logEntryDeserializer.readValue(clientResponse.getEntityInputStream(), OlogLog.class);
        } else if (clientResponse.getStatus() == 401) {
            logger.log(Level.SEVERE, "Submission of log entry returned HTTP status, invalid credentials");
            throw new LogbookException(Messages.SubmissionFailedInvalidCredentials);
        } else {
            logger.log(Level.SEVERE, "Submission of log entry returned HTTP status" + clientResponse.getStatus());
            throw new LogbookException(MessageFormat.format(Messages.SubmissionFailedWithHttpStatus, clientResponse.getStatus()));
        }
    } catch (UniformInterfaceException | ClientHandlerException | IOException e) {
        logger.log(Level.SEVERE, "Failed to submit log entry, got client exception", e);
        throw new LogbookException(e);
    }
}
Also used : ClientResponse(com.sun.jersey.api.client.ClientResponse) ClientHandlerException(com.sun.jersey.api.client.ClientHandlerException) MultivaluedMapImpl(com.sun.jersey.core.util.MultivaluedMapImpl) IOException(java.io.IOException) OlogLog(org.phoebus.olog.es.api.model.OlogLog) UniformInterfaceException(com.sun.jersey.api.client.UniformInterfaceException) FormDataBodyPart(com.sun.jersey.multipart.FormDataBodyPart) FormDataMultiPart(com.sun.jersey.multipart.FormDataMultiPart) LogbookException(org.phoebus.logbook.LogbookException) FileDataBodyPart(com.sun.jersey.multipart.file.FileDataBodyPart)

Example 2 with LogbookException

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

the class LogGroupPropertyTest method testCheckLogEntryGroupProperty.

@Test
public void testCheckLogEntryGroupProperty() {
    Map<String, String> attributes1 = new HashMap<>();
    attributes1.put(LogGroupProperty.ATTRIBUTE_ID, "id value");
    Property property1 = PropertyImpl.of(LogGroupProperty.NAME, attributes1);
    OlogLog logEntry = new OlogLog();
    logEntry.setProperties(Arrays.asList(property1));
    Map<String, String> attributes2 = new HashMap<>();
    attributes1.put("another attr", "value");
    Property property2 = PropertyImpl.of("another name", attributes2);
    OlogLog logEntry2 = new OlogLog();
    logEntry2.setProperties(Arrays.asList(property2));
    try {
        Property property = LogGroupProperty.getLogEntryGroupProperty(Arrays.asList(logEntry, logEntry2));
        assertEquals("id value", property.getAttributes().get(LogGroupProperty.ATTRIBUTE_ID));
    } catch (LogbookException e) {
        fail();
    }
}
Also used : HashMap(java.util.HashMap) LogbookException(org.phoebus.logbook.LogbookException) Property(org.phoebus.logbook.Property) Test(org.junit.Test)

Example 3 with LogbookException

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

the class LogEntryTableViewController method createLogEntryGroup.

private void createLogEntryGroup() {
    List<Long> logEntryIds = selectedLogEntries.stream().map(LogEntry::getId).collect(Collectors.toList());
    JobManager.schedule("Group log entries", monitor -> {
        try {
            client.groupLogEntries(logEntryIds);
            search();
        } catch (LogbookException e) {
            logger.log(Level.INFO, "Unable to create log entry group from selection");
            Platform.runLater(() -> {
                final Alert dialog = new Alert(AlertType.ERROR);
                dialog.setHeaderText(Messages.GroupingFailed);
                DialogHelper.positionDialog(dialog, tableView, 0, 0);
                dialog.showAndWait();
            });
        }
    });
}
Also used : Alert(javafx.scene.control.Alert) LogbookException(org.phoebus.logbook.LogbookException)

Example 4 with LogbookException

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

the class LogGroupPropertyTest method testCheckLogEntryGroupPropertyNoEntryHasProperties.

@Test
public void testCheckLogEntryGroupPropertyNoEntryHasProperties() {
    Map<String, String> attributes1 = new HashMap<>();
    attributes1.put("attr", "id value");
    Property property1 = PropertyImpl.of("some name", attributes1);
    OlogLog logEntry = new OlogLog();
    logEntry.setProperties(Arrays.asList(property1));
    Map<String, String> attributes2 = new HashMap<>();
    attributes1.put("another attr", "value");
    Property property2 = PropertyImpl.of("another name", attributes2);
    OlogLog logEntry2 = new OlogLog();
    logEntry2.setProperties(Arrays.asList(property2));
    try {
        Property property = LogGroupProperty.getLogEntryGroupProperty(Arrays.asList(logEntry, logEntry2));
        assertNotNull(property.getAttributes().get(LogGroupProperty.ATTRIBUTE_ID));
    } catch (LogbookException e) {
        fail();
    }
}
Also used : HashMap(java.util.HashMap) LogbookException(org.phoebus.logbook.LogbookException) Property(org.phoebus.logbook.Property) Test(org.junit.Test)

Example 5 with LogbookException

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

the class LogEntryEditorController method submit.

@FXML
public void submit() {
    submissionInProgress.set(true);
    JobManager.schedule("Submit Log Entry", monitor -> {
        OlogLog ologLog = new OlogLog();
        ologLog.setTitle(fieldsViewController.getTitle());
        ologLog.setDescription(fieldsViewController.getDescription());
        ologLog.setLevel(fieldsViewController.getSelectedLevel());
        ologLog.setLogbooks(fieldsViewController.getSelectedLogbooks());
        ologLog.setTags(fieldsViewController.getSelectedTags());
        ologLog.setAttachments(attachmentsViewController.getAttachments());
        ologLog.setProperties(logPropertiesEditorController.getProperties());
        LogClient logClient = logFactory.getLogClient(new SimpleAuthenticationToken(fieldsViewController.getUsernameProperty(), fieldsViewController.getPasswordProperty()));
        LogEntry result;
        try {
            if (replyTo == null) {
                result = logClient.set(ologLog);
            } else {
                result = logClient.reply(ologLog, replyTo);
            }
            if (result != null) {
                if (completionHandler != null) {
                    completionHandler.handleResult(result);
                }
                // Set username and password in secure store if submission of log entry completes successfully
                if (LogbookUIPreferences.save_credentials) {
                    // Get the SecureStore. Store username and password.
                    try {
                        SecureStore store = new SecureStore();
                        ScopedAuthenticationToken scopedAuthenticationToken = new ScopedAuthenticationToken(LogService.AUTHENTICATION_SCOPE, fieldsViewController.getUsernameProperty(), fieldsViewController.getPasswordProperty());
                        store.setScopedAuthentication(scopedAuthenticationToken);
                    } catch (Exception ex) {
                        logger.log(Level.WARNING, "Secure Store file not found.", ex);
                    }
                }
                attachmentsViewController.deleteTemporaryFiles();
                // This will close the editor
                Platform.runLater(() -> cancel());
            }
        } catch (LogbookException e) {
            logger.log(Level.WARNING, "Unable to submit log entry", e);
            Platform.runLater(() -> {
                if (e.getCause() != null && e.getCause().getMessage() != null) {
                    completionMessageLabel.textProperty().setValue(e.getCause().getMessage());
                } else if (e.getMessage() != null) {
                    completionMessageLabel.textProperty().setValue(e.getMessage());
                } else {
                    completionMessageLabel.textProperty().setValue(org.phoebus.logbook.Messages.SubmissionFailed);
                }
            });
        }
        submissionInProgress.set(false);
    });
}
Also used : LogClient(org.phoebus.logbook.LogClient) ScopedAuthenticationToken(org.phoebus.security.tokens.ScopedAuthenticationToken) LogbookException(org.phoebus.logbook.LogbookException) OlogLog(org.phoebus.olog.es.api.model.OlogLog) SecureStore(org.phoebus.security.store.SecureStore) LogEntry(org.phoebus.logbook.LogEntry) LogbookException(org.phoebus.logbook.LogbookException) ExecutionException(java.util.concurrent.ExecutionException) SimpleAuthenticationToken(org.phoebus.security.tokens.SimpleAuthenticationToken) FXML(javafx.fxml.FXML)

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