package Ġorg . cloudfoundry . cred hub . credentials Ċ Ċ import Ġcom . fasterxml . jackson . databind . JsonNode Ċ import Ġcom . fasterxml . jackson . databind . ObjectMapper Ċ import Ġorg . apache . commons . lang 3 . StringUtils Ċ import Ġorg . cloudfoundry . cred hub . Error Messages Ċ import Ġorg . cloudfoundry . cred hub . audit . CE FA udit Record Ċ import Ġorg . cloudfoundry . cred hub . audit . entities . Delete Credential Ċ import Ġorg . cloudfoundry . cred hub . audit . entities . Find Credential Ċ import Ġorg . cloudfoundry . cred hub . audit . entities . Generate Credential Ċ import Ġorg . cloudfoundry . cred hub . audit . entities . Get Credential Ċ import Ġorg . cloudfoundry . cred hub . audit . entities . Re generate Credential Ċ import Ġorg . cloudfoundry . cred hub . audit . entities . Set Credential Ċ import Ġorg . cloudfoundry . cred hub . exceptions . Invalid QueryParameter Exception Ċ import Ġorg . cloudfoundry . cred hub . regener ate . Re generate Handler Ċ import Ġorg . cloudfoundry . cred hub . requests . Base Credential Generate Request Ċ import Ġorg . cloudfoundry . cred hub . requests . Base Credential Set Request Ċ import Ġorg . cloudfoundry . cred hub . requests . Credential Re generate Request Ċ import Ġorg . cloudfoundry . cred hub . views . Credential View Ċ import Ġorg . cloudfoundry . cred hub . views . Data Response Ċ import Ġorg . cloudfoundry . cred hub . views . Find Credential Results Ċ import Ġorg . springframework . http . HttpStatus Ċ import Ġorg . springframework . http . MediaType Ċ import Ġorg . springframework . web . bind . annotation . PathVariable Ċ import Ġorg . springframework . web . bind . annotation . RequestBody Ċ import Ġorg . springframework . web . bind . annotation . RequestMapping Ċ import Ġorg . springframework . web . bind . annotation . Request Method . DELETE Ċ import Ġorg . springframework . web . bind . annotation . Request Method . GET Ċ import Ġorg . springframework . web . bind . annotation . Request Method . POST Ċ import Ġorg . springframework . web . bind . annotation . Request Method . PUT Ċ import Ġorg . springframework . web . bind . annotation . RequestParam Ċ import Ġorg . springframework . web . bind . annotation . ResponseStatus Ċ import Ġorg . springframework . web . bind . annotation . RestController Ċ import Ġjava . io . IOException Ċ Ċ @ RestController Ċ @ RequestMapping ( ĊĠĠĠ Ġpath Ġ= Ġ[ Credentials Controller . ENDPOINT ], ĊĠĠĠ Ġproduces Ġ= Ġ[ MediaType . APPLICATION _ JSON _ UTF 8 _ VALUE ] Ċ ) Ċ class ĠCredentials Controller ( ĊĠĠĠ Ġprivate Ġval Ġcredentials Handler : ĠCredentials Handler , ĊĠĠĠ Ġprivate Ġval Ġaudit Record : ĠCE FA udit Record , ĊĠĠĠ Ġval Ġre generate Handler : ĠRe generate Handler , ĊĠĠĠ Ġprivate Ġval ĠobjectMapper : ĠObjectMapper Ċ ) Ġ{ ĊĠĠĠ Ġcompanion Ġobject Ġ{ ĊĠĠĠĠĠĠĠ Ġconst Ġval ĠEND POINT Ġ= Ġ"/ api / v 1 / data " ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ RequestMapping ( method Ġ= Ġ[ GET ], Ġpath Ġ= Ġ[" /{ id }" ]) ĊĠĠĠ Ġ@ ResponseStatus ( HttpStatus . OK ) ĊĠĠĠ Ġfun Ġget ById (@ PathVariable Ġid : ĠString ): ĠCredential View Ġ{ ĊĠĠĠĠĠĠĠ Ġreturn Ġcredentials Handler . getC redential Version By UUID ( id ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ RequestMapping ( method Ġ= Ġ[ GET ], Ġpath Ġ= Ġ[ "" ]) ĊĠĠĠ Ġ@ ResponseStatus ( HttpStatus . OK ) ĊĠĠĠ Ġfun Ġget ByName ( ĊĠĠĠĠĠĠĠ Ġ@ RequestParam (" name ") Ġcredential Name : ĠString , ĊĠĠĠĠĠĠĠ Ġ@ RequestParam ( value Ġ= Ġ" versions ", Ġrequired Ġ= Ġfalse ) ĠnumberOf Versions : ĠInt ?, ĊĠĠĠĠĠĠĠ Ġ@ RequestParam ( value Ġ= Ġ" current ", Ġrequired Ġ= Ġfalse , ĠdefaultValue Ġ= Ġ" false ") Ġcurrent : ĠBoolean ĊĠĠĠ Ġ): ĠData Response Ġ{ ĊĠĠĠĠĠĠĠ Ġif Ġ( StringUtils . isEmpty ( credential Name )) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġthrow ĠInvalid QueryParameter Exception ( Error Messages . MISSING _ QUERY _ PARAMETER , Ġ" name ") ĊĠĠĠĠĠĠĠ Ġ} ĊĊĠĠĠĠĠĠĠ Ġif Ġ( current Ġ&& ĠnumberOf Versions Ġ!= Ġnull ) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġthrow ĠInvalid QueryParameter Exception ( Error Messages . C ANT _ USE _ VERSION S _ AND _ CURRENT , Ġ" name ") ĊĠĠĠĠĠĠĠ Ġ} ĊĊĠĠĠĠĠĠĠ Ġval Ġcredential Name With Pre pended Slash Ġ= ĠStringUtils . prepend If Missing ( credential Name , Ġ"/") ĊĊĠĠĠĠĠĠĠ Ġaudit Record . request Details Ġ= ĠGet Credential ( credential Name , ĠnumberOf Versions , Ġcurrent ) ĊĊĠĠĠĠĠĠĠ Ġreturn Ġif Ġ( current ) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġcredentials Handler . getCurrent Credential Versions ( credential Name With Pre pended Slash ) ĊĠĠĠĠĠĠĠ Ġ} Ġelse Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġcredentials Handler . get NC redential Versions ( credential Name With Pre pended Slash , ĠnumberOf Versions ) ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ RequestMapping ( method Ġ= Ġ[ GET ], Ġpath Ġ= Ġ[ "" ], Ġparams Ġ= Ġ[" path "]) ĊĠĠĠ Ġ@ ResponseStatus ( HttpStatus . OK ) ĊĠĠĠ Ġfun ĠfindBy Path ( ĊĠĠĠĠĠĠĠ Ġ@ RequestParam (" path ") Ġpath : ĠString , ĊĠĠĠĠĠĠĠ Ġ@ RequestParam ( value Ġ= Ġ" expires - within - days ", Ġrequired Ġ= Ġfalse , ĠdefaultValue Ġ= Ġ"") Ġexpires Within Days : ĠString ĊĠĠĠ Ġ): ĠFind Credential Results Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġfind Credential Ġ= ĠFind Credential () ĊĠĠĠĠĠĠĠ Ġfind Credential . path Ġ= Ġpath ĊĠĠĠĠĠĠĠ Ġfind Credential . expires Within Days Ġ= Ġexpires Within Days ĊĠĠĠĠĠĠĠ Ġaudit Record . request Details Ġ= Ġfind Credential ĊĊĠĠĠĠĠĠĠ Ġreturn ĠFind Credential Results ( credentials Handler . find Starting With Path ( path , Ġexpires Within Days )) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ RequestMapping ( method Ġ= Ġ[ GET ], Ġpath Ġ= Ġ[ "" ], Ġparams Ġ= Ġ[" name - like "]) ĊĠĠĠ Ġ@ ResponseStatus ( HttpStatus . OK ) ĊĠĠĠ Ġfun Ġfind ByName Like ( ĊĠĠĠĠĠĠĠ Ġ@ RequestParam (" name - like ") Ġname Like : ĠString , ĊĠĠĠĠĠĠĠ Ġ@ RequestParam ( value Ġ= Ġ" expires - within - days ", Ġrequired Ġ= Ġfalse , ĠdefaultValue Ġ= Ġ"") Ġexpires Within Days : ĠString ĊĠĠĠ Ġ): ĠFind Credential Results Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġfind Credential Ġ= ĠFind Credential () ĊĠĠĠĠĠĠĠ Ġfind Credential . name Like Ġ= Ġname Like ĊĠĠĠĠĠĠĠ Ġfind Credential . expires Within Days Ġ= Ġexpires Within Days ĊĠĠĠĠĠĠĠ Ġaudit Record . request Details Ġ= Ġfind Credential ĊĊĠĠĠĠĠĠĠ Ġreturn ĠFind Credential Results ( credentials Handler . find Containing Name ( name Like , Ġexpires Within Days )) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Synchron ized ĊĠĠĠ Ġ@ RequestMapping ( method Ġ= Ġ[ POST ], Ġpath Ġ= Ġ[ "" ]) ĊĠĠĠ Ġ@ ResponseStatus ( HttpStatus . OK ) ĊĠĠĠ Ġ@ Throws ( IOException :: class ) ĊĠĠĠ Ġfun Ġgenerate (@ RequestBody Ġrequest : ĠJsonNode ): ĠCredential View Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġis Re generate Ġ= Ġrequest [" regener ate "] ?. as Boolean () Ġ?: Ġfalse ĊĠĠĠĠĠĠĠ Ġtry Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġif Ġ( is Re generate ) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġval Ġre generate Request Ġ= ĠobjectMapper . readValue ( request . toString (), ĠCredential Re generate Request :: class . java ) ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġre generate Request . validate () ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġval Ġre generate Credential Ġ= ĠRe generate Credential () ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġre generate Credential . name Ġ= Ġre generate Request . name ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġaudit Record . request Details Ġ= Ġre generate Credential ĊĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġreturn Ġre generate Handler . handle Re generate ( regener ate Request . name !! , Ġre generate Request . metadata ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ} Ġelse Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġval Ġrequest String Ġ= Ġrequest . toString () ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġval Ġgenerate Request Ġ= ĠobjectMapper . readValue ( request String , ĠBase Credential Generate Request :: class . java ) ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġgenerate Request . validate () ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġval Ġgenerate Credential Ġ= ĠGenerate Credential () ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġgenerate Credential . name Ġ= Ġgenerate Request . name ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġgenerate Credential . type Ġ= Ġgenerate Request . type ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġaudit Record . request Details Ġ= Ġgenerate Credential ĊĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġreturn Ġcredentials Handler . generate Credential ( generate Request ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠ Ġ} Ġcatch Ġ( e : ĠIOException ) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġthrow ĠRuntimeException ( e ) ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ RequestMapping ( method Ġ= Ġ[ PUT ], Ġpath Ġ= Ġ[ "" ]) ĊĠĠĠ Ġ@ ResponseStatus ( HttpStatus . OK ) ĊĠĠĠ Ġ@ Synchron ized ĊĠĠĠ Ġfun Ġset (@ RequestBody Ġrequest Body : ĠBase Credential Set Request < *> ): ĠCredential View Ġ{ ĊĠĠĠĠĠĠĠ Ġrequest Body . validate () ĊĠĠĠĠĠĠĠ Ġaudit Record . request Details Ġ= ĠSet Credential ( request Body . name !! , Ġrequest Body . type !! ) ĊĊĠĠĠĠĠĠĠ Ġreturn Ġcredentials Handler . set Credential ( request Body ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ RequestMapping ( method Ġ= Ġ[ DELETE ], Ġpath Ġ= Ġ[ "" ]) ĊĠĠĠ Ġ@ ResponseStatus ( HttpStatus . NO _ CONTENT ) ĊĠĠĠ Ġfun Ġdelete (@ RequestParam (" name ") Ġcredential Name : ĠString ) Ġ{ ĊĠĠĠĠĠĠĠ Ġif Ġ( StringUtils . isEmpty ( credential Name )) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġthrow ĠInvalid QueryParameter Exception ( Error Messages . MISSING _ QUERY _ PARAMETER , Ġ" name ") ĊĠĠĠĠĠĠĠ Ġ} ĊĊĠĠĠĠĠĠĠ Ġval Ġcredential Name With Pre pended Slash Ġ= ĠStringUtils . prepend If Missing ( credential Name , Ġ"/") ĊĊĠĠĠĠĠĠĠ Ġval Ġrequest Details Ġ= ĠDelete Credential ( credential Name With Pre pended Slash ) ĊĠĠĠĠĠĠĠ Ġaudit Record . request Details Ġ= Ġrequest Details ĊĊĠĠĠĠĠĠĠ Ġcredentials Handler . delete Credential ( credential Name With Pre pended Slash ) ĊĠĠĠ Ġ} Ċ } Ċ