Search in sources :

Example 1 with URLBasedVersionStrategy

use of org.apache.synapse.rest.version.URLBasedVersionStrategy in project wso2-synapse by wso2.

the class API method process.

void process(MessageContext synCtx) {
    auditDebug("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);
    synCtx.setProperty(RESTConstants.SYNAPSE_REST_API_VERSION_STRATEGY, versionStrategy.getVersionType());
    // get API log for this message and attach to the message context
    ((Axis2MessageContext) synCtx).setServiceLog(apiLog);
    // Calculate REST_URL_POSTFIX from full request path
    String restURLPostfix = (String) synCtx.getProperty(RESTConstants.REST_FULL_REQUEST_PATH);
    if (!synCtx.isResponse() && restURLPostfix != null) {
        // Skip for response path
        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) {
        auditDebug("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()) {
            auditDebug("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 LinkedHashSet<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) {
                if (synCtx.getEnvironment().isDebuggerEnabled()) {
                    if (!synCtx.isResponse()) {
                        SynapseWireLogHolder wireLogHolder = (SynapseWireLogHolder) ((Axis2MessageContext) synCtx).getAxis2MessageContext().getProperty(SynapseDebugInfoHolder.SYNAPSE_WIRE_LOG_HOLDER_PROPERTY);
                        if (wireLogHolder == null) {
                            wireLogHolder = new SynapseWireLogHolder();
                        }
                        if (synCtx.getProperty(RESTConstants.SYNAPSE_REST_API) != null && !synCtx.getProperty(RESTConstants.SYNAPSE_REST_API).toString().isEmpty()) {
                            wireLogHolder.setApiName(synCtx.getProperty(RESTConstants.SYNAPSE_REST_API).toString());
                            if (resource.getDispatcherHelper() != null) {
                                if (resource.getDispatcherHelper().getString() != null && !resource.getDispatcherHelper().getString().isEmpty()) {
                                    wireLogHolder.setResourceUrlString(resource.getDispatcherHelper().getString());
                                }
                            }
                        }
                        ((Axis2MessageContext) synCtx).getAxis2MessageContext().setProperty(SynapseDebugInfoHolder.SYNAPSE_WIRE_LOG_HOLDER_PROPERTY, wireLogHolder);
                    }
                }
                resource.process(synCtx);
                return;
            }
        }
        handleResourceNotFound(synCtx);
    } else {
        // This will get executed only in unhappy path. So ok to have the iterator.
        boolean resourceFound = false;
        boolean matchingMethodFound = false;
        for (RESTDispatcher dispatcher : RESTUtils.getDispatchers()) {
            Resource resource = dispatcher.findResource(synCtx, resources.values());
            if (resource != null) {
                resourceFound = true;
                String method = (String) msgCtx.getProperty(Constants.Configuration.HTTP_METHOD);
                matchingMethodFound = resource.hasMatchingMethod(method);
                break;
            }
        }
        if (!resourceFound) {
            handleResourceNotFound(synCtx);
        } else if (resourceFound && !matchingMethodFound) {
            // Resource found, but in that resource, requested method not allowed. So sending method not allowed http status (405)
            msgCtx.setProperty(SynapseConstants.HTTP_SC, HttpStatus.SC_METHOD_NOT_ALLOWED);
            msgCtx.removeProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS);
            msgCtx.setProperty("NIO-ACK-Requested", true);
        } else {
            // Resource found, and matching method also found, which means request is BAD_REQUEST(400)
            msgCtx.setProperty(SynapseConstants.HTTP_SC, HttpStatus.SC_BAD_REQUEST);
            msgCtx.setProperty("NIO-ACK-Requested", true);
        }
    }
}
Also used : SynapseWireLogHolder(org.apache.synapse.transport.http.conn.SynapseWireLogHolder) RESTDispatcher(org.apache.synapse.rest.dispatch.RESTDispatcher) URLBasedVersionStrategy(org.apache.synapse.rest.version.URLBasedVersionStrategy) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext)

Example 2 with URLBasedVersionStrategy

use of org.apache.synapse.rest.version.URLBasedVersionStrategy in project wso2-synapse by wso2.

the class APIDispatcherTest method testAPIURLVersionBasedDispatch.

