use of org.wso2.carbon.apimgt.rest.api.service.catalog.utils.FileBasedServicesImportExportManager in project carbon-apimgt by wso2.
the class ServicesApiServiceImpl method importService.
@Override
public Response importService(InputStream fileInputStream, Attachment fileDetail, Boolean overwrite, String verifier, MessageContext messageContext) throws APIManagementException {
String userName = RestApiCommonUtil.getLoggedInUsername();
int tenantId = APIUtil.getTenantId(userName);
String tempDirPath = FileBasedServicesImportExportManager.createDir(RestApiConstants.JAVA_IO_TMPDIR);
List<ServiceInfoDTO> serviceList;
HashMap<String, ServiceEntry> serviceEntries;
HashMap<String, String> newResourcesHash;
List<ServiceEntry> serviceListToImport = new ArrayList<>();
List<ServiceEntry> serviceListToIgnore = new ArrayList<>();
List<ServiceEntry> servicesWithInvalidDefinition = new ArrayList<>();
// unzip the uploaded zip
try {
FileBasedServicesImportExportManager importExportManager = new FileBasedServicesImportExportManager(tempDirPath);
importExportManager.importService(fileInputStream);
} catch (APIMgtResourceAlreadyExistsException e) {
RestApiUtil.handleResourceAlreadyExistsError("Error while importing Service", e, log);
}
newResourcesHash = Md5HashGenerator.generateHash(tempDirPath);
serviceEntries = ServiceEntryMappingUtil.fromDirToServiceEntryMap(tempDirPath);
Map<String, Boolean> validationResults = new HashMap<>();
if (overwrite && StringUtils.isNotEmpty(verifier)) {
validationResults = validateVerifier(verifier, tenantId);
}
try {
for (Map.Entry<String, ServiceEntry> entry : serviceEntries.entrySet()) {
String key = entry.getKey();
serviceEntries.get(key).setMd5(newResourcesHash.get(key));
ServiceEntry service = serviceEntries.get(key);
byte[] definitionFileByteArray = getDefinitionFromInput(service.getEndpointDef());
if (validateAndRetrieveServiceDefinition(definitionFileByteArray, service.getDefUrl(), service.getDefinitionType()).isValid() || (ServiceEntry.DefinitionType.WSDL1.equals(service.getDefinitionType()) && APIMWSDLReader.validateWSDLFile(definitionFileByteArray).isValid())) {
service.setEndpointDef(new ByteArrayInputStream(definitionFileByteArray));
} else {
servicesWithInvalidDefinition.add(service);
}
if (overwrite) {
if (StringUtils.isNotEmpty(verifier) && validationResults.containsKey(service.getKey()) && !validationResults.get(service.getKey())) {
serviceListToIgnore.add(service);
} else {
serviceListToImport.add(service);
}
} else {
serviceListToImport.add(service);
}
}
} catch (IOException e) {
RestApiUtil.handleInternalServerError("Error when reading the service definition content", log);
}
if (servicesWithInvalidDefinition.size() > 0) {
serviceList = ServiceEntryMappingUtil.fromServiceListToDTOList(servicesWithInvalidDefinition);
String errorMsg = "The Service import has been failed as invalid service definition provided";
return Response.status(Response.Status.BAD_REQUEST).entity(getErrorDTO(RestApiConstants.STATUS_BAD_REQUEST_MESSAGE_DEFAULT, 400L, errorMsg, new JSONArray(serviceList).toString())).build();
}
if (serviceListToIgnore.size() > 0) {
serviceList = ServiceEntryMappingUtil.fromServiceListToDTOList(serviceListToIgnore);
String errorMsg = "The Service import has been failed since to verifier validation fails";
return Response.status(Response.Status.BAD_REQUEST).entity(getErrorDTO(RestApiConstants.STATUS_BAD_REQUEST_MESSAGE_DEFAULT, 400L, errorMsg, new JSONArray(serviceList).toString())).build();
} else {
List<ServiceEntry> importedServiceList = new ArrayList<>();
List<ServiceEntry> retrievedServiceList = new ArrayList<>();
try {
if (serviceListToImport.size() > 0) {
importedServiceList = serviceCatalog.importServices(serviceListToImport, tenantId, userName, overwrite);
}
} catch (APIManagementException e) {
if (ExceptionCodes.SERVICE_IMPORT_FAILED_WITHOUT_OVERWRITE.getErrorCode() == e.getErrorHandler().getErrorCode()) {
RestApiUtil.handleBadRequest("Cannot update existing services when overwrite is false", log);
} else {
RestApiUtil.handleInternalServerError("Error when importing services to service catalog", e, log);
}
}
if (importedServiceList == null) {
RestApiUtil.handleBadRequest("Cannot update the name or version or key or definition type of an " + "existing service", log);
}
for (ServiceEntry service : importedServiceList) {
retrievedServiceList.add(serviceCatalog.getServiceByKey(service.getKey(), tenantId));
}
serviceList = ServiceEntryMappingUtil.fromServiceListToDTOList(retrievedServiceList);
return Response.ok().entity(ServiceEntryMappingUtil.fromServiceInfoDTOToServiceInfoListDTO(serviceList)).build();
}
}
use of org.wso2.carbon.apimgt.rest.api.service.catalog.utils.FileBasedServicesImportExportManager in project carbon-apimgt by wso2.
the class ServicesApiServiceImpl method exportService.
@Override
public Response exportService(String name, String version, MessageContext messageContext) {
File exportedServiceArchiveFile = null;
// creates a directory in default temporary-file directory
String pathToExportDir = FileBasedServicesImportExportManager.createDir(RestApiConstants.JAVA_IO_TMPDIR);
String userName = RestApiCommonUtil.getLoggedInUsername();
int tenantId = APIUtil.getTenantId(userName);
String archiveName = name + APIConstants.KEY_SEPARATOR + version;
ServiceEntry serviceEntry;
String exportedFileName = null;
ExportArchive exportArchive;
if (StringUtils.isBlank(name) || StringUtils.isBlank(version)) {
RestApiUtil.handleBadRequest("Service name or owner should not be empty or null.", log);
}
try {
serviceEntry = serviceCatalog.getServiceByNameAndVersion(name, version, tenantId);
if (serviceEntry != null) {
FileBasedServicesImportExportManager importExportManager = new FileBasedServicesImportExportManager(pathToExportDir);
exportArchive = importExportManager.createArchiveFromExportedServices(ServiceEntryMappingUtil.generateServiceFiles(serviceEntry), pathToExportDir, archiveName);
exportedServiceArchiveFile = new File(exportArchive.getArchiveName());
exportedFileName = exportedServiceArchiveFile.getName();
Response.ResponseBuilder responseBuilder = Response.status(Response.Status.OK).entity(exportedServiceArchiveFile).type(MediaType.APPLICATION_OCTET_STREAM);
responseBuilder.header("Content-Disposition", "attachment; filename=\"" + exportedFileName + "\"");
return responseBuilder.build();
} else {
return Response.status(Response.Status.NOT_FOUND).build();
}
} catch (APIManagementException e) {
RestApiUtil.handleInternalServerError("Error while exporting Services: " + archiveName, e, log);
}
return null;
}
Aggregations