<commit_before># Stackmob Custom Code SDK

The StackMob Custom Code SDK supports both Java and Scala custom code. Ruby support is also available via a Heroku add-on. Please see the [stackmob-ruby](https://github.com/stackmob/stackmob-ruby) SDK for more details.

## Using the SDK

The Custom Code SDK is available via the Central Maven Repository. Below are examples of using the Custom Code SDK with a number of popular build tools.

**Maven**

    <dependency>
      <groupId>com.stackmob</groupId>
      <artifactId>customcode</artifactId>
      <version>0.1.0</version>
    </dependency>

**sbt**

    val stackmob = "com.stackmob" % "customcode" % "0.1.0"

**Apache Ivy (Ant)**

    <dependency org="com.stackmob" name="customcode" rev="0.1.0"/>

**Apache Buildr**

    'com.stackmob:customcode:jar:0.1.0'

## Javadocs

[Javadocs are available here](http://stackmob.github.com/stackmob-customcode-sdk/0.1.0/apidocs/).

## Extend your REST API

### Why should I extend my REST API?

StackMob generates your persistence layer and a REST API for you automatically via the [Object Model creation process](https://www.stackmob.com/platform/api/objects/create), but you'll likely want to do more than just save and fetch data. The SDK enables you to run custom server-side code, interact with the datastore, and extend the REST API to support your own custom methods.

### Write a new REST API method

Once the JAR file is in your classpath, let's try a "Hello World" example. We will be extending your REST API so that calling:

    http://yourclient.mob1.stackmob.com/api/1/yourapp/hello_world_example

will return a JSON object:

    {"greeting": "hello world!"}

**Java**

    ```java
    package com.stackmob.example.helloworld;

    import com.stackmob.core.customcode.CustomCodeMethod;
    import com.stackmob.core.rest.ProcessedAPIRequest;
    import com.stackmob.core.rest.ResponseToProcess;
    import com.stackmob.sdkapi.SDKServiceProvider;
    import java.util.HashMap;

    public class HelloWorldExample implements CustomCodeMethod {

      /**
       * This method simply returns the name of your method that we'll expose over REST for
       * this class. Although this name can be anything you want, we recommend replacing the
       * camel case convention in your class name with underscores, as shown here.
       *
       * @return the name of the method that should be exposed over REST
       */
      @Override
      public String getMethodName() {
        return "hello_world_example";
      }

      /**
       * This method returns the parameters that your method should expect in its query string.
       * Here we are using no parameters, so we just return an empty list.
       *
       * @return a list of the parameters to expect for this REST method
       */
      @Override
      public List<String> getParams() {
        return Arrays.asList();
      }

      /**
       * This method contains the code that you want to execute.
       *
       * @return the response
       */
      @Override
      public ResponseToProcess execute(ProcessedAPIRequest request, SDKServiceProvider serviceProvider) {
        return new ResponseToProcess(200, new HashMap<String, String>("greeting", "hello world!"));
      }

    }
    ```

**Scala**

    package com.stackmob.example.helloworld

    import com.stackmob.core.customcode.CustomCodeMethod
    import com.stackmob.sdkapi.SDKServiceProvider
    import com.stackmob.core.rest.{ResponseToProcess, ProcessedAPIRequest}
    import java.util.{Arrays, List}
    import scala.collection.JavaConversions._

    class HelloWorldExample extends CustomCodeMethod {
<commit_msg>test didn't work..<commit_after># Stackmob Custom Code SDK

The StackMob Custom Code SDK supports both Java and Scala custom code. Ruby support is also available via a Heroku add-on. Please see the [stackmob-ruby](https://github.com/stackmob/stackmob-ruby) SDK for more details.

## Using the SDK

The Custom Code SDK is available via the Central Maven Repository. Below are examples of using the Custom Code SDK with a number of popular build tools.

**Maven**

    <dependency>
      <groupId>com.stackmob</groupId>
      <artifactId>customcode</artifactId>
      <version>0.1.0</version>
    </dependency>

**sbt**

    val stackmob = "com.stackmob" % "customcode" % "0.1.0"

**Apache Ivy (Ant)**

    <dependency org="com.stackmob" name="customcode" rev="0.1.0"/>

**Apache Buildr**

    'com.stackmob:customcode:jar:0.1.0'

## Javadocs

[Javadocs are available here](http://stackmob.github.com/stackmob-customcode-sdk/0.1.0/apidocs/).

## Extend your REST API

### Why should I extend my REST API?

StackMob generates your persistence layer and a REST API for you automatically via the [Object Model creation process](https://www.stackmob.com/platform/api/objects/create), but you'll likely want to do more than just save and fetch data. The SDK enables you to run custom server-side code, interact with the datastore, and extend the REST API to support your own custom methods.

### Write a new REST API method

Once the JAR file is in your classpath, let's try a "Hello World" example. We will be extending your REST API so that calling:

    http://yourclient.mob1.stackmob.com/api/1/yourapp/hello_world_example

will return a JSON object:

    {"greeting": "hello world!"}

**Java**

```java
    package com.stackmob.example.helloworld;

    import com.stackmob.core.customcode.CustomCodeMethod;
    import com.stackmob.core.rest.ProcessedAPIRequest;
    import com.stackmob.core.rest.ResponseToProcess;
    import com.stackmob.sdkapi.SDKServiceProvider;
    import java.util.HashMap;

    public class HelloWorldExample implements CustomCodeMethod {

      /**
       * This method simply returns the name of your method that we'll expose over REST for
       * this class. Although this name can be anything you want, we recommend replacing the
       * camel case convention in your class name with underscores, as shown here.
       *
       * @return the name of the method that should be exposed over REST
       */
      @Override
      public String getMethodName() {
        return "hello_world_example";
      }

      /**
       * This method returns the parameters that your method should expect in its query string.
       * Here we are using no parameters, so we just return an empty list.
       *
       * @return a list of the parameters to expect for this REST method
       */
      @Override
      public List<String> getParams() {
        return Arrays.asList();
      }

      /**
       * This method contains the code that you want to execute.
       *
       * @return the response
       */
      @Override
      public ResponseToProcess execute(ProcessedAPIRequest request, SDKServiceProvider serviceProvider) {
        return new ResponseToProcess(200, new HashMap<String, String>("greeting", "hello world!"));
      }

    }
```

**Scala**

    package com.stackmob.example.helloworld

    import com.stackmob.core.customcode.CustomCodeMethod
    import com.stackmob.sdkapi.SDKServiceProvider
    import com.stackmob.core.rest.{ResponseToProcess, ProcessedAPIRequest}
    import java.util.{Arrays, List}
    import scala.collection.JavaConversions._

    class HelloWorldExample extends CustomCodeMethod {

{'fraction_non_alphanumeric': 0.08846523110684405, 'fraction_numerical': 0.00548621588259498, 'mean_word_length': 3.2223508975101334, 'pattern_counts': {'":': 2, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 4, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1230209', 'n_tokens_mistral': 2112, 'n_tokens_neox': 2003, 'n_words': 734}