Search in sources :

Example 1 with GetFilterRulesResponse

use of com.zimbra.soap.mail.message.GetFilterRulesResponse in project zm-mailbox by Zimbra.

the class GetFilterRules method handle.

@Override
public Element handle(Element req, Map<String, Object> context) throws ServiceException {
    ZimbraSoapContext zsc = getZimbraSoapContext(context);
    Account account = getRequestedAccount(zsc);
    if (!canAccessAccount(zsc, account)) {
        throw ServiceException.PERM_DENIED("cannot access account");
    }
    GetFilterRulesResponse resp = new GetFilterRulesResponse();
    resp.addFilterRule(RuleManager.getIncomingRulesAsXML(account));
    return zsc.jaxbToElement(resp);
}
Also used : Account(com.zimbra.cs.account.Account) ZimbraSoapContext(com.zimbra.soap.ZimbraSoapContext) GetFilterRulesResponse(com.zimbra.soap.mail.message.GetFilterRulesResponse)

Example 2 with GetFilterRulesResponse

use of com.zimbra.soap.mail.message.GetFilterRulesResponse in project zm-mailbox by Zimbra.

the class JaxbToJsonTest method bug65572_BooleanAndXmlElements.

/**
     *
    {
        "filterRules": [{
            "filterRule": [{
                "name": "filter.bug65572",
                "active": false,
                "filterTests": [{
                    "condition": "anyof",
                    "headerTest": [{
                        "index": 0,
                        "header": "X-Spam-Score",
                        "caseSensitive": false,
                        "stringComparison": "contains",
                        "value": "0"
                      }]
                  }],
                "filterActions": [{
                    "actionFlag": [{
                        "flagName": "flagged",
                        "index": 0
                      }],
                    "actionStop": [{
                        "index": 1
                      }]
                  }]
              }]
          }],
        "_jsns": "urn:zimbraMail"
      }
     */
/**
     * This also tests {@link XmlElements} - It is used in {@link FilterTests}
     * @throws Exception
     */
