Web Server Response Adapter

Introduction

The Spotfire Streaming Web Server Response Adapter allows the system to handle HTTP and WebSocket responses. Each request from a Web Server Request adapter must end up in a Web Server Request Adapter. The adapter uses the requestId field of the incoming tuple to determine which web server and request/response object to send the HTTP response to. To close a websocket send a none null status code in with the requestId of the websocket.

Adapter Properties

This section describes the properties you can set for this adapter, using the various tabs of the Properties view in StreamBase Studio.

General Tab

Name: Use this required field to specify or change the name of this instance of this component. The name must be unique within the current EventFlow module. The name can contain alphanumeric characters, underscores, and escaped special characters. Special characters can be escaped as described in Identifier Naming Rules. The first character must be alphabetic or an underscore.

Operator: A read-only field that shows the formal name of the operator.

Class name: Shows the fully qualified class name that implements the functionality of this operator. If you need to reference this class name elsewhere in your application, you can right-click this field and select Copy from the context menu to place the full class name in the system clipboard.

Start options: This field provides a link to the Cluster Aware tab, where you configure the conditions under which this adapter starts.

Enable Error Output Port: Select this checkbox to add an Error Port to this component. In the EventFlow canvas, the Error Port shows as a red output port, always the last port for the component. See Using Error Ports to learn about Error Ports.

Description: Optionally, enter text to briefly describe the purpose and function of the component. In the EventFlow Editor canvas, you can see the description by pressing Ctrl while the component's tooltip is displayed.

Adapter Properties Tab

Property Type Description
WebServer Configuration Edit Button Shortcut to the StreamBase Configuration File Editor, used for adapter configuration or converting an existing application's adapter-configurations.xml file to HOCON format.
Request Id Field Name string The field in the incoming tuple which represents the requestId value to be used to look up the original HTTP request and corresponding web server. This field must be of type string.
Data Field Name string The field in the incoming tuple which represents the data to send as the HTTP response. This field must be of type string or blob.
Http Headers Field Name string The field in the incoming tuple which represents the HTTP headers to send with the HTTP response. This field must be of type list<tuple<string Key,string Value>>.

For example (long lines wrap to the next for clarity):

