- Index

< Previous |  | Next >

# Web Server Response Adapter

## Introduction

The TIBCO StreamBase® 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.

## 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 component's name, which must be unique in the current module. The name must contain only alphabetic characters, numbers, and underscores, and no hyphens or other special characters. The first character must be alphabetic or an underscore.

Adapter: A read-only field that shows the formal name of the adapter.

Class name: Shows the fully qualified class name that implements the functionality of this adapter. 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 with application: If this field is set to Yes (default) or to a module parameter that evaluates to `true`, this instance of this adapter starts as part of the JVM engine that runs this EventFlow fragment. If this field is set to No or to a module parameter that evaluates to `false`, the adapter instance is loaded with the engine, but does not start until you send an epadmin container resume command (or its sbadmin equivalent), or until you start the component with StreamBase Manager.

Enable Error Output Port: Select this check box 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 component's purpose and function. In the EventFlow canvas, you can see the description by pressing Ctrl while the component's tooltip is displayed.

### Adapter Properties Tab

Property | Type | Description
--- | --- | ---
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.
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 | drop-down list | 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 Properties 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 Properties 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.

### 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.

## Related Topics

- TIBCO StreamBase® Web Server Request Adapter

- Using Embedded Adapters