@Test
public void bug65572_BooleanAndXmlElements() throws Exception {
    Element legacyXmlElem = mkFilterRulesResponse(XMLElement.mFactory);
    Element legacyJsonElem = mkFilterRulesResponse(JSONElement.mFactory);
    GetFilterRulesResponse jaxb = new GetFilterRulesResponse();
    FilterTests tests = FilterTests.createForCondition("anyof");
    FilterTest.HeaderTest hdrTest = FilterTest.HeaderTest.createForIndexNegative(0, null);
    hdrTest.setHeaders("X-Spam-Score");
    hdrTest.setCaseSensitive(false);
    hdrTest.setStringComparison("contains");
    hdrTest.setValue("0");
    tests.addTest(hdrTest);
    FilterAction.FlagAction flagAction = new FilterAction.FlagAction("flagged");
    flagAction.setIndex(0);
    FilterAction.StopAction stopAction = new FilterAction.StopAction();
    stopAction.setIndex(1);
    FilterRule rule1 = FilterRule.createForNameFilterTestsAndActiveSetting("filter.bug65572", tests, false);
    rule1.addFilterAction(flagAction);
    rule1.addFilterAction(stopAction);
    jaxb.addFilterRule(rule1);
    Element xmlElem = JaxbUtil.jaxbToElement(jaxb, Element.XMLElement.mFactory);
    logDebug("legacyXMLElement ---> prettyPrint\n%1$s", legacyXmlElem.prettyPrint());
    logDebug("XMLElement from JAXB ---> prettyPrint\n%1$s", xmlElem.prettyPrint());
    // Attribute Ordering not reliable: Assert.assertEquals("XML", legacyXmlElem.prettyPrint(), xmlElem.prettyPrint());
    Element xmlFr = xmlElem.getElement(MailConstants.E_FILTER_RULES).getElement(MailConstants.E_FILTER_RULE);
    Assert.assertEquals("XMLElement from JAXB filter rule name", "filter.bug65572", xmlFr.getAttribute(MailConstants.A_NAME));
    Assert.assertEquals("XMLElement from JAXB filter rule active", false, xmlFr.getAttributeBool(MailConstants.A_ACTIVE));
    Element xmlFT = xmlFr.getElement(MailConstants.E_FILTER_TESTS);
    Assert.assertEquals("XMLElement from JAXB filter tests condition", "anyof", xmlFT.getAttribute(MailConstants.A_CONDITION));
    Element xmlHdrT = xmlFT.getElement(MailConstants.E_HEADER_TEST);
    Assert.assertEquals("XMLElement from JAXB filter hdr test index", 0, xmlHdrT.getAttributeInt(MailConstants.A_INDEX));
    Assert.assertEquals("XMLElement from JAXB filter hdr test hdr", "X-Spam-Score", xmlHdrT.getAttribute(MailConstants.A_HEADER));
    Assert.assertEquals("XMLElement from JAXB filter hdr test caseSense", false, xmlHdrT.getAttributeBool(MailConstants.A_CASE_SENSITIVE));
    Assert.assertEquals("XMLElement from JAXB filter hdr test comparison", "contains", xmlHdrT.getAttribute(MailConstants.A_STRING_COMPARISON));
    Assert.assertEquals("XMLElement from JAXB filter hdr test value", 0, xmlHdrT.getAttributeInt(MailConstants.A_VALUE));
    Element xmlFA = xmlFr.getElement(MailConstants.E_FILTER_ACTIONS);
    Element xmlFlag = xmlFA.getElement(MailConstants.E_ACTION_FLAG);
    Assert.assertEquals("XMLElement from JAXB action flag name", "flagged", xmlFlag.getAttribute(MailConstants.A_FLAG_NAME));
    Assert.assertEquals("XMLElement from JAXB action flag index", 0, xmlFlag.getAttributeInt(MailConstants.A_INDEX));
    Element xmlStop = xmlFA.getElement(MailConstants.E_ACTION_STOP);
    Assert.assertEquals("XMLElement from JAXB action stop index", 1, xmlStop.getAttributeInt(MailConstants.A_INDEX));
    Element jsonJaxbElem = JacksonUtil.jaxbToJSONElement(jaxb, MailConstants.GET_FILTER_RULES_RESPONSE);
    logDebug("GetFilterRulesResponse legacyJSONElement ---> prettyPrint\n%1$s", legacyJsonElem.prettyPrint());
    logDebug("GetFilterRulesResponse JSONElement from JAXB ---> prettyPrint\n%1$s", jsonJaxbElem.prettyPrint());
    Assert.assertEquals("JSON", legacyJsonElem.prettyPrint(), jsonJaxbElem.prettyPrint());
    GetFilterRulesResponse roundtripped = JaxbUtil.elementToJaxb(jsonJaxbElem, GetFilterRulesResponse.class);
    List<FilterRule> rules = roundtripped.getFilterRules();
    Assert.assertEquals("num roundtripped rules", 1, rules.size());
    FilterRule rtRule = rules.get(0);
    Assert.assertEquals("roundtripped rule name", "filter.bug65572", rtRule.getName());
    Assert.assertEquals("roundtripped rule active setting", false, rtRule.isActive());
    Assert.assertEquals("roundtripped rule action count", 2, rtRule.getActionCount());
    FilterTests rtTests = rtRule.getFilterTests();
    Assert.assertEquals("roundtripped filterTests condition", "anyof", rtTests.getCondition());
    List<FilterTest> rtFilterTests = rtTests.getTests();
    Assert.assertEquals("num roundtripped filter tests", 1, rtFilterTests.size());
    FilterTest.HeaderTest rtHdrTest = (FilterTest.HeaderTest) rtFilterTests.get(0);
    Assert.assertEquals("roundtripped header test index", 0, rtHdrTest.getIndex());
    Assert.assertEquals("roundtripped header test header", "X-Spam-Score", rtHdrTest.getHeaders());
    Assert.assertEquals("roundtripped header test caseSens", false, rtHdrTest.isCaseSensitive());
    Assert.assertEquals("roundtripped header test stringComparison", "contains", rtHdrTest.getStringComparison());
    Assert.assertEquals("roundtripped header test value", "0", rtHdrTest.getValue());
    List<FilterAction> rtActions = rtRule.getFilterActions();
    Assert.assertEquals("num roundtripped actions", 2, rtActions.size());
    FilterAction.FlagAction rtFlagAction = (FilterAction.FlagAction) rtActions.get(0);
    Assert.assertEquals("roundtripped FlagAction name", "flagged", rtFlagAction.getFlag());
    Assert.assertEquals("roundtripped FlagAction index", 0, rtFlagAction.getIndex());
    FilterAction.StopAction rtStopAction = (FilterAction.StopAction) rtActions.get(1);
    Assert.assertEquals("roundtripped StopAction index", 1, rtStopAction.getIndex());
}
Also used : XmlAnyElement(javax.xml.bind.annotation.XmlAnyElement) Element(com.zimbra.common.soap.Element) XMLElement(com.zimbra.common.soap.Element.XMLElement) JSONElement(com.zimbra.common.soap.Element.JSONElement) XmlElement(javax.xml.bind.annotation.XmlElement) FilterRule(com.zimbra.soap.mail.type.FilterRule) FilterTests(com.zimbra.soap.mail.type.FilterTests) FilterTest(com.zimbra.soap.mail.type.FilterTest) FilterAction(com.zimbra.soap.mail.type.FilterAction) GetFilterRulesResponse(com.zimbra.soap.mail.message.GetFilterRulesResponse) FilterTest(com.zimbra.soap.mail.type.FilterTest) Test(org.junit.Test)

