Search in sources :

Example 21 with MethodStats

use of org.wso2.carbon.apimgt.gateway.MethodStats in project carbon-apimgt by wso2.

the class APIKeyValidator method getResourceAuthenticationScheme.

@MethodStats
public String getResourceAuthenticationScheme(MessageContext synCtx) throws APISecurityException {
    String authType = "";
    List<VerbInfoDTO> verbInfoList;
    try {
        verbInfoList = findMatchingVerb(synCtx);
        if (verbInfoList != null && verbInfoList.toArray().length > 0) {
            for (VerbInfoDTO verb : verbInfoList) {
                authType = verb.getAuthType();
                if (authType == null || !StringUtils.capitalize(APIConstants.AUTH_TYPE_NONE.toLowerCase()).equals(authType)) {
                    authType = StringUtils.capitalize(APIConstants.AUTH_APPLICATION_OR_USER_LEVEL_TOKEN.toLowerCase());
                    break;
                }
            }
            synCtx.setProperty(APIConstants.VERB_INFO_DTO, verbInfoList);
        }
    } catch (ResourceNotFoundException e) {
        log.error("Could not find matching resource for request", e);
        return APIConstants.NO_MATCHING_AUTH_SCHEME;
    }
    if (!authType.isEmpty()) {
        return authType;
    } else {
        // No matching resource found. return the highest level of security
        return APIConstants.NO_MATCHING_AUTH_SCHEME;
    }
}
Also used : VerbInfoDTO(org.wso2.carbon.apimgt.impl.dto.VerbInfoDTO) MethodStats(org.wso2.carbon.apimgt.gateway.MethodStats)

Example 22 with MethodStats

use of org.wso2.carbon.apimgt.gateway.MethodStats in project carbon-apimgt by wso2.

the class APIKeyValidator method getAPIProductURITemplates.

@MethodStats
protected ArrayList<URITemplate> getAPIProductURITemplates(MessageContext messageContext, String context, String apiVersion) throws APISecurityException {
    if (uriTemplates == null) {
        synchronized (this) {
            if (uriTemplates == null) {
                String swagger = (String) messageContext.getProperty(APIMgtGatewayConstants.OPEN_API_STRING);
                if (swagger != null) {
                    APIDefinition oasParser;
                    try {
                        oasParser = OASParserUtil.getOASParser(swagger);
                        uriTemplates = new ArrayList<>();
                        uriTemplates.addAll(oasParser.getURITemplates(swagger));
                        return uriTemplates;
                    } catch (APIManagementException e) {
                        log.error("Error while parsing swagger content to get URI Templates", e);
                    }
                }
                uriTemplates = dataStore.getAPIProductURITemplates(context, apiVersion);
            }
        }
    }
    return uriTemplates;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) APIDefinition(org.wso2.carbon.apimgt.api.APIDefinition) MethodStats(org.wso2.carbon.apimgt.gateway.MethodStats)

Example 23 with MethodStats

use of org.wso2.carbon.apimgt.gateway.MethodStats in project carbon-apimgt by wso2.

the class APIManagerExtensionHandler method mediate.

@MethodStats
public boolean mediate(MessageContext messageContext, String direction) {
    // In order to avoid a remote registry call occurring on each invocation, we
    // directly get the extension sequences from the local registry.
    Map localRegistry = messageContext.getConfiguration().getLocalRegistry();
    Object sequence = localRegistry.get(EXT_SEQUENCE_PREFIX + direction);
    if (sequence instanceof Mediator) {
        if (!((Mediator) sequence).mediate(messageContext)) {
            return false;
        }
    }
    String apiName = (String) messageContext.getProperty(RESTConstants.SYNAPSE_REST_API);
    sequence = localRegistry.get(apiName + "--" + direction);
    if (sequence instanceof Mediator) {
        return ((Mediator) sequence).mediate(messageContext);
    }
    return true;
}
Also used : Mediator(org.apache.synapse.Mediator) Map(java.util.Map) MethodStats(org.wso2.carbon.apimgt.gateway.MethodStats)

Example 24 with MethodStats

use of org.wso2.carbon.apimgt.gateway.MethodStats in project carbon-apimgt by wso2.

the class APIManagerExtensionHandler method handleRequest.

