com.alipay.sofa.rpc.core.response.SofaResponse Java Examples
The following examples show how to use
com.alipay.sofa.rpc.core.response.SofaResponse.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BytebuddyInvocationHandler.java From sofa-rpc with Apache License 2.0 | 6 votes |
@RuntimeType public Object byteBuddyInvoke(@This Object proxy, @Origin Method method, @AllArguments @RuntimeType Object[] args) throws Throwable { String name = method.getName(); if ("equals".equals(name)) { Object another = args[0]; return proxy == another || (proxy.getClass().isInstance(another) && proxyInvoker.equals(BytebuddyProxy.parseInvoker(another))); } else if ("hashCode".equals(name)) { return proxyInvoker.hashCode(); } else if ("toString".equals(name)) { return proxyInvoker.toString(); } SofaRequest request = MessageBuilder.buildSofaRequest(method.getDeclaringClass(), method, method.getParameterTypes(), args); SofaResponse response = proxyInvoker.invoke(request); return response.getAppResponse(); }
Example #2
Source File: RestLookoutAdapter.java From sofa-rpc with Apache License 2.0 | 6 votes |
public static void sendRestServerSendEvent(RestServerSendEvent restServerSendEvent) { //this is special for rest if (EventBus.isEnable(ServerSendEvent.class)) { SofaRequest request = new SofaRequest(); String appName = (String) RpcRuntimeContext.get(RpcRuntimeContext.KEY_APPNAME); request.setTargetAppName(appName); request.addRequestProp(RemotingConstants.HEAD_APP_NAME, restServerSendEvent.getRequest().getHttpHeaders() .getHeaderString(RemotingConstants.HEAD_APP_NAME)); RpcInternalContext context = RpcInternalContext.getContext(); request.setTargetServiceUniqueName((String) context.getAttachment(INTERNAL_KEY_PREFIX + RestConstants.REST_SERVICE_KEY)); request.setMethodName((String) context.getAttachment(INTERNAL_KEY_PREFIX + RestConstants.REST_METHODNAME_KEY)); request.addRequestProp(RemotingConstants.HEAD_PROTOCOL, RpcConstants.PROTOCOL_TYPE_REST); request.setInvokeType(RpcConstants.INVOKER_TYPE_SYNC); SofaResponse response = new SofaResponse(); if (restServerSendEvent.getThrowable() != null) { response.setErrorMsg(restServerSendEvent.getThrowable().getMessage()); } final ServerSendEvent event = new ServerSendEvent(request, response, restServerSendEvent.getThrowable()); EventBus.post(event); } }
Example #3
Source File: ProtobufSerializer.java From sofa-rpc with Apache License 2.0 | 6 votes |
protected AbstractByteBuf encodeSofaResponse(SofaResponse sofaResponse, Map<String, String> context) throws SofaRpcException { AbstractByteBuf byteBuf; if (sofaResponse.isError()) { // 框架异常:错误则body序列化的是错误字符串 byteBuf = encode(sofaResponse.getErrorMsg(), context); } else { // 正确返回则解析序列化的protobuf返回对象 Object appResponse = sofaResponse.getAppResponse(); if (appResponse instanceof Throwable) { // 业务异常序列化的是错误字符串 byteBuf = encode(((Throwable) appResponse).getMessage(), context); } else { byteBuf = encode(appResponse, context); } } return byteBuf; }
Example #4
Source File: ConsumerTracerFilter.java From sofa-rpc with Apache License 2.0 | 6 votes |
@Override public SofaResponse invoke(FilterInvoker invoker, SofaRequest request) throws SofaRpcException { SofaTraceContext sofaTraceContext = SofaTraceContextHolder.getSofaTraceContext(); SofaTracerSpan clientSpan = sofaTraceContext.getCurrentSpan(); clientSpan.setTag(RpcSpanTags.INVOKE_TYPE, request.getInvokeType()); RpcInternalContext context = RpcInternalContext.getContext(); clientSpan.setTag(RpcSpanTags.ROUTE_RECORD, (String) context.getAttachment(RpcConstants.INTERNAL_KEY_ROUTER_RECORD)); ProviderInfo providerInfo = context.getProviderInfo(); if (providerInfo != null) { clientSpan.setTag(RpcSpanTags.REMOTE_APP, providerInfo.getStaticAttr(ProviderInfoAttrs.ATTR_APP_NAME)); clientSpan.setTag(RpcSpanTags.REMOTE_IP, providerInfo.getHost() + ":" + providerInfo.getPort()); } return invoker.invoke(request); // 因为异步的场景,所以received不写在这里 }
Example #5
Source File: CustomEchoFilter.java From sofa-rpc with Apache License 2.0 | 6 votes |
@Override public SofaResponse invoke(FilterInvoker invoker, SofaRequest request) throws SofaRpcException { LOGGER.info("echo request : {}, {}", request.getInterfaceName() + "." + request.getMethodName(), request.getMethodArgs()); SofaResponse response = invoker.invoke(request); if (response == null) { return response; } else if (response.isError()) { LOGGER.info("server rpc error: {}", response.getErrorMsg()); } else { Object ret = response.getAppResponse(); if (ret instanceof Throwable) { LOGGER.error("server biz error: {}", (Throwable) ret); } else { LOGGER.info("echo response : {}", response.getAppResponse()); } } return response; }
Example #6
Source File: HttpResponseFuture.java From sofa-rpc with Apache License 2.0 | 6 votes |
/** * Waits if necessary for at most the given time for the computation * to complete, and then retrieves its result, if available. * * @param timeout the maximum time to wait * @param unit the time unit of the timeout argument * @return the computed result * @throws CancellationException if the computation was cancelled * @throws ExecutionException if the computation threw an * exception * @throws InterruptedException if the current thread was interrupted * while waiting * @throws TimeoutException if the wait timed out */ public SofaResponse getSofaResponse(int timeout, TimeUnit unit) throws CancellationException, TimeoutException, InterruptedException, ExecutionException { long realTimeOut = unit.toMillis(timeout); long remainTime = realTimeOut - (sentTime - genTime); // 剩余时间 if (remainTime <= 0) { // 没有剩余时间不等待 if (isDone()) { // 直接看是否已经返回 return getNowResponse(); } } else { // 等待剩余时间 if (await(remainTime, TimeUnit.MILLISECONDS)) { return getNowResponse(); } } this.setDoneTime(); throw new TimeoutException(); }
Example #7
Source File: JacksonSerializer.java From sofa-rpc with Apache License 2.0 | 6 votes |
protected AbstractByteBuf encodeSofaResponse(SofaResponse sofaResponse, Map<String, String> context) throws SofaRpcException { AbstractByteBuf byteBuf; if (sofaResponse.isError()) { // rpc exception:error when body is illegal string byteBuf = encode(sofaResponse.getErrorMsg(), context); } else { //ok: when json can be deserialize correctly. Object appResponse = sofaResponse.getAppResponse(); if (appResponse instanceof Throwable) { // biz exception:error when body is illegal string byteBuf = encode(((Throwable) appResponse).getMessage(), context); } else { byteBuf = encode(appResponse, context); } } return byteBuf; }
Example #8
Source File: BaggageResolver.java From sofa-rpc with Apache License 2.0 | 6 votes |
/** * 从响应里获取透传数据 * * @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()); } } } }
Example #9
Source File: AbstractHttpClientHandler.java From sofa-rpc with Apache License 2.0 | 6 votes |
protected void decode(SofaResponse response) { AbstractByteBuf byteBuffer = response.getData(); if (byteBuffer != null) { try { Map<String, String> context = new HashMap<String, String>(4); if (response.isError()) { context.put(RemotingConstants.HEAD_RESPONSE_ERROR, response.isError() + ""); String errorMsg = StringSerializer.decode(byteBuffer.array()); response.setAppResponse(new SofaRpcException(RpcErrorType.SERVER_UNDECLARED_ERROR, errorMsg)); } else { context.put(RemotingConstants.HEAD_TARGET_SERVICE, request.getTargetServiceUniqueName()); context.put(RemotingConstants.HEAD_METHOD_NAME, request.getMethodName()); Serializer serializer = SerializerFactory.getSerializer(response.getSerializeType()); serializer.decode(byteBuffer, response, context); } } finally { byteBuffer.release(); response.setData(null); } } }
Example #10
Source File: MsgPackSerializer.java From sofa-rpc with Apache License 2.0 | 6 votes |
protected AbstractByteBuf encodeSofaResponse(SofaResponse sofaResponse, Map<String, String> context) throws SofaRpcException { AbstractByteBuf byteBuf; if (sofaResponse.isError()) { // 框架异常:错误则body序列化的是错误字符串 byteBuf = encode(sofaResponse.getErrorMsg(), context); } else { // 正确返回则解析序列化的Msgpack返回对象 Object appResponse = sofaResponse.getAppResponse(); if (appResponse instanceof Throwable) { // 业务异常序列化的是错误字符串 byteBuf = encode(((Throwable) appResponse).getMessage(), context); } else { byteBuf = encode(appResponse, context); } } return byteBuf; }
Example #11
Source File: ProtostuffSerializer.java From sofa-rpc with Apache License 2.0 | 6 votes |
protected AbstractByteBuf encodeSofaResponse(SofaResponse sofaResponse, Map<String, String> context) throws SofaRpcException { AbstractByteBuf byteBuf; if (sofaResponse.isError()) { // 框架异常:错误则body序列化的是错误字符串 byteBuf = encode(sofaResponse.getErrorMsg(), context); } else { // 正确返回则解析序列化的protobuf返回对象 Object appResponse = sofaResponse.getAppResponse(); if (appResponse instanceof Throwable) { // 业务异常序列化的是错误字符串 byteBuf = encode(((Throwable) appResponse).getMessage(), context); } else { byteBuf = encode(appResponse, context); } } return byteBuf; }
Example #12
Source File: TripleClientTransport.java From sofa-rpc with Apache License 2.0 | 6 votes |
@Override public SofaResponse syncSend(SofaRequest request, int timeout) throws SofaRpcException { SofaResponse sofaResponse = null; SofaRpcException throwable = null; try { RpcInternalContext context = RpcInternalContext.getContext(); beforeSend(context, request); RpcInvokeContext invokeContext = RpcInvokeContext.getContext(); invokeContext.put(TripleContants.SOFA_REQUEST_KEY, request); invokeContext.put(TripleContants.SOFA_CONSUMER_CONFIG_KEY, transportConfig.getConsumerConfig()); sofaResponse = tripleClientInvoker.invoke(request, timeout); return sofaResponse; } catch (Exception e) { throwable = convertToRpcException(e); throw throwable; } finally { if (EventBus.isEnable(ClientSyncReceiveEvent.class)) { EventBus.post(new ClientSyncReceiveEvent(transportConfig.getConsumerConfig(), transportConfig.getProviderInfo(), request, sofaResponse, throwable)); } } }
Example #13
Source File: ProtobufSerializer.java From sofa-rpc with Apache License 2.0 | 5 votes |
@Override public void decode(AbstractByteBuf data, Object template, Map<String, String> context) throws SofaRpcException { if (template == null) { throw buildDeserializeError("template is null!"); } else if (template instanceof SofaRequest) { decodeSofaRequest(data, (SofaRequest) template, context); } else if (template instanceof SofaResponse) { decodeSofaResponse(data, (SofaResponse) template, context); } else { throw buildDeserializeError("Only support decode from SofaRequest and SofaResponse template"); } }
Example #14
Source File: SofaHessianSerializer.java From sofa-rpc with Apache License 2.0 | 5 votes |
/** * Instantiates a new Sofa hessian serializer. */ public SofaHessianSerializer() { try { ClassUtils.forName("com.caucho.hessian.io.ShortHandle"); if (LOGGER.isInfoEnabled()) { LOGGER.info("Version of sofa-hessian is v4.x"); } } catch (Exception e) { if (LOGGER.isInfoEnabled()) { LOGGER.info("Version of sofa-hessian is v3.x"); } } boolean enableMultipleClassLoader = RpcConfigs.getBooleanValue(RpcOptions.MULTIPLE_CLASSLOADER_ENABLE); serializerFactory = getSerializerFactory(enableMultipleClassLoader, false); genericSerializerFactory = getSerializerFactory(enableMultipleClassLoader, true); if (RpcConfigs.getBooleanValue(RpcOptions.SERIALIZE_BLACKLIST_ENABLE) && SofaConfigs.getBooleanValue(SofaOptions.CONFIG_SERIALIZE_BLACKLIST, true)) { ClassNameResolver resolver = new ClassNameResolver(); resolver.addFilter(new NameBlackListFilter(BlackListFileLoader.SOFA_SERIALIZE_BLACK_LIST, 8192)); serializerFactory.setClassNameResolver(resolver); genericSerializerFactory.setClassNameResolver(resolver); } else { serializerFactory.setClassNameResolver(null); genericSerializerFactory.setClassNameResolver(null); } CustomHessianSerializerManager.addSerializer(SofaRequest.class, new SofaRequestHessianSerializer(serializerFactory, genericSerializerFactory)); CustomHessianSerializerManager.addSerializer(SofaResponse.class, new SofaResponseHessianSerializer(serializerFactory, genericSerializerFactory)); }
Example #15
Source File: SofaResponseHessianSerializer.java From sofa-rpc with Apache License 2.0 | 5 votes |
@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); } }
Example #16
Source File: JacksonSerializer.java From sofa-rpc with Apache License 2.0 | 5 votes |
private void decodeSofaResponse(AbstractByteBuf data, SofaResponse sofaResponse, Map<String, String> head) { if (head == null) { throw buildDeserializeError("head is null!"); } String targetService = head.remove(RemotingConstants.HEAD_TARGET_SERVICE); if (targetService == null) { throw buildDeserializeError("HEAD_TARGET_SERVICE is null"); } String methodName = head.remove(RemotingConstants.HEAD_METHOD_NAME); if (methodName == null) { throw buildDeserializeError("HEAD_METHOD_NAME is null"); } boolean isError = false; if (StringUtils.TRUE.equals(head.remove(RemotingConstants.HEAD_RESPONSE_ERROR))) { isError = true; } if (!head.isEmpty()) { sofaResponse.setResponseProps(head); } if (isError) { String errorMessage = (String) decode(data, String.class, head); sofaResponse.setErrorMsg(errorMessage); } else { // according interface and method name to find paramter types JavaType respType = jacksonHelper.getResClass(targetService, methodName); Object result; try { result = mapper.readValue(data.array(), respType); } catch (IOException e) { throw buildDeserializeError(e.getMessage()); } sofaResponse.setAppResponse(result); } }
Example #17
Source File: TestChainFilter6.java From sofa-rpc with Apache License 2.0 | 5 votes |
@Override public SofaResponse invoke(FilterInvoker invoker, SofaRequest request) throws SofaRpcException { request.getMethodArgs()[0] = request.getMethodArgs()[0] + "_q6"; SofaResponse response = invoker.invoke(request); if (!request.isAsync()) { response.setAppResponse(response.getAppResponse() + "_s6"); } return response; }
Example #18
Source File: MockTimeoutFilter.java From sofa-rpc with Apache License 2.0 | 5 votes |
@Override public SofaResponse invoke(FilterInvoker invoker, SofaRequest request) throws SofaRpcException { try { Thread.sleep(sleep); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } return invoker.invoke(request); }
Example #19
Source File: TestChainFilter7.java From sofa-rpc with Apache License 2.0 | 5 votes |
@Override public SofaResponse invoke(FilterInvoker invoker, SofaRequest request) throws SofaRpcException { request.getMethodArgs()[0] = request.getMethodArgs()[0] + "_q7"; SofaResponse response = invoker.invoke(request); if (!request.isAsync()) { response.setAppResponse(response.getAppResponse() + "_s7"); } return response; }
Example #20
Source File: ProviderBaggageFilter.java From sofa-rpc with Apache License 2.0 | 5 votes |
@Override public SofaResponse invoke(FilterInvoker invoker, SofaRequest request) throws SofaRpcException { SofaResponse response = null; try { BaggageResolver.pickupFromRequest(RpcInvokeContext.peekContext(), request, true); response = invoker.invoke(request); } finally { if (response != null) { BaggageResolver.carryWithResponse(RpcInvokeContext.peekContext(), response); } } return response; }
Example #21
Source File: ProtostuffSerializer.java From sofa-rpc with Apache License 2.0 | 5 votes |
@Override public void decode(AbstractByteBuf data, Object template, Map<String, String> context) throws SofaRpcException { if (template == null) { throw buildDeserializeError("template is null!"); } else if (template instanceof SofaRequest) { decodeSofaRequest(data, (SofaRequest) template, context); } else if (template instanceof SofaResponse) { decodeSofaResponse(data, (SofaResponse) template, context); } else { throw buildDeserializeError("Only support decode from SofaRequest and SofaResponse template"); } }
Example #22
Source File: MessageBuilder.java From sofa-rpc with Apache License 2.0 | 5 votes |
/** * 构建rpc错误结果 * * @param errorMsg 错误消息 * @return rpc结果 */ public static SofaResponse buildSofaErrorResponse(String errorMsg) { SofaResponse sofaResponse = new SofaResponse(); sofaResponse.setErrorMsg(errorMsg); return sofaResponse; }
Example #23
Source File: TestChainFilter4.java From sofa-rpc with Apache License 2.0 | 5 votes |
@Override public SofaResponse invoke(FilterInvoker invoker, SofaRequest request) throws SofaRpcException { request.getMethodArgs()[0] = request.getMethodArgs()[0] + "_q4"; SofaResponse response = invoker.invoke(request); if (!request.isAsync()) { response.setAppResponse(response.getAppResponse() + "_s4"); } return response; }
Example #24
Source File: SofaRpcSerializationRegister.java From sofa-rpc with Apache License 2.0 | 5 votes |
/** * we can override or rewrite the method */ protected static void innerRegisterCustomSerializer() { // 注册序列化器到bolt if (CustomSerializerManager.getCustomSerializer(SofaRequest.class.getName()) == null) { CustomSerializerManager.registerCustomSerializer(SofaRequest.class.getName(), RPC_SERIALIZATION); } if (CustomSerializerManager.getCustomSerializer(SofaResponse.class.getName()) == null) { CustomSerializerManager.registerCustomSerializer(SofaResponse.class.getName(), RPC_SERIALIZATION); } }
Example #25
Source File: BoltServerProcessor.java From sofa-rpc with Apache License 2.0 | 5 votes |
private SofaResponse doInvoke(String serviceName, Invoker invoker, SofaRequest request) throws SofaRpcException { // 开始调用,先记下当前的ClassLoader ClassLoader rpcCl = Thread.currentThread().getContextClassLoader(); try { // 切换线程的ClassLoader到 服务 自己的ClassLoader ClassLoader serviceCl = ReflectCache.getServiceClassLoader(serviceName); Thread.currentThread().setContextClassLoader(serviceCl); return invoker.invoke(request); } finally { Thread.currentThread().setContextClassLoader(rpcCl); } }
Example #26
Source File: AbstractInvokeCallback.java From sofa-rpc with Apache License 2.0 | 5 votes |
protected void pickupBaggage(SofaResponse response) { if (RpcInvokeContext.isBaggageEnable()) { RpcInvokeContext invokeCtx = null; if (context != null) { invokeCtx = (RpcInvokeContext) context.getAttachment(RpcConstants.HIDDEN_KEY_INVOKE_CONTEXT); } if (invokeCtx == null) { invokeCtx = RpcInvokeContext.getContext(); } else { RpcInvokeContext.setContext(invokeCtx); } BaggageResolver.pickupFromResponse(invokeCtx, response); } }
Example #27
Source File: TestChainFilter2.java From sofa-rpc with Apache License 2.0 | 5 votes |
@Override public SofaResponse invoke(FilterInvoker invoker, SofaRequest request) throws SofaRpcException { request.getMethodArgs()[0] = request.getMethodArgs()[0] + "_q2"; SofaResponse response = invoker.invoke(request); if (!request.isAsync()) { response.setAppResponse(response.getAppResponse() + "_s2"); } Map<String, Object> context = invoker.getConfigContext(); Assert.assertNotNull(context); invoker.getMethodParam(request.getMethodName(), "invokeType"); invoker.getStringMethodParam(request.getMethodName(), "invokeType", "sync"); invoker.getIntMethodParam(request.getMethodName(), "timeout", 3000); invoker.getBooleanMethodParam(request.getMethodName(), "cache", false); return response; }
Example #28
Source File: BoltSendableResponseCallback.java From sofa-rpc with Apache License 2.0 | 5 votes |
/** * A->B(当前)->C的场景下,将远程服务端C的RPc异常异步返回给调用者A * * @see SofaResponseCallback#onSofaException(SofaRpcException, String, RequestBase) */ @Override public void sendSofaException(SofaRpcException sofaException) { checkState(); SofaResponse response = new SofaResponse(); response.setErrorMsg(sofaException.getMessage()); sendSofaResponse(response, sofaException); }
Example #29
Source File: ConsumerExceptionFilter.java From sofa-rpc with Apache License 2.0 | 5 votes |
@Override public SofaResponse invoke(FilterInvoker invoker, SofaRequest request) throws SofaRpcException { try { // 先调用 return invoker.invoke(request); } catch (SofaRpcException e) { throw e; } catch (Throwable t) { throw new SofaRpcException(RpcErrorType.CLIENT_FILTER, t); } // TODO 是否特殊处理? }
Example #30
Source File: ComplexDataGenerator.java From sofa-hessian with Apache License 2.0 | 5 votes |
public SofaResponse generateSofaResponse() { SofaResponse response = new SofaResponse(); response.setErrorMsg("just_error"); response.setAppResponse(generatePerson_6()); return response; }