Request-Response Processing for REST Services
Introduction
This document will go through the steps in adding Request-Response processing for REST services integrated with a WaveMaker application.
Overview
HttpRequest
and HttpReponse
processors are available for all REST Services with proxy enabled. They are used to process the request and response of the REST Service at runtime.
HttpRequestProcessor: This is used to process the request before sending it to the server. Processing of request can be used to add/delete/modify request headers or update request body before hitting the Service End Point.
HttpResponseProcessor: This is used to process the response sent by the server before sending it to the client. Processing of response includes adding/removing/modifying response headers or modifying the response body.
Following is the Request life cycle for a REST Service in a WaveMaker app: The following request and response processors are provided, by default, for every REST service integrated into WaveMaker Apps.
PassDefaultHeadersRequestProcessor
This process parses the defaultHeaders from the incoming request if present and passes it onto the target endpoint. This processor takes a set of defaultHeaders as the input parameters. Eg: User-Agent, Content-Type, Accept etc.
PrefixHttpResponseHeadersResponseProcessor
This processor is used to add a headerPrefix to all response headers returned by the server. A set of response headers can be excluded from getting prefixed by giving excludeList as input to the processor.
HttpResponseCookieProcessor
This processor can be used to configure whether to include or exclude the cookies sent by the server to the client. The PersistenceStrategy property can be set to:
- NONE - removes all cookies.
- USER_SESSION - persists cookies in the user session with attribute “wm.cookieStore“
- CLIENT - sends all response cookies to the client.
A RestRuntimeConfigBean is generated by WaveMaker wherein a list of Request-Response processors, as mentioned above, are injected. Sample restRuntimeConfigBean looks as shown below.
Each HttpRequestProcessor will be called sequentially in the input http request and each HttpResponseProcessor will be called sequentially before writing the response to WaveMaker client.
<bean class="com.wavemaker.runtime.rest.processor.RestRuntimeConfig" id="restRuntimeConfig">
<property name="httpRequestProcessorList">
<list>
<bean class="com.wavemaker.runtime.rest.processor.request.PassDefaultHeadersRequestProcessor">
<property name="enabled" value="true"/>
<property name="defaultHeaders">
<list>
<value>User-Agent</value>
<value>Content-Type</value>
<value>Accept</value>
<value>Accept-Charset</value>
<value>Accept-Language</value>
</list>
</property>
</bean>
</list>
</property>
<property name="httpResponseProcessorList">
<list>
<bean class="com.wavemaker.runtime.rest.processor.response.HttpResponseCookieProcessor">
<property name="enabled" value="true"/>
<property name="persistenceStrategy" value="CLIENT || USER_SESSION || NONE"/>
</bean>
<bean class="com.wavemaker.runtime.rest.processor.response.PrefixHttpResponseHeadersResponseProcessor">
<property name="enabled" value="true"/>
<property name="headerPrefix" value="X-WM-"/>
<property name="excludeList">
<list>
<value>Content-Disposition</value>
<value>Content-Type</value>
<value>Set-Cookie</value>
</list>
</property>
</bean>
</list>
</property>
</bean>
Configuring Request and Response Processor properties in WaveMaker
In order to configure the request and response processors:
- From File Explorer, open
<serviceId>.spring.xml
file (services/serviceName/src/serviceId.spring.xml
) of the REST Service you wish to configure. - You can configure properties of the request and response processors by editing property values of bean with id: “serviceId+RestRuntimeConfig”.
- All the Request-Response processors are enabled by default. In order to disable a processor set enabled flag to false.
Custom Request-Response Processors
You can add your own custom Request-Response Processors. In order to create custom HttpRequestProcessor/HttpResponseProcessor
:
- Create a Java Service that implements the HttpRequestProcessor/HttpResponseProcessor. You can also import the Java Class directly into
src/main/Java/<package_path>
. (Note this<package_path>
needs to be created and should be given when adding the Java Class Bean in the next step.) - Add the Java Class Bean to httpRequestProcessorList/httpResponseProcessorList of restRuntimeConfigBean, in the above mentioned
<serviceId>.spring.xml
file (services/serviceName/src/serviceName.spring.xml) of the REST Service you wish to customize.
Interface to implement for creating custom request processor
package com.wavemaker.runtime.rest.processor.request;
public interface HttpRequestProcessor {
void process(HttpRequestProcessorContext httpRequestProcessorContext);
}
Use Case: Consider the case where the logged in user tenant id values need to be added to the request header, the following is the Java implementation:
public class DefaultHeaderRequestProcessor implements HttpRequestProcessor {
@Override
public void process(HttpRequestProcessorContext httpRequestProcessorContext) {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (authentication != null && authentication.getPrincipal() instanceof WMUserDetails) {
WMUserDetails principal = (WMUserDetails) authentication.getPrincipal();
int tenantId = principal.getTenantId();
httpRequestProcessorContext.getHttpRequestDetails().getHttpHeaders().add("tenantId", Integer.toString(tenantId));
}
}
}
Ensure that proper classes are imported, for example for the above example:
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import com.wavemaker.runtime.rest.processor.request.HttpRequestProcessor;
import com.wavemaker.runtime.rest.processor.request.HttpRequestProcessorContext;
import com.wavemaker.runtime.security.WMUserDetails;
Interface to implement for creating custom response processor
package com.wavemaker.runtime.rest.processor.response;
public interface HttpResponseProcessor {
void process(HttpResponseProcessorContext httpResponseProcessorContext);
}
HttpRequestProcessorContext
HttpRequestProcessorContext
has the following structure:
public class HttpRequestProcessorContext {
private HttpServletRequest httpServletRequest;
private HttpRequestDetails httpRequestDetails;
private HttpRequestData httpRequestData;
public HttpRequestProcessorContext(HttpServletRequest httpServletRequest, HttpRequestDetails httpRequestDetails, HttpRequestData httpRequestData) {
this.httpServletRequest = httpServletRequest;
this.httpRequestDetails = httpRequestDetails;
this.httpRequestData = httpRequestData;
}
public HttpServletRequest getHttpServletRequest() {
return httpServletRequest;
}
public HttpRequestDetails getHttpRequestDetails() {
return httpRequestDetails;
}
public HttpRequestData getHttpRequestData() {
return httpRequestData;
}
}
HttpResponseProcessorContext
HttpResponseProcessorContext has the following structure:
public class HttpResponseProcessorContext {
private HttpServletRequest httpServletRequest;
private HttpResponseDetails httpResponseDetails;
private HttpRequestDetails httpRequestDetails;
private HttpRequestData httpRequestData;
public HttpResponseProcessorContext(HttpServletRequest httpServletRequest, HttpResponseDetails httpResponseDetails, HttpRequestDetails httpRequestDetails, HttpRequestData httpRequestData) {
this.httpServletRequest = httpServletRequest;
this.httpResponseDetails = httpResponseDetails;
this.httpRequestDetails = httpRequestDetails;
this.httpRequestData = httpRequestData;
}
}
HttpRequestData
HttpRequestData
has the following structure:
public class HttpRequestData {
private Map<String, String> pathVariablesMap;
private MultiValueMap<String, String> queryParametersMap;
private HttpHeaders httpHeaders = new HttpHeaders();
private InputStream requestBody;
public HttpHeaders getHttpHeaders() {
return httpHeaders;
}
public void setHttpHeaders(HttpHeaders httpHeaders) {
this.httpHeaders = httpHeaders;
}
public Map<String, String> getPathVariablesMap() {
return pathVariablesMap;
}
public void setPathVariablesMap(Map<String, String> pathVariablesMap) {
this.pathVariablesMap = pathVariablesMap;
}
public InputStream getRequestBody() {
return requestBody;
}
public void setRequestBody(InputStream requestBody) {
this.requestBody = requestBody;
}
public MultiValueMap<String, String> getQueryParametersMap() {
return queryParametersMap;
}
public void setQueryParametersMap(MultiValueMap<String, String> queryParametersMap) {
this.queryParametersMap = queryParametersMap;
}
}
HttpRequestDetails
HttpRequestDetails
has the following structure:
public class HttpRequestDetails {
@NotNull
private String endpointAddress;
private String method;
@JsonIgnore
private InputStream body;
private HttpHeaders headers = new HttpHeaders();
private boolean redirectEnabled = true;
//Needed for jackson deserialization
public HttpRequestDetails() {
}
public boolean isRedirectEnabled() {
return redirectEnabled;
}
public void setRedirectEnabled(final boolean redirectEnabled) {
this.redirectEnabled = redirectEnabled;
}
public String getEndpointAddress() {
return endpointAddress;
}
public void setEndpointAddress(String endpointAddress) {
this.endpointAddress = endpointAddress;
}
public HttpHeaders getHeaders() {
return headers;
}
public void setHeaders(HttpHeaders headers) {
this.headers = headers;
}
public String getMethod() {
return method;
}
public void setMethod(String method) {
this.method = method;
}
public InputStream getBody() {
return body;
}
public void setBody(InputStream body) {
this.body = body;
}
}
HttpResponseDetails
HttpResponseDetails
has the following structure:
public class HttpResponseDetails {
private int statusCode;
private HttpHeaders headers = new HttpHeaders();
@JsonIgnore
private InputStream body;
public int getStatusCode() {
return statusCode;
}
public void setStatusCode(int statusCode) {
this.statusCode = statusCode;
}
public HttpHeaders getHeaders() {
return headers;
}
public void setHeaders(HttpHeaders headers) {
this.headers = headers;
}
public InputStream getBody() {
return body;
}
public void setBody(InputStream body) {
this.body = body;
}
Java Service Use Cases