Example 3 with GetFilterRulesResponse

use of com.zimbra.soap.mail.message.GetFilterRulesResponse in project zm-mailbox by Zimbra.

the class ZMailbox method getIncomingFilterRules.

public ZFilterRules getIncomingFilterRules(boolean refresh) throws ServiceException {
    if (incomingRules == null || refresh) {
        GetFilterRulesResponse resp = invokeJaxb(new GetFilterRulesRequest());
        incomingRules = new ZFilterRules(resp.getFilterRules());
    }
    return new ZFilterRules(incomingRules);
}
Also used : GetFilterRulesRequest(com.zimbra.soap.mail.message.GetFilterRulesRequest) GetFilterRulesResponse(com.zimbra.soap.mail.message.GetFilterRulesResponse)

Example 4 with GetFilterRulesResponse

use of com.zimbra.soap.mail.message.GetFilterRulesResponse in project zm-mailbox by Zimbra.

the class JaxbToJsonTest method bug65572BooleanAndXmlElements.

/**
 *    {
 *        "filterRules": [{
 *            "filterRule": [{
 *                "name": "filter.bug65572",
 *                "active": false,
 *                "filterTests": [{
 *                    "condition": "anyof",
 *                    "headerTest": [{
 *                        "index": 0,
 *                        "header": "X-Spam-Score",
 *                        "caseSensitive": false,
 *                        "stringComparison": "contains",
 *                        "value": "0"
 *                      }]
 *                  }],
 *                "filterActions": [{
 *                    "actionFlag": [{
 *                        "flagName": "flagged",
 *                        "index": 0
 *                      }],
 *                    "actionStop": [{
 *                        "index": 1
 *                      }]
 *                  }]
 *              }]
 *          }],
 *        "_jsns": "urn:zimbraMail"
 *      }
 */
/**
 * This also tests {@link XmlElements} - It is used in {@link FilterTests}
 * @throws Exception
 */
