Skip to content

Commit dd27e18

Browse files
authored
Merge pull request #1 from OpenTOSCA/feature/bodylessPUTPOST
PUT and POST methods can now have an empty body defined
2 parents 152b9fd + 71155ae commit dd27e18

File tree

30 files changed

+1393
-39
lines changed

30 files changed

+1393
-39
lines changed

bpel-rest-extensions/src/main/java/org/apache/ode/bpel/extension/bpel4restlight/Bpel4RestLightOperation.java

+37-17
Original file line numberDiff line numberDiff line change
@@ -63,26 +63,41 @@ public void runSync(ExtensionContext context, Element element) throws FaultExcep
6363
switch (HttpMethod.valueOf(httpMethod)) {
6464

6565
case PUT: {
66-
String requestPayload = Bpel4RestLightUtil.extractRequestPayload(context, element);
67-
68-
responseMessage = HighLevelRestApi.Put(requestUri, requestPayload, acceptHeader, contentType);
69-
70-
if (logger.isDebugEnabled()) {
71-
logger.debug("Request message payload: " + requestPayload);
72-
}
73-
66+
if (Bpel4RestLightUtil.specifiesRequest(context, element)) {
67+
String requestPayload = Bpel4RestLightUtil.extractRequestPayload(context, element);
68+
69+
responseMessage = HighLevelRestApi.Put(requestUri, requestPayload, acceptHeader, contentType);
70+
71+
if (logger.isDebugEnabled()) {
72+
logger.debug("Request message payload: " + requestPayload);
73+
}
74+
} else {
75+
if (logger.isDebugEnabled()) {
76+
logger.debug("Request message payload is empty");
77+
}
78+
79+
responseMessage = HighLevelRestApi.Put(requestUri, acceptHeader);
80+
}
81+
7482
break;
7583
}
7684

7785
case POST: {
78-
String requestPayload = Bpel4RestLightUtil.extractRequestPayload(context, element);
79-
80-
responseMessage = HighLevelRestApi.Post(requestUri, requestPayload, acceptHeader, contentType);
81-
82-
if (logger.isDebugEnabled()) {
83-
logger.debug("Request message payload: \n" + requestPayload);
84-
}
85-
86+
if (Bpel4RestLightUtil.specifiesRequest(context, element)) {
87+
String requestPayload = Bpel4RestLightUtil.extractRequestPayload(context, element);
88+
89+
responseMessage = HighLevelRestApi.Post(requestUri, requestPayload, acceptHeader, contentType);
90+
91+
if (logger.isDebugEnabled()) {
92+
logger.debug("Request message payload: \n" + requestPayload);
93+
}
94+
} else {
95+
if (logger.isDebugEnabled()) {
96+
logger.debug("Request message payload is empty");
97+
}
98+
99+
responseMessage = HighLevelRestApi.Post(requestUri, acceptHeader);
100+
}
86101
break;
87102
}
88103

@@ -113,8 +128,13 @@ private void processResponseMessage(HttpResponseMessage responseMessage,
113128
element, MethodAttribute.RESPONSE_PAYLOAD_VARIABLE);
114129
String statusCodeVariableName = Bpel4RestLightUtil.getMethodAttributeValue(context, element,
115130
MethodAttribute.STATUS_CODE_VARIABLE);
131+
132+
String responsePayload = responseMessage.getResponseBody();
133+
134+
if (responsePayload != null && !responsePayload.isEmpty()) {
135+
responsePayload = responsePayload.trim();
136+
}
116137

117-
String responsePayload = responseMessage.getResponseBody().trim();
118138
int statusCode = responseMessage.getStatusCode();
119139

120140
if (responsePayloadVariableName != null && !responsePayloadVariableName.isEmpty()) {

bpel-rest-extensions/src/main/java/org/apache/ode/bpel/extension/bpel4restlight/Bpel4RestLightUtil.java

+25
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,31 @@ public class Bpel4RestLightUtil {
4141
private static final Logger logger = LoggerFactory.getLogger(Bpel4RestLightUtil.class);
4242

4343
private static final String VARIABLE_VALUE_REFERENCE = "$bpelvar[";
44+
45+
/**
46+
* This method checks whether a request payload is specified or not.
47+
*
48+
* @param context The extension context required to resolve variable values.
49+
* @param element element The extension activity DOM element containing the request payload
50+
* @return True, if the provided extension activity specifies a request. False, otherwise
51+
*/
52+
public static boolean specifiesRequest(ExtensionContext context, Element element) {
53+
boolean specifiesRequest = false;
54+
55+
// Check if a reference to a variable is specified
56+
if (element.hasAttribute("request") || element.hasAttribute("requestPayload")) {
57+
specifiesRequest = true;
58+
} else {
59+
// If no variable was specified, check if a static request payload is specified
60+
Node request = DOMUtils.findChildByType(element, Node.ELEMENT_NODE);
61+
62+
if (request != null) {
63+
specifiesRequest = true;
64+
}
65+
}
66+
67+
return specifiesRequest;
68+
}
4469

4570
/**
4671
* This method extracts the request message payload from the provided extension activity. This

bpel-rest-extensions/src/main/java/org/apache/ode/bpel/extension/bpel4restlight/http/HighLevelRestApi.java

+52
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public class HighLevelRestApi {
4040
* @param uri The URI of the target resource
4141
* @param requestPayload The payload of the request message
4242
* @param acceptHeaderValue The value of the accept header field to be set
43+
* @param contentType The contentType of the request payload
4344
* @return A HttpResponseMessage providing the response message payload and status code.
4445
*
4546
* @exception FaultException
@@ -67,13 +68,36 @@ public static HttpResponseMessage Put(String uri, String requestPayload,
6768

6869
return responseMessage;
6970
}
71+
72+
/**
73+
* This method implements the HTTP PUT Method
74+
*
75+
* @param uri The URI of the target resource
76+
* @param acceptHeaderValue The value of the accept header field to be set
77+
* @return A HttpResponseMessage providing the response message payload and status code.
78+
*
79+
* @exception FaultException
80+
*/
81+
public static HttpResponseMessage Put(String uri,
82+
String acceptHeaderValue) throws FaultException {
83+
PutMethod method = new PutMethod(uri);
84+
85+
HighLevelRestApi.setAcceptHeader(method, acceptHeaderValue);
86+
87+
HttpResponseMessage responseMessage = LowLevelRestApi.executeHttpMethod(method);
88+
// Remove <?xml... in front of response
89+
HighLevelRestApi.cleanResponseBody(responseMessage);
90+
91+
return responseMessage;
92+
}
7093

7194
/**
7295
* This method implements the HTTP POST Method
7396
*
7497
* @param uri The URI of the target resource
7598
* @param requestPayload The payload of the request message
7699
* @param acceptHeaderValue The value of the accept header field to be set
100+
* @param contentType The contentType of the request payload
77101
* @return A HttpResponseMessage providing the response message payload and status code.
78102
*
79103
* @exception FaultException
@@ -108,6 +132,34 @@ public static HttpResponseMessage Post(String uri, String requestPayload,
108132

109133
return responseMessage;
110134
}
135+
136+
/**
137+
* This method implements the HTTP POST Method
138+
*
139+
* @param uri The URI of the target resource
140+
* @param acceptHeaderValue The value of the accept header field to be set
141+
* @return A HttpResponseMessage providing the response message payload and status code.
142+
*
143+
* @exception FaultException
144+
*/
145+
public static HttpResponseMessage Post(String uri,
146+
String acceptHeaderValue) throws FaultException {
147+
PostMethod method = null;
148+
if (uri.contains("?")) {
149+
String[] split = uri.split("\\?");
150+
method = new PostMethod(split[0]);
151+
method.setQueryString(HighLevelRestApi.createNameValuePairArrayFromQuery(split[1]));
152+
} else {
153+
method = new PostMethod(uri);
154+
}
155+
156+
HighLevelRestApi.setAcceptHeader(method, acceptHeaderValue);
157+
HttpResponseMessage responseMessage = LowLevelRestApi.executeHttpMethod(method);
158+
// Remove <?xml... in front of response
159+
HighLevelRestApi.cleanResponseBody(responseMessage);
160+
161+
return responseMessage;
162+
}
111163

112164
/**
113165
* This method implements the HTTP GET Method

bpel-runtime/src/main/java/org/apache/ode/bpel/runtime/ExtensionContextImpl.java

+9-2
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,15 @@ public void writeVariable(Variable variable, Node value) throws FaultException {
118118
_context.writeVariable(vi, value);
119119
}
120120

121-
private Variable getVisibleVariable(String varName) {
122-
return _scopeFrame.oscope.getVisibleVariable(varName);
121+
private Variable getVisibleVariable(String varName) throws FaultException {
122+
Variable var = _scopeFrame.oscope.getVisibleVariable(varName);
123+
124+
if (var == null) {
125+
throw new FaultException(new QName(Bpel20QNames.NS_WSBPEL2_0, "subLanguageExecutionFault"),
126+
"Attempt to reference undeclared variable '" + varName + "' in BPEL extension activity.");
127+
}
128+
129+
return var;
123130
}
124131

125132
public BpelRuntimeContext getBpelRuntimeContext() {

bpel-test/src/test/java/org/apache/ode/test/RestExtensionActivitiesTest.java

+96-20
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,46 @@ public void testPostExtAct() throws Throwable {
9696
public void testPostExtActWithWrappedRequest() throws Throwable {
9797
go("/bpel/2.0/TestRestPostExtAct2");
9898
}
99+
100+
/**
101+
* Tests the "POST" REST extension activity without a request message specified.
102+
*
103+
* @throws Throwable
104+
*/
105+
@Test
106+
public void testPostExtActWithoutRequest() throws Throwable {
107+
go("/bpel/2.0/TestRestPostExtActNoRequest");
108+
}
109+
110+
/**
111+
* Tests the "POST" REST extension activity with undeclared request variable specified.
112+
*
113+
* @throws Throwable
114+
*/
115+
@Test
116+
public void testPostExtActWithUndeclaredRequestVariable() throws Throwable {
117+
go("/bpel/2.0/TestRestPostExtActUndeclaredRequestVariable");
118+
}
119+
120+
/**
121+
* Tests the "POST" REST extension activity with non-initialized request variable specified.
122+
*
123+
* @throws Throwable
124+
*/
125+
@Test
126+
public void testPostExtActWithNonInitializedRequestVariable() throws Throwable {
127+
go("/bpel/2.0/TestRestPostExtActNonInitializedRequestVariable");
128+
}
129+
130+
/**
131+
* Tests the "POST" REST extension activity with an empty request variable specified (variableName="").
132+
*
133+
* @throws Throwable
134+
*/
135+
@Test
136+
public void testPostExtActWithEmptyRequestVariable() throws Throwable {
137+
go("/bpel/2.0/TestRestPostExtActEmptyRequestVariable");
138+
}
99139

100140
/**
101141
* Tests the "PUT" REST extension activity.
@@ -106,6 +146,16 @@ public void testPostExtActWithWrappedRequest() throws Throwable {
106146
public void testPutExtAct() throws Throwable {
107147
go("/bpel/2.0/TestRestPutExtAct");
108148
}
149+
150+
/**
151+
* Tests the "PUT" REST extension activity without a request message specified..
152+
*
153+
* @throws Throwable
154+
*/
155+
@Test
156+
public void testPutExtActWithoutRequest() throws Throwable {
157+
go("/bpel/2.0/TestRestPutExtActNoRequest");
158+
}
109159

110160
/**
111161
* Tests the "DELETE" REST extension activity.
@@ -172,42 +222,56 @@ private void handleHttpRequest(HttpExchange exchange) throws IOException {
172222
} else if (method.toUpperCase().equals("POST")) {
173223
String request = IOUtils.toString(exchange.getRequestBody());
174224

175-
String requestValue = "";
176-
try {
177-
Node reqNode = DOMUtils.stringToDOM(request);
178-
179-
NodeList list = reqNode.getChildNodes();
180-
int i = 0;
181-
while (i < list.getLength()) {
182-
Node node = list.item(i);
183-
if (node.getNodeType() == Node.ELEMENT_NODE
184-
&& ((Element) node).getLocalName().equals("value")) {
185-
requestValue = node.getTextContent();
225+
if (request != null && !request.isEmpty()) {
226+
String requestValue = "";
227+
try {
228+
Node reqNode = DOMUtils.stringToDOM(request);
229+
230+
NodeList list = reqNode.getChildNodes();
231+
int i = 0;
232+
while (i < list.getLength()) {
233+
Node node = list.item(i);
234+
if (node.getNodeType() == Node.ELEMENT_NODE
235+
&& ((Element) node).getLocalName().equals("value")) {
236+
requestValue = node.getTextContent();
237+
}
238+
i++;
186239
}
187-
i++;
188-
}
189240

190-
String response =
241+
String response =
242+
"<service:postResponse xmlns:service=\"http://www.example.org/restApi\">\n"
243+
+ " <service:result>" + requestValue
244+
+ " Result</service:result>\n"
245+
+ " </service:postResponse>";
246+
247+
byte[] bResponse = response.getBytes();
248+
249+
exchange.sendResponseHeaders(HttpURLConnection.HTTP_OK, bResponse.length);
250+
exchange.getResponseBody().write(bResponse);
251+
} catch (SAXException e) {
252+
// TODO Auto-generated catch block
253+
e.printStackTrace();
254+
255+
exchange.sendResponseHeaders(HttpURLConnection.HTTP_INTERNAL_ERROR, 0);
256+
}
257+
} else {
258+
String response =
191259
"<service:postResponse xmlns:service=\"http://www.example.org/restApi\">\n"
192-
+ " <service:result>" + requestValue
260+
+ " <service:result>" + "No request specified"
193261
+ " Result</service:result>\n"
194262
+ " </service:postResponse>";
195263

196264
byte[] bResponse = response.getBytes();
197265

198266
exchange.sendResponseHeaders(HttpURLConnection.HTTP_OK, bResponse.length);
199267
exchange.getResponseBody().write(bResponse);
200-
} catch (SAXException e) {
201-
// TODO Auto-generated catch block
202-
e.printStackTrace();
203-
204-
exchange.sendResponseHeaders(HttpURLConnection.HTTP_INTERNAL_ERROR, 0);
205268
}
206269

207270
exchange.close();
208271
} else if (method.toUpperCase().equals("PUT")) {
209272
String request = IOUtils.toString(exchange.getRequestBody());
210273

274+
if (request != null && !request.isEmpty()) {
211275
String requestValue = "";
212276
try {
213277
Node reqNode = DOMUtils.stringToDOM(request);
@@ -239,6 +303,18 @@ private void handleHttpRequest(HttpExchange exchange) throws IOException {
239303

240304
exchange.sendResponseHeaders(HttpURLConnection.HTTP_INTERNAL_ERROR, 0);
241305
}
306+
} else {
307+
String response =
308+
"<service:putResponse xmlns:service=\"http://www.example.org/restApi\">\n"
309+
+ " <service:result>" + "No request specified"
310+
+ " Result</service:result>\n"
311+
+ " </service:putResponse>";
312+
313+
byte[] bResponse = response.getBytes();
314+
315+
exchange.sendResponseHeaders(HttpURLConnection.HTTP_OK, bResponse.length);
316+
exchange.getResponseBody().write(bResponse);
317+
}
242318

243319
exchange.close();
244320
} else if (method.toUpperCase().equals("DELETE")) {

0 commit comments

Comments
 (0)