public Privilege createPrivilege ( String name , String version , List < ? > statements ) throws OAuthSystemException , OAuthProblemException , URISyntaxException { cleanError ( ) ; prepareToken ( ) ; OneloginURLConnectionClient httpClient = new OneloginURLConnectionClient ( ) ; OAuthClient oAuthClient = new OAuthClient ( httpClient ) ; URIBuilder url = new URIBuilder ( settings . getURL ( Constants . CREATE_PRIVILEGE_URL ) ) ; OAuthClientRequest bearerRequest = new OAuthBearerClientRequest ( url . toString ( ) ) . buildHeaderMessage ( ) ; Map < String , String > headers = getAuthorizedHeader ( ) ; bearerRequest . setHeaders ( headers ) ; List < HashMap < String , Object > > statementData = new ArrayList < HashMap < String , Object > > ( ) ; if ( ! statements . isEmpty ( ) ) { if ( statements . get ( 0 ) instanceof Statement ) { List < Statement > data = ( List < Statement > ) statements ; HashMap < String , Object > dataObj ; for ( Statement statement : data ) { dataObj = new HashMap < String , Object > ( ) ; dataObj . put ( "Effect" , statement . effect ) ; dataObj . put ( "Action" , statement . actions ) ; dataObj . put ( "Scope" , statement . scopes ) ; statementData . add ( dataObj ) ; } } else if ( statements . get ( 0 ) instanceof HashMap ) { List < HashMap < String , Object > > data = ( List < HashMap < String , Object > > ) statements ; for ( HashMap < String , Object > statement : data ) { statementData . add ( statement ) ; } } } Map < String , Object > privilegeData = new HashMap < String , Object > ( ) ; privilegeData . put ( "Version" , version ) ; privilegeData . put ( "Statement" , statementData ) ; Map < String , Object > params = new HashMap < String , Object > ( ) ; params . put ( "name" , name ) ; params . put ( "privilege" , privilegeData ) ; String body = JSONUtils . buildJSON ( params ) ; bearerRequest . setBody ( body ) ; Privilege privilege = null ; OneloginOAuth2JSONResourceResponse oAuth2Response = oAuthClient . resource ( bearerRequest , OAuth . HttpMethod . POST , OneloginOAuth2JSONResourceResponse . class ) ; if ( oAuth2Response . getResponseCode ( ) == 201 ) { String id = ( String ) oAuth2Response . getFromContent ( "id" ) ; if ( id != null && ! id . isEmpty ( ) ) { privilege = new Privilege ( id , name , version , statements ) ; } } else { error = oAuth2Response . getError ( ) ; errorDescription = oAuth2Response . getErrorDescription ( ) ; } return privilege ; }
Creates a Privilege