Server Side Custom Code

App42 platform has various technical and business services using which app development can be done without writing a single line of backend code. However there might be a case where the app developer might need to have some server side custom code for their app. There could be multiple scenarios for example-

bullet You want to add server side validation and or custom logic on the input data instead of on the client.

bullet You want to make a custom service by aggregating two or more App42 services. For example aggregating Push and Storage service for custom requirement.

bullet App logic that might get changed in future and hence you don’t want to bundle it in your app. Instead you would like to put it on the server where it can be accessed through App42 SDK/REST calls from your app.

1. Writing Custom Code

bullet Download sample code from GitHub.

bullet Import the Sample Project in your Eclipse IDE.

bullet Open Java source file MyCustomCode.java inside com.shephertz.app42.paas.customcode.sample folder and modify the execute method of it as shown below.

package com.shephertz.app42.paas.customcode.sample;
//Your Custom Code Class
public class MyCustomCode implements Executor {
private ServiceAPI sp = new ServiceAPI("YOUR_APIKEY","YOUR_SECRETKEY");
private final int HTTP_STATUS_SUCCESS = 200;
private String moduleName = "App42CustomCodeTest";
/**
* Write your custom code inside this method
*/
@Override
public HttpResponseObject execute(HttpRequestObject request) {
		JSONObject body = request.getBody();
		// Build Log Service For logging in Your Code
		LogService logger = sp.buildLogService();
		logger.debug(" Recieved Request Body : :" + body.toString(), moduleName);
		 
		// Write Your Custom Code Here
		// ......//
		 
		logger.info("Running Custom Code ", moduleName);
		// Create JSON Response Based on Your business logic
		JSONObject jsonResponse = new JSONObject();
		try {
			jsonResponse.put("status", "success");
			jsonResponse.put("message", "From Custom Code");
		//....//
		} catch (JSONException e) {
			// Do exception Handling for JSON Parsing
		}
		// Return JSON Response and Status Code
		return new HttpResponseObject(HTTP_STATUS_SUCCESS, jsonResponse);
	}
}

bullet In the above example, if you want to rename or change the class name, you are free to do so however it should have parent package as com.shephertz.app42.paas.

bullet Above example uses App42 Logging Service to do the logging inside your custom code. These logs would be available to you inside AppHQ console.

bullet Custom Code deployed in our cloud runs inside a sand-boxed environment for each service. There are restrictions on using certain JAVA APIs like System, FileIO, Reflection, ByteCode injection etc. Also, your package name should start with com.shephertz.app42.paas.customcode to get it deployed on App42 cloud. If you have any requirement where you are looking to use these restricted APIs or any other third party Jar file, please write to us at support@shephertz.com for further help.

2. Deploying Custom Code from Command Line

bullet Once you have written your custom code, you can deploy it either through ant command line or using our APIs.

bullet To deploy your custom code through Ant script, run < ant deploy > from a command line inside your root folder of the sample project that you downloaded from above. Before running this command, modify build.properties to enter your APIKey and SecretKey for the same. Once you run the above command, you will be asked to enter the name for the custom code to be deployed on App42 Cloud. It’s default name is MyCustomCode. If you get BUILD SUCCESSFUL message after entering the name, your App is deployed on App42 Cloud. Otherwise you will get an appropriate exception message

3. Deploying Custom Code from AppHQ console

You can also deploy your server side custom code from AppHQ console. To deploy it, your custom code class should be wrapped onside jar file. You can use any java utility to create jar file of your code. Once your jar file is ready, you can deploy it from dashboard using Custom Code:

bullet Login to AppHQ Management Console

bullet Click on Custom Code -> Deploy Jar as shown below by passing name and target jar file.

DeployCC

Once your code is deployed successfully, you should be able to see it in service listing option under Custom Code panel in dashboard as shown below.

DeployedSuccess

4. Running Custom Code

Once your custom code service is deployed, it can be accessed through App42 SDKs available in different languages using Custom Code Service and calling runJavaCode on it. Example of Calling Custom code from Android/JAVA SDK is shown below

  • create User Api for Android
  • create User Api for J2ME
  • create User Api for Android
  • create User Api for iOS
  • create User Api for Java
  • create User Api for .NET
  • create User Api for Unity
  • create User Api for Ruby
  • create User Api for Rest
  •  create User Api for WP7/WP8
  • create User Api for Flash
