use of org.wso2.carbon.apimgt.rest.api.core.dto.SubscriptionDTO in project carbon-apimgt by wso2.
the class MappingUtil method fromSubscription.
/**
* Converts Subscription to SubscriptionDTO
*
* @param subscription subscription model containg subscription details
* @return SubscriptionDTO containing subscription list
*/
public static SubscriptionDTO fromSubscription(Subscription subscription) {
SubscriptionDTO subscriptionDTO = new SubscriptionDTO();
subscriptionDTO.setSubscriptionId(subscription.getId());
subscriptionDTO.setSubscriptionStatus(SubscriptionDTO.SubscriptionStatusEnum.fromValue(subscription.getStatus().toString()));
subscriptionDTO.setApplicationInfo(toApplicationDto(subscription.getApplication()));
subscriptionDTO.setPolicy(subscription.getPolicy().getPolicyName());
return subscriptionDTO;
}
use of org.wso2.carbon.apimgt.rest.api.core.dto.SubscriptionDTO in project carbon-apimgt by wso2.
the class SubscriptionsApiServiceImpl method subscriptionsBlockSubscriptionPost.
/**
* Block an existing subscription
*
* @param subscriptionId ID of the subscription
* @param blockState Subscription block state
* @param ifMatch If-Match header value
* @param ifUnmodifiedSince If-Unmodified-Since header value
* @param request ms4j request object
* @return Updated subscription DTO as the response
* @throws NotFoundException When the particular resource does not exist in the system
*/
@Override
public Response subscriptionsBlockSubscriptionPost(String subscriptionId, String blockState, String ifMatch, String ifUnmodifiedSince, Request request) throws NotFoundException {
String username = RestApiUtil.getLoggedInUsername(request);
try {
APIPublisher apiPublisher = RestAPIPublisherUtil.getApiPublisher(username);
Subscription subscription = apiPublisher.getSubscriptionByUUID(subscriptionId);
if (subscription == null) {
String errorMessage = "Subscription not found : " + subscriptionId;
APIMgtResourceNotFoundException e = new APIMgtResourceNotFoundException(errorMessage, ExceptionCodes.SUBSCRIPTION_NOT_FOUND);
HashMap<String, String> paramList = new HashMap<String, String>();
paramList.put(APIMgtConstants.ExceptionsConstants.SUBSCRIPTION_ID, subscriptionId);
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
log.error(errorMessage, e);
return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
} else if (subscription.getStatus().equals(APIMgtConstants.SubscriptionStatus.REJECTED) || subscription.getStatus().equals(APIMgtConstants.SubscriptionStatus.ON_HOLD)) {
String errorMessage = "Cannot update subcription from " + subscription.getStatus() + "to " + blockState;
APIMgtResourceNotFoundException e = new APIMgtResourceNotFoundException(errorMessage, ExceptionCodes.SUBSCRIPTION_STATE_INVALID);
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler());
log.error(errorMessage, e);
return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
}
apiPublisher.updateSubscriptionStatus(subscriptionId, APIMgtConstants.SubscriptionStatus.valueOf(blockState));
Subscription newSubscription = apiPublisher.getSubscriptionByUUID(subscriptionId);
SubscriptionDTO subscriptionDTO = MappingUtil.fromSubscription(newSubscription);
return Response.ok().entity(subscriptionDTO).build();
} catch (GatewayException e) {
String errorMessage = "Failed to block subscription :" + subscriptionId + " in gateway";
log.error(errorMessage, e);
return Response.status(Response.Status.ACCEPTED).build();
} catch (APIManagementException e) {
String errorMessage = "Error while blocking the subscription " + subscriptionId;
HashMap<String, String> paramList = new HashMap<String, String>();
paramList.put(APIMgtConstants.ExceptionsConstants.SUBSCRIPTION_ID, subscriptionId);
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.core.dto.SubscriptionDTO in project carbon-apimgt by wso2.
the class SubscriptionsApiServiceImpl method subscriptionsSubscriptionIdGet.
/**
* Retrieves a single subscription
*
* @param subscriptionId Id of the subscription
* @param ifNoneMatch If-None-Match header value
* @param ifModifiedSince If-Modified-Since header value
* @param request msf4j request object
* @return Requested subscription DTO as the payload
* @throws NotFoundException When the particular resource does not exist in the system
*/
@Override
public Response subscriptionsSubscriptionIdGet(String subscriptionId, String ifNoneMatch, String ifModifiedSince, Request request) throws NotFoundException {
String username = RestApiUtil.getLoggedInUsername(request);
SubscriptionDTO subscriptionDTO = null;
try {
APIStore apiStore = RestApiUtil.getConsumer(username);
String existingFingerprint = subscriptionsSubscriptionIdGetFingerprint(subscriptionId, ifNoneMatch, ifModifiedSince, request);
if (!StringUtils.isEmpty(ifNoneMatch) && !StringUtils.isEmpty(existingFingerprint) && ifNoneMatch.contains(existingFingerprint)) {
return Response.notModified().build();
}
Subscription subscription = apiStore.getSubscriptionByUUID(subscriptionId);
subscriptionDTO = SubscriptionMappingUtil.fromSubscriptionToDTO(subscription);
return Response.ok().entity(subscriptionDTO).header(HttpHeaders.ETAG, "\"" + existingFingerprint + "\"").build();
} catch (APIManagementException e) {
String errorMessage = "Error while retrieving subscription information - " + subscriptionId;
HashMap<String, String> paramList = new HashMap<String, String>();
paramList.put(APIMgtConstants.ExceptionsConstants.SUBSCRIPTION_ID, subscriptionId);
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.core.dto.SubscriptionDTO in project carbon-apimgt by wso2.
the class SubscriptionsApiServiceImpl method subscriptionsPost.
/**
* Adds a new subscription
*
* @param body Subscription details to be added
* @param request msf4j request object
* @return Newly added subscription as the response
* @throws NotFoundException When the particular resource does not exist in the system
*/
@Override
public Response subscriptionsPost(SubscriptionDTO body, Request request) throws NotFoundException {
String username = RestApiUtil.getLoggedInUsername(request);
SubscriptionDTO subscriptionDTO = null;
URI location = null;
try {
APIStore apiStore = RestApiUtil.getConsumer(username);
String applicationId = body.getApplicationId();
String apiId = body.getApiIdentifier();
String tier = body.getPolicy();
Application application = apiStore.getApplicationByUuid(applicationId);
if (application != null && !ApplicationStatus.APPLICATION_APPROVED.equals(application.getStatus())) {
String errorMessage = "Application " + applicationId + " is not active";
ExceptionCodes exceptionCode = ExceptionCodes.APPLICATION_INACTIVE;
APIManagementException e = new APIManagementException(errorMessage, exceptionCode);
Map<String, String> paramList = new HashMap<>();
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
log.error(errorMessage, e);
return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
}
if (application != null) {
SubscriptionResponse addSubResponse = apiStore.addApiSubscription(apiId, applicationId, tier);
String subscriptionId = addSubResponse.getSubscriptionUUID();
Subscription subscription = apiStore.getSubscriptionByUUID(subscriptionId);
location = new URI(RestApiConstants.RESOURCE_PATH_SUBSCRIPTION + "/" + subscriptionId);
subscriptionDTO = SubscriptionMappingUtil.fromSubscriptionToDTO(subscription);
// be in either pending or approved state) send back the workflow response
if (SubscriptionStatus.ON_HOLD == subscription.getStatus()) {
WorkflowResponseDTO workflowResponse = MiscMappingUtil.fromWorkflowResponseToDTO(addSubResponse.getWorkflowResponse());
return Response.status(Response.Status.ACCEPTED).header(RestApiConstants.LOCATION_HEADER, location).entity(workflowResponse).build();
}
} else {
String errorMessage = null;
ExceptionCodes exceptionCode = null;
exceptionCode = ExceptionCodes.APPLICATION_NOT_FOUND;
errorMessage = "Application not found";
APIMgtResourceNotFoundException e = new APIMgtResourceNotFoundException(errorMessage, exceptionCode);
Map<String, String> paramList = new HashMap<>();
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
log.error(errorMessage, e);
return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
}
} catch (GatewayException e) {
String errorMessage = "Failed to add subscription of API : " + body.getApiIdentifier() + " to gateway";
log.error(errorMessage, e);
return Response.status(Response.Status.ACCEPTED).build();
} catch (APIManagementException e) {
String errorMessage = "Error while adding subscriptions";
Map<String, String> paramList = new HashMap<>();
paramList.put(APIMgtConstants.ExceptionsConstants.API_ID, body.getApiIdentifier());
paramList.put(APIMgtConstants.ExceptionsConstants.APPLICATION_ID, body.getApplicationId());
paramList.put(APIMgtConstants.ExceptionsConstants.TIER, body.getPolicy());
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 subscription : " + body.getSubscriptionId();
Map<String, String> paramList = new HashMap<>();
paramList.put(APIMgtConstants.ExceptionsConstants.SUBSCRIPTION_ID, body.getSubscriptionId());
ErrorHandler errorHandler = ExceptionCodes.LOCATION_HEADER_INCORRECT;
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(errorHandler, paramList);
log.error(errorMessage, e);
return Response.status(errorHandler.getHttpStatusCode()).entity(errorDTO).build();
}
return Response.status(Response.Status.CREATED).header(RestApiConstants.LOCATION_HEADER, location).entity(subscriptionDTO).build();
}
use of org.wso2.carbon.apimgt.rest.api.core.dto.SubscriptionDTO in project carbon-apimgt by wso2.
the class SubscriptionMappingUtil method fromSubscriptionListToDTO.
/**
* Converts a List object of SubscribedAPIs into a DTO
*
* @param subscriptions a list of SubscribedAPI objects
* @param limit max number of objects returned
* @param offset starting index
* @return SubscriptionListDTO object containing SubscriptionDTOs
*/
public static SubscriptionListDTO fromSubscriptionListToDTO(List<Subscription> subscriptions, Integer limit, Integer offset) {
SubscriptionListDTO subscriptionListDTO = new SubscriptionListDTO();
List<SubscriptionDTO> subscriptionDTOs = subscriptionListDTO.getList();
if (subscriptionDTOs == null) {
subscriptionDTOs = new ArrayList<>();
subscriptionListDTO.setList(subscriptionDTOs);
}
// identifying the proper start and end indexes
int size = subscriptions.size();
int start = offset < size && offset >= 0 ? offset : Integer.MAX_VALUE;
int end = offset + limit - 1 <= size - 1 ? offset + limit - 1 : size - 1;
for (int i = start; i <= end; i++) {
Subscription subscription = subscriptions.get(i);
subscriptionDTOs.add(fromSubscriptionToDTO(subscription));
}
subscriptionListDTO.setCount(subscriptionDTOs.size());
return subscriptionListDTO;
}
Aggregations