org.mule.intents.TypeChecker.java Source code

Java tutorial

Introduction

Here is the source code for org.mule.intents.TypeChecker.java

Source

/*
 * $Id$
 * --------------------------------------------------------------------------------------
 * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
 *
 * The software in this package is published under the terms of the CPAL v1.0
 * license, a copy of which has been included with this distribution in the
 * LICENSE.txt file.
 */
package org.mule.intents;

import org.mule.api.MuleEventContext;
import org.mule.api.lifecycle.Callable;
import org.mule.api.lifecycle.Initialisable;
import org.mule.api.lifecycle.InitialisationException;
import org.mule.routing.filters.WildcardFilter;

import javax.activation.MimeType;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * TODO
 */
public class TypeChecker implements Callable, Initialisable {
    /**
     * logger used by this class
     */
    protected static final Log logger = LogFactory.getLog(TypeChecker.class);

    private String types;
    private String template;
    private WildcardFilter filter;

    public String getTypes() {
        return types;
    }

    public void setTypes(String types) {
        this.types = types;
    }

    public String getTemplate() {
        return template;
    }

    public void setTemplate(String template) {
        this.template = template;
    }

    public void initialise() throws InitialisationException {
        filter = new WildcardFilter(getTypes());
        filter.setCaseSensitive(false);
    }

    public Object onCall(MuleEventContext eventContext) throws Exception {
        //Check outbound first since this would override the inbound Content-type in the flow
        String contentType = (String) eventContext.getMessage().getOutboundProperty(("Content-Type"));
        if (contentType == null) {
            contentType = (String) eventContext.getMessage().getInboundProperty(("Content-Type"));
            if (contentType == null) {
                logger.error("current message doesn't have a 'Content-Type' header set");
                logger.error(eventContext.getMessage().toString());
                return eventContext;
            }
        }

        MimeType mimeType = new MimeType(contentType);
        if (!filter.accept(mimeType.getPrimaryType() + "/" + mimeType.getSubType())) {
            throw new IllegalArgumentException("The current message content type: " + contentType
                    + " is not compatible with return data-type for template: " + getTemplate()
                    + ". This Template output data type should be: " + getTypes());
        }

        return eventContext.getMessage();
    }
}