org.apache.flume.source.http

## Class JSONHandler

- java.lang.Object
- - org.apache.flume.source.http.JSONHandler

- All Implemented Interfaces:
    Configurable, HTTPSourceHandler

      public class JSONHandler
      extends Object
      implements HTTPSourceHandler

  JSONHandler for HTTPSource that accepts an array of events. This handler throws exception if the deserialization fails because of bad format or any other reason. Each event must be encoded as a map with two key-value pairs.

  1. headers - the key for this key-value pair is "headers". The value for this key is another map, which represent the event headers. These headers are inserted into the Flume event as is.

  2. body - The body is a string which represents the body of the event. The key for this key-value pair is "body". All key-value pairs are considered to be headers. An example:

  [{"headers" : {"a":"b", "c":"d"},"body": "random_body"}, {"headers" : {"e": "f"},"body": "random_body2"}]

  would be interpreted as the following two flume events:

  * Event with body: "random_body" (in UTF-8/UTF-16/UTF-32 encoded bytes) and headers : (a:b, c:d)

  * Event with body: "random_body2" (in UTF-8/UTF-16/UTF-32 encoded bytes) and headers : (e:f)

  The charset of the body is read from the request and used. If no charset is set in the request, then the charset is assumed to be JSON's default - UTF-8. The JSON handler supports UTF-8, UTF-16 and UTF-32. To set the charset, the request must have content type specified as "application/json; charset=UTF-8" (replace UTF-8 with UTF-16 or UTF-32 as required). One way to create an event in the format expected by this handler, is to use JSONEvent and use Gson to create the JSON string using the Gson.toJson(java.lang.Object, java.lang.reflect.Type) method. The type token to pass as the 2nd argument of this method for list of events can be created by:

  `Type type = new TypeToken<List<JSONEvent>>() {}.getType();`

- - ### Constructor Summary

    Constructors
    Constructor and Description
    `JSONHandler()`

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `void` | `configure(Context context)` Request the implementing class to (re)configure itself.
    `List<Event>` | `getEvents(javax.servlet.http.HttpServletRequest request)` Takes an HttpServletRequest and returns a list of Flume Events.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Constructor Detail

    - #### JSONHandler

          public JSONHandler()

  - ### Method Detail

    - #### getEvents

          public List<Event> getEvents(javax.servlet.http.HttpServletRequest request)
                                throws Exception

      Takes an HttpServletRequest and returns a list of Flume Events. If this request cannot be parsed into Flume events based on the format this method will throw an exception. This method may also throw an exception if there is some sort of other error.

      Specified by:
        `getEvents` in interface `HTTPSourceHandler`
      Parameters:
        `request` - The request to be parsed into Flume events.
      Returns:
        List of Flume events generated from the request.
      Throws:
        `HTTPBadRequestException` - If the was not parsed correctly into an event because the request was not in the expected format.
        `Exception` - If there was an unexpected error.

    - #### configure

          public void configure(Context context)

      Description copied from interface: `Configurable`

      Request the implementing class to (re)configure itself.

      When configuration parameters are changed, they must be reflected by the component asap.

      There are no thread safety guarantees on when configure might be called.

      Specified by:
        `configure` in interface `Configurable`

Copyright © 2009-2016 Apache Software Foundation. All Rights Reserved.