use of org.wso2.carbon.apimgt.rest.api.store.v1.dto.APIDTO in project carbon-apimgt by wso2.
the class ApisApiServiceImpl method apisPost.
/**
* Creates a new API
*
* @param body DTO model including the API details
* @param request msf4j request object
* @return Newly created API
* @throws NotFoundException When the particular resource does not exist in the system
*/
@Override
public Response apisPost(APIDTO body, Request request) throws NotFoundException {
String username = RestApiUtil.getLoggedInUsername(request);
try {
API.APIBuilder apiBuilder = MappingUtil.toAPI(body);
APIPublisher apiPublisher = RestAPIPublisherUtil.getApiPublisher(username);
apiPublisher.addAPI(apiBuilder);
API returnAPI = apiPublisher.getAPIbyUUID(apiBuilder.getId());
return Response.status(Response.Status.CREATED).entity(MappingUtil.toAPIDto(returnAPI)).build();
} catch (APIManagementException e) {
String errorMessage = "Error while adding new API : " + body.getProvider() + "-" + body.getName() + "-" + body.getVersion();
HashMap<String, String> paramList = new HashMap<String, String>();
paramList.put(APIMgtConstants.ExceptionsConstants.API_NAME, body.getName());
paramList.put(APIMgtConstants.ExceptionsConstants.API_VERSION, body.getVersion());
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
log.error(errorMessage, e);
return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
} catch (JsonProcessingException e) {
String errorMessage = "Error while adding new API";
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(errorMessage, 900313L, errorMessage);
log.error(errorMessage, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorDTO).build();
} catch (IOException e) {
String errorMessage = "Error while adding new API";
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(errorMessage, 900313L, errorMessage);
log.error(errorMessage, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorDTO).build();
}
}
use of org.wso2.carbon.apimgt.rest.api.store.v1.dto.APIDTO in project carbon-apimgt by wso2.
the class ApisApiServiceImplTestCase method testApisApiIdPutException.
@Test
public void testApisApiIdPutException() throws Exception {
printTestMethodName();
ApisApiServiceImpl apisApiService = new ApisApiServiceImpl();
APIPublisher apiPublisher = Mockito.mock(APIPublisherImpl.class);
PowerMockito.mockStatic(RestAPIPublisherUtil.class);
PowerMockito.when(RestAPIPublisherUtil.getApiPublisher(USER)).thenReturn(apiPublisher);
String apiId = UUID.randomUUID().toString();
API.APIBuilder apiBuilder = SampleTestObjectCreator.createDefaultAPI();
API api = apiBuilder.id(apiId).build();
APIDTO apidto = MappingUtil.toAPIDto(api);
Mockito.when(apiPublisher.getApiSwaggerDefinition(apiId)).thenReturn(SampleTestObjectCreator.apiDefinition);
Mockito.doNothing().doThrow(new IllegalArgumentException()).when(apiPublisher).updateAPI(apiBuilder);
Mockito.doThrow(new APIManagementException("Error occurred", ExceptionCodes.API_TYPE_INVALID)).when(apiPublisher).getAPIbyUUID(apiId);
Response response = apisApiService.apisApiIdPut(apiId, apidto, null, null, getRequest());
assertEquals(response.getStatus(), 400);
assertTrue(response.getEntity().toString().contains("API Type specified is invalid"));
}
use of org.wso2.carbon.apimgt.rest.api.store.v1.dto.APIDTO in project carbon-apimgt by wso2.
the class FileBasedApiImportExportManager method getApiDefinitionFromExtractedArchive.
/**
* Creates {@link APIDTO} instance from API Definition file
*
* @param apiDefinitionFilePath path to api definition file
* @return {@link APIDTO} instance
* @throws APIManagementException if an error occurs while creating API definition object
*/
private API getApiDefinitionFromExtractedArchive(String apiDefinitionFilePath) throws APIMgtEntityImportExportException {
String apiDefinitionString;
try {
apiDefinitionString = APIFileUtils.readFileContentAsText(apiDefinitionFilePath);
} catch (APIMgtDAOException e) {
// Unable to read the API definition file, skip this API
String errorMsg = "Error reading API definition from file at: " + apiDefinitionFilePath;
throw new APIMgtEntityImportExportException(errorMsg, e);
}
// convert to bean
Gson gson = new GsonBuilder().create();
try {
return new API.APIBuilder(gson.fromJson(apiDefinitionString, FileApi.class)).build();
} catch (Exception e) {
String errorMsg = "Error in building APIDTO from api definition read from file at: " + apiDefinitionFilePath;
throw new APIMgtEntityImportExportException(errorMsg, e);
}
}
use of org.wso2.carbon.apimgt.rest.api.store.v1.dto.APIDTO in project carbon-apimgt by wso2.
the class MappingUtil method toAPIDto.
/**
* This method converts the API Object from models into APIDTO object.
*
* @param api API object
* @return APIDTO object with provided API object
*/
public static APIDTO toAPIDto(API api) throws IOException {
APIDTO apidto = new APIDTO();
apidto.setId(api.getId());
apidto.setName(api.getName());
apidto.version(api.getVersion());
apidto.setContext(api.getContext());
apidto.setDescription(api.getDescription());
apidto.setIsDefaultVersion(api.isDefaultVersion());
apidto.setVisibility(APIDTO.VisibilityEnum.valueOf(api.getVisibility().toString()));
apidto.setResponseCaching(Boolean.toString(api.isResponseCachingEnabled()));
apidto.setCacheTimeout(api.getCacheTimeout());
apidto.setVisibleRoles(new ArrayList<>(api.getVisibleRoles()));
apidto.setProvider(api.getProvider());
apidto.setPermission(api.getApiPermission());
apidto.setLifeCycleStatus(api.getLifeCycleStatus());
apidto.setWorkflowStatus(api.getWorkflowStatus());
apidto.setTags(new ArrayList<>(api.getTags()));
apidto.hasOwnGateway(api.hasOwnGateway());
apidto.setLabels(new ArrayList<>(api.getLabels()));
apidto.setTransport(new ArrayList<>(api.getTransport()));
apidto.setUserPermissionsForApi(api.getUserSpecificApiPermissions());
apidto.setSecurityScheme(mapSecuritySchemeIntToList(api.getSecurityScheme()));
for (Policy policy : api.getPolicies()) {
apidto.addPoliciesItem(policy.getPolicyName());
}
apidto.setScopes(api.getScopes());
BusinessInformation businessInformation = api.getBusinessInformation();
API_businessInformationDTO apiBusinessInformationDTO = new API_businessInformationDTO();
apiBusinessInformationDTO.setBusinessOwner(businessInformation.getBusinessOwner());
apiBusinessInformationDTO.setBusinessOwnerEmail(businessInformation.getBusinessOwnerEmail());
apiBusinessInformationDTO.setTechnicalOwner(businessInformation.getTechnicalOwner());
apiBusinessInformationDTO.setTechnicalOwnerEmail(businessInformation.getTechnicalOwnerEmail());
apidto.setBusinessInformation(apiBusinessInformationDTO);
CorsConfiguration corsConfiguration = api.getCorsConfiguration();
API_corsConfigurationDTO apiCorsConfigurationDTO = new API_corsConfigurationDTO();
apiCorsConfigurationDTO.setAccessControlAllowCredentials(corsConfiguration.isAllowCredentials());
apiCorsConfigurationDTO.setAccessControlAllowHeaders(corsConfiguration.getAllowHeaders());
apiCorsConfigurationDTO.setAccessControlAllowMethods(corsConfiguration.getAllowMethods());
apiCorsConfigurationDTO.setAccessControlAllowOrigins(corsConfiguration.getAllowOrigins());
apiCorsConfigurationDTO.setCorsConfigurationEnabled(corsConfiguration.isEnabled());
apidto.setCorsConfiguration(apiCorsConfigurationDTO);
apidto.setEndpoint(fromEndpointToList(api.getEndpoint()));
for (UriTemplate uriTemplate : api.getUriTemplates().values()) {
API_operationsDTO apiOperationsDTO = new API_operationsDTO();
apiOperationsDTO.setId(uriTemplate.getTemplateId());
apiOperationsDTO.setUritemplate(uriTemplate.getUriTemplate());
apiOperationsDTO.setAuthType(uriTemplate.getAuthType());
apiOperationsDTO.setEndpoint(fromEndpointToList(uriTemplate.getEndpoint()));
apiOperationsDTO.setHttpVerb(uriTemplate.getHttpVerb());
apiOperationsDTO.setPolicy(uriTemplate.getPolicy().getPolicyName());
apiOperationsDTO.setScopes(uriTemplate.getScopes());
apidto.addOperationsItem(apiOperationsDTO);
}
if (api.getApiPolicy() != null) {
apidto.setApiPolicy(api.getApiPolicy().getPolicyName());
}
apidto.setCreatedTime(api.getCreatedTime().toString());
apidto.setLastUpdatedTime(api.getLastUpdatedTime().toString());
if (api.getThreatProtectionPolicies() != null) {
List<String> policyIdList = new ArrayList<>(api.getThreatProtectionPolicies());
List<API_threatProtectionPolicies_listDTO> listDTOS = new ArrayList<>();
for (String policyId : policyIdList) {
API_threatProtectionPolicies_listDTO threatProtectionPoliciesListDTO = new API_threatProtectionPolicies_listDTO();
threatProtectionPoliciesListDTO.setPolicyId(policyId);
// set priority when implementing
// threatProtectionPoliciesListDTO.setPriority(10);
listDTOS.add(threatProtectionPoliciesListDTO);
}
API_threatProtectionPoliciesDTO threatProtectionPoliciesDTO = new API_threatProtectionPoliciesDTO();
threatProtectionPoliciesDTO.setList(listDTOS);
apidto.setThreatProtectionPolicies(threatProtectionPoliciesDTO);
}
return apidto;
}
use of org.wso2.carbon.apimgt.rest.api.store.v1.dto.APIDTO in project carbon-apimgt by wso2.
the class ApisApiServiceImplTestCase method testApisImportDefinitionPostWSDLWithInvalidFileExtension.
@Test
public void testApisImportDefinitionPostWSDLWithInvalidFileExtension() throws Exception {
printTestMethodName();
File file = new File(getClass().getClassLoader().getResource("swagger.json").getFile());
FileInputStream fis = new FileInputStream(file);
FileInfo fileInfo = new FileInfo();
fileInfo.setFileName("swagger.json");
ApisApiServiceImpl apisApiService = new ApisApiServiceImpl();
String apiId = UUID.randomUUID().toString();
API.APIBuilder apiBuilder = SampleTestObjectCreator.createDefaultAPI().id(apiId);
API api = apiBuilder.build();
APIDTO apiDto = MappingUtil.toAPIDto(api);
ObjectMapper objectMapper = new ObjectMapper();
String additionalProperties = objectMapper.writeValueAsString(apiDto);
powerMockDefaultAPIPublisher();
Response response = apisApiService.apisImportDefinitionPost(WSDL, fis, fileInfo, null, additionalProperties, null, null, null, getRequest());
fis.close();
assertEquals(response.getStatus(), 400);
}
Aggregations