Skip to content

Fix elog client exceptions issue #3393

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 47 additions & 7 deletions app/logbook/elog/src/main/java/org/phoebus/elog/api/ElogApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public long post( Map<String, String> attributes, List<File> attachments, Long m

String encoded_Text = new String(attributes.get("Text").getBytes(), StandardCharsets.ISO_8859_1); // charset hell - this seems stupid but works
attributes.put("Text", encoded_Text);
String encoded_Subject = new String(attributes.get("Text").getBytes(), StandardCharsets.UTF_8); // charset hell - this seems stupid but works
String encoded_Subject = new String(attributes.get("Subject").getBytes(), StandardCharsets.UTF_8); // charset hell - this seems stupid but works
attributes.put("Subject", encoded_Subject);

Map<String, String> new_attributes = new HashMap<>(attributes);
Expand Down Expand Up @@ -235,19 +235,43 @@ public void delete( Long msgId ) throws LogbookException {
* @param search_term
*
*/
public List<ElogEntry> search( Map<String, String> search_term ) throws LogbookException {
public ElogSearchResult search( Map<String, String> search_term, Integer from, Integer size ) throws LogbookException {

Map<String, Object> params = new HashMap<>();
params.put( "mode", "summary" );
params.put( "reverse", "1" );
params.put( "npp", 20 ); // number of returned results...

// Sort by date
if(search_term.containsKey("sort")) {
String sortDirection = search_term.get("sort");
if("up".equalsIgnoreCase(sortDirection)) {
params.put( "reverse", "0" );
} else if("down".equalsIgnoreCase(sortDirection)) {
params.put( "reverse", "1" );
}
search_term.remove("sort");
} else {
params.put( "reverse", "1" );
}

// Pagination parameters
String requestUrl = this.url;
if(from != null && size != null) {
if(size != 0) {
int page = from / size + 1;
requestUrl = String.format("%spage%d", requestUrl, page);
params.put( "npp", size );
}
} else {
params.put( "npp", 20 ); // number of returned results...
}

params.putAll( search_term );

Map<String, Object> cookies = new HashMap<>();
cookies.put( "unm", this.username );
cookies.put( "upwd", this.password );

Response<String> resp = Requests.get( this.url )
Response<String> resp = Requests.get( requestUrl )
.cookies(cookies)
.params(params)
.followRedirect(false)
Expand All @@ -257,12 +281,25 @@ public List<ElogEntry> search( Map<String, String> search_term ) throws LogbookE
validateResponse( resp );

List<ElogEntry> entries = new ArrayList<>();
int totalCount = 0;

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 msgIds = (NodeList) xpath.evaluate( "(//tr/td[@class=\"list1\" or @class=\"list2\"][1])/a/@href", doc, XPathConstants.NODESET );

// Extract the number of all entries
String expression = "//b[contains(., 'Entries')]";
String result = (String) xpath.evaluate(expression, doc, XPathConstants.STRING);
if(result != null && !result.isEmpty()) {
java.util.regex.Pattern pattern = java.util.regex.Pattern.compile("(\\d+)\\s+Entries");
java.util.regex.Matcher matcher = pattern.matcher(result);
if(matcher.find()) {
totalCount = Integer.parseInt(matcher.group(1));
}
}

for( int i = 0; i < msgIds.getLength(); i++ ) {
String msgIdStr = msgIds.item(i).getNodeValue();
entries.add( read( Long.valueOf( msgIdStr.substring( msgIdStr.lastIndexOf('/') + 1 ) )));
Expand All @@ -271,7 +308,7 @@ public List<ElogEntry> search( Map<String, String> search_term ) throws LogbookE
throw new LogbookException( "could not parse the elog response", e );
}

return entries;
return ElogSearchResult.of(entries, totalCount);
}


Expand Down Expand Up @@ -504,7 +541,10 @@ private long validateResponse( Response<String> resp ) throws LogbookException {
} else {
URI loc = URI.create( location );
String[] path = loc.getPath().split("/");
msgId = Integer.parseInt( path[ path.length-1 ] );
String msgIdStr = path[ path.length-1 ];
if(msgIdStr != null && msgIdStr.matches("\\d+")) {
msgId = Integer.parseInt( msgIdStr );
}
}
}
if( body.contains("form name=form1") || body.contains("type=password") ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,11 @@ public Collection<Attachment> listAttachments(Long logId) {

@Override
public List<LogEntry> findLogs(Map<String, String> map) {
throw new RuntimeException(new UnsupportedOperationException());
}


public SearchResult findLogsWithPagination(Map<String, String> map) {
Map<String, String> query = new HashMap<>(map);
DateTimeFormatter simple_datetime_formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS", Locale.ENGLISH);

Expand Down Expand Up @@ -330,9 +335,30 @@ public List<LogEntry> findLogs(Map<String, String> map) {
query.remove("tag");
}

Integer from = null;
Integer size = null;

if(map.containsKey("from")) {
try {
from = Integer.parseInt(map.get("from"));
} catch (NumberFormatException e) {
e.printStackTrace();
}
}

if(map.containsKey("size")) {
try {
size = Integer.parseInt(map.get("size"));
} catch (NumberFormatException e) {
e.printStackTrace();
}
}

List<LogEntry> entries = new ArrayList<>();
ElogSearchResult result = null;
try {
for( ElogEntry entry : service.search( query ) ) {
result = service.search( query, from, size );
for( ElogEntry entry : result.getLogs() ) {
LogEntryBuilder logBuilder = LogEntryImpl.LogEntryBuilder.log();
logBuilder.id( Long.valueOf( entry.getAttribute("$@MID@$") ));
logBuilder.description( entry.getAttribute("Text") );
Expand Down Expand Up @@ -372,7 +398,7 @@ public List<LogEntry> findLogs(Map<String, String> map) {
} catch(LogbookException e){
e.printStackTrace();
}
return entries;
return SearchResult.of(entries, result.getHitCount());
}


Expand Down Expand Up @@ -532,7 +558,8 @@ public List<LogEntry> findLogsBySearch(String pattern) {

List<LogEntry> entries = new ArrayList<>();
try{
for( ElogEntry entry : service.search( query ) ) {
ElogSearchResult result = service.search( query, null, null );
for( ElogEntry entry : result.getLogs() ) {
LogEntryBuilder logBuilder = LogEntryImpl.LogEntryBuilder.log();
logBuilder.id( Long.valueOf( entry.getAttribute("$@MID@$") ));
logBuilder.description( entry.getAttribute("Text") );
Expand Down Expand Up @@ -583,7 +610,8 @@ public List<LogEntry> findLogsByTag(String pattern) {

List<LogEntry> entries = new ArrayList<>();
try{
for( ElogEntry entry : service.search( query ) ) {
ElogSearchResult result = service.search( query, null, null );
for( ElogEntry entry : result.getLogs() ) {
LogEntryBuilder logBuilder = LogEntryImpl.LogEntryBuilder.log();
logBuilder.id( Long.valueOf( entry.getAttribute("$@MID@$") ));
logBuilder.description( entry.getAttribute("Text") );
Expand Down Expand Up @@ -634,7 +662,8 @@ public List<LogEntry> findLogsByLogbook(String logbook) {

List<LogEntry> entries = new ArrayList<>();
try{
for( ElogEntry entry : service.search( query ) ) {
ElogSearchResult result = service.search( query, null, null );
for( ElogEntry entry : result.getLogs() ) {
LogEntryBuilder logBuilder = LogEntryImpl.LogEntryBuilder.log();
logBuilder.id( Long.valueOf( entry.getAttribute("$@MID@$") ));
logBuilder.description( entry.getAttribute("Text") );
Expand Down Expand Up @@ -697,5 +726,11 @@ public void delete(Collection<LogEntry> logIds) throws LogbookException {
}
}


@Override
public SearchResult search(Map<String, String> map) {
return findLogsWithPagination(map);
}

}

Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package org.phoebus.elog.api;

import java.util.List;

public class ElogSearchResult {

private int hitCount;
private List<ElogEntry> logs;

private ElogSearchResult(List<ElogEntry> logs, int hitCount) {
this.logs = logs;
this.hitCount = hitCount;
}

public static ElogSearchResult of(List<ElogEntry> logs, int hits) {
return new ElogSearchResult(logs, hits);
}

public int getHitCount() {
return hitCount;
}

public void setHitCount(int hitCount) {
this.hitCount = hitCount;
}

public List<ElogEntry> getLogs() {
return logs;
}

public void setLogs(List<ElogEntry> logs) {
this.logs = logs;
}
}