Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Post using string rather than Mendix object #104

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified RestServices.mpr
Binary file not shown.
51 changes: 51 additions & 0 deletions javasource/restservices/actions/postString.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// This file was generated by Mendix Modeler.
//
// WARNING: Only the following code will be retained when actions are regenerated:
// - the import list
// - the code between BEGIN USER CODE and END USER CODE
// - the code between BEGIN EXTRA CODE and END EXTRA CODE
// Other code you write will be lost the next time you deploy the project.
// Special characters, e.g., é, ö, à, etc. are supported in comments.

package restservices.actions;

import com.mendix.systemwideinterfaces.core.IContext;
import com.mendix.systemwideinterfaces.core.IMendixObject;
import com.mendix.webui.CustomJavaAction;
import restservices.consume.RestConsumer;
import restservices.proxies.HttpMethod;

public class postString extends CustomJavaAction<IMendixObject>
{
private java.lang.String collectionUrl;
private java.lang.String requestData;
private java.lang.Boolean submitAsFormData;

public postString(IContext context, java.lang.String collectionUrl, java.lang.String requestData, java.lang.Boolean submitAsFormData)
{
super(context);
this.collectionUrl = collectionUrl;
this.requestData = requestData;
this.submitAsFormData = submitAsFormData;
}

@Override
public IMendixObject executeAction() throws Exception
{
// BEGIN USER CODE
return RestConsumer.request(getContext(), HttpMethod.POST, collectionUrl, null, null, submitAsFormData, requestData).getMendixObject();
// END USER CODE
}

/**
* Returns a string representation of this action
*/
@Override
public java.lang.String toString()
{
return "postString";
}

// BEGIN EXTRA CODE
// END EXTRA CODE
}
24 changes: 21 additions & 3 deletions javasource/restservices/consume/RestConsumer.java
Original file line number Diff line number Diff line change
Expand Up @@ -447,8 +447,15 @@ public Boolean apply(IMendixObject item) {
});
}

// Marcel Groeneweg Overload to keep existing stuff running OK.
public static RequestResult request(final IContext context, HttpMethod method, String url,
final IMendixObject source, final IMendixObject target, final boolean asFormData) throws Exception {
return request(context, method, url, source, target, asFormData, null);
}

// Marcel Groeneweg Added requestData parameter
public static RequestResult request(final IContext context, HttpMethod method, String url,
final IMendixObject source, final IMendixObject target, final boolean asFormData, final String requestData) throws Exception {
lastConsumeError.set(null);

if (context == null)
Expand All @@ -468,9 +475,15 @@ public static RequestResult request(final IContext context, HttpMethod method, S
Map<String, String> params = new HashMap<String, String>();
RequestEntity requestEntity = null;

final JSONObject data = source == null ? null : JsonSerializer.writeMendixObjectToJson(context, source, false);
// Marcel Groeneweg use requestData string if available, use existing logic otherwise.
final JSONObject data;
if (requestData != null) {
data = new JSONObject(requestData);
} else {
data = source == null ? null : JsonSerializer.writeMendixObjectToJson(context, source, false);
}

boolean appendDataToUrl = source != null && (asFormData || method == HttpMethod.GET || method == HttpMethod.DELETE);
boolean appendDataToUrl = (source != null || requestData != null) && (asFormData || method == HttpMethod.GET || method == HttpMethod.DELETE);
url = updateUrlPathComponentsWithParams(url, appendDataToUrl, isFileSource, data, params);

//Setup request entity for file
Expand All @@ -480,8 +493,13 @@ public static RequestResult request(final IContext context, HttpMethod method, S
else if (source != null && asFormData && (isFileSource || hasFileParts)) {
requestEntity = buildMultiPartEntity(context, source, params);
}
else if (asFormData && !isFileSource)
else if (asFormData && !isFileSource) {
requestHeaders.put(RestServices.HEADER_CONTENTTYPE, RestServices.CONTENTTYPE_FORMENCODED);
// Marcel Groeneweg also log body here.
if (RestServices.LOGCONSUME.isDebugEnabled() && data != null) {
RestServices.LOGCONSUME.debug("[Body JSON Data] " + data.toString());
}
}
else if (data != null && data.length() != 0) {
requestEntity = new StringRequestEntity(data.toString(4), RestServices.CONTENTTYPE_APPLICATIONJSON, RestServices.UTF8);
if (RestServices.LOGCONSUME.isDebugEnabled()) {
Expand Down
1 change: 1 addition & 0 deletions javasource/system/UserActionsRegistrar.java
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ public void registerActions(IActionRegistrator registrator)
registrator.registerUserAction(restservices.actions.LoadService.class);
registrator.registerUserAction(restservices.actions.post.class);
registrator.registerUserAction(restservices.actions.post2.class);
registrator.registerUserAction(restservices.actions.postString.class);
registrator.registerUserAction(restservices.actions.postWithResult.class);
registrator.registerUserAction(restservices.actions.publishDelete.class);
registrator.registerUserAction(restservices.actions.publishUpdate.class);
Expand Down