public void testAPIURLVersionBasedDispatch() throws Exception {
    API api = new API(TEST_API, "/test");
    api.setVersionStrategy(new URLBasedVersionStrategy(api, TEST_API_VERSION, null));
    SynapseConfiguration synapseConfig = new SynapseConfiguration();
    synapseConfig.addAPI(api.getName(), api);
    RESTRequestHandler handler = new RESTRequestHandler();
    // Messages with '/test' context should NOT be dispatched
    MessageContext synCtx = getMessageContext(synapseConfig, false, "/test/", "GET");
    handler.process(synCtx);
    assertNull(synCtx.getProperty(RESTConstants.SYNAPSE_REST_API));
    assertNull(synCtx.getProperty(RESTConstants.SYNAPSE_REST_API_VERSION));
    synCtx = getMessageContext(synapseConfig, false, "/test/1.0.0", "GET");
    handler.process(synCtx);
    assertEquals(api.getName(), synCtx.getProperty(RESTConstants.SYNAPSE_REST_API));
    assertEquals(TEST_API_VERSION, synCtx.getProperty(RESTConstants.SYNAPSE_REST_API_VERSION));
    synCtx = getMessageContext(synapseConfig, false, "/test/1.0.0/", "GET");
    handler.process(synCtx);
    assertEquals(api.getName(), synCtx.getProperty(RESTConstants.SYNAPSE_REST_API));
    assertEquals(TEST_API_VERSION, synCtx.getProperty(RESTConstants.SYNAPSE_REST_API_VERSION));
    synCtx = getMessageContext(synapseConfig, false, "/test/1.0.0/foo/bar?a=5", "GET");
    handler.process(synCtx);
    assertEquals(api.getName(), synCtx.getProperty(RESTConstants.SYNAPSE_REST_API));
    assertEquals(TEST_API_VERSION, synCtx.getProperty(RESTConstants.SYNAPSE_REST_API_VERSION));
    synCtx = getMessageContext(synapseConfig, false, "/test/1.0.0?a=5", "GET");
    handler.process(synCtx);
    assertEquals(api.getName(), synCtx.getProperty(RESTConstants.SYNAPSE_REST_API));
    assertEquals(TEST_API_VERSION, synCtx.getProperty(RESTConstants.SYNAPSE_REST_API_VERSION));
    // Message with '/test' context & URL as a Query Parameter should be dispatched
    synCtx = getMessageContext(synapseConfig, false, "/test/1.0.0?a=http://localhost.com", "GET");
    handler.process(synCtx);
    assertEquals(api.getName(), synCtx.getProperty(RESTConstants.SYNAPSE_REST_API));
    assertEquals(TEST_API_VERSION, synCtx.getProperty(RESTConstants.SYNAPSE_REST_API_VERSION));
    // Messages WITHOUT the '/test' context should NOT be dispatched
    synCtx = getMessageContext(synapseConfig, false, "/foo/test/bar?a=5", "GET");
    handler.process(synCtx);
    assertNull(synCtx.getProperty(RESTConstants.SYNAPSE_REST_API));
    assertNull(synCtx.getProperty(RESTConstants.SYNAPSE_REST_API_VERSION));
    // Messages WITHOUT the '/test' context and proper version should NOT be dispatched
    synCtx = getMessageContext(synapseConfig, false, "/test/1.0.1/foo/bar?a=5", "GET");
    handler.process(synCtx);
    assertNull(synCtx.getProperty(RESTConstants.SYNAPSE_REST_API));
    assertNull(synCtx.getProperty(RESTConstants.SYNAPSE_REST_API_VERSION));
    synCtx = getMessageContext(synapseConfig, false, "/test/2.0/foo/bar?a=5", "GET");
    handler.process(synCtx);
    assertNull(synCtx.getProperty(RESTConstants.SYNAPSE_REST_API));
    assertNull(synCtx.getProperty(RESTConstants.SYNAPSE_REST_API_VERSION));
    synCtx = getMessageContext(synapseConfig, false, "/test/2.0.0.0/foo/bar?a=5", "GET");
    handler.process(synCtx);
    assertNull(synCtx.getProperty(RESTConstants.SYNAPSE_REST_API));
    assertNull(synCtx.getProperty(RESTConstants.SYNAPSE_REST_API_VERSION));
}
Also used : MessageContext(org.apache.synapse.MessageContext) SynapseConfiguration(org.apache.synapse.config.SynapseConfiguration) URLBasedVersionStrategy(org.apache.synapse.rest.version.URLBasedVersionStrategy)

Example 3 with URLBasedVersionStrategy

use of org.apache.synapse.rest.version.URLBasedVersionStrategy in project wso2-synapse by wso2.

the class BasicAPIMediationTest method testRestURLPostfix1.

public void testRestURLPostfix1() throws Exception {
    API api = new API(TEST_API, "/test");
    SynapseConfiguration synapseConfig = new SynapseConfiguration();
    synapseConfig.addAPI(TEST_API, api);
    RESTRequestHandler handler = new RESTRequestHandler();
    MessageContext synCtx = getMessageContext(synapseConfig, false, "/test", "GET");
    handler.process(synCtx);
    checkRestURLPostfix(synCtx, "");
    synCtx = getMessageContext(synapseConfig, false, "/test/me/now", "GET");
    handler.process(synCtx);
    checkRestURLPostfix(synCtx, "/me/now");
    synCtx = getMessageContext(synapseConfig, false, "/test?a=5", "GET");
    handler.process(synCtx);
    checkRestURLPostfix(synCtx, "?a=5");
    api.setVersionStrategy(new URLBasedVersionStrategy(api, "1.0.0", null));
    synCtx = getMessageContext(synapseConfig, false, "/test/1.0.0?a=5", "GET");
    handler.process(synCtx);
    checkRestURLPostfix(synCtx, "?a=5");
    synCtx = getMessageContext(synapseConfig, false, "/test/1.0.0/foo?a=5", "GET");
    handler.process(synCtx);
    checkRestURLPostfix(synCtx, "/foo?a=5");
}
Also used : MessageContext(org.apache.synapse.MessageContext) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext) SynapseConfiguration(org.apache.synapse.config.SynapseConfiguration) URLBasedVersionStrategy(org.apache.synapse.rest.version.URLBasedVersionStrategy)

Aggregations

URLBasedVersionStrategy (org.apache.synapse.rest.version.URLBasedVersionStrategy)3 MessageContext (org.apache.synapse.MessageContext)2 SynapseConfiguration (org.apache.synapse.config.SynapseConfiguration)2 Axis2MessageContext (org.apache.synapse.core.axis2.Axis2MessageContext)2 RESTDispatcher (org.apache.synapse.rest.dispatch.RESTDispatcher)1 SynapseWireLogHolder (org.apache.synapse.transport.http.conn.SynapseWireLogHolder)1