001/**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.camel;
018
019import java.util.HashMap;
020import java.util.Map;
021
022import javax.xml.bind.annotation.XmlEnum;
023import javax.xml.bind.annotation.XmlType;
024
025/**
026 * Represents the kind of message exchange pattern
027 *
028 * @version 
029 */
030@XmlType
031@XmlEnum
032public enum ExchangePattern {
033    InOnly, RobustInOnly, InOut, InOptionalOut, OutOnly, RobustOutOnly, OutIn, OutOptionalIn;
034
035    // TODO: We should deprecate and only support InOnly, InOut, and InOptionalOut
036
037    protected static final Map<String, ExchangePattern> MAP = new HashMap<>();
038
039    /**
040     * Returns the WSDL URI for this message exchange pattern
041     */
042    public String getWsdlUri() {
043        switch (this) {
044        case InOnly:
045            return "http://www.w3.org/ns/wsdl/in-only";
046        case InOptionalOut:
047            return "http://www.w3.org/ns/wsdl/in-opt-out";
048        case InOut:
049            return "http://www.w3.org/ns/wsdl/in-out";
050        case OutIn:
051            return "http://www.w3.org/ns/wsdl/out-in";
052        case OutOnly:
053            return "http://www.w3.org/ns/wsdl/out-only";
054        case OutOptionalIn:
055            return "http://www.w3.org/ns/wsdl/out-opt-in";
056        case RobustInOnly:
057            return "http://www.w3.org/ns/wsdl/robust-in-only";
058        case RobustOutOnly:
059            return "http://www.w3.org/ns/wsdl/robust-out-only";
060        default:
061            throw new IllegalArgumentException("Unknown message exchange pattern: " + this);
062        }
063    }
064
065    /**
066     * Return true if there can be an IN message
067     */
068    public boolean isInCapable() {
069        switch (this) {
070        case OutOnly:
071        case RobustOutOnly:
072            return false;
073        default:
074            return true;
075        }
076    }
077
078    /**
079     * Return true if there can be an OUT message
080     */
081    public boolean isOutCapable() {
082        switch (this) {
083        case InOnly:
084        case RobustInOnly:
085            return false;
086        default:
087            return true;
088        }
089    }
090
091    /**
092     * Return true if there can be a FAULT message
093     */
094    public boolean isFaultCapable() {
095        switch (this) {
096        case InOnly:
097        case OutOnly:
098            return false;
099        default:
100            return true;
101        }
102    }
103
104    /**
105     * Converts the WSDL URI into a {@link ExchangePattern} instance
106     */
107    public static ExchangePattern fromWsdlUri(String wsdlUri) {
108        return MAP.get(wsdlUri);
109    }
110    
111    public static ExchangePattern asEnum(String value) {
112        try {
113            return valueOf(value);
114        } catch (Exception e) {
115            throw new IllegalArgumentException("Unknown message exchange pattern: " + value, e);
116        }
117    }
118
119    static {
120        for (ExchangePattern mep : values()) {
121            String uri = mep.getWsdlUri();
122            MAP.put(uri, mep);
123            String name = uri.substring(uri.lastIndexOf('/') + 1);
124            MAP.put("http://www.w3.org/2004/08/wsdl/" + name, mep);
125            MAP.put("http://www.w3.org/2006/01/wsdl/" + name, mep);
126        }
127    }
128}