Java tutorial
/* * Copyright (c) 2011-2014, MOBICAGE NV * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by Mobicage NV. * 4. Neither the name of the Mobicage NV nor the * names of its contributors may be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY MOBICAGE NV ''AS IS'' AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL MOBICAGE NV BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * @@license_version:1.7@@ */ package com.mobicage.rogerthat; import java.util.logging.Level; import java.util.logging.Logger; import org.json.simple.JSONObject; import com.mobicage.rogerthat.callbacks.RequestContext; import com.mobicage.rogerthat.callbacks.test.TestHandler; import com.mobicage.rogerthat.callbacks.test.TestRequest; import com.mobicage.rogerthat.callbacks.test.TestResponse; public class CallbackProcessor { private static final Logger log = Logger.getLogger(CallbackProcessor.class.getName()); public com.mobicage.rogerthat.callbacks.test.TestHandler testTestHandler = new TestHandler() { @Override public TestResponse handle(TestRequest request) { TestResponse result = new TestResponse(); result.setValue(request.getValue()); return result; } }; public com.mobicage.rogerthat.callbacks.messaging.UpdateHandler messagingUpdateHandler = null; public com.mobicage.rogerthat.callbacks.messaging.PokeHandler messagingPokeHandler = null; public com.mobicage.rogerthat.callbacks.messaging.FormUpdateHandler messagingFormUpdateHandler = null; public com.mobicage.rogerthat.callbacks.messaging.FlowMemberResultHandler messagingFlowMemberResultHandler = null; public com.mobicage.rogerthat.callbacks.friend.InviteResultHandler friendInviteResultHandler = null; public com.mobicage.rogerthat.callbacks.friend.InvitedHandler friendInvitedHandler = null; public com.mobicage.rogerthat.callbacks.friend.BrokeUpHandler friendBrokeUpHandler = null; public com.mobicage.rogerthat.callbacks.friend.InReachHandler friendInReachHandler = null; public com.mobicage.rogerthat.callbacks.friend.OutOfReachHandler friendOutOfReachHandler = null; public com.mobicage.rogerthat.callbacks.system.ApiCallHandler systemApiCallHandler = null; @SuppressWarnings("unchecked") public void process(final JSONObject request, final JSONObject response, final RequestContext requestContext) { final String method = (String) request.get("method"); final JSONObject params = (JSONObject) request.get("params"); response.put("id", requestContext.getRequestId()); response.put("error", null); response.put("result", null); try { // Handle if ("test.test".equals(method)) { if (testTestHandler != null) { com.mobicage.rogerthat.callbacks.test.TestRequest req = new com.mobicage.rogerthat.callbacks.test.TestRequest( requestContext, params); com.mobicage.rogerthat.callbacks.test.TestResponse resp = testTestHandler.handle(req); response.put("result", resp.toJSONObject()); } } else if ("messaging.update".equals(method)) { if (messagingUpdateHandler != null) { com.mobicage.rogerthat.callbacks.messaging.UpdateRequest req = new com.mobicage.rogerthat.callbacks.messaging.UpdateRequest( requestContext, params); com.mobicage.rogerthat.callbacks.messaging.UpdateResponse resp = messagingUpdateHandler .handle(req); response.put("result", resp == null ? null : resp.toJSONObject()); } } else if ("messaging.poke".equals(method)) { if (messagingPokeHandler != null) { com.mobicage.rogerthat.callbacks.messaging.PokeRequest req = new com.mobicage.rogerthat.callbacks.messaging.PokeRequest( requestContext, params); com.mobicage.rogerthat.callbacks.messaging.PokeResponse resp = messagingPokeHandler.handle(req); response.put("result", resp == null ? null : resp.toJSONObject()); } } else if ("friend.invite_result".equals(method)) { if (friendInviteResultHandler != null) { com.mobicage.rogerthat.callbacks.friend.InviteResultRequest req = new com.mobicage.rogerthat.callbacks.friend.InviteResultRequest( requestContext, params); com.mobicage.rogerthat.callbacks.friend.InviteResultResponse resp = friendInviteResultHandler .handle(req); response.put("result", resp == null ? null : resp.toJSONObject()); } } else if ("friend.invited".equals(method)) { if (friendInvitedHandler != null) { com.mobicage.rogerthat.callbacks.friend.InvitedRequest req = new com.mobicage.rogerthat.callbacks.friend.InvitedRequest( requestContext, params); com.mobicage.rogerthat.callbacks.friend.InvitedResponse resp = friendInvitedHandler.handle(req); response.put("result", resp.getResult()); } } else if ("friend.broke_up".equals(method)) { if (friendBrokeUpHandler != null) { com.mobicage.rogerthat.callbacks.friend.BrokeUpRequest req = new com.mobicage.rogerthat.callbacks.friend.BrokeUpRequest( requestContext, params); com.mobicage.rogerthat.callbacks.friend.BrokeUpResponse resp = friendBrokeUpHandler.handle(req); response.put("result", resp == null ? null : resp.toJSONObject()); } } else if ("friend.in_reach".equals(method)) { if (friendInReachHandler != null) { com.mobicage.rogerthat.callbacks.friend.InReachRequest req = new com.mobicage.rogerthat.callbacks.friend.InReachRequest( requestContext, params); com.mobicage.rogerthat.callbacks.friend.InReachResponse resp = friendInReachHandler.handle(req); response.put("result", resp == null ? null : resp.toJSONObject()); } } else if ("friend.out_of_reach".equals(method)) { if (friendOutOfReachHandler != null) { com.mobicage.rogerthat.callbacks.friend.OutOfReachRequest req = new com.mobicage.rogerthat.callbacks.friend.OutOfReachRequest( requestContext, params); com.mobicage.rogerthat.callbacks.friend.OutOfReachResponse resp = friendOutOfReachHandler .handle(req); response.put("result", resp == null ? null : resp.toJSONObject()); } } else if ("messaging.form_update".equals(method)) { if (messagingFormUpdateHandler != null) { com.mobicage.rogerthat.callbacks.messaging.FormUpdateRequest req = new com.mobicage.rogerthat.callbacks.messaging.FormUpdateRequest( requestContext, params); com.mobicage.rogerthat.callbacks.messaging.FormUpdateResponse resp = messagingFormUpdateHandler .handle(req); response.put("result", resp == null ? null : resp.toJSONObject()); } } else if ("messaging.flow_member_result".equals(method)) { if (messagingFlowMemberResultHandler != null) { com.mobicage.rogerthat.callbacks.messaging.FlowMemberResultRequest req = new com.mobicage.rogerthat.callbacks.messaging.FlowMemberResultRequest( requestContext, params); com.mobicage.rogerthat.callbacks.messaging.FlowMemberResultResponse resp = messagingFlowMemberResultHandler .handle(req); response.put("result", resp == null ? null : resp.toJSONObject()); } } else if ("system.api_call".equals(method)) { if (systemApiCallHandler != null) { com.mobicage.rogerthat.callbacks.system.ApiCallRequest req = new com.mobicage.rogerthat.callbacks.system.ApiCallRequest( requestContext, params); com.mobicage.rogerthat.callbacks.system.ApiCallResponse resp = systemApiCallHandler.handle(req); response.put("result", resp == null ? null : resp.toJSONObject()); } } } catch (Throwable e) { log.log(Level.WARNING, "Caught exception in CallBackApiListener", e); response.put("error", e.toString()); } } }