/*
 * Copyright 2012  Research Studios Austria Forschungsges.m.b.H.
 *
 *    Licensed 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.
 */

package won.node.camel.predicate;


import org.apache.camel.Exchange;
import org.apache.camel.Predicate;
import won.protocol.message.WonMessage;
import won.protocol.message.WonMessageDirection;
import won.protocol.message.WonMessageType;
import won.protocol.message.processor.camel.WonCamelConstants;

/**
 * Ture if the WonMessage in the wonOriginalMessage header is FROM_SYSTEM.
 */
public class IsSystemMessageToOwnerPredicate implements Predicate {

    @Override
    public boolean matches(Exchange exchange) {
        WonMessage message = (WonMessage) exchange.getIn().getHeader(WonCamelConstants.ORIGINAL_MESSAGE_HEADER);
        if (message == null) return false;
        if (message.getMessageType() == WonMessageType.SUCCESS_RESPONSE) return false;
        if (message.getMessageType() == WonMessageType.FAILURE_RESPONSE) return false;
        if (message.getEnvelopeType() != WonMessageDirection.FROM_SYSTEM) return false;
        return true;
    }
}

@@ -961,18 +961,18 @@
 /**%0A * T
-u
 r
+u
 e if the
@@ -1020,23 +1020,145 @@
 der 
-is FROM_SYSTEM.
+meets the criteria for echoing it to the owner:%0A * - sent from the system%0A * - if it is a response, it is not directed at the remote need
 %0A */
@@ -1464,38 +1464,39 @@
 (message.get
-Messag
+Envelop
 eType() 
 == WonMessag
@@ -1475,33 +1475,33 @@
 tEnvelopeType() 
-=
+!
 = WonMessageType
@@ -1500,29 +1500,29 @@
 sage
-Type.SUCCESS_RESPONSE
+Direction.FROM_SYSTEM
 ) re
@@ -1524,32 +1524,40 @@
 M) return false;
+        
 %0A        if (mes
@@ -1545,32 +1545,109 @@
    %0A        if (
+%0A            (
+message.getMessageType() == WonMessageType.SUCCESS_RESPONSE %7C%7C 
 message.getMessa
@@ -1691,29 +1691,16 @@
 SPONSE) 
-return false;
 %0A       
@@ -1692,34 +1692,38 @@
 PONSE) %0A        
-if
+    &&
  (message.getEnv
@@ -1723,58 +1723,199 @@
 .get
-EnvelopeType() != WonMessageDirection.FROM_SYSTEM)
+SenderNeedURI() != null && ! message.getSenderNeedURI().equals(message.getReceiverNeedURI()))) %7B%0A            //responses directed at remote needs are not to be echoed to the owner%0A           
  ret
@@ -1917,32 +1917,42 @@
   return false;%0A
+        %7D%0A
         return t

Allow echoing of local responses