use of org.wso2.carbon.apimgt.rest.api.admin.NotFoundException in project carbon-apimgt by wso2.
the class ApisApiServiceImpl method apisApiIdSwaggerPut.
/**
* Updates the swagger defnition of an API
*
* @param apiId UUID of API
* @param apiDefinition updated swagger defintion
* @param ifMatch If-Match header value
* @param ifUnmodifiedSince If-Unmodified-Since header value
* @param request msf4j request object
* @return Updated swagger definition
* @throws NotFoundException When the particular resource does not exist in the system
*/
@Override
public Response apisApiIdSwaggerPut(String apiId, String apiDefinition, String ifMatch, String ifUnmodifiedSince, Request request) throws NotFoundException {
String username = RestApiUtil.getLoggedInUsername(request);
try {
APIPublisher apiPublisher = RestAPIPublisherUtil.getApiPublisher(username);
String existingFingerprint = apisApiIdSwaggerGetFingerprint(apiId, null, null, request);
if (!StringUtils.isEmpty(ifMatch) && !StringUtils.isEmpty(existingFingerprint) && !ifMatch.contains(existingFingerprint)) {
return Response.status(Response.Status.PRECONDITION_FAILED).build();
}
KeyMgtConfigurations keyManagerConfiguration = APIMConfigurationService.getInstance().getApimConfigurations().getKeyManagerConfigs();
Map<String, String> scopes = new APIDefinitionFromSwagger20().getScopesFromSecurityDefinition(apiDefinition);
for (String scopeName : scopes.keySet()) {
if (scopeName.contains(keyManagerConfiguration.getProductRestApiScopesKeyWord())) {
String message = "scope name couldn't have the restricted keyword " + keyManagerConfiguration.getProductRestApiScopesKeyWord();
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(message, 900313L, message);
return Response.status(Response.Status.PRECONDITION_FAILED).entity(errorDTO).build();
}
}
apiPublisher.saveSwagger20Definition(apiId, apiDefinition);
String apiSwagger = apiPublisher.getApiSwaggerDefinition(apiId);
String newFingerprint = apisApiIdSwaggerGetFingerprint(apiId, null, null, request);
return Response.ok().header(HttpHeaders.ETAG, "\"" + newFingerprint + "\"").entity(apiSwagger).build();
} catch (APIManagementException e) {
String errorMessage = "Error while put swagger for API : " + apiId;
HashMap<String, String> paramList = new HashMap<String, String>();
paramList.put(APIMgtConstants.ExceptionsConstants.API_ID, apiId);
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
log.error(errorMessage, e);
return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
}
}
use of org.wso2.carbon.apimgt.rest.api.admin.NotFoundException in project carbon-apimgt by wso2.
the class ApisApiServiceImpl method apisChangeLifecyclePost.
/**
* Change the lifecycle state of an API
*
* @param action lifecycle action
* @param apiId UUID of API
* @param lifecycleChecklist lifecycle check list items
* @param ifMatch If-Match header value
* @param ifUnmodifiedSince If-Unmodified-Since header value
* @param request msf4j request object
* @return 200 OK if the operation is succesful
* @throws NotFoundException When the particular resource does not exist in the system
*/
@Override
public Response apisChangeLifecyclePost(String action, String apiId, String lifecycleChecklist, String ifMatch, String ifUnmodifiedSince, Request request) throws NotFoundException {
String username = RestApiUtil.getLoggedInUsername(request);
Map<String, Boolean> lifecycleChecklistMap = new HashMap<>();
WorkflowResponseDTO response = null;
try {
if (lifecycleChecklist != null) {
String[] checkList = lifecycleChecklist.split(",");
for (String checkList1 : checkList) {
StringTokenizer attributeTokens = new StringTokenizer(checkList1, ":");
String attributeName = attributeTokens.nextToken();
Boolean attributeValue = Boolean.valueOf(attributeTokens.nextToken());
lifecycleChecklistMap.put(attributeName, attributeValue);
}
}
if (action.trim().equals(APIMgtConstants.CHECK_LIST_ITEM_CHANGE_EVENT)) {
RestAPIPublisherUtil.getApiPublisher(username).updateCheckListItem(apiId, action, lifecycleChecklistMap);
WorkflowResponse workflowResponse = new GeneralWorkflowResponse();
// since workflows are not defined for checklist items
workflowResponse.setWorkflowStatus(WorkflowStatus.APPROVED);
response = MappingUtil.toWorkflowResponseDTO(workflowResponse);
return Response.ok().entity(response).build();
} else {
WorkflowResponse workflowResponse = RestAPIPublisherUtil.getApiPublisher(username).updateAPIStatus(apiId, action, lifecycleChecklistMap);
response = MappingUtil.toWorkflowResponseDTO(workflowResponse);
// be in either pending or approved state) send back the workflow response
if (WorkflowStatus.CREATED == workflowResponse.getWorkflowStatus()) {
URI location = new URI(RestApiConstants.RESOURCE_PATH_APIS + "/" + apiId);
return Response.status(Response.Status.ACCEPTED).header(RestApiConstants.LOCATION_HEADER, location).entity(response).build();
} else {
return Response.ok().entity(response).build();
}
}
} catch (APIManagementException e) {
String errorMessage = "Error while updating lifecycle of API" + apiId + " to " + action;
Map<String, String> paramList = new HashMap<>();
paramList.put(APIMgtConstants.ExceptionsConstants.API_ID, apiId);
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
log.error(errorMessage, e);
return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
} catch (URISyntaxException e) {
String errorMessage = "Error while adding location header in response for api : " + apiId;
Map<String, String> paramList = new HashMap<>();
paramList.put(APIMgtConstants.ExceptionsConstants.API_ID, apiId);
ErrorHandler errorHandler = ExceptionCodes.LOCATION_HEADER_INCORRECT;
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(errorHandler, paramList);
log.error(errorMessage, e);
return Response.status(errorHandler.getHttpStatusCode()).entity(errorDTO).build();
}
}
use of org.wso2.carbon.apimgt.rest.api.admin.NotFoundException in project carbon-apimgt by wso2.
the class ApisApiServiceImpl method apisValidateDefinitionPost.
/**
* Validates a provided API definition
*
* @param type API definition type (SWAGGER or WSDL)
* @param fileInputStream file content stream
* @param fileDetail file details
* @param url URL of the definition
* @param request msf4j request
* @return API definition validation information
* @throws NotFoundException
*/
@Override
public Response apisValidateDefinitionPost(String type, InputStream fileInputStream, FileInfo fileDetail, String url, Request request) throws NotFoundException {
String errorMessage = "Error while validating the definition";
String username = RestApiUtil.getLoggedInUsername(request);
try {
APIPublisher apiPublisher = RestAPIPublisherUtil.getApiPublisher(username);
if (StringUtils.isBlank(type)) {
type = APIDefinitionValidationResponseDTO.DefinitionTypeEnum.SWAGGER.toString();
}
Response responseIfParamsInvalid = buildResponseIfParamsInvalid(type, fileInputStream, url);
if (responseIfParamsInvalid != null) {
return responseIfParamsInvalid;
}
if (APIDefinitionValidationResponseDTO.DefinitionTypeEnum.SWAGGER.toString().equals(type)) {
if (log.isDebugEnabled()) {
log.debug("Validating a swagger file.");
}
// TODO implement swagger validation
return Response.noContent().build();
} else {
// WSDL type
WSDLProcessor processor = null;
WSDLInfo info = null;
if (!StringUtils.isBlank(url)) {
processor = WSDLProcessFactory.getInstance().getWSDLProcessor(url);
info = processor.getWsdlInfo();
if (log.isDebugEnabled()) {
log.debug("Successfully validated WSDL URL " + url);
}
} else {
if (fileDetail.getFileName().endsWith(".zip")) {
WSDLArchiveInfo archiveInfo = apiPublisher.extractAndValidateWSDLArchive(fileInputStream);
info = archiveInfo.getWsdlInfo();
if (log.isDebugEnabled()) {
log.debug("Successfully validated WSDL archive " + fileDetail.getFileName());
}
} else if (fileDetail.getFileName().endsWith(".wsdl")) {
byte[] wsdlContent = IOUtils.toByteArray(fileInputStream);
processor = WSDLProcessFactory.getInstance().getWSDLProcessor(wsdlContent);
info = processor.getWsdlInfo();
if (log.isDebugEnabled()) {
log.debug("Successfully validated WSDL file " + fileDetail.getFileName());
}
} else {
String msg = "Unsupported extension type of file: " + fileDetail.getFileName();
log.error(msg);
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(msg, 900700L, msg);
return Response.status(Response.Status.BAD_REQUEST).entity(errorDTO).build();
}
}
if (info != null) {
APIDefinitionValidationResponseDTO responseDTO = MappingUtil.toWSDLValidationResponseDTO(info);
return Response.ok(responseDTO).build();
}
APIDefinitionValidationResponseDTO responseDTO = new APIDefinitionValidationResponseDTO();
responseDTO.isValid(false);
return Response.ok().entity(responseDTO).build();
}
} catch (APIManagementException e) {
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler());
log.error(errorMessage, e);
return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
} catch (IOException e) {
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.admin.NotFoundException in project carbon-apimgt by wso2.
the class ApisApiServiceImpl method apisApiIdLifecycleLifecyclePendingTaskDelete.
/**
* Remove pending lifecycle state change workflow tasks.
*
* @param apiId api id
* @param request msf4j request object
* @return Empty payload
* @throws NotFoundException When the particular resource does not exist in the system
*/
@Override
public Response apisApiIdLifecycleLifecyclePendingTaskDelete(String apiId, Request request) throws NotFoundException {
try {
String username = RestApiUtil.getLoggedInUsername(request);
APIPublisher apiPublisher = RestAPIPublisherUtil.getApiPublisher(username);
apiPublisher.removePendingLifecycleWorkflowTaskForAPI(apiId);
return Response.ok().build();
} catch (APIManagementException e) {
String errorMessage = "Error while removing pending task for API state change for api " + apiId;
Map<String, String> paramList = new HashMap<>();
paramList.put(APIMgtConstants.ExceptionsConstants.API_ID, apiId);
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
log.error(errorMessage, e);
return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
}
}
use of org.wso2.carbon.apimgt.rest.api.admin.NotFoundException in project carbon-apimgt by wso2.
the class ApisApiServiceImpl method apisApiIdThreatProtectionPoliciesDelete.
/**
* Delete a threat protection policy from an API
* @param apiId APIID
* @param policyId Threat protection policy id
* @param request MSF4J Request
* @return HTTP status 200 if success, 500 otherwise
* @throws NotFoundException When the particular resource does not exist in the system
*/
@Override
public Response apisApiIdThreatProtectionPoliciesDelete(String apiId, String policyId, Request request) throws NotFoundException {
String username = RestApiUtil.getLoggedInUsername(request);
try {
APIPublisher apiPublisher = RestAPIPublisherUtil.getApiPublisher(username);
if (!apiPublisher.isAPIExists(apiId)) {
ErrorDTO errorDTO = new ErrorDTO();
errorDTO.setCode(404l);
errorDTO.setDescription("Specified API was not found");
return Response.status(404).entity(errorDTO).build();
}
apiPublisher.deleteThreatProtectionPolicy(apiId, policyId);
return Response.ok().build();
} catch (APIManagementException e) {
log.error(e.getMessage(), e);
return Response.status(500).entity("Internal Server Error.").build();
}
}
Aggregations