33import com .fasterxml .jackson .core .JsonProcessingException ;
44import com .fasterxml .jackson .core .type .TypeReference ;
55import io .mosip .biosdk .client .config .LoggerConfig ;
6+ import io .mosip .biosdk .client .constant .ResponseStatus ;
67import io .mosip .biosdk .client .dto .*;
8+ import io .mosip .biosdk .client .exception .BioSdkClientException ;
79import io .mosip .biosdk .client .utils .Util ;
810import io .mosip .kernel .biometrics .constant .BiometricType ;
911import io .mosip .kernel .biometrics .entities .BiometricRecord ;
@@ -50,11 +52,18 @@ public class Client_V_1_0 implements IBioApiV2 {
5052
5153 private static final String VERSION = "1.0" ;
5254
53- TypeReference <List <ErrorDto >> errorDtoListTypeRef = new TypeReference <List <ErrorDto >>() {
55+ private TypeReference <List <ErrorDto >> errorDtoListTypeRef = new TypeReference <List <ErrorDto >>() {
5456 };
5557
5658 private Map <String , String > sdkUrlsMap ;
5759
60+ private static final String TAG_HTTP_URL = "HTTP url: " ;
61+ private static final String TAG_HTTP_STATUS = "HTTP status: " ;
62+ private static final String TAG_ERRORS = "errors" ;
63+ private static final String TAG_RESPONSE = "response" ;
64+ private static final String TAG_STATUS_CODE = "statusCode" ;
65+ private static final String TAG_STATUS_MESSAGE = "statusMessage" ;
66+
5867 /**
5968 * Initializes the BioSDK client using the provided initialization parameters.
6069 *
@@ -155,7 +164,7 @@ private void addOtherSdkInfoDetails(SDKInfo sdkInfo, SDKInfo aggregatedSdkInfo)
155164 * @param initParams A map of initialization parameters to be included in the init request.
156165 * @param sdkServiceUrl The URL of the SDK service to be initialized.
157166 * @return A {@link SDKInfo} object containing details about the initialized SDK.
158- * @throws RuntimeException if there is an error during HTTP communication or response parsing.
167+ * @throws BioSdkClientException if there is an error during HTTP communication or response parsing.
159168 */
160169 private SDKInfo initForSdkUrl (Map <String , String > initParams , String sdkServiceUrl ) {
161170 SDKInfo sdkInfo = null ;
@@ -167,7 +176,8 @@ private SDKInfo initForSdkUrl(Map<String, String> initParams, String sdkServiceU
167176 ResponseEntity <?> responseEntity = Util .restRequest (sdkServiceUrl + "/init" , HttpMethod .POST , MediaType .APPLICATION_JSON , requestDto , null , String .class );
168177 if (!responseEntity .getStatusCode ().is2xxSuccessful ()) {
169178 logger .debug (LOGGER_SESSIONID , LOGGER_IDTYPE , "HTTP status: " , responseEntity .getStatusCode ().toString ());
170- throw new RuntimeException ("HTTP status: " + responseEntity .getStatusCode ().toString ());
179+ throw new BioSdkClientException (ResponseStatus .UNKNOWN_ERROR + "" ,
180+ TAG_HTTP_STATUS + responseEntity .getStatusCode ().toString ());
171181 }
172182 String responseBody = responseEntity .getBody ().toString ();
173183 JSONParser parser = new JSONParser ();
@@ -180,10 +190,10 @@ private SDKInfo initForSdkUrl(Map<String, String> initParams, String sdkServiceU
180190 });
181191 } catch (ParseException e ) {
182192 logger .error (LOGGER_SESSIONID , LOGGER_IDTYPE , "error" , e );
183- throw new RuntimeException ( e );
193+ throw new BioSdkClientException ( ResponseStatus . UNKNOWN_ERROR + "" , e . getLocalizedMessage (), e );
184194 } catch (JsonProcessingException e ) {
185195 logger .error (LOGGER_SESSIONID , LOGGER_IDTYPE , "error" , e );
186- throw new RuntimeException ( e );
196+ throw new BioSdkClientException ( ResponseStatus . UNKNOWN_ERROR + "" , e . getLocalizedMessage (), e );
187197 }
188198 return sdkInfo ;
189199 }
@@ -270,6 +280,9 @@ private String getDefaultSdkServiceUrl() {
270280 * @return The default SDK service URL from environment variables, or null if not defined.
271281 */
272282 private String getDefaultSdkServiceUrlFromEnv () {
283+ if (System .getProperty (MOSIP_BIOSDK_SERVICE ) != null )
284+ return System .getProperty (MOSIP_BIOSDK_SERVICE );
285+
273286 return System .getenv (MOSIP_BIOSDK_SERVICE );
274287 }
275288
@@ -280,7 +293,7 @@ private String getDefaultSdkServiceUrlFromEnv() {
280293 * @param modalitiesToCheck List of biometric modalities to check quality for.
281294 * @param flags Additional configuration flags.
282295 * @return A response containing the quality check result.
283- * @throws RuntimeException if parsing or processing of response fails.
296+ * @throws BioSdkClientException if parsing or processing of response fails.
284297 */
285298 @ Override
286299 public Response <QualityCheck > checkQuality (BiometricRecord sample , List <BiometricType > modalitiesToCheck , Map <String , String > flags ) {
@@ -297,7 +310,8 @@ public Response<QualityCheck> checkQuality(BiometricRecord sample, List<Biometri
297310 ResponseEntity <?> responseEntity = Util .restRequest (url , HttpMethod .POST , MediaType .APPLICATION_JSON , requestDto , null , String .class );
298311 if (!responseEntity .getStatusCode ().is2xxSuccessful ()) {
299312 logger .debug (LOGGER_SESSIONID , LOGGER_IDTYPE , "HTTP status: " , responseEntity .getStatusCode ().toString ());
300- throw new RuntimeException ("HTTP status: " + responseEntity .getStatusCode ().toString ());
313+ throw new BioSdkClientException (ResponseStatus .UNKNOWN_ERROR .getStatusCode () + "" ,
314+ TAG_HTTP_STATUS + responseEntity .getStatusCode ().toString ());
301315 }
302316 String responseBody = responseEntity .getBody ().toString ();
303317 JSONParser parser = new JSONParser ();
@@ -309,9 +323,10 @@ public Response<QualityCheck> checkQuality(BiometricRecord sample, List<Biometri
309323
310324 qualityCheck = Util .getObjectMapper ().readValue (responseJson .toString (), new TypeReference <QualityCheck >() {
311325 });
312- } catch (ParseException | JsonProcessingException e ) {
326+ } catch (Exception e ) {
313327 logger .error (LOGGER_SESSIONID , LOGGER_IDTYPE , "error" , e );
314- throw new RuntimeException (e );
328+ throw new BioSdkClientException (ResponseStatus .UNKNOWN_ERROR .getStatusCode () + "" , e .getLocalizedMessage (),
329+ e );
315330 }
316331 response .setResponse (qualityCheck );
317332 return response ;
@@ -325,7 +340,7 @@ public Response<QualityCheck> checkQuality(BiometricRecord sample, List<Biometri
325340 * @param modalitiesToMatch List of biometric modalities to consider for matching.
326341 * @param flags Additional configuration flags.
327342 * @return A response containing an array of match decisions.
328- * @throws RuntimeException if parsing or processing of response fails.
343+ * @throws BioSdkClientException if parsing or processing of response fails.
329344 */
330345 @ Override
331346 public Response <MatchDecision []> match (BiometricRecord sample , BiometricRecord [] gallery ,
@@ -343,7 +358,8 @@ public Response<MatchDecision[]> match(BiometricRecord sample, BiometricRecord[]
343358 ResponseEntity <?> responseEntity = Util .restRequest (url , HttpMethod .POST , MediaType .APPLICATION_JSON , requestDto , null , String .class );
344359 if (!responseEntity .getStatusCode ().is2xxSuccessful ()) {
345360 logger .debug (LOGGER_SESSIONID , LOGGER_IDTYPE , "HTTP status: " , responseEntity .getStatusCode ().toString ());
346- throw new RuntimeException ("HTTP status: " + responseEntity .getStatusCode ().toString ());
361+ throw new BioSdkClientException (ResponseStatus .UNKNOWN_ERROR + "" ,
362+ TAG_HTTP_STATUS + responseEntity .getStatusCode ().toString ());
347363 }
348364 String responseBody = responseEntity .getBody ().toString ();
349365 JSONParser parser = new JSONParser ();
@@ -363,12 +379,10 @@ public Response<MatchDecision[]> match(BiometricRecord sample, BiometricRecord[]
363379 jsonResponse .get ("response" ) != null ? Util .getObjectMapper ().readValue (jsonResponse .get ("response" ).toString (), new TypeReference <MatchDecision []>() {
364380 }) : null
365381 );
366- } catch (ParseException e ) {
367- logger .error (LOGGER_SESSIONID , LOGGER_IDTYPE , "error" , e );
368- throw new RuntimeException (e );
369- } catch (JsonProcessingException e ) {
382+ } catch (Exception e ) {
370383 logger .error (LOGGER_SESSIONID , LOGGER_IDTYPE , "error" , e );
371- throw new RuntimeException (e );
384+ throw new BioSdkClientException (ResponseStatus .UNKNOWN_ERROR .getStatusCode () + "" , e .getLocalizedMessage (),
385+ e );
372386 }
373387 return response ;
374388 }
@@ -380,7 +394,7 @@ public Response<MatchDecision[]> match(BiometricRecord sample, BiometricRecord[]
380394 * @param modalitiesToExtract List of biometric modalities to extract templates for.
381395 * @param flags Additional configuration flags that may influence the extraction process.
382396 * @return A response containing the extracted biometric template.
383- * @throws RuntimeException if parsing or processing of response fails.
397+ * @throws BioSdkClientException if parsing or processing of response fails.
384398 */
385399 @ Override
386400 public Response <BiometricRecord > extractTemplate (BiometricRecord sample , List <BiometricType > modalitiesToExtract , Map <String , String > flags ) {
@@ -396,16 +410,15 @@ public Response<BiometricRecord> extractTemplate(BiometricRecord sample, List<Bi
396410 ResponseEntity <?> responseEntity = Util .restRequest (url , HttpMethod .POST , MediaType .APPLICATION_JSON , requestDto , null , String .class );
397411 if (!responseEntity .getStatusCode ().is2xxSuccessful ()) {
398412 logger .debug (LOGGER_SESSIONID , LOGGER_IDTYPE , "HTTP status: " , responseEntity .getStatusCode ().toString ());
399- throw new RuntimeException ("HTTP status: " + responseEntity .getStatusCode ().toString ());
413+ throw new BioSdkClientException (ResponseStatus .UNKNOWN_ERROR .getStatusCode () + "" ,
414+ TAG_HTTP_STATUS + responseEntity .getStatusCode ().toString ());
400415 }
401416 convertAndSetResponseObject (response , responseEntity );
402417
403- } catch (ParseException e ) {
404- logger .error (LOGGER_SESSIONID , LOGGER_IDTYPE , "error" , e );
405- throw new RuntimeException (e );
406- } catch (JsonProcessingException e ) {
418+ } catch (Exception e ) {
407419 logger .error (LOGGER_SESSIONID , LOGGER_IDTYPE , "error" , e );
408- throw new RuntimeException (e );
420+ throw new BioSdkClientException (ResponseStatus .UNKNOWN_ERROR .getStatusCode () + "" , e .getLocalizedMessage (),
421+ e );
409422 }
410423 return response ;
411424 }
@@ -443,7 +456,7 @@ private String getSdkServiceUrl(List<BiometricType> modalitiesToExtract, Map<Str
443456 * @param modalitiesToSegment List of biometric modalities to perform segmentation on.
444457 * @param flags Additional configuration flags that may influence the segmentation process.
445458 * @return A response containing the segmented biometric record.
446- * @throws RuntimeException if parsing or processing of the response fails.
459+ * @throws BioSdkClientException if parsing or processing of the response fails.
447460 */
448461 @ Override
449462 public Response <BiometricRecord > segment (BiometricRecord biometricRecord , List <BiometricType > modalitiesToSegment , Map <String , String > flags ) {
@@ -459,12 +472,14 @@ public Response<BiometricRecord> segment(BiometricRecord biometricRecord, List<B
459472 ResponseEntity <?> responseEntity = Util .restRequest (url , HttpMethod .POST , MediaType .APPLICATION_JSON , requestDto , null , String .class );
460473 if (!responseEntity .getStatusCode ().is2xxSuccessful ()) {
461474 logger .debug (LOGGER_SESSIONID , LOGGER_IDTYPE , "HTTP status: " , responseEntity .getStatusCode ().toString ());
462- throw new RuntimeException ("HTTP status: " + responseEntity .getStatusCode ().toString ());
475+ throw new BioSdkClientException (ResponseStatus .UNKNOWN_ERROR .getStatusCode () + "" ,
476+ TAG_HTTP_STATUS + responseEntity .getStatusCode ().toString ());
463477 }
464478 convertAndSetResponseObject (response , responseEntity );
465- } catch (ParseException | JsonProcessingException e ) {
479+ } catch (Exception e ) {
466480 logger .error (LOGGER_SESSIONID , LOGGER_IDTYPE , "error" , e );
467- throw new RuntimeException (e );
481+ throw new BioSdkClientException (ResponseStatus .UNKNOWN_ERROR .getStatusCode () + "" , e .getLocalizedMessage (),
482+ e );
468483 }
469484 return response ;
470485 }
@@ -476,7 +491,7 @@ public Response<BiometricRecord> segment(BiometricRecord biometricRecord, List<B
476491 * @param response The response object to populate with parsed data.
477492 * @param responseEntity The raw HTTP response entity received from the service call.
478493 * @throws ParseException If there is an error while parsing the JSON response body.
479- * @throws JsonProcessingException If the JSON mapping fails during response conversion.
494+ * @throws BioSdkClientException If the JSON mapping fails during response conversion.
480495 */
481496 private void convertAndSetResponseObject (Response <BiometricRecord > response , ResponseEntity <?> responseEntity ) throws ParseException , JsonProcessingException {
482497 String responseBody = responseEntity .getBody ().toString ();
@@ -510,7 +525,7 @@ private void convertAndSetResponseObject(Response<BiometricRecord> response, Res
510525 * @param targetParams Additional target format parameters.
511526 * @param modalitiesToConvert List of biometric modalities to be converted.
512527 * @return Converted {@link BiometricRecord}.
513- * @throws RuntimeException if parsing or processing of the response fails.
528+ * @throws BioSdkClientException if parsing or processing of the response fails.
514529 */
515530 @ Override
516531 @ Deprecated
@@ -531,7 +546,8 @@ public BiometricRecord convertFormat(BiometricRecord sample, String sourceFormat
531546 ResponseEntity <?> responseEntity = Util .restRequest (url , HttpMethod .POST , MediaType .APPLICATION_JSON , requestDto , null , String .class );
532547 if (!responseEntity .getStatusCode ().is2xxSuccessful ()) {
533548 logger .debug (LOGGER_SESSIONID , LOGGER_IDTYPE , "HTTP status: " , responseEntity .getStatusCode ().toString ());
534- throw new RuntimeException ("HTTP status: " + responseEntity .getStatusCode ().toString ());
549+ throw new BioSdkClientException (ResponseStatus .UNKNOWN_ERROR .getStatusCode () + "" ,
550+ TAG_HTTP_STATUS + responseEntity .getStatusCode ().toString ());
535551 }
536552 String responseBody = responseEntity .getBody ().toString ();
537553 JSONParser parser = new JSONParser ();
@@ -542,9 +558,10 @@ public BiometricRecord convertFormat(BiometricRecord sample, String sourceFormat
542558
543559 resBiometricRecord = Util .getObjectMapper ().readValue (js .get ("response" ).toString (), new TypeReference <BiometricRecord >() {
544560 });
545- } catch (ParseException | JsonProcessingException e ) {
561+ } catch (Exception e ) {
546562 logger .error (LOGGER_SESSIONID , LOGGER_IDTYPE , "error" , e );
547- throw new RuntimeException (e );
563+ throw new BioSdkClientException (ResponseStatus .UNKNOWN_ERROR .getStatusCode () + "" , e .getLocalizedMessage (),
564+ e );
548565 }
549566 return resBiometricRecord ;
550567 }
@@ -560,7 +577,7 @@ public BiometricRecord convertFormat(BiometricRecord sample, String sourceFormat
560577 * @param targetParams Additional target format parameters.
561578 * @param modalitiesToConvert List of biometric modalities to be converted.
562579 * @return Response containing converted {@link BiometricRecord} and status information.
563- * @throws RuntimeException if parsing or processing of the response fails.
580+ * @throws BioSdkClientException if parsing or processing of the response fails.
564581 */
565582 @ Override
566583 public Response <BiometricRecord > convertFormatV2 (BiometricRecord sample , String sourceFormat , String targetFormat ,
@@ -581,16 +598,15 @@ public Response<BiometricRecord> convertFormatV2(BiometricRecord sample, String
581598 ResponseEntity <?> responseEntity = Util .restRequest (url , HttpMethod .POST , MediaType .APPLICATION_JSON , requestDto , null , String .class );
582599 if (!responseEntity .getStatusCode ().is2xxSuccessful ()) {
583600 logger .debug (LOGGER_SESSIONID , LOGGER_IDTYPE , "HTTP status: " , responseEntity .getStatusCode ().toString ());
584- throw new RuntimeException ("HTTP status: " + responseEntity .getStatusCode ().toString ());
601+ throw new BioSdkClientException (ResponseStatus .UNKNOWN_ERROR .getStatusCode () + "" ,
602+ TAG_HTTP_STATUS + responseEntity .getStatusCode ().toString ());
585603 }
586604 String responseBody = responseEntity .getBody ().toString ();
587605 convertAndSetResponseObject (response , responseBody , new TypeReference <BiometricRecord >() {
588606 });
589- } catch (ParseException e ) {
590- logger .error (LOGGER_SESSIONID , LOGGER_IDTYPE , "error" , e );
591- throw new RuntimeException (e );
592- } catch (JsonProcessingException e ) {
593- throw new RuntimeException (e );
607+ } catch (Exception e ) {
608+ throw new BioSdkClientException (ResponseStatus .UNKNOWN_ERROR .getStatusCode () + "" , e .getLocalizedMessage (),
609+ e );
594610 }
595611 return response ;
596612 }
@@ -649,10 +665,20 @@ private RequestDto generateNewRequestDto(Object body) throws JsonProcessingExcep
649665 * @throws RuntimeException If one or more errors are present in the list.
650666 */
651667 private void errorHandler (List <ErrorDto > errors ) {
652- if (errors != null ) {
653- for (ErrorDto errorDto : errors ) {
654- throw new RuntimeException (errorDto .getCode () + " ---> " + errorDto .getMessage ());
668+ if (errors == null ) {
669+ return ;
670+ }
671+
672+ StringBuilder errorMessages = new StringBuilder ();
673+ for (ErrorDto errorDto : errors ) {
674+ if (errorDto != null ) {
675+ errorMessages .append ("Code: " ).append (errorDto .getCode ()).append (", Message: " )
676+ .append (errorDto .getMessage ()).append (System .lineSeparator ());
655677 }
656678 }
679+ if (!errorMessages .isEmpty ()) {
680+ throw new BioSdkClientException (ResponseStatus .UNKNOWN_ERROR .getStatusCode () + "" ,
681+ errorMessages .toString ());
682+ }
657683 }
658684}
0 commit comments