Search in sources :

Example 36 with Request

use of org.wso2.msf4j.Request in project carbon-apimgt by wso2.

the class LabelsApiServiceImplTestCase method getRequest.

// Sample request to be used by tests
private Request getRequest() throws Exception {
    HTTPCarbonMessage carbonMessage = Mockito.mock(HTTPCarbonMessage.class);
    Mockito.when(carbonMessage.getProperty("LOGGED_IN_USER")).thenReturn(USER);
    Request request = new Request(carbonMessage);
    return request;
}
Also used : HTTPCarbonMessage(org.wso2.transport.http.netty.message.HTTPCarbonMessage) Request(org.wso2.msf4j.Request)

Example 37 with Request

use of org.wso2.msf4j.Request in project carbon-apimgt by wso2.

the class EndpointsApiServiceImplTestCase method getRequest.

// Sample request to be used by tests
private Request getRequest() throws Exception {
    HTTPCarbonMessage carbonMessage = Mockito.mock(HTTPCarbonMessage.class);
    Mockito.when(carbonMessage.getProperty("LOGGED_IN_USER")).thenReturn(USER);
    Request request = new Request(carbonMessage);
    return request;
}
Also used : HTTPCarbonMessage(org.wso2.transport.http.netty.message.HTTPCarbonMessage) Request(org.wso2.msf4j.Request)

Example 38 with Request

use of org.wso2.msf4j.Request in project carbon-apimgt by wso2.

the class PoliciesApiServiceImplTestCase method getRequest.

// Sample request to be used by tests
private Request getRequest() throws Exception {
    HTTPCarbonMessage carbonMessage = Mockito.mock(HTTPCarbonMessage.class);
    Mockito.when(carbonMessage.getProperty("LOGGED_IN_USER")).thenReturn(USER);
    Request request = new Request(carbonMessage);
    return request;
}
Also used : HTTPCarbonMessage(org.wso2.transport.http.netty.message.HTTPCarbonMessage) Request(org.wso2.msf4j.Request)

Example 39 with Request

use of org.wso2.msf4j.Request 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;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) HashMap(java.util.HashMap) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO) APIPublisher(org.wso2.carbon.apimgt.core.api.APIPublisher) Subscription(org.wso2.carbon.apimgt.core.models.Subscription) SubscriptionListDTO(org.wso2.carbon.apimgt.rest.api.publisher.dto.SubscriptionListDTO)

Example 40 with Request

use of org.wso2.msf4j.Request 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();
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) HashMap(java.util.HashMap) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO) GatewayException(org.wso2.carbon.apimgt.core.exception.GatewayException) APIPublisher(org.wso2.carbon.apimgt.core.api.APIPublisher) Subscription(org.wso2.carbon.apimgt.core.models.Subscription) APIMgtResourceNotFoundException(org.wso2.carbon.apimgt.core.exception.APIMgtResourceNotFoundException) SubscriptionDTO(org.wso2.carbon.apimgt.rest.api.publisher.dto.SubscriptionDTO)

Aggregations

APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)194 ErrorDTO (org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)143 APIStore (org.wso2.carbon.apimgt.core.api.APIStore)136 Request (org.wso2.msf4j.Request)126 HashMap (java.util.HashMap)106 Response (javax.ws.rs.core.Response)94 Test (org.junit.Test)92 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)89 APIPublisher (org.wso2.carbon.apimgt.core.api.APIPublisher)48 APIMgtAdminService (org.wso2.carbon.apimgt.core.api.APIMgtAdminService)44 ArrayList (java.util.ArrayList)37 WorkflowResponse (org.wso2.carbon.apimgt.core.api.WorkflowResponse)37 HTTPCarbonMessage (org.wso2.transport.http.netty.message.HTTPCarbonMessage)34 GeneralWorkflowResponse (org.wso2.carbon.apimgt.core.workflow.GeneralWorkflowResponse)30 Map (java.util.Map)25 ApplicationCreationResponse (org.wso2.carbon.apimgt.core.workflow.ApplicationCreationResponse)24 Application (org.wso2.carbon.apimgt.core.models.Application)23 Test (org.testng.annotations.Test)20 API (org.wso2.carbon.apimgt.core.models.API)17 APIMgtResourceNotFoundException (org.wso2.carbon.apimgt.core.exception.APIMgtResourceNotFoundException)16