Skip to content

Commit

Permalink
Merge pull request #2767 from ControlSystemStudio/CSSTUDIO-1818
Browse files Browse the repository at this point in the history
Olog client to use new REST endpoint when creating entries
  • Loading branch information
shroffk authored Aug 15, 2023
2 parents a6bea69 + 03fd012 commit f067330
Show file tree
Hide file tree
Showing 6 changed files with 206 additions and 143 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,55 +2,29 @@

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientHandlerException;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.*;
import com.sun.jersey.api.client.ClientResponse.Status;
import com.sun.jersey.api.client.UniformInterfaceException;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;
import com.sun.jersey.api.client.filter.HTTPBasicAuthFilter;
import com.sun.jersey.client.urlconnection.HTTPSProperties;
import com.sun.jersey.core.util.MultivaluedMapImpl;
import com.sun.jersey.multipart.FormDataBodyPart;
import com.sun.jersey.multipart.FormDataMultiPart;
import com.sun.jersey.multipart.file.FileDataBodyPart;
import com.sun.jersey.multipart.impl.MultiPartWriter;
import org.phoebus.logbook.Attachment;
import org.phoebus.logbook.LogClient;
import org.phoebus.logbook.LogEntry;
import org.phoebus.logbook.LogService;
import org.phoebus.logbook.Logbook;
import org.phoebus.logbook.LogbookException;
import org.phoebus.logbook.Messages;
import org.phoebus.logbook.Property;
import org.phoebus.logbook.SearchResult;
import org.phoebus.logbook.Tag;
import org.phoebus.logbook.*;
import org.phoebus.olog.es.api.model.OlogLog;
import org.phoebus.olog.es.api.model.OlogObjectMappers;
import org.phoebus.olog.es.api.model.OlogSearchResult;
import org.phoebus.security.store.SecureStore;
import org.phoebus.security.tokens.ScopedAuthenticationToken;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.UriBuilder;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.security.NoSuchAlgorithmException;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;

Expand Down Expand Up @@ -204,59 +178,40 @@ public LogEntry set(LogEntry log) throws LogbookException {
* @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)

FormDataMultiPart form = new FormDataMultiPart();
try {
form.bodyPart(new FormDataBodyPart("logEntry", OlogObjectMappers.logEntrySerializer.writeValueAsString(log), MediaType.APPLICATION_JSON_TYPE));
} catch (JsonProcessingException e) {
logger.log(Level.SEVERE, "Got unexpected exception", e);
throw e;
}
log.getAttachments().forEach(attachment -> {
// Add all files as represented in the attachment objects. Note that each gets
// the "multipart name" files, but that is OK.
form.bodyPart(new FileDataBodyPart("files", attachment.getFile()));
});

ClientResponse clientResponse = service.path("logs")
.path("multipart")
.type(MediaType.MULTIPART_FORM_DATA)
.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().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()));
.put(ClientResponse.class, form);

if (clientResponse.getStatus() > 300) {
logger.log(Level.SEVERE, "Failed to create log entry: " + clientResponse.toString());
throw new LogbookException(clientResponse.toString());
}

return OlogObjectMappers.logEntryDeserializer.readValue(clientResponse.getEntityInputStream(), OlogLog.class);

} catch (UniformInterfaceException | ClientHandlerException | IOException e) {
logger.log(Level.SEVERE, "Failed to submit log entry, got client exception", e);
throw new LogbookException(e);
Expand Down Expand Up @@ -317,11 +272,11 @@ private SearchResult findLogs(MultivaluedMap<String, String> searchParams) throw
.get(String.class),
OlogSearchResult.class);
return SearchResult.of(new ArrayList<>(ologSearchResult.getLogs()),
ologSearchResult.getHitCount());
ologSearchResult.getHitCount());
} catch (UniformInterfaceException | ClientHandlerException | IOException e) {
logger.log(Level.WARNING, "failed to retrieve log entries", e);
if(e instanceof UniformInterfaceException){
if(((UniformInterfaceException) e).getResponse().getStatus() == Status.BAD_REQUEST.getStatusCode()){
if (e instanceof UniformInterfaceException) {
if (((UniformInterfaceException) e).getResponse().getStatus() == Status.BAD_REQUEST.getStatusCode()) {
throw new RuntimeException(Messages.BadRequestFailure);
}
}
Expand Down Expand Up @@ -480,7 +435,7 @@ public SearchResult search(Map<String, String> map) {
}

@Override
public void groupLogEntries(List<Long> logEntryIds) throws LogbookException{
public void groupLogEntries(List<Long> logEntryIds) throws LogbookException {
try {
ClientResponse clientResponse = service.path("logs/group")
.type(MediaType.APPLICATION_JSON)
Expand Down Expand Up @@ -523,7 +478,7 @@ public void authenticate(String username, String password) throws Exception {
}

@Override
public String serviceInfo(){
public String serviceInfo() {
ClientResponse clientResponse = service.path("").get(ClientResponse.class);
return clientResponse.getEntity(String.class);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,24 @@ public class Messages
File,
FileSave,
FileSaveFailed,
FileTooLarge,
GroupingFailed,
GroupSelectedEntries,
Level,
Logbooks,
LogbooksSearchFailTitle,
LogbookServiceUnavailableTitle,
LogbookServiceHasNoLogbooks,
LogbooksTitle,
LogbooksTooltip,
NewLogEntry,
NoAttachments,
NoClipboardContent,
Normal,
NoSearchResults,
PreviewOpenErrorBody,
PreviewOpenErrorTitle,
RequestTooLarge,
SelectFile,
SelectFolder,
ShowHideDetails,
SizeLimitsText,
UpdateLogEntry;

static
Expand Down
Loading

0 comments on commit f067330

Please sign in to comment.