Examples: Sample code snippets and usage for REST local binding
The entry point for local binding is within the
RESTTag. However, it can be further interacted with by reproducing
the same parameters and calling the same required methods for a local
binding request. The following samples can be used to make both local
or remote binding calls with a servletRequest.
- Example of fetching all required configuration values for local
binding:The following snippet must be initialized for retrieving the local binding configuration:
/** * The configuration file given the current component ID */ private static final ComponentConfiguration CONFIG = ComponentConfigurationRegistry.instance().getComponentConfiguration(COMPONENT_ID); /** * This String will be set with the configuration file's property for Local Binding's enablement. */ private static final boolean LOCAL_BINDING_ENABLED = Boolean.valueOf(CONFIG.getValueByConfigGroupingNameAndPropertyName(REST_LOCALBINDING_CONFIGURATION_GROUP_NAME, LOCALBINDING_ENABLED_PROPERTY_NAME)); /** * This Array will be set with the configuration file's property for Local Binding's url path prefix. */ public static final String[] LOCAL_BINDING_PROPERTY_NAME_PATH = CONFIG.getValueByConfigGroupingNameAndPropertyName(REST_LOCALBINDING_CONFIGURATION_GROUP_NAME, RestProviderConstants.LOCALBINDING_PATH_PREFIX_PROPERTY_NAME).split(","); /** * This String will be set with the request's url path prefix. */ public static String localBindingPathPrefix = null;
- Example of determining whether context path is used for local
binding:The path prefix check is required to determine whether the context path in a request is used for local binding:
boolean pathPrefixFound = false; if (LOCAL_BINDING_ENABLED) { URL aURL = new URL(h.getUrl()); for (String paths : LOCAL_BINDING_PROPERTY_NAME_PATH) { if (aURL.getPath().startsWith(paths)) { pathPrefixFound = true; localBindingPathPrefix = paths; break; } } }
- Example of calling RESTHandler.executeLocal with request, response,
and
params
map. Theparams
map is optional and an empty map is used if none is passed. The resultValue is retrieved from the request attribute:if (LOCAL_BINDING_ENABLED && pathPrefixFound) { try { pageContext.getRequest().setAttribute(RestProviderConstants.LOCALBINDING_PATH_PREFIX_PROPERTY_NAME, localBindingPathPrefix); InternalHttpServletRequestWrapper o = h.executeLocal(pageContext.getRequest(), pageContext.getResponse(), params); if (o.getAttribute(RestProviderConstants.LOCALBINDING_RESPONSE_STATUS_CODE).equals(HTTP_CODE_200)) { resultValue = o.getAttribute(RestProviderConstants.LOCAL_BINDING_RESPONSE); if (!(resultValue instanceof JSONObject)) { resultValue = new JSONObject(o.getAttribute(RestProviderConstants.LOCAL_BINDING_RESPONSE)); } resultValue = instrumentResult(resultValue, h, metric ); pageContext.setAttribute(var, resultValue, scope); pageContext.setAttribute(headerSig, resultValue, scope); } } catch (Exception e) { if (entryExitTraceEnabled) { LOGGER.exiting(CLASSNAME, METHODNAME, e); } throw new JspException(e); } }
- Example of calling RESTHandler.executeLocal with request and response.
No
params
map is provided so an empty one is created:RESTHandler restHandler = new RESTHandler(); if (LOCAL_BINDING_ENABLED && pathPrefixFound) { pageContext.getRequest().setAttribute(RestProviderConstants.LOCALBINDING_PATH_PREFIX_PROPERTY_NAME, localBindingPathPrefix); InternalHttpServletRequestWrapper requestWrapper = restHandler.executeLocal(request, response); result = toJSONObject(requestWrapper.getAttribute(RestProviderConstants.LOCAL_BINDING_RESPONSE)); } else { HttpObj httpObj = restHandler.execute(); result = toJSONObject(httpObj.getResponseAsString()); }