Ejemplos: Fragmentos de código de ejemplo y uso para enlaces locales de REST
El punto de entrada para el enlace local está dentro de RESTTag. Sin embargo, se puede interactuar con ellos al reproducir los mismos parámetros y al llamar a los mismos métodos necesarios para una petición de enlace local. Los siguientes ejemplos se pueden utilizar para realizar llamadas de enlace local o remota con un servletRequest.
- Ejemplo de obtención de todos los valores de configuración necesarios para el enlace local:El siguiente fragmento de código se debe inicializar para recuperar la configuración de enlace local:
/** * 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;
- Ejemplo de cómo determinar si la vía de acceso de contexto se utiliza para el enlace local:La comprobación del prefijo de vía de acceso es necesaria para determinar si la vía de acceso de contexto de una solicitud se utiliza para el enlace local:
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; } } }
- Ejemplo de la llamada a RESTHandler.executeLocal con la solicitud, la respuesta y la correlación
params
. La correlaciónparams
es opcional y se utiliza una correlación vacía si no se pasa ninguna. El resultValue se recupera del atributo de petición: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); } }
- Ejemplo de llamada a RESTHandler.executeLocal con la solicitud y la respuesta. No se proporciona ninguna correlación
params
, por lo que se crea una vacía: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()); }