Skip to content

Commit fe56add

Browse files
authored
Merge pull request #3393 from wanglin86769/fix-elog-client-exceptions-issue
Fix elog client exceptions issue
2 parents 10cf49d + 916f3ec commit fe56add

File tree

3 files changed

+121
-12
lines changed

3 files changed

+121
-12
lines changed

app/logbook/elog/src/main/java/org/phoebus/elog/api/ElogApi.java

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public long post( Map<String, String> attributes, List<File> attachments, Long m
9090

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

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

240240
Map<String, Object> params = new HashMap<>();
241241
params.put( "mode", "summary" );
242-
params.put( "reverse", "1" );
243-
params.put( "npp", 20 ); // number of returned results...
242+
243+
// Sort by date
244+
if(search_term.containsKey("sort")) {
245+
String sortDirection = search_term.get("sort");
246+
if("up".equalsIgnoreCase(sortDirection)) {
247+
params.put( "reverse", "0" );
248+
} else if("down".equalsIgnoreCase(sortDirection)) {
249+
params.put( "reverse", "1" );
250+
}
251+
search_term.remove("sort");
252+
} else {
253+
params.put( "reverse", "1" );
254+
}
255+
256+
// Pagination parameters
257+
String requestUrl = this.url;
258+
if(from != null && size != null) {
259+
if(size != 0) {
260+
int page = from / size + 1;
261+
requestUrl = String.format("%spage%d", requestUrl, page);
262+
params.put( "npp", size );
263+
}
264+
} else {
265+
params.put( "npp", 20 ); // number of returned results...
266+
}
267+
244268
params.putAll( search_term );
245269

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

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

259283
List<ElogEntry> entries = new ArrayList<>();
284+
int totalCount = 0;
260285

261286
try {
262287
TagNode tagNode = new HtmlCleaner().clean( resp.body() );
263288
org.w3c.dom.Document doc = new DomSerializer( new CleanerProperties()).createDOM(tagNode);
264289
XPath xpath = XPathFactory.newInstance().newXPath();
265290
NodeList msgIds = (NodeList) xpath.evaluate( "(//tr/td[@class=\"list1\" or @class=\"list2\"][1])/a/@href", doc, XPathConstants.NODESET );
291+
292+
// Extract the number of all entries
293+
String expression = "//b[contains(., 'Entries')]";
294+
String result = (String) xpath.evaluate(expression, doc, XPathConstants.STRING);
295+
if(result != null && !result.isEmpty()) {
296+
java.util.regex.Pattern pattern = java.util.regex.Pattern.compile("(\\d+)\\s+Entries");
297+
java.util.regex.Matcher matcher = pattern.matcher(result);
298+
if(matcher.find()) {
299+
totalCount = Integer.parseInt(matcher.group(1));
300+
}
301+
}
302+
266303
for( int i = 0; i < msgIds.getLength(); i++ ) {
267304
String msgIdStr = msgIds.item(i).getNodeValue();
268305
entries.add( read( Long.valueOf( msgIdStr.substring( msgIdStr.lastIndexOf('/') + 1 ) )));
@@ -271,7 +308,7 @@ public List<ElogEntry> search( Map<String, String> search_term ) throws LogbookE
271308
throw new LogbookException( "could not parse the elog response", e );
272309
}
273310

274-
return entries;
311+
return ElogSearchResult.of(entries, totalCount);
275312
}
276313

277314

@@ -504,7 +541,10 @@ private long validateResponse( Response<String> resp ) throws LogbookException {
504541
} else {
505542
URI loc = URI.create( location );
506543
String[] path = loc.getPath().split("/");
507-
msgId = Integer.parseInt( path[ path.length-1 ] );
544+
String msgIdStr = path[ path.length-1 ];
545+
if(msgIdStr != null && msgIdStr.matches("\\d+")) {
546+
msgId = Integer.parseInt( msgIdStr );
547+
}
508548
}
509549
}
510550
if( body.contains("form name=form1") || body.contains("type=password") ) {

app/logbook/elog/src/main/java/org/phoebus/elog/api/ElogClient.java

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,11 @@ public Collection<Attachment> listAttachments(Long logId) {
271271

272272
@Override
273273
public List<LogEntry> findLogs(Map<String, String> map) {
274+
throw new RuntimeException(new UnsupportedOperationException());
275+
}
276+
277+
278+
public SearchResult findLogsWithPagination(Map<String, String> map) {
274279
Map<String, String> query = new HashMap<>(map);
275280
DateTimeFormatter simple_datetime_formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS", Locale.ENGLISH);
276281

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

338+
Integer from = null;
339+
Integer size = null;
340+
341+
if(map.containsKey("from")) {
342+
try {
343+
from = Integer.parseInt(map.get("from"));
344+
} catch (NumberFormatException e) {
345+
e.printStackTrace();
346+
}
347+
}
348+
349+
if(map.containsKey("size")) {
350+
try {
351+
size = Integer.parseInt(map.get("size"));
352+
} catch (NumberFormatException e) {
353+
e.printStackTrace();
354+
}
355+
}
356+
333357
List<LogEntry> entries = new ArrayList<>();
358+
ElogSearchResult result = null;
334359
try {
335-
for( ElogEntry entry : service.search( query ) ) {
360+
result = service.search( query, from, size );
361+
for( ElogEntry entry : result.getLogs() ) {
336362
LogEntryBuilder logBuilder = LogEntryImpl.LogEntryBuilder.log();
337363
logBuilder.id( Long.valueOf( entry.getAttribute("$@MID@$") ));
338364
logBuilder.description( entry.getAttribute("Text") );
@@ -372,7 +398,7 @@ public List<LogEntry> findLogs(Map<String, String> map) {
372398
} catch(LogbookException e){
373399
e.printStackTrace();
374400
}
375-
return entries;
401+
return SearchResult.of(entries, result.getHitCount());
376402
}
377403

378404

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

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

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

635663
List<LogEntry> entries = new ArrayList<>();
636664
try{
637-
for( ElogEntry entry : service.search( query ) ) {
665+
ElogSearchResult result = service.search( query, null, null );
666+
for( ElogEntry entry : result.getLogs() ) {
638667
LogEntryBuilder logBuilder = LogEntryImpl.LogEntryBuilder.log();
639668
logBuilder.id( Long.valueOf( entry.getAttribute("$@MID@$") ));
640669
logBuilder.description( entry.getAttribute("Text") );
@@ -697,5 +726,11 @@ public void delete(Collection<LogEntry> logIds) throws LogbookException {
697726
}
698727
}
699728

729+
730+
@Override
731+
public SearchResult search(Map<String, String> map) {
732+
return findLogsWithPagination(map);
733+
}
734+
700735
}
701736

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package org.phoebus.elog.api;
2+
3+
import java.util.List;
4+
5+
public class ElogSearchResult {
6+
7+
private int hitCount;
8+
private List<ElogEntry> logs;
9+
10+
private ElogSearchResult(List<ElogEntry> logs, int hitCount) {
11+
this.logs = logs;
12+
this.hitCount = hitCount;
13+
}
14+
15+
public static ElogSearchResult of(List<ElogEntry> logs, int hits) {
16+
return new ElogSearchResult(logs, hits);
17+
}
18+
19+
public int getHitCount() {
20+
return hitCount;
21+
}
22+
23+
public void setHitCount(int hitCount) {
24+
this.hitCount = hitCount;
25+
}
26+
27+
public List<ElogEntry> getLogs() {
28+
return logs;
29+
}
30+
31+
public void setLogs(List<ElogEntry> logs) {
32+
this.logs = logs;
33+
}
34+
}

0 commit comments

Comments
 (0)