Search in sources :

Example 26 with ScopeDTO

use of org.wso2.carbon.apimgt.internal.service.dto.ScopeDTO in project carbon-apimgt by wso2.

the class AMDefaultKeyManagerImpl method registerScope.

/**
 * This method will be used to register a Scope in the authorization server.
 *
 * @param scope Scope to register
 * @throws APIManagementException if there is an error while registering a new scope.
 */
@Override
public void registerScope(Scope scope) throws APIManagementException {
    String scopeKey = scope.getKey();
    ScopeDTO scopeDTO = new ScopeDTO();
    scopeDTO.setName(scopeKey);
    scopeDTO.setDisplayName(scope.getName());
    scopeDTO.setDescription(scope.getDescription());
    if (StringUtils.isNotBlank(scope.getRoles()) && scope.getRoles().trim().split(",").length > 0) {
        scopeDTO.setBindings(Arrays.asList(scope.getRoles().trim().split(",")));
    }
    try (Response response = scopeClient.registerScope(scopeDTO)) {
        if (response.status() != HttpStatus.SC_CREATED) {
            String responseString = readHttpResponseAsString(response.body());
            throw new APIManagementException("Error occurred while registering scope: " + scopeKey + ". Error" + " Status: " + response.status() + " . Error Response: " + responseString);
        }
    } catch (KeyManagerClientException e) {
        handleException("Cannot register scope : " + scopeKey, e);
    }
}
Also used : Response(feign.Response) KeyManagerClientException(org.wso2.carbon.apimgt.impl.kmclient.KeyManagerClientException) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) ScopeDTO(org.wso2.carbon.apimgt.impl.dto.ScopeDTO)

Example 27 with ScopeDTO

use of org.wso2.carbon.apimgt.internal.service.dto.ScopeDTO in project carbon-apimgt by wso2.

the class AMDefaultKeyManagerImpl method fromDTOToScope.

/**
 * Get Scope object from ScopeDTO response received from authorization server.
 *
 * @param scopeDTO ScopeDTO response
 * @return Scope model object
 */
private Scope fromDTOToScope(ScopeDTO scopeDTO) {
    Scope scope = new Scope();
    scope.setName(scopeDTO.getDisplayName());
    scope.setKey(scopeDTO.getName());
    scope.setDescription(scopeDTO.getDescription());
    scope.setRoles((scopeDTO.getBindings() != null && !scopeDTO.getBindings().isEmpty()) ? String.join(",", scopeDTO.getBindings()) : StringUtils.EMPTY);
    return scope;
}
Also used : Scope(org.wso2.carbon.apimgt.api.model.Scope)

Example 28 with ScopeDTO

use of org.wso2.carbon.apimgt.internal.service.dto.ScopeDTO in project carbon-apimgt by wso2.

the class ApplicationMappingUtil method getScopeInfoDTO.

public static List<ScopeInfoDTO> getScopeInfoDTO(Set<Scope> scopes) {
    List<ScopeInfoDTO> scopeDto = new ArrayList<ScopeInfoDTO>();
    for (Scope scope : scopes) {
        ScopeInfoDTO scopeInfoDTO = new ScopeInfoDTO();
        scopeInfoDTO.setKey(scope.getKey());
        scopeInfoDTO.setName(scope.getName());
        scopeInfoDTO.setDescription(scope.getDescription());
        if (StringUtils.isNotBlank(scope.getRoles())) {
            scopeInfoDTO.setRoles(Arrays.asList(scope.getRoles().trim().split(",")));
        }
        scopeDto.add(scopeInfoDTO);
    }
    return scopeDto;
}
Also used : Scope(org.wso2.carbon.apimgt.api.model.Scope) ScopeInfoDTO(org.wso2.carbon.apimgt.rest.api.admin.v1.dto.ScopeInfoDTO) ArrayList(java.util.ArrayList)

Example 29 with ScopeDTO

use of org.wso2.carbon.apimgt.internal.service.dto.ScopeDTO in project carbon-apimgt by wso2.

the class SystemScopesMappingUtil method createJsonObjectOfScopeMapping.

/**
 * Extract scope and roles and create JSONObject
 *
 * @param body          body of a Role Scope  Mapping
 * @return JSONObject   role scope mapping data
 */
public static JSONObject createJsonObjectOfScopeMapping(ScopeListDTO body) throws APIManagementException {
    JSONObject responseJson = new JSONObject();
    JSONArray scopeJson = new JSONArray();
    for (ScopeDTO scope : body.getList()) {
        JSONObject scopeRoleJson = new JSONObject();
        String roles = scope.getRoles().toString().replaceAll("\\[", "").replaceAll("\\]", "").replaceAll("\\s", "");
        if (!roles.isEmpty()) {
            scopeRoleJson.put("Name", scope.getName());
            scopeRoleJson.put("Roles", roles);
            scopeJson.put(scopeRoleJson);
        }
    }
    responseJson.put("Scope", scopeJson);
    return responseJson;
}
Also used : JSONObject(org.json.simple.JSONObject) ScopeDTO(org.wso2.carbon.apimgt.rest.api.admin.v1.dto.ScopeDTO) JSONArray(org.json.JSONArray)

Aggregations

Scope (org.wso2.carbon.apimgt.api.model.Scope)11 ScopeDTO (org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.ScopeDTO)11 ArrayList (java.util.ArrayList)10 APIScopeDTO (org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIScopeDTO)8 HashMap (java.util.HashMap)7 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)6 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)4 Scope (org.wso2.carbon.apimgt.core.models.Scope)4 JSONObject (org.json.simple.JSONObject)3 APIPublisher (org.wso2.carbon.apimgt.core.api.APIPublisher)3 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)3 ErrorDTO (org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)3 URI (java.net.URI)2 URISyntaxException (java.net.URISyntaxException)2 Timestamp (java.sql.Timestamp)2 Date (java.util.Date)2 LinkedHashSet (java.util.LinkedHashSet)2 Map (java.util.Map)2 APICategory (org.wso2.carbon.apimgt.api.model.APICategory)2 CORSConfiguration (org.wso2.carbon.apimgt.api.model.CORSConfiguration)2