Search in sources :

Example 71 with Request

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

the class LabelInfoApiServiceImpl method labelInfoGet.

/**
 * Get label information for labels provided.
 *
 * @param labels          List of labels
 * @param ifNoneMatch     If-None-Match header value
 * @param ifModifiedSince If-Modified-Since header value
 * @param request         msf4j request object
 * @return Label List
 * @throws NotFoundException If failed to get the label values
 */
@Override
public Response labelInfoGet(String labels, String ifNoneMatch, String ifModifiedSince, Request request) throws NotFoundException {
    String username = RestApiUtil.getLoggedInUsername(request);
    LabelListDTO labelListDTO;
    try {
        APIStore apiStore = RestApiUtil.getConsumer(username);
        if (labels != null) {
            List<String> labelNames = Arrays.asList(labels.split(","));
            List<Label> labelList = apiStore.getLabelInfo(labelNames, username);
            labelListDTO = LabelMappingUtil.toLabelListDTO(labelList);
        } else {
            // mandatory parameters not provided
            String errorMessage = "Labels parameter should be provided";
            ErrorHandler errorHandler = ExceptionCodes.PARAMETER_NOT_PROVIDED;
            ErrorDTO errorDTO = RestApiUtil.getErrorDTO(errorHandler);
            log.error(errorMessage);
            return Response.status(errorHandler.getHttpStatusCode()).entity(errorDTO).build();
        }
    } catch (APIManagementException e) {
        String errorMessage = "Error occurred while retrieving label information";
        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(labelListDTO).build();
}
Also used : ErrorHandler(org.wso2.carbon.apimgt.core.exception.ErrorHandler) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) HashMap(java.util.HashMap) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO) Label(org.wso2.carbon.apimgt.core.models.Label) LabelListDTO(org.wso2.carbon.apimgt.rest.api.store.dto.LabelListDTO) APIStore(org.wso2.carbon.apimgt.core.api.APIStore)

Example 72 with Request

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

the class LabelsApiServiceImpl method labelsGet.

/**
 * Get all the labels.
 *
 * @param labelType       the type of the labels to be fetched
 * @param accept          Accept header value
 * @param ifNoneMatch     If-None-Match header value
 * @param request         ms4j request object
 * @return Lable List
 * @throws NotFoundException If failed to get the label values
 */
@Override
public Response labelsGet(String labelType, String accept, String ifNoneMatch, String ifModifiedSince, Request request) throws NotFoundException {
    String username = RestApiUtil.getLoggedInUsername(request);
    try {
        APIStore apiStore = RestApiUtil.getConsumer(username);
        List<Label> labels;
        if (labelType == null) {
            labels = apiStore.getAllLabels();
        } else {
            labels = apiStore.getLabelsByType(labelType);
        }
        LabelListDTO labelListDTO = LabelMappingUtil.toLabelListDTO(labels);
        return Response.ok().entity(labelListDTO).build();
    } catch (APIManagementException e) {
        String errorMessage = "Error occurred while retrieving Labels";
        HashMap<String, String> paramList = new HashMap<String, String>();
        org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
        log.error(errorMessage, e);
        return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
    }
}
Also used : HashMap(java.util.HashMap) Label(org.wso2.carbon.apimgt.core.models.Label) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) LabelListDTO(org.wso2.carbon.apimgt.rest.api.store.dto.LabelListDTO) APIStore(org.wso2.carbon.apimgt.core.api.APIStore)

Example 73 with Request

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

the class PoliciesApiServiceImpl method policiesTierLevelTierNameGet.

/**
 * Retrieves a tier by tier name and level
 *
 * @param tierName        Name of the tier
 * @param tierLevel       Level of the tier
 * @param ifNoneMatch     If-Non-Match header value
 * @param ifModifiedSince If-Modified-Since header value
 * @param request         msf4j request object
 * @return The requested tier as the response
 * @throws NotFoundException When the particular resource does not exist in the system
 */
@Override
public Response policiesTierLevelTierNameGet(String tierName, String tierLevel, String ifNoneMatch, String ifModifiedSince, Request request) throws NotFoundException {
    TierDTO tierDTO = null;
    String username = RestApiUtil.getLoggedInUsername(request);
    try {
        APIStore apiStore = RestApiUtil.getConsumer(username);
        String existingFingerprint = policiesTierLevelTierNameGetFingerprint(tierName, tierLevel, ifNoneMatch, ifModifiedSince, request);
        if (!StringUtils.isEmpty(ifNoneMatch) && !StringUtils.isEmpty(existingFingerprint) && ifNoneMatch.contains(existingFingerprint)) {
            return Response.notModified().build();
        }
        Policy tier = apiStore.getPolicy(RestApiUtil.mapRestApiPolicyLevelToPolicyLevelEnum(tierLevel), tierName);
        tierDTO = TierMappingUtil.fromTierToDTO(tier, tierLevel);
        return Response.ok().entity(tierDTO).header(HttpHeaders.ETAG, "\"" + existingFingerprint + "\"").build();
    } catch (APIManagementException e) {
        String errorMessage = "Error while retrieving tier";
        HashMap<String, String> paramList = new HashMap<String, String>();
        paramList.put(APIMgtConstants.ExceptionsConstants.TIER_LEVEL, tierLevel);
        ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
        log.error(errorMessage, e);
        return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
    }
}
Also used : Policy(org.wso2.carbon.apimgt.core.models.policy.Policy) TierDTO(org.wso2.carbon.apimgt.rest.api.store.dto.TierDTO) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) HashMap(java.util.HashMap) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO) APIStore(org.wso2.carbon.apimgt.core.api.APIStore)

Example 74 with Request

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

the class SelfSignupApiServiceImpl method selfSignupPost.

@Override
public Response selfSignupPost(UserDTO body, Request request) throws NotFoundException {
    try {
        APIStore apiStore = RestApiUtil.getConsumer();
        apiStore.selfSignUp(MiscMappingUtil.fromUserDTOToUser(body));
    } catch (APIManagementException e) {
        String errorMessage = "Error occurred while user signup: " + body.getUsername();
        Map<String, String> paramList = new HashMap<>();
        paramList.put(APIMgtConstants.ExceptionsConstants.USERNAME, body.getUsername());
        ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
        errorDTO.setDescription(e.getMessage());
        log.error(errorMessage, e);
        return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
    }
    body.setPassword(null);
    return Response.ok(body).build();
}
Also used : APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO) HashMap(java.util.HashMap) Map(java.util.Map) APIStore(org.wso2.carbon.apimgt.core.api.APIStore)

Example 75 with Request

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

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