@Test
public void bug65572BooleanAndXmlElements() throws Exception {
    Element legacyXmlElem = mkFilterRulesResponse(XMLElement.mFactory);
    Element legacyJsonElem = mkFilterRulesResponse(JSONElement.mFactory);
    GetFilterRulesResponse jaxb = new GetFilterRulesResponse();
    FilterTests tests = FilterTests.createForCondition("anyof");
    FilterTest.HeaderTest hdrTest = FilterTest.HeaderTest.createForIndexNegative(0, null);
    hdrTest.setHeaders("X-Spam-Score");
    hdrTest.setCaseSensitive(false);
    hdrTest.setStringComparison("contains");
    hdrTest.setValue("0");
    tests.addTest(hdrTest);
    FilterAction.FlagAction flagAction = new FilterAction.FlagAction("flagged");
    flagAction.setIndex(0);
    FilterAction.StopAction stopAction = new FilterAction.StopAction();
    stopAction.setIndex(1);
    FilterRule rule1 = FilterRule.createForNameFilterTestsAndActiveSetting("filter.bug65572", tests, false);
    rule1.addFilterAction(flagAction);
    rule1.addFilterAction(stopAction);
    jaxb.addFilterRule(rule1);
    Element xmlElem = JaxbUtil.jaxbToElement(jaxb, Element.XMLElement.mFactory);
    logDebug("legacyXMLElement ---> prettyPrint\n%1$s", legacyXmlElem.prettyPrint());
    logDebug("XMLElement from JAXB ---> prettyPrint\n%1$s", xmlElem.prettyPrint());
    // Attribute Ordering not reliable: Assert.assertEquals("XML", legacyXmlElem.prettyPrint(), xmlElem.prettyPrint());
    Element xmlFr = xmlElem.getElement(MailConstants.E_FILTER_RULES).getElement(MailConstants.E_FILTER_RULE);
    Assert.assertEquals("XMLElement from JAXB filter rule name", "filter.bug65572", xmlFr.getAttribute(MailConstants.A_NAME));
    Assert.assertEquals("XMLElement from JAXB filter rule active", false, xmlFr.getAttributeBool(MailConstants.A_ACTIVE));
    Element xmlFT = xmlFr.getElement(MailConstants.E_FILTER_TESTS);
    Assert.assertEquals("XMLElement from JAXB filter tests condition", "anyof", xmlFT.getAttribute(MailConstants.A_CONDITION));
    Element xmlHdrT = xmlFT.getElement(MailConstants.E_HEADER_TEST);
    Assert.assertEquals("XMLElement from JAXB filter hdr test index", 0, xmlHdrT.getAttributeInt(MailConstants.A_INDEX));
    Assert.assertEquals("XMLElement from JAXB filter hdr test hdr", "X-Spam-Score", xmlHdrT.getAttribute(MailConstants.A_HEADER));
    Assert.assertEquals("XMLElement from JAXB filter hdr test caseSense", false, xmlHdrT.getAttributeBool(MailConstants.A_CASE_SENSITIVE));
    Assert.assertEquals("XMLElement from JAXB filter hdr test comparison", "contains", xmlHdrT.getAttribute(MailConstants.A_STRING_COMPARISON));
    Assert.assertEquals("XMLElement from JAXB filter hdr test value", 0, xmlHdrT.getAttributeInt(MailConstants.A_VALUE));
    Element xmlFA = xmlFr.getElement(MailConstants.E_FILTER_ACTIONS);
    Element xmlFlag = xmlFA.getElement(MailConstants.E_ACTION_FLAG);
    Assert.assertEquals("XMLElement from JAXB action flag name", "flagged", xmlFlag.getAttribute(MailConstants.A_FLAG_NAME));
    Assert.assertEquals("XMLElement from JAXB action flag index", 0, xmlFlag.getAttributeInt(MailConstants.A_INDEX));
    Element xmlStop = xmlFA.getElement(MailConstants.E_ACTION_STOP);
    Assert.assertEquals("XMLElement from JAXB action stop index", 1, xmlStop.getAttributeInt(MailConstants.A_INDEX));
    Element jsonJaxbElem = JacksonUtil.jaxbToJSONElement(jaxb, MailConstants.GET_FILTER_RULES_RESPONSE);
    logDebug("GetFilterRulesResponse legacyJSONElement ---> prettyPrint\n%1$s", legacyJsonElem.prettyPrint());
    logDebug("GetFilterRulesResponse JSONElement from JAXB ---> prettyPrint\n%1$s", jsonJaxbElem.prettyPrint());
    Assert.assertEquals("JSON", legacyJsonElem.prettyPrint(), jsonJaxbElem.prettyPrint());
    GetFilterRulesResponse roundtripped = JaxbUtil.elementToJaxb(jsonJaxbElem, GetFilterRulesResponse.class);
    List<FilterRule> rules = roundtripped.getFilterRules();
    Assert.assertEquals("num roundtripped rules", 1, rules.size());
    FilterRule rtRule = rules.get(0);
    Assert.assertEquals("roundtripped rule name", "filter.bug65572", rtRule.getName());
    Assert.assertEquals("roundtripped rule active setting", false, rtRule.isActive());
    Assert.assertEquals("roundtripped rule action count", 2, rtRule.getActionCount());
    FilterTests rtTests = rtRule.getFilterTests();
    Assert.assertEquals("roundtripped filterTests condition", "anyof", rtTests.getCondition());
    List<FilterTest> rtFilterTests = rtTests.getTests();
    Assert.assertEquals("num roundtripped filter tests", 1, rtFilterTests.size());
    FilterTest.HeaderTest rtHdrTest = (FilterTest.HeaderTest) rtFilterTests.get(0);
    Assert.assertEquals("roundtripped header test index", 0, rtHdrTest.getIndex());
    Assert.assertEquals("roundtripped header test header", "X-Spam-Score", rtHdrTest.getHeaders());
    Assert.assertEquals("roundtripped header test caseSens", false, rtHdrTest.isCaseSensitive());
    Assert.assertEquals("roundtripped header test stringComparison", "contains", rtHdrTest.getStringComparison());
    Assert.assertEquals("roundtripped header test value", "0", rtHdrTest.getValue());
    List<FilterAction> rtActions = rtRule.getFilterActions();
    Assert.assertEquals("num roundtripped actions", 2, rtActions.size());
    FilterAction.FlagAction rtFlagAction = (FilterAction.FlagAction) rtActions.get(0);
    Assert.assertEquals("roundtripped FlagAction name", "flagged", rtFlagAction.getFlag());
    Assert.assertEquals("roundtripped FlagAction index", 0, rtFlagAction.getIndex());
    FilterAction.StopAction rtStopAction = (FilterAction.StopAction) rtActions.get(1);
    Assert.assertEquals("roundtripped StopAction index", 1, rtStopAction.getIndex());
}
Also used : XmlAnyElement(javax.xml.bind.annotation.XmlAnyElement) Element(com.zimbra.common.soap.Element) XMLElement(com.zimbra.common.soap.Element.XMLElement) JSONElement(com.zimbra.common.soap.Element.JSONElement) XmlElement(javax.xml.bind.annotation.XmlElement) FilterRule(com.zimbra.soap.mail.type.FilterRule) FilterTests(com.zimbra.soap.mail.type.FilterTests) FilterTest(com.zimbra.soap.mail.type.FilterTest) FilterAction(com.zimbra.soap.mail.type.FilterAction) GetFilterRulesResponse(com.zimbra.soap.mail.message.GetFilterRulesResponse) FilterTest(com.zimbra.soap.mail.type.FilterTest) Test(org.junit.Test)

Aggregations

GetFilterRulesResponse (com.zimbra.soap.mail.message.GetFilterRulesResponse)4 Element (com.zimbra.common.soap.Element)2 JSONElement (com.zimbra.common.soap.Element.JSONElement)2 XMLElement (com.zimbra.common.soap.Element.XMLElement)2 FilterAction (com.zimbra.soap.mail.type.FilterAction)2 FilterRule (com.zimbra.soap.mail.type.FilterRule)2 FilterTest (com.zimbra.soap.mail.type.FilterTest)2 FilterTests (com.zimbra.soap.mail.type.FilterTests)2 XmlAnyElement (javax.xml.bind.annotation.XmlAnyElement)2 XmlElement (javax.xml.bind.annotation.XmlElement)2 Test (org.junit.Test)2 Account (com.zimbra.cs.account.Account)1 ZimbraSoapContext (com.zimbra.soap.ZimbraSoapContext)1 GetFilterRulesRequest (com.zimbra.soap.mail.message.GetFilterRulesRequest)1