Skip to content

Commit

Permalink
Merge pull request #1 from OpenTOSCA/feature/bodylessPUTPOST
Browse files Browse the repository at this point in the history
PUT and POST methods can now have an empty body defined
  • Loading branch information
nyuuyn authored Mar 19, 2019
2 parents 152b9fd + 71155ae commit dd27e18
Show file tree
Hide file tree
Showing 30 changed files with 1,393 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,26 +63,41 @@ public void runSync(ExtensionContext context, Element element) throws FaultExcep
switch (HttpMethod.valueOf(httpMethod)) {

case PUT: {
String requestPayload = Bpel4RestLightUtil.extractRequestPayload(context, element);

responseMessage = HighLevelRestApi.Put(requestUri, requestPayload, acceptHeader, contentType);

if (logger.isDebugEnabled()) {
logger.debug("Request message payload: " + requestPayload);
}

if (Bpel4RestLightUtil.specifiesRequest(context, element)) {
String requestPayload = Bpel4RestLightUtil.extractRequestPayload(context, element);

responseMessage = HighLevelRestApi.Put(requestUri, requestPayload, acceptHeader, contentType);

if (logger.isDebugEnabled()) {
logger.debug("Request message payload: " + requestPayload);
}
} else {
if (logger.isDebugEnabled()) {
logger.debug("Request message payload is empty");
}

responseMessage = HighLevelRestApi.Put(requestUri, acceptHeader);
}

break;
}

case POST: {
String requestPayload = Bpel4RestLightUtil.extractRequestPayload(context, element);

responseMessage = HighLevelRestApi.Post(requestUri, requestPayload, acceptHeader, contentType);

if (logger.isDebugEnabled()) {
logger.debug("Request message payload: \n" + requestPayload);
}

if (Bpel4RestLightUtil.specifiesRequest(context, element)) {
String requestPayload = Bpel4RestLightUtil.extractRequestPayload(context, element);

responseMessage = HighLevelRestApi.Post(requestUri, requestPayload, acceptHeader, contentType);

if (logger.isDebugEnabled()) {
logger.debug("Request message payload: \n" + requestPayload);
}
} else {
if (logger.isDebugEnabled()) {
logger.debug("Request message payload is empty");
}

responseMessage = HighLevelRestApi.Post(requestUri, acceptHeader);
}
break;
}

Expand Down Expand Up @@ -113,8 +128,13 @@ private void processResponseMessage(HttpResponseMessage responseMessage,
element, MethodAttribute.RESPONSE_PAYLOAD_VARIABLE);
String statusCodeVariableName = Bpel4RestLightUtil.getMethodAttributeValue(context, element,
MethodAttribute.STATUS_CODE_VARIABLE);

String responsePayload = responseMessage.getResponseBody();

if (responsePayload != null && !responsePayload.isEmpty()) {
responsePayload = responsePayload.trim();
}

String responsePayload = responseMessage.getResponseBody().trim();
int statusCode = responseMessage.getStatusCode();

if (responsePayloadVariableName != null && !responsePayloadVariableName.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,31 @@ public class Bpel4RestLightUtil {
private static final Logger logger = LoggerFactory.getLogger(Bpel4RestLightUtil.class);

private static final String VARIABLE_VALUE_REFERENCE = "$bpelvar[";

/**
* This method checks whether a request payload is specified or not.
*
* @param context The extension context required to resolve variable values.
* @param element element The extension activity DOM element containing the request payload
* @return True, if the provided extension activity specifies a request. False, otherwise
*/
public static boolean specifiesRequest(ExtensionContext context, Element element) {
boolean specifiesRequest = false;

// Check if a reference to a variable is specified
if (element.hasAttribute("request") || element.hasAttribute("requestPayload")) {
specifiesRequest = true;
} else {
// If no variable was specified, check if a static request payload is specified
Node request = DOMUtils.findChildByType(element, Node.ELEMENT_NODE);

if (request != null) {
specifiesRequest = true;
}
}

return specifiesRequest;
}

/**
* This method extracts the request message payload from the provided extension activity. This
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public class HighLevelRestApi {
* @param uri The URI of the target resource
* @param requestPayload The payload of the request message
* @param acceptHeaderValue The value of the accept header field to be set
* @param contentType The contentType of the request payload
* @return A HttpResponseMessage providing the response message payload and status code.
*
* @exception FaultException
Expand Down Expand Up @@ -67,13 +68,36 @@ public static HttpResponseMessage Put(String uri, String requestPayload,

return responseMessage;
}

/**
* This method implements the HTTP PUT Method
*
* @param uri The URI of the target resource
* @param acceptHeaderValue The value of the accept header field to be set
* @return A HttpResponseMessage providing the response message payload and status code.
*
* @exception FaultException
*/
public static HttpResponseMessage Put(String uri,
String acceptHeaderValue) throws FaultException {
PutMethod method = new PutMethod(uri);

HighLevelRestApi.setAcceptHeader(method, acceptHeaderValue);

HttpResponseMessage responseMessage = LowLevelRestApi.executeHttpMethod(method);
// Remove <?xml... in front of response
HighLevelRestApi.cleanResponseBody(responseMessage);

return responseMessage;
}

/**
* This method implements the HTTP POST Method
*
* @param uri The URI of the target resource
* @param requestPayload The payload of the request message
* @param acceptHeaderValue The value of the accept header field to be set
* @param contentType The contentType of the request payload
* @return A HttpResponseMessage providing the response message payload and status code.
*
* @exception FaultException
Expand Down Expand Up @@ -108,6 +132,34 @@ public static HttpResponseMessage Post(String uri, String requestPayload,

return responseMessage;
}

/**
* This method implements the HTTP POST Method
*
* @param uri The URI of the target resource
* @param acceptHeaderValue The value of the accept header field to be set
* @return A HttpResponseMessage providing the response message payload and status code.
*
* @exception FaultException
*/
public static HttpResponseMessage Post(String uri,
String acceptHeaderValue) throws FaultException {
PostMethod method = null;
if (uri.contains("?")) {
String[] split = uri.split("\\?");
method = new PostMethod(split[0]);
method.setQueryString(HighLevelRestApi.createNameValuePairArrayFromQuery(split[1]));
} else {
method = new PostMethod(uri);
}

HighLevelRestApi.setAcceptHeader(method, acceptHeaderValue);
HttpResponseMessage responseMessage = LowLevelRestApi.executeHttpMethod(method);
// Remove <?xml... in front of response
HighLevelRestApi.cleanResponseBody(responseMessage);

return responseMessage;
}

/**
* This method implements the HTTP GET Method
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,15 @@ public void writeVariable(Variable variable, Node value) throws FaultException {
_context.writeVariable(vi, value);
}

private Variable getVisibleVariable(String varName) {
return _scopeFrame.oscope.getVisibleVariable(varName);
private Variable getVisibleVariable(String varName) throws FaultException {
Variable var = _scopeFrame.oscope.getVisibleVariable(varName);

if (var == null) {
throw new FaultException(new QName(Bpel20QNames.NS_WSBPEL2_0, "subLanguageExecutionFault"),
"Attempt to reference undeclared variable '" + varName + "' in BPEL extension activity.");
}

return var;
}

public BpelRuntimeContext getBpelRuntimeContext() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,46 @@ public void testPostExtAct() throws Throwable {
public void testPostExtActWithWrappedRequest() throws Throwable {
go("/bpel/2.0/TestRestPostExtAct2");
}

/**
* Tests the "POST" REST extension activity without a request message specified.
*
* @throws Throwable
*/
@Test
public void testPostExtActWithoutRequest() throws Throwable {
go("/bpel/2.0/TestRestPostExtActNoRequest");
}

/**
* Tests the "POST" REST extension activity with undeclared request variable specified.
*
* @throws Throwable
*/
@Test
public void testPostExtActWithUndeclaredRequestVariable() throws Throwable {
go("/bpel/2.0/TestRestPostExtActUndeclaredRequestVariable");
}

/**
* Tests the "POST" REST extension activity with non-initialized request variable specified.
*
* @throws Throwable
*/
@Test
public void testPostExtActWithNonInitializedRequestVariable() throws Throwable {
go("/bpel/2.0/TestRestPostExtActNonInitializedRequestVariable");
}

/**
* Tests the "POST" REST extension activity with an empty request variable specified (variableName="").
*
* @throws Throwable
*/
@Test
public void testPostExtActWithEmptyRequestVariable() throws Throwable {
go("/bpel/2.0/TestRestPostExtActEmptyRequestVariable");
}

/**
* Tests the "PUT" REST extension activity.
Expand All @@ -106,6 +146,16 @@ public void testPostExtActWithWrappedRequest() throws Throwable {
public void testPutExtAct() throws Throwable {
go("/bpel/2.0/TestRestPutExtAct");
}

/**
* Tests the "PUT" REST extension activity without a request message specified..
*
* @throws Throwable
*/
@Test
public void testPutExtActWithoutRequest() throws Throwable {
go("/bpel/2.0/TestRestPutExtActNoRequest");
}

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

String requestValue = "";
try {
Node reqNode = DOMUtils.stringToDOM(request);

NodeList list = reqNode.getChildNodes();
int i = 0;
while (i < list.getLength()) {
Node node = list.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE
&& ((Element) node).getLocalName().equals("value")) {
requestValue = node.getTextContent();
if (request != null && !request.isEmpty()) {
String requestValue = "";
try {
Node reqNode = DOMUtils.stringToDOM(request);

NodeList list = reqNode.getChildNodes();
int i = 0;
while (i < list.getLength()) {
Node node = list.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE
&& ((Element) node).getLocalName().equals("value")) {
requestValue = node.getTextContent();
}
i++;
}
i++;
}

String response =
String response =
"<service:postResponse xmlns:service=\"http://www.example.org/restApi\">\n"
+ " <service:result>" + requestValue
+ " Result</service:result>\n"
+ " </service:postResponse>";

byte[] bResponse = response.getBytes();

exchange.sendResponseHeaders(HttpURLConnection.HTTP_OK, bResponse.length);
exchange.getResponseBody().write(bResponse);
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();

exchange.sendResponseHeaders(HttpURLConnection.HTTP_INTERNAL_ERROR, 0);
}
} else {
String response =
"<service:postResponse xmlns:service=\"http://www.example.org/restApi\">\n"
+ " <service:result>" + requestValue
+ " <service:result>" + "No request specified"
+ " Result</service:result>\n"
+ " </service:postResponse>";

byte[] bResponse = response.getBytes();

exchange.sendResponseHeaders(HttpURLConnection.HTTP_OK, bResponse.length);
exchange.getResponseBody().write(bResponse);
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();

exchange.sendResponseHeaders(HttpURLConnection.HTTP_INTERNAL_ERROR, 0);
}

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

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

exchange.sendResponseHeaders(HttpURLConnection.HTTP_INTERNAL_ERROR, 0);
}
} else {
String response =
"<service:putResponse xmlns:service=\"http://www.example.org/restApi\">\n"
+ " <service:result>" + "No request specified"
+ " Result</service:result>\n"
+ " </service:putResponse>";

byte[] bResponse = response.getBytes();

exchange.sendResponseHeaders(HttpURLConnection.HTTP_OK, bResponse.length);
exchange.getResponseBody().write(bResponse);
}

exchange.close();
} else if (method.toUpperCase().equals("DELETE")) {
Expand Down
Loading

0 comments on commit dd27e18

Please sign in to comment.