/** * 从响应里获取透传数据 * * @param context RpcInvokeContext * @param response 响应 * @param init 传入上下文为空时,是否初始化 */ public static void pickupFromResponse(RpcInvokeContext context, SofaResponse response, boolean init) { if (context == null && !init) { return; } Map<String, String> responseBaggage = response.getResponseProps(); if (CommonUtils.isNotEmpty(responseBaggage)) { String prefix = RemotingConstants.RPC_RESPONSE_BAGGAGE + "."; for (Map.Entry<String, String> entry : responseBaggage.entrySet()) { if (entry.getKey().startsWith(prefix)) { if (context == null) { context = RpcInvokeContext.getContext(); } context.putResponseBaggage(entry.getKey().substring(prefix.length()), entry.getValue()); } } } }
/** * 从响应里获取透传数据 * * @param context RpcInvokeContext * @param response 响应 * @param init 传入上下文为空时,是否初始化 */ public static void pickupFromResponse(RpcInvokeContext context, SofaResponse response, boolean init) { if (context == null && !init) { return; } Map<String, String> responseBaggage = response.getResponseProps(); if (CommonUtils.isNotEmpty(responseBaggage)) { String prefix = RemotingConstants.RPC_RESPONSE_BAGGAGE + "."; for (Map.Entry<String, String> entry : responseBaggage.entrySet()) { if (entry.getKey().startsWith(prefix)) { if (context == null) { context = RpcInvokeContext.getContext(); } context.putResponseBaggage(entry.getKey().substring(prefix.length()), entry.getValue()); } } } }
@Override public <Response extends ResponseCommand> boolean serializeHeader(Response response) throws SerializationException { if (response instanceof RpcResponseCommand) { RpcInternalContext.getContext().getStopWatch().tick(); Object responseObject = ((RpcResponseCommand) response).getResponseObject(); if (responseObject instanceof SofaResponse) { SofaResponse sofaResponse = (SofaResponse) responseObject; if (sofaResponse.isError() || sofaResponse.getAppResponse() instanceof Throwable) { sofaResponse.addResponseProp(RemotingConstants.HEAD_RESPONSE_ERROR, StringUtils.TRUE); } response.setHeader(mapSerializer.encode(sofaResponse.getResponseProps())); } return true; } return false; }
@Override public <Response extends ResponseCommand> boolean serializeHeader(Response response) throws SerializationException { if (response instanceof RpcResponseCommand) { RpcInternalContext.getContext().getStopWatch().tick(); Object responseObject = ((RpcResponseCommand) response).getResponseObject(); if (responseObject instanceof SofaResponse) { SofaResponse sofaResponse = (SofaResponse) responseObject; if (sofaResponse.isError() || sofaResponse.getAppResponse() instanceof Throwable) { sofaResponse.addResponseProp(RemotingConstants.HEAD_RESPONSE_ERROR, StringUtils.TRUE); } response.setHeader(mapSerializer.encode(sofaResponse.getResponseProps())); } return true; } return false; }
@Override public void decodeObjectByTemplate(AbstractByteBuf data, Map<String, String> context, SofaResponse template) throws SofaRpcException { try { UnsafeByteArrayInputStream inputStream = new UnsafeByteArrayInputStream(data.array()); Hessian2Input input = new Hessian2Input(inputStream); // 根据SerializeType信息决定序列化器 boolean genericSerialize = context != null && isGenericResponse( context.get(RemotingConstants.HEAD_GENERIC_TYPE)); if (genericSerialize) { input.setSerializerFactory(genericSerializerFactory); GenericObject genericObject = (GenericObject) input.readObject(); template.setErrorMsg((String) genericObject.getField("errorMsg")); template.setAppResponse(genericObject.getField("appResponse")); template.setResponseProps((Map<String, String>) genericObject.getField("responseProps")); } else { input.setSerializerFactory(serializerFactory); SofaResponse tmp = (SofaResponse) input.readObject(); // copy values to template template.setErrorMsg(tmp.getErrorMsg()); template.setAppResponse(tmp.getAppResponse()); template.setResponseProps(tmp.getResponseProps()); } input.close(); } catch (IOException e) { throw buildDeserializeError(e.getMessage(), e); } }
@Override public void decodeObjectByTemplate(AbstractByteBuf data, Map<String, String> context, SofaResponse template) throws SofaRpcException { try { UnsafeByteArrayInputStream inputStream = new UnsafeByteArrayInputStream(data.array()); Hessian2Input input = new Hessian2Input(inputStream); // 根据SerializeType信息决定序列化器 boolean genericSerialize = context != null && isGenericResponse( context.get(RemotingConstants.HEAD_GENERIC_TYPE)); if (genericSerialize) { input.setSerializerFactory(genericSerializerFactory); GenericObject genericObject = (GenericObject) input.readObject(); template.setErrorMsg((String) genericObject.getField("errorMsg")); template.setAppResponse(genericObject.getField("appResponse")); template.setResponseProps((Map<String, String>) genericObject.getField("responseProps")); } else { input.setSerializerFactory(serializerFactory); SofaResponse tmp = (SofaResponse) input.readObject(); // copy values to template template.setErrorMsg(tmp.getErrorMsg()); template.setAppResponse(tmp.getAppResponse()); template.setResponseProps(tmp.getResponseProps()); } input.close(); } catch (IOException e) { throw buildDeserializeError(e.getMessage(), e); } }
/** * 从响应里获取透传数据 * * @param context RpcInvokeContext * @param response 响应 * @param init 传入上下文为空时,是否初始化 */ public static void pickupFromResponse(RpcInvokeContext context, SofaResponse response, boolean init) { if (context == null && !init) { return; } Map<String, String> responseBaggage = response.getResponseProps(); if (CommonUtils.isNotEmpty(responseBaggage)) { String prefix = RemotingConstants.RPC_RESPONSE_BAGGAGE + "."; for (Map.Entry<String, String> entry : responseBaggage.entrySet()) { if (entry.getKey().startsWith(prefix)) { if (context == null) { context = RpcInvokeContext.getContext(); } context.putResponseBaggage(entry.getKey().substring(prefix.length()), entry.getValue()); } } } }
@Override public <Response extends ResponseCommand> boolean serializeHeader(Response response) throws SerializationException { if (response instanceof RpcResponseCommand) { RpcInternalContext.getContext().getStopWatch().tick(); Object responseObject = ((RpcResponseCommand) response).getResponseObject(); if (responseObject instanceof SofaResponse) { SofaResponse sofaResponse = (SofaResponse) responseObject; if (sofaResponse.isError() || sofaResponse.getAppResponse() instanceof Throwable) { sofaResponse.addResponseProp(RemotingConstants.HEAD_RESPONSE_ERROR, StringUtils.TRUE); } response.setHeader(mapSerializer.encode(sofaResponse.getResponseProps())); } return true; } return false; }
@Override public void decodeObjectByTemplate(AbstractByteBuf data, Map<String, String> context, SofaResponse template) throws SofaRpcException { try { UnsafeByteArrayInputStream inputStream = new UnsafeByteArrayInputStream(data.array()); Hessian2Input input = new Hessian2Input(inputStream); // 根据SerializeType信息决定序列化器 boolean genericSerialize = context != null && isGenericResponse( context.get(RemotingConstants.HEAD_GENERIC_TYPE)); if (genericSerialize) { input.setSerializerFactory(genericSerializerFactory); GenericObject genericObject = (GenericObject) input.readObject(); template.setErrorMsg((String) genericObject.getField("errorMsg")); template.setAppResponse(genericObject.getField("appResponse")); template.setResponseProps((Map<String, String>) genericObject.getField("responseProps")); } else { input.setSerializerFactory(serializerFactory); SofaResponse tmp = (SofaResponse) input.readObject(); // copy values to template template.setErrorMsg(tmp.getErrorMsg()); template.setAppResponse(tmp.getAppResponse()); template.setResponseProps(tmp.getResponseProps()); } input.close(); } catch (IOException e) { throw buildDeserializeError(e.getMessage(), e); } }