summaryrefslogtreecommitdiffstats
path: root/wso2bpel-ext/wso2bpel-core/BPEL4RESTLight/src/main/java/de/unistuttgart/iaas/bpel/extensions/bpel4restlight/Bpel4RestLightOperation.java
blob: 2335f498f7bd2d71e4edd660825b809728522a44 (plain)
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
package de.unistuttgart.iaas.bpel.extensions.bpel4restlight;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Properties;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.ode.bpel.common.FaultException;
import org.apache.ode.bpel.runtime.extension.AbstractSyncExtensionOperation;
import org.apache.ode.bpel.runtime.extension.ExtensionContext;
import org.opentosca.bpel4restlight.rest.HighLevelRestApi;
import org.opentosca.bpel4restlight.rest.HttpMethod;
import org.opentosca.bpel4restlight.rest.HttpResponseMessage;
/**
 * 
 * Copyright 2011 IAAS University of Stuttgart <br>
 * <br>
 * 
 * This class provides 4 BPEL4RestLight ExtensionActivity-operations which
 * correspond to the 4 typical REST-Operations GET, PUT, POST and Delete.
 * 
 * @author uwe.breitenbuecher@iaas.uni-stuttgart.de
 * 
 */
import org.w3c.dom.Element;

import de.unistuttgart.iaas.bpel.extensions.bpel4restlight.util.Bpel4RestLightUtil;
import de.unistuttgart.iaas.bpel.util.BPELVariableInjectionUtil;
import de.unistuttgart.iaas.bpel.util.BpelUtil;


public class Bpel4RestLightOperation extends AbstractSyncExtensionOperation {
  protected static final Log log = LogFactory.getLog(Bpel4RestLightOperation.class);
  private static final String EXT_PROPERTIES = "wso2bps-ext.properties"; // extra properties setting
  private static final String MSB_URL = "MSB_URL";  // http://msb:port, ext property msb url, if exist replace the request url 
  
  
	public static String wrapper = "temporary-simple-type-wrapper";
	private static String msbUrl = null;
	
	static {
      
      try {
          File file = new File(EXT_PROPERTIES);
          if(file.exists()) {
              Properties p=new Properties();
              InputStream inputStream = new FileInputStream(file);
              p.load(inputStream);
              inputStream.close();
              msbUrl = p.getProperty(MSB_URL);
          }
          
      } catch (Exception e) {
          e.printStackTrace();
      }
      
  }
	
	
	private void processResponseMessage(HttpResponseMessage responseMessage, ExtensionContext context, Element element) throws FaultException {
		// Write responsePayload to designated variable
		String responsePayloadVariableName = Bpel4RestLightUtil.getMethodAttributeValue(element, MethodAttribute.RESPONSEPAYLOADVARIABLE);
		String statusCodeVariableName = Bpel4RestLightUtil.getMethodAttributeValue(element, MethodAttribute.STATUSCODEVARIABLE);
		
		if (responsePayloadVariableName != null && !responsePayloadVariableName.equals("")) {
			BpelUtil.writeResponsePayloadToVariable(context, responseMessage.getResponseBody(), responsePayloadVariableName, Bpel4RestLightOperation.wrapper);
		}
		
		if (statusCodeVariableName != null && !statusCodeVariableName.equals("")) {
			String StatusCode = "" + responseMessage.getStatusCode();  // int can not be casted to String
			
			BpelUtil.writeResponsePayloadToVariable(context, StatusCode, statusCodeVariableName, Bpel4RestLightOperation.wrapper);
			
		}
	}
	
	/** {@inheritDoc} */
	@Override
	protected void runSync(ExtensionContext context, Element element) throws FaultException {
		element = BPELVariableInjectionUtil.replaceExtensionVariables(context, element);
		log.debug("LocalName of edited element: " + element.getLocalName());
		String httpMethod = element.getLocalName();
		
		// Extract requestUri
		String requestUri = getRequestUrl(element);
		
		HttpResponseMessage responseMessage = null;
		String acceptHeader = Bpel4RestLightUtil.extractAcceptHeader(context, element);
		String contentTypeHeader = Bpel4RestLightUtil.extractContentTypeHeader(context, element);
		String requestPayload = Bpel4RestLightUtil.extractRequestPayload(context, element);
		
		// Execute corresponding HttpMethod via the HighLevelRestApi
		switch (HttpMethod.valueOf(httpMethod)) {
		
			case PUT: {
				responseMessage = HighLevelRestApi.Put(requestUri, requestPayload, acceptHeader, contentTypeHeader);
				break;
			}

			case POST: {
				responseMessage = HighLevelRestApi.Post(requestUri, requestPayload, acceptHeader, contentTypeHeader);
				break;
			}
			
			case GET: {
				responseMessage = HighLevelRestApi.Get(requestUri, acceptHeader, contentTypeHeader);
				break;
			}
			
			case DELETE: {
				responseMessage = HighLevelRestApi.Delete(requestUri, acceptHeader, contentTypeHeader);
				break;
			}
		}
		
		processResponseMessage(responseMessage, context, element);
		// Bpel4RestLightOperation.wrapper = null;
	}
	
	
	/**
	 * getRequestUrl
	 * get request url from element, if exists msbUrl property, then replace the ip and port
	 * @param element
	 * @return
	 */
	private String getRequestUrl(Element element) {
	  String requestUri = Bpel4RestLightUtil.getMethodAttributeValue(element, MethodAttribute.REQUESTURI);
	  log.debug("original url:" + requestUri);
      if(msbUrl == null || "".equals(msbUrl) || "http://msb:port".equals(msbUrl)) {
          return requestUri;
      } else {
          requestUri = requestUri.substring(requestUri.indexOf("//") + 2);
          int index = requestUri.indexOf("/");
          if(index == -1) {
              
          } else {
              requestUri = requestUri.substring(index);
          }
          requestUri = msbUrl + requestUri;
          log.debug("changed url:" + requestUri);
          return requestUri;
      }
  }
}