String name = "DEPLOYED_CUSTOM_CODE_NAME";
JSONObject jsonBody = new JSONObject();
jsonBody.put("Company", "Shephertz");
customCode.runJavaCode(name,jsonBody,new App42CallBack() {
public void onSuccess(Object response) 
{
	JSONObject  res= (JSONObject)response;      
	System.out.println("response is " + res) ;
}
public void onException(Exception ex) 
{
	System.out.println("Exception Message"+ex.getMessage());
}
});
NSString *name = @"DEPLOYED_CUSTOM_CODE_NAME"; 
NSDictionary *requestBody = [NSDictionary dictionaryWithObjectsAndKeys:@"Company",@"Shephertz", nil];
App42Response *response = = [customCodeService runJavaCode:name requestBody:requestBody]; 
NSString *jsonResponse = [response toString]; 
String name = "DEPLOYED_CUSTOM_CODE_NAME";
JObject jsonBody = new JObject();
jsonBody.Add("Company", "Shephertz");
customCodeService.RunJavaCode(name, jsonBody, new Callback());  
public class Callback : App42Callback  
{  
	public void OnException(App42Exception exception)  
	{  
		Console.WriteLine("Exception Message : " + exception);  
	}  
	public void OnSuccess(Object response)  
	{  
		 JObject json = (JObject) response;     
		 String jsonResponse = json.ToString();  
    }  
} 
String name = "DEPLOYED_CUSTOM_CODE_NAME";
JSONObject jsonBody = new JSONObject();
jsonBody.put("Company", "Shephertz");
JSONObject response = customCodeService.runJavaCode(name, jsonBody);
System.out.println("response is " + response);
Not Available
String name = "DEPLOYED_CUSTOM_CODE_NAME";
JSONObject jsonBody = new JSONObject();
jsonBody.put("Company", "Shephertz");
JSONObject response = customCodeService.runJavaCode(name, jsonBody);
System.out.println("response is " + response);	
String name = "DEPLOYED_CUSTOM_CODE_NAME";
JObject jsonBody = new JObject();
jsonBody.Add("Company", "Shephertz");
App42Log.SetDebug(true);		//Print output in your editor console
customCodeService.RunJavaCode(name, jsonBody, new UnityCallBack()); 
public class UnityCallBack : App42CallBack
{
	public void OnSuccess(object response)
	{
		JObject objecti = (JObject)response;
		App42Log.Console("objectName is : " + objecti["name"]);
		App42Log.Console("Success : " + response);
	}

	public void OnException(Exception e)
	{
		App42Log.Console("Exception : " + e);
	}
}
String name = "DEPLOYED_CUSTOM_CODE_NAME";
JObject jsonBody = new JObject();
jsonBody.Add("Company", "Shephertz");
JObject response = customCodeService.RunJavaCode(name, jsonBody);
Console.WriteLine("response is " + response);	
NA
NA
var name:String = "DEPLOYED_CUSTOM_CODE_NAME";  
var jsonObject:Object = new Object;
jsonObject.Company = "Shephertz";
customcodeService.runJavaCode(name,jsonObject,new callback()); 
public class callback implements App42CallBack  
{  
	public function onException(exception:App42Exception):void  
	{  
		trace("Exception Message " + exception);  
	}  
	public function onSuccess(response:Object):void  
	{  
		trace("Response is :" + Util.toString(response));
		trace("Object Name is:"+Util.toString(response.name));
	}  
}
Coming Soon

5. Scheduling Custom Code

You can also schedule server side custom code if you require to use it as scheduled job on server side instead of calling from API. Here are the steps to schedule server side custom code from AppHQ console.

bullet Select Custom Code -> Deploy Jar -> Select your App and Custom Code name from the list

bullet Click on Manage Button adjacent to Custom Code name.

bullet Fill all required details in pop up window as shown below:

Scheduled

JSON Body : JSON to be passed as input to your code (non-mandatory)

Date : Date for your custom code job execution

Time : Time for job execution

Time Zone : Time Zone in which you have passed the above two values i.e. date and time

Repeat Count : How many times your job should run after first execution. Pass 0 if you want to run it only one time otherwise pass increment from first execution.

Repeat Forever : Select if you want your job to be run always. This will be equivalent to infinite repeat count value.

Repeat Interval : Interval in seconds between consecutive job execution time.

bullet Once you are done with above details, click on Schedule button.

Once your code is scheduled you should be able to see scheduled service details like next fire time, previous fire time, repeat count, and interval defined as shown below.

ViewScheduled

You can cancel your schedule job any time by clicking on Cancel Schedule button in top menu in center panel. Also, you can check scheduled job logs under Scheduling Logs option.

More Details?

Watch complete video on YouTube

Custom Code Video