<?xml version="1.0" encoding="ISO-8859-1"?>
<!--
  ~  Copyright (c) 2005-2010, WSO2 Inc. (http://wso2.com) All Rights Reserved.
  ~
  ~  WSO2 Inc. licenses this file to you under the Apache License,
  ~  Version 2.0 (the "License"); you may not use this file except
  ~  in compliance with the License.
  ~  You may obtain a copy of the License at
  ~
  ~    http://www.apache.org/licenses/LICENSE-2.0
  ~
  ~  Unless required by applicable law or agreed to in writing,
  ~  software distributed under the License is distributed on an
  ~  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  ~  KIND, either express or implied.  See the License for the
  ~  specific language governing permissions and limitations
  ~  under the License.
  -->

<!-- Default main sequence shipped with the WSO2 ESB -->
<sequence xmlns="http://ws.apache.org/ns/synapse" name="main">
    <description>The default main sequence for API manager - Returns 404 Not Found</description>
    <in>
        <log level="custom">
            <property name="STATUS" value="Message dispatched to the main sequence. Invalid URL."/>
            <property name="RESOURCE" expression="fn:concat('/', $axis2:REST_URL_POSTFIX)"/>
            <property name="HEALTH CHECK URL" expression="$axis2:TransportInURL"/>
        </log>
        <property name="message.builder.invoked" value="true" scope="axis2" type="BOOLEAN"/>
        <property name="url_axis2" expression="$axis2:TransportInURL"/>
        <filter source="$axis2:TransportInURL" regex="/">
            <payloadFactory media-type="xml">
                <format>
                    <html>
                        <body>
                            <H1>Welcome to APIM</H1>
                            <a href="/service" style="display: none">service</a>
                        </body>
                    </html>
                </format>
            </payloadFactory>
            <property name="messageType" value="text/html" scope="axis2"/>
            <property name="ContentType" value="text/html" scope="axis2"/>
            <respond/>
        </filter>
        <filter source="$ctx:TRANSPORT_DENIED" regex="true">
            <payloadFactory media-type="json">
                <format>{"code":"403","type":"Status report","message":"Forbidden","description":"Unsupported Transport [ $1 ]. The requested resource is not available."}
                </format>
                <args>
                     <arg expression="$ctx:IN_TRANSPORT"/>
                </args>
            </payloadFactory>
            <property name="HTTP_SC" value="403" scope="axis2"/>
            <property name="RESPONSE" value="true"/>
            <header name="To" action="remove"/>
            <property name="NO_ENTITY_BODY" scope="axis2" action="remove"/>
            <property name="ContentType" scope="axis2" action="remove"/>
            <property name="Authorization" scope="transport" action="remove"/>
            <property name="Host" scope="transport" action="remove"/>
            <property name="Accept" scope="transport" action="remove"/>
            <send/>
            <drop/>
        </filter>
        <filter source="get-property('MESSAGE_FORMAT')" regex="soap1[1-2]">
            <then>
                <property name="SOAP_FAULT_CODE" value="Server"/>
                <makefault>
                    <code expression="$ctx:SOAP_FAULT_CODE"/>
                    <reason value="The requested endpoint is not available."/>
                </makefault>
            </then>
            <else>
                <payloadFactory media-type="json">
                    <format>{"code":"404","type":"Status report","message":"Not Found","description":"The requested resource is not available."}
                    </format>
                    <args>
                        <arg expression="$ctx:IN_TRANSPORT"/>
                    </args>
                </payloadFactory>
            </else>
        </filter>
        <filter source="$axis2:HTTP_METHOD" regex="^(?!.*(POST|PUT|PATCH)).*$">
            <property name="messageType" value="application/json" scope="axis2"/>
        </filter>
        <property name="HTTP_SC" value="404" scope="axis2"/>
        <property name="RESPONSE" value="true"/>
        <header name="To" action="remove"/>
        <property name="NO_ENTITY_BODY" scope="axis2" action="remove"/>
        <property name="ContentType" scope="axis2" action="remove"/>
        <property name="Authorization" scope="transport" action="remove"/>
        <property name="Host" scope="transport" action="remove"/>
        <property name="Accept" scope="transport" action="remove"/>
        <property name="X-JWT-Assertion" scope="transport" action="remove"/>
        <send/>
        <drop/>
    </in>
    <out>
        <log level="full">
            <property name="STATUS" value="Unexpected response message in out flow"/>
        </log>
        <drop/>
    </out>
</sequence>