list(
    tuple('Access-Control-Allow-Origin' as Key, '*' as Value),
    tuple('Access-Control-Allow-Headers' as Key, 'Origin, 
      X-Requested-With, Content-Type, Accept' as Value),
    tuple('Test-Header-page' as Key, '1234' as Value)
)
Status Code Field Name string *Optional. The field in the incoming tuple which represents the status code to send with the HTTP response. The field must be of type int. If the value is not specified, the default 200 is used. If the requestId corresponds to a websocket and the status code is not null the socket will be closed with the status code given.
Content Type Field Name string *Optional. The field in the incoming tuple which represents the content type to send with the HTTP response. The field must be of type string. If the value is not specified, the system tries to determine the best content type based on the data field.
Cookies Field Name string *Optional. The field in the incoming tuple which represents the cookies to send with the HTTP response. The field can have the following schemas:
  • list<tuple<sting name,string value>>

  • list<tuple<sting name,string value, int maxAge>>

  • list<tuple<sting name,string value,int maxAge,string comment,string domain,boolean httpOnly,string path,boolean secure,int version>>

Log Level INFO Controls the level of verbosity the adapter uses to issue informational traces to the console. This setting is independent of the containing application's overall log level. Available values, in increasing order of verbosity, are: OFF, ERROR, WARN, INFO, DEBUG, TRACE.

Response Tab

Property Type Description
Response Data Transformer Accept Type Map string, string

The key/value map which has a regex key that matches to the requests ACCEPT header and the value is a class that implements com.streambase.sb.adapter.webserver.data.IResponseDataTransformer. By default JSON and XML data transformers are set up to handle any JSON or XML standard response.

Response Data Transformer Settings string, string The key/value map of settings to pass to the Response Data Transformers. The key is the classname.setting of each setting to send to individual data transformers. The value is the setting value to set. For example to set the JSONResponseDataTransformer timestamp format you would have a key of JSONResponseDataTransformer.TimestampFormat and some string format for the value.

HTTP Headers Tab

Property Type Description
Default HTTP Headers string, string

The default HTTP headers that are added to each output response. These values are overridden by any HTTP fields given by the input tuple.

An example of an HTTP header might be Access-Control-Allow-Origin with a value of * to allow all cross-server requests.

Cluster Aware Tab

Use the settings in this tab to enable this operator or adapter for runtime start and stop conditions in a multi-node cluster. During initial development of the fragment that contains this operator or adapter, and for maximum compatibility with releases before 10.5.0, leave the Cluster start policy control in its default setting, Start with module.

Cluster awareness is an advanced topic that requires an understanding of StreamBase Runtime architecture features, including clusters, quorums, availability zones, and partitions. See Cluster Awareness Tab Settings on the Using Cluster Awareness page for instructions on configuring this tab.

Concurrency Tab

Use the Concurrency tab to specify parallel regions for this instance of this component, or multiplicity options, or both. The Concurrency tab settings are described in Concurrency Options, and dispatch styles are described in Dispatch Styles.

Caution

Concurrency settings are not suitable for every application, and using these settings requires a thorough analysis of your application. For details, see Execution Order and Concurrency, which includes important guidelines for using the concurrency options.

Response Data Transformer

The response data transformer interface com.streambase.sb.adapter.webserver.data.IResponseDataTransformer is used to convert tuples to a POST response.

com.streambase.sb.adapter.webserver.data.IRequestDataTransformer

package com.streambase.sb.adapter.webserver.data;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.eclipse.jetty.server.Request;
import org.slf4j.Logger;

import com.streambase.sb.Schema;
import com.streambase.sb.StreamBaseException;
import com.streambase.sb.Tuple;
import com.streambase.sb.operator.TypecheckException;

public interface IResponseDataTransformer {
    /***
     * This method will be called during typecheck time to allow 
     * a response data transformer to validate a key value map of settings 
     * against the transformer
     * 
     * @param settings A key value map of settings the end user specified for 
     * this adapters custom data transformation
     * @param schema The schema of the input data to transform
     * @throws TypecheckException
     */
    public void validateSettings(Map<String, String> settings, Schema schema) throws 
    TypecheckException;

    /***
     * This method allows the tuple to be sent to the HTTP response with any custom 
     * logic required
     * 
     * @param logger A logger than can be used to log any extra information which may 
     * be required
     * @param settings A key value map of settings the end user specified for this 
     * adapters custom data transformation
     * @param httpServletRequest The HTTP request
     * @param request The base HTTP request
     * @param tuple The tuple to convert
     * @throws StreamBaseException
     */
    public void transformResponseData(Logger logger, Map<String, String> settings, 
    HttpServletRequest httpServletRequest, Request request, HttpServletResponse 
    httpServletResponse, Tuple tuple) throws StreamBaseException;
}

com.streambase.sb.adapter.webserver.data.JSONResponseDataTransformer

The JSON response data transform converts a tuple to a JSON response. If no content type is set, this will set it as application/json.

Settings

Setting Type Default Description
TimestampFormat string yyyy-MM-dd HH:mm:ss.SSSZ The timestamp format used when converting a tuple timestamp fields to JSON
TimestampAsLong true/false false If true timestamp fields will be output as long values
IncludeNullFields true/false false If true null fields are included in the output JSON
SerializedSubTupleType Map/List Map The type of JSON to create

com.streambase.sb.adapter.webserver.data.XMLResponseDataTransformer

The XML response data transform converts a tuple to an XML response. If no content type is set, this will set it as application/xml.

Settings

Setting Type Default Description
TimestampFormat string yyyy-MM-dd HH:mm:ss.SSSZ The timestamp format used when converting a timestamp field to XML
IncludeNullListValues true/false true If true null fields in the tuple will be included in the XML response
NullListValueRepresentation string null The string value to set for any field that is null in the input tuple
WrapperFieldName string Response The outer most XML element to create when converting the tuple to XML to wrap the tuples fields.