Skip to main content
summaryrefslogtreecommitdiffstats
blob: eb8e5102b30b11dc657326ca9167aa1c0d65bf2b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/*******************************************************************************
 * Copyright (c) 2001, 2005 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.wst.wsdl.binding.http.internal.generator;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.eclipse.wst.wsdl.Binding;
import org.eclipse.wst.wsdl.BindingFault;
import org.eclipse.wst.wsdl.BindingInput;
import org.eclipse.wst.wsdl.BindingOperation;
import org.eclipse.wst.wsdl.BindingOutput;
import org.eclipse.wst.wsdl.ExtensibilityElement;
import org.eclipse.wst.wsdl.Fault;
import org.eclipse.wst.wsdl.Input;
import org.eclipse.wst.wsdl.Operation;
import org.eclipse.wst.wsdl.Output;
import org.eclipse.wst.wsdl.Port;
import org.eclipse.wst.wsdl.PortType;
import org.eclipse.wst.wsdl.binding.http.HTTPAddress;
import org.eclipse.wst.wsdl.binding.http.HTTPBinding;
import org.eclipse.wst.wsdl.binding.http.HTTPFactory;
import org.eclipse.wst.wsdl.binding.http.HTTPOperation;
import org.eclipse.wst.wsdl.binding.http.HTTPUrlEncoded;
import org.eclipse.wst.wsdl.internal.generator.ContentGenerator;
import org.w3c.dom.Element;

public class HTTPContentGenerator implements ContentGenerator
{
  public static final int VERB_POST = 0;
  public static final int VERB_GET = 1;
  public static final int VERB_NOT_SET = -1;

  private int verbOption = VERB_NOT_SET;
  protected String addressLocation = ContentGenerator.ADDRESS_LOCATION;

  protected final static String[] requiredNamespaces = { "http://schemas.xmlsoap.org/wsdl/mime/", "http://schemas.xmlsoap.org/wsdl/http/" };

  public void setVerb(int verb) {
	  verbOption = verb;
  }
  
  public void setAddressLocation(String addressLocation) {
	  this.addressLocation = addressLocation;
  }
  
  public String[] getRequiredNamespaces()
  {
    return requiredNamespaces;
  }

  public String getPreferredNamespacePrefix(String namespace)
  {
	  if (namespace.equals("http://schemas.xmlsoap.org/wsdl/mime/")) {
		  return "mime";
	  }
	  else if (namespace.equals("http://schemas.xmlsoap.org/wsdl/http/")) {
		  return "http";
	  }

	  return "";
  }

  public void generatePortContent(Port port)
  {
	  // We blow away any existing ExtensibilityElements/content before we generate it
	  // Is it worth being smarter?  Look for matching content first and create those which aren't found????
	  List removeList = new ArrayList(port.getEExtensibilityElements());
	  removeExtensebilityElements(port.getEExtensibilityElements(), removeList);

	  HTTPAddress httpAddress= HTTPFactory.eINSTANCE.createHTTPAddress();
	  httpAddress.setLocationURI(addressLocation);
	  port.addExtensibilityElement(httpAddress);
  }

  public void generateBindingContent(Binding binding, PortType portType)
  {
	  // We blow away any existing ExtensibilityElements/content before we generate it
	  // Is it worth being smarter?  Look for matching content first and create those which aren't found????
	  List removeList = new ArrayList(binding.getEExtensibilityElements());
	  removeExtensebilityElements(binding.getEExtensibilityElements(), removeList);
	
	  HTTPBinding httpBinding = HTTPFactory.eINSTANCE.createHTTPBinding();
	  httpBinding.setVerb(getVerbOption(binding) == VERB_POST ? "POST" : "GET");

	  binding.addExtensibilityElement(httpBinding);
  }

  public void generateBindingOperationContent(BindingOperation bindingOperation, Operation operation)
  {
	  // We blow away any existing ExtensibilityElements/content before we generate it
	  // Is it worth being smarter?  Look for matching content first and create those which aren't found????
	  List removeList = new ArrayList(bindingOperation.getEExtensibilityElements());
	  removeExtensebilityElements(bindingOperation.getEExtensibilityElements(), removeList);
	
	  HTTPOperation httpOperation = HTTPFactory.eINSTANCE.createHTTPOperation();
	  httpOperation.setLocationURI("/" + operation.getName());
	  bindingOperation.addExtensibilityElement(httpOperation);
  }

  public void generateBindingInputContent(BindingInput bindingInput, Input input)
  {
	  // We blow away any existing ExtensibilityElements/content before we generate it
	  // Is it worth being smarter?  Look for matching content first and create those which aren't found????
	  List removeList = new ArrayList(bindingInput.getEExtensibilityElements());
	  
	  // hack: we're being sneaky here. Set the 'verb' options now.
	  int option = getVerbOption(bindingInput);
	  removeExtensebilityElements(bindingInput.getEExtensibilityElements(), removeList);
	  
	  if (option == VERB_POST)
	  {
		  // TODO: Is there an equivalent HTTP Model Object for this?....
		  Element element = createElement(bindingInput.getElement(), "mime", "content");
		  element.setAttribute("type", "application/x-www-form-urlencoded");
	  }
	  else
	  {
		  HTTPUrlEncoded urlEncoded= HTTPFactory.eINSTANCE.createHTTPUrlEncoded();
		  bindingInput.addExtensibilityElement(urlEncoded);
	  }
  }

  public void generateBindingOutputContent(BindingOutput bindingOutput, Output output)
  {
	  // We blow away any existing ExtensibilityElements/content before we generate it
	  // Is it worth being smarter?  Look for matching content first and create those which aren't found????
	  List removeList = new ArrayList(bindingOutput.getEExtensibilityElements());

	  // hack: we're being sneaky here. Set the 'verb' options now.
	  getVerbOption(bindingOutput);
	  removeExtensebilityElements(bindingOutput.getEExtensibilityElements(), removeList);

	  // TODO: Is there an equivalent HTTP Model Object for this?....
	  Element bindingOutputElement = bindingOutput.getElement();
	  Element element = createElement(bindingOutputElement, "mime", "content");
	  element.setAttribute("type", "text/xml");
  }

  public void generateBindingFaultContent(BindingFault bindingFault, Fault fault)
  {
    //TODO!!
  }

  protected Element createElement(Element parentElement, String prefix, String elementName)
  {
    String name = prefix != null ? (prefix + ":" + elementName) : elementName;
    Element result = parentElement.getOwnerDocument().createElement(name);
	parentElement.insertBefore(result, parentElement.getFirstChild());
//    parentElement.appendChild(result);
    return result;
  }
  
  /////////////////////  
  private int getVerbOption(Object genericBindingObject) {
	  if (verbOption == VERB_NOT_SET && genericBindingObject != null) {
		  // init() was never called, try to determine the 'verb' based on what we have/know
		  Binding binding = getBindingObject(genericBindingObject);
		  
		  // Try to determine the verb from the Binding Object
		  if (binding != null) {
			  List list = binding.getEExtensibilityElements();
			  Iterator valuesIt = getExtensibilityElementAttributeValue(list, "verb").iterator();

			  while (valuesIt.hasNext()) {
				  String verb = (String) valuesIt.next();
				  
				  if (verb.equals("POST")) {
					  verbOption = VERB_POST;
				  }
				  else if (verb.equals("GET")) {
					  verbOption = VERB_GET;
				  }
				  
				  if (verbOption != VERB_NOT_SET) {
					  break;
				  }
			  }
		  }
	  }
	  
	  if (verbOption == VERB_NOT_SET) {
		  verbOption = VERB_GET;
	  }
	  
	  return verbOption;
  }
  
  private List getExtensibilityElementAttributeValue(List eeList, String attributeKey) {
	  List values = new ArrayList();
	  Iterator eeElementsIt = eeList.iterator();
	  
	  while (eeElementsIt.hasNext()) {
		  ExtensibilityElement eeElement = (ExtensibilityElement) eeElementsIt.next();
		  String attributeValue = eeElement.getElement().getAttribute(attributeKey);
		  if (attributeValue != null && !attributeValue.equals("")) {
			  values.add(attributeValue);
		  }		  
	  }
	  
	  return values;
  }
  
  private Binding getBindingObject(Object genericBindingObject) {
	  Object parent = genericBindingObject;
	  
	  int index = 0;
	  while (parent != null && index < 5) {
		  parent = getGenericBindingObjectParent(parent);
		  if (parent instanceof Binding) {
			  break;
		  }
		  index++;
	  }

	  return (parent instanceof Binding)? (Binding) parent : null;
  }
  
  private List getMessageReferenceBindingObjects(Object genericBindingObject) {
	  List list = new ArrayList();
	  Binding binding = getBindingObject(genericBindingObject);
	  
	  if (binding != null) {
		  Iterator operationsIt = binding.getEBindingOperations().iterator();
	  
		  while (operationsIt.hasNext()) {
			  BindingOperation op = (BindingOperation) operationsIt.next();
			  list.add(op.getEBindingInput());
			  list.add(op.getEBindingOutput());
			  list.addAll(op.getEBindingFaults());
		  }	  
	  }
	  
	  return list;
  }
  
  private Object getGenericBindingObjectParent(Object genericBindingObject) {
	  Object parent = null;
	  
	  if (genericBindingObject != null) {
		  if (genericBindingObject instanceof BindingOperation) {
			  parent = ((BindingOperation) genericBindingObject).getContainer();
		  }
		  else if (genericBindingObject instanceof BindingInput) {
			  parent = ((BindingInput) genericBindingObject).getContainer();
		  }
		  else if (genericBindingObject instanceof BindingOutput) {
			  parent = ((BindingOutput) genericBindingObject).getContainer();
		  }
		  else if (genericBindingObject instanceof BindingFault) {
			  parent = ((BindingFault) genericBindingObject).getContainer();
		  }	  
	  }
	  
	  return parent;	  
  }
  
  private void removeExtensebilityElements(List originalList, List removeList) {
	  Iterator removeIt = removeList.iterator();
	  while (removeIt.hasNext()) {
		  originalList.remove(removeIt.next());
	  }
  }
  
  public String getProtocol() {
	  return "HTTP";
  }
}

Back to the top