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);
}
}
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();
}
}
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();
});
}
});
}
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();
}
}
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);
});
}
Aggregations