Created
May 17, 2012 14:40
-
-
Save jtwaleson/2719351 to your computer and use it in GitHub Desktop.
RequestHandlers3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class RestHandler extends RequestHandler | |
{ | |
@Override | |
public void processRequest(IMxRuntimeRequest request, IMxRuntimeResponse response, String path) throws Exception | |
{ | |
Core.getLogger("RestHandler").info("Received request for path '" + path + "', resourcepath '" + request.getResourcePath() + "'."); | |
String[] pathParts = request.getResourcePath().split("/"); | |
String queryName = pathParts[pathParts.length-1]; | |
IContext systemContext = Core.createSystemContext(); | |
List objects = Core.retrieveXPathQuery(systemContext, "//MyFirstModule.Person[Name='" + queryName + "']"); | |
IMendixObject ourObject = objects.get(0); | |
String jsonString = toJSONString(systemContext, ourObject); | |
response.setContentType("application/json"); | |
OutputStream outputStream = response.getOutputStream(); | |
InputStream answerStream = IOUtils.toInputStream(jsonString); | |
IOUtils.copy(answerStream, outputStream); | |
IOUtils.closeQuietly(outputStream); | |
} | |
private String toJSONString(IContext systemContext, IMendixObject ourObject) | |
{ | |
JSONObject jsonObject = new JSONObject(); | |
Set members = ourObject.getMembers(systemContext).keySet(); | |
for (String memberName : members) | |
{ | |
Object value = ourObject.getValue(systemContext, memberName); | |
if (value instanceof IMendixIdentifier) | |
jsonObject.put(memberName, ((IMendixIdentifier)value).getGuid()); | |
else | |
jsonObject.put(memberName, value); | |
} | |
return jsonObject.toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment