use of org.wso2.carbon.messaging.Header 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.messaging.Header in project carbon-apimgt by wso2.
the class TagsApiServiceImpl method tagsGet.
/**
* Retrieve tags of APIs
*
* @param limit Maximum number of tags to return
* @param offset Starting position of the pagination
* @param ifNoneMatch If-None-Match header value
* @param request msf4j request object
* @return A list of qualifying tags as the response
* @throws NotFoundException When the particular resource does not exist in the system
*/
@Override
public Response tagsGet(Integer limit, Integer offset, String ifNoneMatch, Request request) throws NotFoundException {
TagListDTO tagListDTO = null;
limit = limit != null ? limit : RestApiConstants.PAGINATION_LIMIT_DEFAULT;
offset = offset != null ? offset : RestApiConstants.PAGINATION_OFFSET_DEFAULT;
String username = RestApiUtil.getLoggedInUsername(request);
try {
APIStore apiStore = RestApiUtil.getConsumer(username);
List<Tag> tagList = apiStore.getAllTags();
tagListDTO = TagMappingUtil.fromTagListToDTO(tagList, limit, offset);
} catch (APIManagementException e) {
String errorMessage = "Error while retrieving tags";
HashMap<String, String> paramList = new HashMap<String, String>();
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
log.error(errorMessage, e);
return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
}
return Response.ok().entity(tagListDTO).build();
}
use of org.wso2.carbon.messaging.Header in project carbon-apimgt by wso2.
the class CompositeApisApiServiceImpl method compositeApisApiIdGet.
/**
* Retrives an API by UUID
*
* @param apiId UUID of API
* @param ifNoneMatch If-None-Match header value
* @param ifModifiedSince If-Modified-Since header value
* @param request msf4j request object
* @return API which is identified by the given UUID
* @throws NotFoundException When the particular resource does not exist in the system
*/
@Override
public Response compositeApisApiIdGet(String apiId, String ifNoneMatch, String ifModifiedSince, Request request) throws NotFoundException {
String username = RestApiUtil.getLoggedInUsername(request);
try {
if (!RestApiUtil.getConsumer(username).isCompositeAPIExist(apiId)) {
String errorMessage = "API not found : " + apiId;
APIMgtResourceNotFoundException e = new APIMgtResourceNotFoundException(errorMessage, ExceptionCodes.API_NOT_FOUND);
HashMap<String, String> paramList = new HashMap<String, String>();
paramList.put(APIMgtConstants.ExceptionsConstants.API_ID, apiId);
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
}
String existingFingerprint = compositeApisApiIdGetFingerprint(apiId, ifNoneMatch, ifModifiedSince, request);
if (!StringUtils.isEmpty(ifNoneMatch) && !StringUtils.isEmpty(existingFingerprint) && ifNoneMatch.contains(existingFingerprint)) {
return Response.notModified().build();
}
CompositeAPIDTO apidto = CompositeAPIMappingUtil.toCompositeAPIDTO(RestApiUtil.getConsumer(username).getCompositeAPIbyId(apiId));
return Response.ok().header(HttpHeaders.ETAG, "\"" + existingFingerprint + "\"").entity(apidto).build();
} catch (APIManagementException e) {
String errorMessage = "Error while retrieving 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.messaging.Header in project carbon-apimgt by wso2.
the class CompositeApisApiServiceImpl method compositeApisApiIdImplementationGet.
@Override
public Response compositeApisApiIdImplementationGet(String apiId, String ifNoneMatch, String ifModifiedSince, Request request) throws NotFoundException {
String username = RestApiUtil.getLoggedInUsername(request);
try {
APIStore apiStore = RestApiUtil.getConsumer(username);
String existingFingerprint = compositeApisApiIdImplementationGetFingerprint(apiId, ifNoneMatch, ifModifiedSince, request);
if (!StringUtils.isEmpty(ifNoneMatch) && !StringUtils.isEmpty(existingFingerprint) && ifNoneMatch.contains(existingFingerprint)) {
return Response.notModified().build();
}
InputStream implementation = apiStore.getCompositeApiImplementation(apiId);
return Response.ok().header(HttpHeaders.ETAG, "\"" + existingFingerprint + "\"").entity(implementation).build();
} catch (APIManagementException e) {
HashMap<String, String> paramList = new HashMap<String, String>();
paramList.put(APIMgtConstants.ExceptionsConstants.API_ID, apiId);
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
}
}
use of org.wso2.carbon.messaging.Header in project carbon-apimgt by wso2.
the class CommonThrottleMappingUtilTestCase method fromHeaderConditionDtoToHeaderConditionModelTest.
@Test(description = "Convert Header Condition DTO to HeaderCondition Model object")
public void fromHeaderConditionDtoToHeaderConditionModelTest() throws Exception {
ThrottleConditionDTO throttleConditionDTO = new ThrottleConditionDTO();
throttleConditionDTO.setType(PolicyConstants.HEADER_CONDITION_TYPE);
HeaderConditionDTO headerConditionDTO = new HeaderConditionDTO();
headerConditionDTO.setHeaderName("testHeader");
headerConditionDTO.setHeaderValue("testHeaderValue");
throttleConditionDTO.setHeaderCondition(headerConditionDTO);
HeaderCondition condition = (HeaderCondition) CommonThrottleMappingUtil.fromDTOToCondition(throttleConditionDTO);
Assert.assertNotNull(condition);
assertEquals(condition.getHeaderName(), "testHeader");
assertEquals(condition.getValue(), "testHeaderValue");
}
Aggregations