Skip to main content
summaryrefslogtreecommitdiffstats
blob: 6a6168ed84b0032441e1d8135490d0417115e60d (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
/*******************************************************************************
 * Copyright (c) 2004 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.ws.internal.explorer.platform.wsdl.actions;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URLConnection;
import java.util.Iterator;
import javax.wsdl.Part;
import org.eclipse.wst.ws.internal.explorer.platform.perspective.Controller;
import org.eclipse.wst.ws.internal.explorer.platform.perspective.MessageQueue;
import org.eclipse.wst.ws.internal.explorer.platform.perspective.Node;
import org.eclipse.wst.ws.internal.explorer.platform.util.MultipartFormDataException;
import org.eclipse.wst.ws.internal.explorer.platform.util.MultipartFormDataParser;
import org.eclipse.wst.ws.internal.explorer.platform.wsdl.constants.WSDLActionInputs;
import org.eclipse.wst.ws.internal.explorer.platform.wsdl.datamodel.WSDLOperationElement;
import org.eclipse.wst.ws.internal.explorer.platform.wsdl.fragment.IFragment;
import org.eclipse.wst.ws.internal.explorer.platform.wsdl.fragment.IXSDFragment;
import org.eclipse.wst.ws.internal.explorer.platform.wsdl.fragment.util.HTTPUtil;
import org.eclipse.wst.ws.internal.explorer.platform.wsdl.perspective.InvokeWSDLOperationTool;

public abstract class InvokeWSDLHttpOperationFormAction extends WSDLPropertiesFormAction
{
  private static final String CONTENT_TYPE_CHARSETEQ = "charset=";
  
  public InvokeWSDLHttpOperationFormAction(Controller controller)
  {
    super(controller);
  }
  
  protected boolean processParsedResults(MultipartFormDataParser parser) throws MultipartFormDataException
  {
    super.processParsedResults(parser);
    String endPoint = parser.getParameter(WSDLActionInputs.END_POINT);
    Node selectedNode = getSelectedNavigatorNode();
    InvokeWSDLOperationTool invokeWSDLOperationTool = (InvokeWSDLOperationTool)(selectedNode.getCurrentToolManager().getSelectedTool());
    invokeWSDLOperationTool.setEndPoint(endPoint);
    WSDLOperationElement operElement = (WSDLOperationElement)(selectedNode.getTreeElement());
    propertyTable_.put(WSDLActionInputs.OPERATION_ELEMENT,operElement);
    Iterator it = operElement.getOrderedBodyParts().iterator();
    boolean resultsValid = true;
    while (it.hasNext()) {
      Part part = (Part)it.next();
      IFragment frag = operElement.getFragment(part);
      if (!frag.processParameterValues(parser))
        resultsValid = false;
    }
    return resultsValid;
  }  
  
  protected String getEndPoint()
  {
    StringBuffer endPoint = new StringBuffer((String)propertyTable_.get(WSDLActionInputs.END_POINT));
    WSDLOperationElement operElement = (WSDLOperationElement)propertyTable_.get(WSDLActionInputs.OPERATION_ELEMENT);
    if (endPoint.charAt(endPoint.length()-1) != '/')
      endPoint.append('/');
    endPoint.append(operElement.getName());
    return endPoint.toString();    
  }
  
  protected void addParameters(StringBuffer buffer)
  {
    WSDLOperationElement operElement = (WSDLOperationElement)propertyTable_.get(WSDLActionInputs.OPERATION_ELEMENT);
    Iterator it = operElement.getOrderedBodyParts().iterator();
    while (it.hasNext())
    {
      Part part = (Part)it.next();
      IXSDFragment frag = operElement.getFragment(part);
      buffer.append(HTTPUtil.genURLEncodedParameters(frag));
      if (it.hasNext())
        buffer.append('&');
    }
  }
  
  protected void recordHttpResponse(URLConnection conn,MessageQueue messageQueue) throws IOException
  {
    BufferedReader br = null;
    try
    {
      InputStreamReader in = null;
      String contentType = conn.getContentType();
      if (contentType != null)
      {
        int charsetEqPos = contentType.indexOf(CONTENT_TYPE_CHARSETEQ);
        if (charsetEqPos != -1)
          in = new InputStreamReader(conn.getInputStream(),contentType.substring(charsetEqPos+CONTENT_TYPE_CHARSETEQ.length()));
      }
      if (in == null)
        in = new InputStreamReader(conn.getInputStream());
      br = new BufferedReader(in);
      String s;
      while ((s = br.readLine()) != null)
        messageQueue.addMessage(s);
      br.close();
      br = null;
    }
    catch (IOException e)
    {
      if (br != null)
        br.close();
      throw e;
    }
  }
}

Back to the top