1 /*
   2  * Copyright (c) 1997, 2017, 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.model;
  27 
  28 import com.sun.xml.internal.ws.api.model.ParameterBinding;
  29 import com.sun.xml.internal.ws.binding.WebServiceFeatureList;
  30 
  31 import javax.jws.WebParam.Mode;
  32 import javax.xml.namespace.QName;
  33 import java.util.HashSet;
  34 import java.util.Iterator;
  35 import java.util.Set;
  36 import java.util.concurrent.locks.Lock;
  37 import java.util.concurrent.locks.ReentrantLock;
  38 
  39 /**
  40  * Creates SOAP specific RuntimeModel
  41  *
  42  * @author Vivek Pandey
  43  */
  44 public class SOAPSEIModel extends AbstractSEIModelImpl {
  45 
  46         private final Lock lock = new ReentrantLock();
  47 
  48     public SOAPSEIModel(WebServiceFeatureList features) {
  49         super(features);
  50     }
  51 
  52     @Override
  53     protected void populateMaps() {
  54         int emptyBodyCount = 0;
  55         for(JavaMethodImpl jm : getJavaMethods()){
  56             put(jm.getMethod(), jm);
  57             boolean bodyFound = false;
  58             for(ParameterImpl p:jm.getRequestParameters()){
  59                 ParameterBinding binding = p.getBinding();
  60                 if(binding.isBody()){
  61                     put(p.getName(), jm);
  62                     bodyFound = true;
  63                 }
  64             }
  65             if(!bodyFound){
  66                 put(emptyBodyName, jm);
  67 //                System.out.println("added empty body for: "+jm.getMethod().getName());
  68                 emptyBodyCount++;
  69             }
  70         }
  71         if(emptyBodyCount > 1){
  72             //TODO throw exception
  73 //            System.out.println("Error: Unqiue signature violation - more than 1 empty body!");
  74         }
  75     }
  76 
  77     public Set<QName> getKnownHeaders() {
  78         Set<QName> headers = new HashSet<QName>();
  79 
  80         try{
  81                 lock.lock();
  82             for (JavaMethodImpl method : getJavaMethods()) {
  83              // fill in request headers
  84              Iterator<ParameterImpl> params = method.getRequestParameters().iterator();
  85              fillHeaders(params, headers, Mode.IN);
  86 
  87             // fill in response headers
  88              params = method.getResponseParameters().iterator();
  89              fillHeaders(params, headers, Mode.OUT);
  90                           }
  91         }finally
  92         {
  93                 lock.unlock();
  94          }
  95         return headers;
  96     }
  97 
  98     /**
  99      * @param params
 100      * @param headers
 101      */
 102     private void fillHeaders(Iterator<ParameterImpl> params, Set<QName> headers, Mode mode) {
 103         while (params.hasNext()) {
 104             ParameterImpl param = params.next();
 105             ParameterBinding binding = (mode == Mode.IN)?param.getInBinding():param.getOutBinding();
 106             QName name = param.getName();
 107             if (binding.isHeader() && !headers.contains(name)) {
 108                 headers.add(name);
 109             }
 110         }
 111     }
 112 }