1 /*
   2  * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.xml.internal.ws.api.model;
  27 
  28 /**
  29  * Denotes the binding of a parameter.
  30  *
  31  * <p>
  32  * This is somewhat like an enumeration (but it is <b>NOT</b> an enumeration.)
  33  *
  34  * <p>
  35  * The possible values are
  36  * BODY, HEADER, UNBOUND, and ATTACHMENT. BODY, HEADER, and UNBOUND
  37  * has a singleton semantics, but there are multiple ATTACHMENT instances
  38  * as it carries additional MIME type parameter.
  39  *
  40  * <p>
  41  * So don't use '==' for testing the equality.
  42  */
  43 public final class ParameterBinding {
  44     /**
  45      * Singleton instance that represents 'BODY'
  46      */
  47     public static final ParameterBinding BODY = new ParameterBinding(Kind.BODY,null);
  48     /**
  49      * Singleton instance that represents 'HEADER'
  50      */
  51     public static final ParameterBinding HEADER = new ParameterBinding(Kind.HEADER,null);
  52     /**
  53      * Singleton instance that represents 'UNBOUND',
  54      * meaning the parameter doesn't have a representation in a SOAP message.
  55      */
  56     public static final ParameterBinding UNBOUND = new ParameterBinding(Kind.UNBOUND,null);
  57     /**
  58      * Creates an instance that represents the attachment
  59      * with a given MIME type.
  60      *
  61      * <p>
  62      * TODO: shall we consider givint the singleton semantics by using
  63      * a cache? It's more elegant to do so, but
  64      * no where in JAX-WS RI two {@link ParameterBinding}s are compared today,
  65      */
  66     public static ParameterBinding createAttachment(String mimeType) {
  67         return new ParameterBinding(Kind.ATTACHMENT,mimeType);
  68     }
  69 
  70     /**
  71      * Represents 4 kinds of binding.
  72      */
  73     public static enum Kind {
  74         BODY, HEADER, UNBOUND, ATTACHMENT;
  75     }
  76 
  77 
  78     /**
  79      * Represents the kind of {@link ParameterBinding}.
  80      * Always non-null.
  81      */
  82     public final Kind kind;
  83 
  84     /**
  85      * Only used with attachment binding.
  86      */
  87     private String mimeType;
  88 
  89     private ParameterBinding(Kind kind,String mimeType) {
  90         this.kind = kind;
  91         this.mimeType = mimeType;
  92     }
  93 
  94 
  95 
  96     public String toString() {
  97         return kind.toString();
  98     }
  99 
 100     /**
 101      * Returns the MIME type associated with this binding.
 102      *
 103      * @throws IllegalStateException
 104      *      if this binding doesn't represent an attachment.
 105      *      IOW, if {@link #isAttachment()} returns false.
 106      * @return
 107      *      Can be null, if the MIME type is not known.
 108      */
 109     public String getMimeType() {
 110         if(!isAttachment())
 111             throw new IllegalStateException();
 112         return mimeType;
 113     }
 114 
 115     public boolean isBody(){
 116         return this==BODY;
 117     }
 118 
 119     public boolean isHeader(){
 120         return this==HEADER;
 121     }
 122 
 123     public boolean isUnbound(){
 124         return this==UNBOUND;
 125     }
 126 
 127     public boolean isAttachment(){
 128         return kind==Kind.ATTACHMENT;
 129     }
 130 }