@MethodStats
public boolean handleRequest(MessageContext messageContext) {
    // (only for graphQL APIs)
    if (messageContext.getProperty(APIConstants.API_TYPE) != null && APIConstants.GRAPHQL_API.equals(messageContext.getProperty(APIConstants.API_TYPE).toString())) {
        ((Axis2MessageContext) messageContext).getAxis2MessageContext().setProperty(Constants.Configuration.HTTP_METHOD, messageContext.getProperty(APIConstants.HTTP_VERB));
    }
    Timer.Context context = startMetricTimer(DIRECTION_IN);
    long executionStartTime = System.nanoTime();
    TracingSpan requestMediationSpan = null;
    if (Util.tracingEnabled()) {
        TracingSpan responseLatencySpan = (TracingSpan) messageContext.getProperty(APIMgtGatewayConstants.RESOURCE_SPAN);
        TracingTracer tracer = Util.getGlobalTracer();
        requestMediationSpan = Util.startSpan(APIMgtGatewayConstants.REQUEST_MEDIATION, responseLatencySpan, tracer);
    }
    try {
        boolean isMediated = mediate(messageContext, DIRECTION_IN);
        if (isMediated) {
            String requestDestination = null;
            EndpointReference objectTo = ((Axis2MessageContext) messageContext).getAxis2MessageContext().getOptions().getTo();
            if (objectTo != null) {
                requestDestination = objectTo.getAddress();
            }
            if (requestDestination != null) {
                messageContext.setProperty(APIMgtGatewayConstants.SYNAPSE_ENDPOINT_ADDRESS, requestDestination);
            }
        }
        return isMediated;
    } catch (Exception e) {
        if (Util.tracingEnabled() && requestMediationSpan != null) {
            Util.setTag(requestMediationSpan, APIMgtGatewayConstants.ERROR, APIMgtGatewayConstants.REQUEST_MEDIATION_ERROR);
        }
        throw e;
    } finally {
        if (Util.tracingEnabled()) {
            Util.finishSpan(requestMediationSpan);
        }
        messageContext.setProperty(APIMgtGatewayConstants.REQUEST_MEDIATION_LATENCY, TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - executionStartTime));
        stopMetricTimer(context);
    }
}
Also used : Timer(org.wso2.carbon.metrics.manager.Timer) TracingTracer(org.wso2.carbon.apimgt.tracing.TracingTracer) TracingSpan(org.wso2.carbon.apimgt.tracing.TracingSpan) EndpointReference(org.apache.axis2.addressing.EndpointReference) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext) MethodStats(org.wso2.carbon.apimgt.gateway.MethodStats)

Example 25 with MethodStats

use of org.wso2.carbon.apimgt.gateway.MethodStats in project carbon-apimgt by wso2.

the class APIManagerExtensionHandler method handleResponse.

@MethodStats
public boolean handleResponse(MessageContext messageContext) {
    Timer.Context context = startMetricTimer(DIRECTION_OUT);
    long executionStartTime = System.nanoTime();
    TracingSpan responseMediationSpan = null;
    if (Util.tracingEnabled()) {
        TracingSpan responseLatencySpan = (TracingSpan) messageContext.getProperty(APIMgtGatewayConstants.RESOURCE_SPAN);
        TracingTracer tracer = Util.getGlobalTracer();
        responseMediationSpan = Util.startSpan(APIMgtGatewayConstants.RESPONSE_MEDIATION, responseLatencySpan, tracer);
    }
    try {
        return mediate(messageContext, DIRECTION_OUT);
    } catch (Exception e) {
        if (Util.tracingEnabled() && responseMediationSpan != null) {
            Util.setTag(responseMediationSpan, APIMgtGatewayConstants.ERROR, APIMgtGatewayConstants.RESPONSE_MEDIATION_ERROR);
        }
        throw e;
    } finally {
        if (Util.tracingEnabled()) {
            Util.finishSpan(responseMediationSpan);
        }
        messageContext.setProperty(APIMgtGatewayConstants.RESPONSE_MEDIATION_LATENCY, TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - executionStartTime));
        stopMetricTimer(context);
    }
}
Also used : Timer(org.wso2.carbon.metrics.manager.Timer) TracingTracer(org.wso2.carbon.apimgt.tracing.TracingTracer) TracingSpan(org.wso2.carbon.apimgt.tracing.TracingSpan) MethodStats(org.wso2.carbon.apimgt.gateway.MethodStats)

Aggregations

MethodStats (org.wso2.carbon.apimgt.gateway.MethodStats)15 MethodStats (org.wso2.carbon.apimgt.rest.api.util.MethodStats)11 Axis2MessageContext (org.apache.synapse.core.axis2.Axis2MessageContext)8 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)8 APISecurityException (org.wso2.carbon.apimgt.gateway.handlers.security.APISecurityException)6 Timer (org.wso2.carbon.metrics.manager.Timer)6 TracingSpan (org.wso2.carbon.apimgt.tracing.TracingSpan)5 TracingTracer (org.wso2.carbon.apimgt.tracing.TracingTracer)5 HashMap (java.util.HashMap)4 Map (java.util.Map)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3 JWTValidationInfo (org.wso2.carbon.apimgt.common.gateway.dto.JWTValidationInfo)3 AuthenticationContext (org.wso2.carbon.apimgt.gateway.handlers.security.AuthenticationContext)3 APIKeyValidationInfoDTO (org.wso2.carbon.apimgt.impl.dto.APIKeyValidationInfoDTO)3 JWTClaimsSet (com.nimbusds.jwt.JWTClaimsSet)2 TreeMap (java.util.TreeMap)2 AuthenticationException (org.apache.cxf.interceptor.security.AuthenticationException)2 Mediator (org.apache.synapse.Mediator)2 AuthenticationResponse (org.wso2.carbon.apimgt.gateway.handlers.security.AuthenticationResponse)2