use of org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO in project carbon-apimgt by wso2.
the class ApisApiServiceImpl method apisApiIdSdksLanguageGet.
/**
* Generates an SDK for a API with provided ID and language
*
* @param apiId API ID
* @param language SDK language
* @param request msf4j request object
* @return ZIP file for the generated SDK
* @throws NotFoundException if failed to find method implementation
*/
@Override
public Response apisApiIdSdksLanguageGet(String apiId, String language, Request request) throws NotFoundException {
if (StringUtils.isBlank(apiId) || StringUtils.isBlank(language)) {
String errorMessage = "API ID or language is not valid";
log.error(errorMessage);
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(errorMessage, 400L, errorMessage);
return Response.status(Response.Status.BAD_REQUEST).entity(errorDTO).build();
}
String userName = RestApiUtil.getLoggedInUsername(request);
ApiStoreSdkGenerationManager sdkGenerationManager = new ApiStoreSdkGenerationManager();
if (!sdkGenerationManager.getSdkGenLanguages().containsKey(language)) {
String errorMessage = "Specified language parameter doesn't exist";
log.error(errorMessage);
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(errorMessage, 400L, errorMessage);
return Response.status(Response.Status.BAD_REQUEST).entity(errorDTO).build();
}
String tempZipFilePath;
try {
tempZipFilePath = sdkGenerationManager.generateSdkForApi(apiId, language, userName);
} catch (ApiStoreSdkGenerationException e) {
String errorMessage = "Error while generating SDK for requested language";
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();
} catch (APIManagementException e) {
String errorMessage = "Error while retrieving API for SDK generation " + 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();
}
File sdkZipFile = new File(tempZipFilePath);
return Response.ok().entity(sdkZipFile).header("Content-Disposition", "attachment; filename=\"" + sdkZipFile.getName() + "\"").build();
}
use of org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO in project carbon-apimgt by wso2.
the class SubscriptionsApiServiceImpl method subscriptionsGet.
/**
* Retrieve all subscriptions for a particular API
*
* @param apiId ID of the API
* @param limit Maximum subscriptions to return
* @param offset Starting position of the pagination
* @param ifNoneMatch If-Match header value
* @param request ms4j request object
* @return List of qualifying subscriptions DTOs as the response
* @throws NotFoundException When the particular resource does not exist in the system
*/
@Override
public Response subscriptionsGet(String apiId, Integer limit, Integer offset, String ifNoneMatch, Request request) throws NotFoundException {
String username = RestApiUtil.getLoggedInUsername(request);
List<Subscription> subscriptionList;
try {
APIPublisher apiPublisher = RestAPIPublisherUtil.getApiPublisher(username);
if (StringUtils.isNotEmpty(apiId)) {
subscriptionList = apiPublisher.getSubscriptionsByAPI(apiId);
SubscriptionListDTO subscriptionListDTO = MappingUtil.fromSubscriptionListToDTO(subscriptionList, limit, offset);
return Response.ok().entity(subscriptionListDTO).build();
} else {
RestApiUtil.handleBadRequest("API ID can not be null", log);
}
} catch (APIManagementException e) {
String errorMessage = "Error while retrieving subscriptions of 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();
}
return null;
}
use of org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO 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.common.dto.ErrorDTO in project carbon-apimgt by wso2.
the class EndpointsApiServiceImpl method endpointsHead.
@Override
public Response endpointsHead(String name, String ifNoneMatch, Request request) throws NotFoundException {
String username = RestApiUtil.getLoggedInUsername(request);
boolean status;
try {
APIPublisher apiPublisher = RestAPIPublisherUtil.getApiPublisher(username);
if (apiPublisher.isEndpointExist(name)) {
return Response.status(Response.Status.OK).build();
} else {
return Response.status(Response.Status.NOT_FOUND).build();
}
} catch (APIManagementException e) {
String errorMessage = "Error while checking status.";
HashMap<String, String> paramList = new HashMap<String, String>();
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList, e);
log.error(errorMessage, e);
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 EndpointsApiServiceImpl method endpointsEndpointIdPut.
/**
* Updates an existing endpoint
*
* @param endpointId ID of the endpoint
* @param body Updated endpoint details
* @param ifMatch If-Match header value
* @param ifUnmodifiedSince If-Unmodified-Since header value
* @param request msf4j request object
* @return updated endpoint
* @throws NotFoundException When the particular resource does not exist in the system
*/
@Override
public Response endpointsEndpointIdPut(String endpointId, EndPointDTO body, String ifMatch, String ifUnmodifiedSince, Request request) throws NotFoundException {
String username = RestApiUtil.getLoggedInUsername(request);
try {
APIPublisher apiPublisher = RestAPIPublisherUtil.getApiPublisher(username);
Endpoint endpoint = MappingUtil.toEndpoint(body);
Endpoint retrievedEndpoint = apiPublisher.getEndpoint(endpointId);
if (retrievedEndpoint == null) {
String msg = "Endpoint not found " + endpointId;
log.error(msg);
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(ExceptionCodes.ENDPOINT_NOT_FOUND);
return Response.status(ExceptionCodes.ENDPOINT_NOT_FOUND.getHttpStatusCode()).entity(errorDTO).build();
}
String existingFingerprint = endpointsEndpointIdGetFingerprint(endpointId, null, null, request);
if (!StringUtils.isEmpty(ifMatch) && !StringUtils.isEmpty(existingFingerprint) && !ifMatch.contains(existingFingerprint)) {
return Response.status(Response.Status.PRECONDITION_FAILED).build();
}
Endpoint updatedEndpint = new Endpoint.Builder(endpoint).id(endpointId).build();
apiPublisher.updateEndpoint(updatedEndpint);
Endpoint updatedEndpoint = apiPublisher.getEndpoint(endpointId);
String newFingerprint = endpointsEndpointIdGetFingerprint(endpointId, null, null, request);
return Response.ok().header(HttpHeaders.ETAG, "\"" + newFingerprint + "\"").entity(MappingUtil.toEndPointDTO(updatedEndpoint)).build();
} catch (APIManagementException e) {
String errorMessage = "Error while getting the endpoint :" + endpointId;
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler());
log.error(errorMessage, e);
return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
} catch (JsonProcessingException e) {
String errorMessage = "Error while Converting Endpoint Security Details in Endpoint :" + endpointId;
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(errorMessage, 900313L, errorMessage);
log.error(errorMessage, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorDTO).build();
} catch (IOException e) {
String errorMessage = "Error while Converting Endpoint Security Details in Endpoint :" + endpointId;
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(errorMessage, 900313L, errorMessage);
log.error(errorMessage, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorDTO).build();
}
}
Aggregations