Skip to content

Commit

Permalink
feat(contentType): add x-www-form-urlencoded content-type (#50)
Browse files Browse the repository at this point in the history
Add `application/x-www-form-urlencoded` to the content-type list for
POST and PUT request

Closes STUDIO-4182
  • Loading branch information
rbioteau authored Nov 23, 2021
1 parent 2d317e4 commit 72f0eae
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 5 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,14 @@

<!--Post Connector -->
<post.def.id>rest-post</post.def.id>
<post.def.version>1.2.0</post.def.version>
<post.def.version>1.3.0</post.def.version>
<post.impl.id>${post.def.id}-impl</post.impl.id>
<post.impl.version>${project.version}</post.impl.version>
<post.main-class>org.bonitasoft.connectors.rest.PostConnectorImpl</post.main-class>

<!--Put Connector -->
<put.def.id>rest-put</put.def.id>
<put.def.version>1.2.0</put.def.version>
<put.def.version>1.3.0</put.def.version>
<put.impl.id>${put.def.id}-impl</put.impl.id>
<put.impl.version>${project.version}</put.impl.version>
<put.main-class>org.bonitasoft.connectors.rest.PutConnectorImpl</put.main-class>
Expand Down
12 changes: 9 additions & 3 deletions src/main/java/org/bonitasoft/connectors/rest/RESTConnector.java
Original file line number Diff line number Diff line change
Expand Up @@ -239,20 +239,26 @@ private Request buildRequest() throws MalformedURLException {
String bodyStr = "";
if (getBody() != null) {
bodyStr = getBody();
request.setContentType(ContentType.create(getContentType(), Charset.forName(getCharset())));
}

request.setBody(bodyStr);
LOGGER.fine("Body set to: " + request.getBody().toString());
request.setRestMethod(HTTPMethod.getRESTHTTPMethodFromValue(getMethod()));

if(request.getRestMethod() == HTTPMethod.POST || request.getRestMethod() == HTTPMethod.PUT) {
request.setContentType(ContentType.create(getContentType(), Charset.forName(getCharset())));
}

LOGGER.fine("Method set to: " + request.getRestMethod().toString());
request.setRedirect(!getDoNotFollowRedirect());
LOGGER.fine("Follow redirect set to: " + request.isRedirect());
request.setIgnore(getIgnoreBody());
LOGGER.fine("Ignore body set to: " + request.isIgnore());
for (final Object urlheader : getUrlHeaders()) {
final List<?> urlheaderRow = (List<?>) urlheader;
request.addHeader(urlheaderRow.get(0).toString(), urlheaderRow.get(1).toString());
String name = urlheaderRow.get(0).toString();
String value = urlheaderRow.get(1).toString();
request.addHeader(name, value);
LOGGER.fine("Add header: " + urlheaderRow.get(0).toString() + " set as " + urlheaderRow.get(1).toString());
}
for (final Object urlCookie : getUrlCookies()) {
Expand Down
1 change: 1 addition & 0 deletions src/main/resources-filtered/rest-post.def
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
<widget xsi:type="definition:Select" id="contentTypeWidget" inputName="contentType">
<items>application/json</items>
<items>application/xml</items>
<items>application/x-www-form-urlencoded</items>
<items>text/html</items>
<items>text/plain</items>
<items>text/xml</items>
Expand Down
1 change: 1 addition & 0 deletions src/main/resources-filtered/rest-put.def
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
<widget xsi:type="definition:Select" id="contentTypeWidget" inputName="contentType">
<items>application/json</items>
<items>application/xml</items>
<items>application/x-www-form-urlencoded</items>
<items>text/html</items>
<items>text/plain</items>
<items>text/xml</items>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,13 @@
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpStatus;
import org.apache.http.entity.ContentType;
import org.bonitasoft.connectors.rest.model.AuthorizationType;
import org.bonitasoft.connectors.rest.model.TrustCertificateStrategy;
import org.bonitasoft.engine.connector.ConnectorException;
Expand Down Expand Up @@ -1034,6 +1037,21 @@ public void should_throw_validation_exception_for_unknown_trust_certificate_stra
.isInstanceOf(ConnectorValidationException.class)
.hasMessage("'unknownStrategy' option is invalid for trust_strategy. Only one of [DEFAULT, TRUST_SELF_SIGNED, TRUST_ALL] is supported.");
}

@Test
public void should_support_url_encoded_content_type() throws Exception {
LinkedHashMap requestBody = new LinkedHashMap();
requestBody.put("name","value1");
requestBody.put("token","value2");
stubFor(post(urlEqualTo("/"))
.withHeader(WM_CONTENT_TYPE, equalTo(ContentType.APPLICATION_FORM_URLENCODED.getMimeType() + "; " + WM_CHARSET + "=" + UTF8))
.withRequestBody(containing(WireMockUtil.toFormUrlEncoded(requestBody)))
.willReturn(aResponse().withStatus(HttpStatus.SC_OK)));

Map<String, Object> parameters = buildContentTypeParametersSet(ContentType.APPLICATION_FORM_URLENCODED.getMimeType());
parameters.put(AbstractRESTConnectorImpl.BODY_INPUT_PARAMETER, "name=value1&token=value2");
checkResultIsPresent(executeConnector(parameters));
}

/**
* Generic test: should return OK STATUS as the WireMock stub is set each time
Expand All @@ -1060,4 +1078,36 @@ private void checkResult(final Map<String, Object> outputs, final int httpStatus
final Integer restStatusCode = (Integer) statusCode;
assertEquals(httpStatus, restStatusCode.intValue());
}

public static class WireMockUtil {
public static String toFormUrlEncoded(LinkedHashMap<String, String> map) {
if (map == null) {
return "";
}
StringBuilder sb = new StringBuilder();
Iterator<String> it = map.keySet().iterator();
while (it.hasNext()) {
String key = it.next();
String value = map.get(key);
appendFormUrlEncoded(key,value,sb);
if (it.hasNext()) {
sb.append('&');
}
}
return sb.toString();
}

public static String toFormUrlEncoded(String key, String value) {
StringBuilder sb = new StringBuilder();
appendFormUrlEncoded(key, value,sb);
return sb.toString();
}

public static void appendFormUrlEncoded(String key, String value, StringBuilder sb) {
sb.append(key).append('=');
if (value != null) {
sb.append(value);
}
}
}
}

0 comments on commit 72f0eae

Please sign in to comment.