void process(MessageContext synCtx) { if (log.isDebugEnabled()) { log.debug( "Processing message with ID: " + synCtx.getMessageID() + " through the " + "API: " + name); } synCtx.setProperty(RESTConstants.SYNAPSE_REST_API, getName()); synCtx.setProperty(RESTConstants.SYNAPSE_REST_API_VERSION, versionStrategy.getVersion()); synCtx.setProperty(RESTConstants.REST_API_CONTEXT, context); // Remove the API context part from the REST_URL_POSTFIX String restURLPostfix = (String) ((Axis2MessageContext) synCtx) .getAxis2MessageContext() .getProperty(NhttpConstants.REST_URL_POSTFIX); if (restURLPostfix != null) { if (!restURLPostfix.startsWith("/")) { restURLPostfix = "/" + restURLPostfix; } if (restURLPostfix.startsWith(context)) { restURLPostfix = restURLPostfix.substring(context.length()); if (versionStrategy instanceof URLBasedVersionStrategy) { String version = versionStrategy.getVersion(); if (restURLPostfix.startsWith(version)) { restURLPostfix = restURLPostfix.substring(version.length()); } else if (restURLPostfix.startsWith("/" + version)) { restURLPostfix = restURLPostfix.substring(version.length() + 1); } } ((Axis2MessageContext) synCtx) .getAxis2MessageContext() .setProperty(NhttpConstants.REST_URL_POSTFIX, restURLPostfix); } } for (Handler handler : handlers) { if (log.isDebugEnabled()) { log.debug( "Processing message with ID: " + synCtx.getMessageID() + " through " + "handler: " + handler.getClass().getName()); } boolean proceed; if (synCtx.isResponse()) { proceed = handler.handleResponse(synCtx); } else { proceed = handler.handleRequest(synCtx); } if (!proceed) { return; } } if (synCtx.isResponse()) { String resourceName = (String) synCtx.getProperty(RESTConstants.SYNAPSE_RESOURCE); if (resourceName != null) { Resource resource = resources.get(resourceName); if (resource != null) { resource.process(synCtx); } } else if (log.isDebugEnabled()) { log.debug("No resource information on the response: " + synCtx.getMessageID()); } return; } String path = RESTUtils.getFullRequestPath(synCtx); String subPath; if (versionStrategy.getVersionType().equals(VersionStrategyFactory.TYPE_URL)) { // for URL based // request --> http://{host:port}/context/version/path/to/resource subPath = path.substring(context.length() + versionStrategy.getVersion().length() + 1); } else { subPath = path.substring(context.length()); } if ("".equals(subPath)) { subPath = "/"; } synCtx.setProperty(RESTConstants.REST_SUB_REQUEST_PATH, subPath); org.apache.axis2.context.MessageContext msgCtx = ((Axis2MessageContext) synCtx).getAxis2MessageContext(); String hostHeader = getHostHeader(msgCtx); if (hostHeader != null) { synCtx.setProperty( RESTConstants.REST_URL_PREFIX, msgCtx.getIncomingTransportName() + "://" + hostHeader); } Set<Resource> acceptableResources = new HashSet<Resource>(); for (Resource r : resources.values()) { if (r.canProcess(synCtx)) { acceptableResources.add(r); } } boolean processed = false; if (!acceptableResources.isEmpty()) { for (RESTDispatcher dispatcher : RESTUtils.getDispatchers()) { Resource resource = dispatcher.findResource(synCtx, acceptableResources); if (resource != null) { resource.process(synCtx); processed = true; break; } } } if (!processed) { if (log.isDebugEnabled()) { log.debug("No matching resource was found for the request: " + synCtx.getMessageID()); } Mediator sequence = synCtx.getSequence(RESTConstants.NO_MATCHING_RESOURCE_HANDLER); if (sequence != null) { sequence.mediate(synCtx); } } }
public VerbInfoDTO findMatchingVerb(MessageContext synCtx) throws ResourceNotFoundException, APISecurityException { VerbInfoDTO verb = null; // This function is used by more than one handler. If on one execution of this function, it has // found and placed // the matching verb in the cache, the same can be re-used from all handlers since all handlers // share the same // MessageContext. The API_RESOURCE_CACHE_KEY property will be set in the MessageContext to // indicate that the // verb has been put into the cache. String resourceCacheKey = (String) synCtx.getProperty(APIConstants.API_RESOURCE_CACHE_KEY); if (resourceCacheKey != null) { verb = (VerbInfoDTO) getResourceCache().get(resourceCacheKey); // Cache hit if (verb != null) { if (log.isDebugEnabled()) { log.debug("Found resource in Cache for key: ".concat(resourceCacheKey)); } return verb; } if (log.isDebugEnabled()) { log.debug("Resource not found in cache for key: ".concat(resourceCacheKey)); } } String resourceString = (String) synCtx.getProperty(APIConstants.API_ELECTED_RESOURCE); String apiContext = (String) synCtx.getProperty(RESTConstants.REST_API_CONTEXT); String apiVersion = (String) synCtx.getProperty(RESTConstants.SYNAPSE_REST_API_VERSION); String fullRequestPath = (String) synCtx.getProperty(RESTConstants.REST_FULL_REQUEST_PATH); String apiName = (String) synCtx.getProperty(RESTConstants.SYNAPSE_REST_API); String requestPath = Utils.getRequestPath(synCtx, fullRequestPath, apiContext, apiVersion); if ("".equals(requestPath)) { requestPath = "/"; } if (log.isDebugEnabled()) { log.debug("Setting REST_SUB_REQUEST_PATH in msg context: ".concat(requestPath)); } synCtx.setProperty(RESTConstants.REST_SUB_REQUEST_PATH, requestPath); String httpMethod = (String) ((Axis2MessageContext) synCtx) .getAxis2MessageContext() .getProperty(Constants.Configuration.HTTP_METHOD); if (resourceString == null) { API selectedApi = synCtx.getConfiguration().getAPI(apiName); Resource selectedResource = null; if (selectedApi != null) { Resource[] selectedAPIResources = selectedApi.getResources(); Set<Resource> acceptableResources = new HashSet<Resource>(); for (Resource resource : selectedAPIResources) { // If the requesting method is OPTIONS or if the Resource contains the requesting method if (RESTConstants.METHOD_OPTIONS.equals(httpMethod) || (resource.getMethods() != null && Arrays.asList(resource.getMethods()).contains(httpMethod))) { acceptableResources.add(resource); } } if (acceptableResources.size() > 0) { for (RESTDispatcher dispatcher : RESTUtils.getDispatchers()) { Resource resource = dispatcher.findResource(synCtx, acceptableResources); if (resource != null && Arrays.asList(resource.getMethods()).contains(httpMethod)) { selectedResource = resource; break; } } } } if (selectedResource == null) { // No matching resource found. String msg = "Could not find matching resource for " + requestPath; log.error(msg); throw new ResourceNotFoundException(msg); } resourceString = selectedResource.getDispatcherHelper().getString(); resourceCacheKey = APIUtil.getResourceInfoDTOCacheKey(apiContext, apiVersion, resourceString, httpMethod); if (log.isDebugEnabled()) { log.debug("Selected Resource: ".concat(resourceString)); } // Set the elected resource synCtx.setProperty(APIConstants.API_ELECTED_RESOURCE, resourceString); } verb = (VerbInfoDTO) getResourceCache().get(resourceCacheKey); // Cache hit if (verb != null) { if (log.isDebugEnabled()) { log.debug("Got Resource from cache for key: ".concat(resourceCacheKey)); } // Set cache key in the message context so that it can be used by the subsequent handlers. synCtx.setProperty(APIConstants.API_RESOURCE_CACHE_KEY, resourceCacheKey); return verb; } if (log.isDebugEnabled()) { log.debug("Cache miss for Resource for key: ".concat(resourceCacheKey)); } String apiCacheKey = APIUtil.getAPIInfoDTOCacheKey(apiContext, apiVersion); APIInfoDTO apiInfoDTO = null; apiInfoDTO = (APIInfoDTO) getResourceCache().get(apiCacheKey); // Cache miss if (apiInfoDTO == null) { if (log.isDebugEnabled()) { log.debug("Could not find API object in cache for key: ".concat(apiCacheKey)); } apiInfoDTO = doGetAPIInfo(apiContext, apiVersion); getResourceCache().put(apiCacheKey, apiInfoDTO); } if (apiInfoDTO.getResources() != null) { for (ResourceInfoDTO resourceInfoDTO : apiInfoDTO.getResources()) { if ((resourceString.trim()).equalsIgnoreCase(resourceInfoDTO.getUrlPattern().trim())) { for (VerbInfoDTO verbDTO : resourceInfoDTO.getHttpVerbs()) { if (verbDTO.getHttpVerb().equals(httpMethod)) { if (log.isDebugEnabled()) { log.debug("Putting resource object in cache with key: ".concat(resourceCacheKey)); } verbDTO.setRequestKey(resourceCacheKey); // Store verb in cache getResourceCache().put(resourceCacheKey, verbDTO); // Set cache key in the message context so that it can be used by the subsequent // handlers. synCtx.setProperty(APIConstants.API_RESOURCE_CACHE_KEY, resourceCacheKey); return verbDTO; } } } } } return null; }