use of org.wso2.ballerinalang.compiler.semantics.model.iterable.Operation in project carbon-apimgt by wso2.
the class APIDefinitionFromSwagger20 method generateSwaggerFromResources.
@Override
public String generateSwaggerFromResources(CompositeAPI.Builder api) {
Swagger swagger = new Swagger();
Info info = new Info();
info.setTitle(api.getName());
info.setDescription(api.getDescription());
info.setVersion(api.getVersion());
swagger.setInfo(info);
Map<String, Path> stringPathMap = new HashMap();
for (UriTemplate uriTemplate : api.getUriTemplates().values()) {
String uriTemplateString = uriTemplate.getUriTemplate();
List<Parameter> parameterList = getParameters(uriTemplateString);
if (!HttpMethod.GET.toString().equalsIgnoreCase(uriTemplate.getHttpVerb()) && !HttpMethod.DELETE.toString().equalsIgnoreCase(uriTemplate.getHttpVerb()) && !HttpMethod.OPTIONS.toString().equalsIgnoreCase(uriTemplate.getHttpVerb()) && !HttpMethod.HEAD.toString().equalsIgnoreCase(uriTemplate.getHttpVerb())) {
parameterList.add(getDefaultBodyParameter());
}
Operation operation = new Operation();
operation.setParameters(parameterList);
operation.setOperationId(uriTemplate.getTemplateId());
operation.addResponse("200", getDefaultResponse());
if (stringPathMap.containsKey(uriTemplateString)) {
Path path = stringPathMap.get(uriTemplateString);
path.set(uriTemplate.getHttpVerb().toLowerCase(), operation);
} else {
Path path = new Path();
path.set(uriTemplate.getHttpVerb().toLowerCase(), operation);
stringPathMap.put(uriTemplateString, path);
}
}
swagger.setPaths(stringPathMap);
swagger.setPaths(stringPathMap);
return Json.pretty(swagger);
}
use of org.wso2.ballerinalang.compiler.semantics.model.iterable.Operation in project carbon-apimgt by wso2.
the class WSDL11ProcessorImpl method getParameters.
/**
* Returns parameters, given http binding operation, verb and content type
*
* @param bindingOperation {@link BindingOperation} object
* @param verb HTTP verb
* @param contentType Content type
* @return parameters, given http binding operation, verb and content type
*/
private List<WSDLOperationParam> getParameters(BindingOperation bindingOperation, String verb, String contentType) {
List<WSDLOperationParam> params = new ArrayList<>();
Operation operation = bindingOperation.getOperation();
// or content type is not provided
if (APIMWSDLUtils.canContainBody(verb) && !APIMWSDLUtils.hasFormDataParams(contentType)) {
WSDLOperationParam param = new WSDLOperationParam();
param.setName("Payload");
param.setParamType(WSDLOperationParam.ParamTypeEnum.BODY);
params.add(param);
if (log.isDebugEnabled()) {
log.debug("Adding default Param for operation:" + operation.getName() + ", contentType: " + contentType);
}
return params;
}
if (operation != null) {
Input input = operation.getInput();
if (input != null) {
Message message = input.getMessage();
if (message != null) {
Map map = message.getParts();
map.forEach((name, partObj) -> {
WSDLOperationParam param = new WSDLOperationParam();
param.setName(name.toString());
if (log.isDebugEnabled()) {
log.debug("Identified param for operation: " + operation.getName() + " param: " + name);
}
if (APIMWSDLUtils.canContainBody(verb)) {
if (log.isDebugEnabled()) {
log.debug("Operation " + operation.getName() + " can contain a body.");
}
// In POST, PUT operations, parameters always in body according to HTTP Binding spec
if (APIMWSDLUtils.hasFormDataParams(contentType)) {
param.setParamType(WSDLOperationParam.ParamTypeEnum.FORM_DATA);
if (log.isDebugEnabled()) {
log.debug("Param " + name + " type was set to formData.");
}
}
// no else block since if content type is not form-data related, there can be only one
// parameter which is payload body. This is handled in the first if block which is
// if (canContainBody(verb) && !hasFormDataParams(contentType)) { .. }
} else {
// In GET operations, parameters always query or path as per HTTP Binding spec
if (isUrlReplacement(bindingOperation)) {
param.setParamType(WSDLOperationParam.ParamTypeEnum.PATH);
if (log.isDebugEnabled()) {
log.debug("Param " + name + " type was set to Path.");
}
} else {
param.setParamType(WSDLOperationParam.ParamTypeEnum.QUERY);
if (log.isDebugEnabled()) {
log.debug("Param " + name + " type was set to Query.");
}
}
}
Part part = (Part) partObj;
param.setDataType(part.getTypeName().getLocalPart());
if (log.isDebugEnabled()) {
log.debug("Param " + name + " data type was set to " + param.getDataType());
}
params.add(param);
});
}
}
}
return params;
}
use of org.wso2.ballerinalang.compiler.semantics.model.iterable.Operation 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.ballerinalang.compiler.semantics.model.iterable.Operation in project carbon-apimgt by wso2.
the class APIStoreImpl method updateComment.
@Override
public void updateComment(Comment comment, String commentId, String apiId, String username) throws APICommentException, APIMgtResourceNotFoundException {
validateCommentMaxCharacterLength(comment.getCommentText());
try {
failIfApiNotExists(apiId);
Comment oldComment = getApiDAO().getCommentByUUID(commentId, apiId);
if (oldComment != null) {
// if the update operation is done by a user who isn't the owner of the comment
if (!oldComment.getCommentedUser().equals(username)) {
checkIfUserIsCommentModerator(username);
}
getApiDAO().updateComment(comment, commentId, apiId);
} else {
String errorMsg = "Couldn't find comment with comment_id : " + commentId + "and api_id : " + apiId;
log.error(errorMsg);
throw new APIMgtResourceNotFoundException(errorMsg, ExceptionCodes.COMMENT_NOT_FOUND);
}
} catch (APIMgtDAOException e) {
String errorMsg = "Error occurred while updating comment " + commentId;
log.error(errorMsg, e);
throw new APICommentException(errorMsg, e, e.getErrorHandler());
}
}
use of org.wso2.ballerinalang.compiler.semantics.model.iterable.Operation in project carbon-apimgt by wso2.
the class APIStoreImpl method deleteComment.
@Override
public void deleteComment(String commentId, String apiId, String username) throws APICommentException, APIMgtResourceNotFoundException {
try {
ApiDAO apiDAO = getApiDAO();
failIfApiNotExists(apiId);
Comment comment = apiDAO.getCommentByUUID(commentId, apiId);
if (comment != null) {
// if the delete operation is done by a user who isn't the owner of the comment
if (!comment.getCommentedUser().equals(username)) {
checkIfUserIsCommentModerator(username);
}
apiDAO.deleteComment(commentId, apiId);
} else {
String errorMsg = "Couldn't find comment with comment_id : " + commentId;
log.error(errorMsg);
throw new APIMgtResourceNotFoundException(errorMsg, ExceptionCodes.COMMENT_NOT_FOUND);
}
} catch (APIMgtDAOException e) {
String errorMsg = "Error occurred while deleting comment " + commentId;
log.error(errorMsg, e);
throw new APICommentException(errorMsg, e, e.getErrorHandler());
}
}
Aggregations