use of org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO in project carbon-apimgt by wso2.
the class ExportApiServiceImpl method exportApplicationsGet.
/**
* Export an existing Application
*
* @param appId Search query
* @param request msf4j request object
* @return Zip file containing exported Applications
* @throws NotFoundException When the particular resource does not exist in the system
*/
@Override
public Response exportApplicationsGet(String appId, Request request) throws NotFoundException {
APIStore consumer = null;
String exportedFilePath, zippedFilePath = null;
Application applicationDetails;
String exportedAppDirName = "exported-application";
String pathToExportDir = System.getProperty("java.io.tmpdir") + File.separator + "exported-app-archives-" + // creates a directory in default temporary-file directory
UUID.randomUUID().toString();
String username = RestApiUtil.getLoggedInUsername(request);
try {
consumer = RestApiUtil.getConsumer(username);
FileBasedApplicationImportExportManager importExportManager = new FileBasedApplicationImportExportManager(consumer, pathToExportDir);
applicationDetails = importExportManager.getApplicationDetails(appId, username);
if (applicationDetails == null) {
// 404
String errorMsg = "No application found for query " + appId;
log.error(errorMsg);
HashMap<String, String> paramList = new HashMap<>();
paramList.put("query", appId);
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(ExceptionCodes.APPLICATION_NOT_FOUND, paramList);
return Response.status(Response.Status.NOT_FOUND).entity(errorDTO).build();
}
exportedFilePath = importExportManager.exportApplication(applicationDetails, exportedAppDirName);
zippedFilePath = importExportManager.createArchiveFromExportedAppArtifacts(exportedFilePath, pathToExportDir, exportedAppDirName);
} catch (APIManagementException e) {
String errorMessage = "Error while exporting Application";
log.error(errorMessage, e);
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler());
return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
}
File exportedApplicationArchiveFile = new File(zippedFilePath);
Response.ResponseBuilder responseBuilder = Response.status(Response.Status.OK).entity(exportedApplicationArchiveFile);
responseBuilder.header("Content-Disposition", "attachment; filename=\"" + exportedApplicationArchiveFile.getName() + "\"");
Response response = responseBuilder.build();
return response;
}
use of org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO in project carbon-apimgt by wso2.
the class ImportApiServiceImpl method importApplicationsPut.
@Override
public Response importApplicationsPut(InputStream fileInputStream, FileInfo fileDetail, Request request) throws NotFoundException {
APIStore consumer = null;
String username = RestApiUtil.getLoggedInUsername(request);
try {
consumer = RestApiUtil.getConsumer(RestApiUtil.getLoggedInUsername(request));
FileBasedApplicationImportExportManager importExportManager = new FileBasedApplicationImportExportManager(consumer, System.getProperty("java.io.tmpdir") + File.separator + "exported-app-archives-" + UUID.randomUUID().toString());
Application applicationDetails = importExportManager.importApplication(fileInputStream);
applicationDetails.setCreatedUser(username);
applicationDetails.setUpdatedUser(username);
Application updatedApplication = importExportManager.updateApplication(applicationDetails, username);
return Response.status(Response.Status.OK).entity(updatedApplication).build();
} catch (APIManagementException e) {
String errorMsg = "Error while importing the Applications";
log.error(errorMsg, e);
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler());
return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
}
}
use of org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO in project carbon-apimgt by wso2.
the class ImportApiServiceImpl method importApplicationsPost.
/**
* Import an Application which has been exported to a zip file
*
* @param fileInputStream content stream of the zip file which contains exported Application
* @param fileDetail meta information of the zip file
* @param request msf4j request object
* @return Application that was imported
* @throws NotFoundException When the particular resource does not exist in the system
*/
@Override
public Response importApplicationsPost(InputStream fileInputStream, FileInfo fileDetail, Request request) throws NotFoundException {
APIStore consumer = null;
String username = RestApiUtil.getLoggedInUsername(request);
try {
consumer = RestApiUtil.getConsumer(username);
FileBasedApplicationImportExportManager importExportManager = new FileBasedApplicationImportExportManager(consumer, System.getProperty("java.io.tmpdir") + File.separator + "exported-app-archives-" + UUID.randomUUID().toString());
Application applicationDetails = importExportManager.importApplication(fileInputStream);
applicationDetails.setCreatedUser(username);
applicationDetails.setUpdatedUser(username);
ApplicationCreationResponse response = consumer.addApplication(applicationDetails);
return Response.status(Response.Status.OK).entity(response).build();
} catch (APIManagementException e) {
String errorMsg = "Error while importing the Applications";
log.error(errorMsg, e);
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler());
return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
}
}
use of org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO in project carbon-apimgt by wso2.
the class AuthUtil method getErrorDTO.
/**
* Returns a generic errorDTO
*
* @param errorHandler The error handler object.
* @param paramList list of parameters for more detail
* @return A generic errorDTO with the specified details
*/
public static ErrorDTO getErrorDTO(ErrorHandler errorHandler, HashMap<String, String> paramList) {
ErrorDTO errorDTO = new ErrorDTO();
errorDTO.setCode(errorHandler.getErrorCode());
errorDTO.setMoreInfo(paramList);
errorDTO.setMessage(errorHandler.getErrorMessage());
errorDTO.setDescription(errorHandler.getErrorDescription());
return errorDTO;
}
use of org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO in project carbon-apimgt by wso2.
the class AuthUtilTestCase method testGetErrorDTO.
@Test
public void testGetErrorDTO() {
ErrorHandler errorHandler = new ErrorHandler() {
@Override
public long getErrorCode() {
return 1234567890L;
}
@Override
public String getErrorMessage() {
return "xxx-error-message-xxx";
}
@Override
public String getErrorDescription() {
return "xxx-error-description-xxx";
}
};
HashMap<String, String> paramList = new HashMap<>();
paramList.put("param_1", "xxx-param_1-xxx");
paramList.put("param_2", "xxx-param_2-xxx");
// // expected error dto
ErrorDTO expectedErrorDTO = new ErrorDTO();
expectedErrorDTO.setCode(1234567890L);
expectedErrorDTO.setMessage("xxx-error-message-xxx");
expectedErrorDTO.setDescription("xxx-error-description-xxx");
expectedErrorDTO.setMoreInfo(paramList);
ErrorDTO actualErrorDTO = AuthUtil.getErrorDTO(errorHandler, paramList);
Assert.assertEquals(expectedErrorDTO.getCode(), actualErrorDTO.getCode());
Assert.assertEquals(expectedErrorDTO.getMessage(), actualErrorDTO.getMessage());
Assert.assertEquals(expectedErrorDTO.getDescription(), actualErrorDTO.getDescription());
Assert.assertEquals(expectedErrorDTO.getMoreInfo(), actualErrorDTO.getMoreInfo());
}
Aggregations