4646import java .util .Map ;
4747import java .util .logging .Level ;
4848import java .util .logging .Logger ;
49- import java .util .stream .Collectors ;
5049
5150/**
5251 * A client to the Olog-es webservice
@@ -68,20 +67,20 @@ public class OlogClient implements LogClient {
6867 */
6968 public static class OlogClientBuilder {
7069 // required
71- private URI ologURI = null ;
70+ private final URI ologURI ;
7271
7372 // optional
7473 private boolean withHTTPAuthentication = true ;
7574
7675 private ClientConfig clientConfig = null ;
7776 @ SuppressWarnings ("unused" )
7877 private SSLContext sslContext = null ;
79- private String protocol = null ;
78+ private final String protocol ;
8079 private String username = null ;
8180 private String password = null ;
8281 private String connectTimeoutAsString = null ;
8382
84- private OlogProperties properties = new OlogProperties ();
83+ private final OlogProperties properties = new OlogProperties ();
8584
8685 private OlogClientBuilder () {
8786 this .ologURI = URI .create (this .properties .getPreferenceValue ("olog_url" ));
@@ -92,7 +91,7 @@ private OlogClientBuilder() {
9291 * Creates a {@link OlogClientBuilder} for a CF client to Default URL in the
9392 * channelfinder.properties.
9493 *
95- * @return
94+ * @return The builder
9695 */
9796 public static OlogClientBuilder serviceURL () {
9897 return new OlogClientBuilder ();
@@ -101,7 +100,7 @@ public static OlogClientBuilder serviceURL() {
101100 /**
102101 * Enable of Disable the HTTP authentication on the client connection.
103102 *
104- * @param withHTTPAuthentication
103+ * @param withHTTPAuthentication Whether to use authentication or not
105104 * @return {@link OlogClientBuilder}
106105 */
107106 public OlogClientBuilder withHTTPAuthentication (boolean withHTTPAuthentication ) {
@@ -112,7 +111,7 @@ public OlogClientBuilder withHTTPAuthentication(boolean withHTTPAuthentication)
112111 /**
113112 * Set the username to be used for HTTP Authentication.
114113 *
115- * @param username
114+ * @param username User's identity
116115 * @return {@link OlogClientBuilder}
117116 */
118117 public OlogClientBuilder username (String username ) {
@@ -123,7 +122,7 @@ public OlogClientBuilder username(String username) {
123122 /**
124123 * Set the password to be used for the HTTP Authentication.
125124 *
126- * @param password
125+ * @param password User's password
127126 * @return {@link OlogClientBuilder}
128127 */
129128 public OlogClientBuilder password (String password ) {
@@ -149,7 +148,7 @@ public OlogClient create() {
149148 this .username = ifNullReturnPreferenceValue (this .username , "username" );
150149 this .password = ifNullReturnPreferenceValue (this .password , "password" );
151150 this .connectTimeoutAsString = ifNullReturnPreferenceValue (this .connectTimeoutAsString , "connectTimeout" );
152- Integer connectTimeout = 0 ;
151+ int connectTimeout = 0 ;
153152 try {
154153 connectTimeout = Integer .parseInt (connectTimeoutAsString );
155154 } catch (NumberFormatException e ) {
@@ -216,7 +215,7 @@ private LogEntry save(LogEntry log, LogEntry inReplyTo) throws LogbookException
216215
217216 if (clientResponse .getStatus () < 300 ) {
218217 OlogLog createdLog = OlogObjectMappers .logEntryDeserializer .readValue (clientResponse .getEntityInputStream (), OlogLog .class );
219- log .getAttachments ().stream (). forEach (attachment -> {
218+ log .getAttachments ().forEach (attachment -> {
220219 FormDataMultiPart form = new FormDataMultiPart ();
221220 // Add id only if it is set, otherwise Jersey will complain and cause the submission to fail.
222221 if (attachment .getId () != null && !attachment .getId ().isEmpty ()) {
@@ -277,12 +276,11 @@ public LogEntry getLog(Long logId) {
277276 @ Override
278277 public LogEntry findLogById (Long logId ) {
279278 try {
280- OlogLog ologLog = OlogObjectMappers .logEntryDeserializer .readValue (
279+ return OlogObjectMappers .logEntryDeserializer .readValue (
281280 service
282281 .path ("logs" )
283282 .path (logId .toString ())
284283 .accept (MediaType .APPLICATION_JSON ).get (String .class ), OlogLog .class );
285- return ologLog ;
286284 } catch (JsonProcessingException e ) {
287285 return null ;
288286 }
@@ -300,7 +298,7 @@ public List<LogEntry> findLogs(Map<String, String> map) throws RuntimeException
300298 *
301299 * @param searchParams Map of search parameters/expressions
302300 * @return A list of matching {@link LogEntry}s
303- * @throws RuntimeException
301+ * @throws RuntimeException If search fails, e.g. due to invalid search parameters
304302 */
305303 private SearchResult findLogs (MultivaluedMap <String , String > searchParams ) throws RuntimeException {
306304 try {
@@ -311,10 +309,15 @@ private SearchResult findLogs(MultivaluedMap<String, String> searchParams) throw
311309 .accept (MediaType .APPLICATION_JSON )
312310 .get (String .class ),
313311 OlogSearchResult .class );
314- return SearchResult .of (ologSearchResult .getLogs (). stream (). collect ( Collectors . toList ()),
312+ return SearchResult .of (new ArrayList <>( ologSearchResult .getLogs ()),
315313 ologSearchResult .getHitCount ());
316314 } catch (UniformInterfaceException | ClientHandlerException | IOException e ) {
317315 logger .log (Level .WARNING , "failed to retrieve log entries" , e );
316+ if (e instanceof UniformInterfaceException ){
317+ if (((UniformInterfaceException ) e ).getResponse ().getStatus () == Status .BAD_REQUEST .getStatusCode ()){
318+ throw new RuntimeException (Messages .BadRequestFailure );
319+ }
320+ }
318321 throw new RuntimeException (e );
319322 }
320323 }
@@ -373,8 +376,7 @@ public Collection<String> listAttributes(String propertyName) {
373376
374377 @ Override
375378 public List <LogEntry > listLogs () {
376- List <LogEntry > logEntries = new ArrayList <>();
377- return logEntries ;
379+ return new ArrayList <>();
378380 }
379381
380382 /**
@@ -400,11 +402,10 @@ public Collection<String> listLevels() {
400402 @ Override
401403 public Collection <Logbook > listLogbooks () {
402404 try {
403- List < Logbook > logbooks = OlogObjectMappers .logEntryDeserializer .readValue (
405+ return OlogObjectMappers .logEntryDeserializer .readValue (
404406 service .path ("logbooks" ).accept (MediaType .APPLICATION_JSON ).get (String .class ),
405407 new TypeReference <List <Logbook >>() {
406408 });
407- return logbooks ;
408409 } catch (UniformInterfaceException | ClientHandlerException | IOException e ) {
409410 logger .log (Level .WARNING , "Unable to get logbooks from service" , e );
410411 return Collections .emptySet ();
@@ -414,11 +415,10 @@ public Collection<Logbook> listLogbooks() {
414415 @ Override
415416 public Collection <Property > listProperties () {
416417 try {
417- List < Property > properties = OlogObjectMappers .logEntryDeserializer .readValue (
418+ return OlogObjectMappers .logEntryDeserializer .readValue (
418419 service .path ("properties" ).accept (MediaType .APPLICATION_JSON ).get (String .class ),
419420 new TypeReference <List <Property >>() {
420421 });
421- return properties ;
422422 } catch (UniformInterfaceException | ClientHandlerException | IOException e ) {
423423 logger .log (Level .WARNING , "failed to list olog properties" , e );
424424 return Collections .emptySet ();
@@ -428,11 +428,10 @@ public Collection<Property> listProperties() {
428428 @ Override
429429 public Collection <Tag > listTags () {
430430 try {
431- List < Tag > tags = OlogObjectMappers .logEntryDeserializer .readValue (
431+ return OlogObjectMappers .logEntryDeserializer .readValue (
432432 service .path ("tags" ).accept (MediaType .APPLICATION_JSON ).get (String .class ),
433433 new TypeReference <List <Tag >>() {
434434 });
435- return tags ;
436435 } catch (UniformInterfaceException | ClientHandlerException | IOException e ) {
437436 logger .log (Level .WARNING , "failed to retrieve olog tags" , e );
438437 return Collections .emptySet ();
@@ -449,7 +448,7 @@ public String getServiceUrl() {
449448 }
450449
451450 @ Override
452- public LogEntry updateLogEntry (LogEntry logEntry ) throws LogbookException {
451+ public LogEntry updateLogEntry (LogEntry logEntry ) {
453452 ClientResponse clientResponse ;
454453
455454 try {
@@ -469,9 +468,7 @@ public LogEntry updateLogEntry(LogEntry logEntry) throws LogbookException {
469468 @ Override
470469 public SearchResult search (Map <String , String > map ) {
471470 MultivaluedMap <String , String > mMap = new MultivaluedMapImpl ();
472- map .forEach ((k , v ) -> {
473- mMap .putSingle (k , v );
474- });
471+ map .forEach (mMap ::putSingle );
475472 return findLogs (mMap );
476473 }
477474
0 commit comments