Skip to content

Commit

Permalink
Add data to request and response. (sofastack#141)
Browse files Browse the repository at this point in the history
  • Loading branch information
ujjboy authored May 27, 2018
1 parent 3d98b80 commit 9a014c7
Show file tree
Hide file tree
Showing 39 changed files with 937 additions and 335 deletions.
10 changes: 8 additions & 2 deletions bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

<properties>
<javassist.version>3.20.0-GA</javassist.version>
<netty.version>4.1.13.Final</netty.version>
<netty.version>4.1.24.Final</netty.version>
<spring.version>4.2.6.RELEASE</spring.version>
<!-- 3rd extends libs -->
<servlet.version>2.5</servlet.version>
Expand All @@ -25,7 +25,7 @@
<hessian.version>3.3.0</hessian.version>
<thrift.version>0.9.2</thrift.version>
<protobuf.version>3.1.0</protobuf.version>
<jackson.version>2.7.8</jackson.version>
<jackson.version>2.7.9</jackson.version>
<msgpack.version>0.6.11</msgpack.version>
<!--common-->
<httpcomponents.version>4.3.3</httpcomponents.version>
Expand Down Expand Up @@ -302,6 +302,12 @@
<groupId>com.alipay.sofa</groupId>
<artifactId>bolt</artifactId>
<version>${bolt.version}</version>
<exclusions>
<exclusion>
<groupId>io.netty</groupId>
<artifactId>netty</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.alipay.sofa.common</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alipay.sofa.rpc.codec.common;

import com.alipay.sofa.rpc.common.RpcConstants;

/**
* @author <a href="mailto:[email protected]">GengZhang</a>
* @since 5.4.0
*/
public final class StringSerializer {

public static byte[] encode(String s) {
return s == null ? new byte[0] : s.getBytes(RpcConstants.DEFAULT_CHARSET);
}

public static String decode(byte[] data) {
return data == null ? null : new String(data, RpcConstants.DEFAULT_CHARSET);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alipay.sofa.rpc.codec.common;

import org.junit.Assert;
import org.junit.Test;

/**
* @author <a href="mailto:[email protected]">GengZhang</a>
*/
public class StringSerializerTest {

@Test
public void encode() {
Assert.assertTrue(StringSerializer.encode("11").length == 2);
Assert.assertTrue(StringSerializer.encode("").length == 0);
Assert.assertTrue(StringSerializer.encode(null).length == 0);
}

@Test
public void decode() {
Assert.assertNull(StringSerializer.decode(null));
Assert.assertEquals("", StringSerializer.decode(new byte[0]));
Assert.assertEquals("11", StringSerializer.decode(StringSerializer.encode("11")));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import com.alipay.sofa.rpc.common.RpcConstants;
import com.alipay.sofa.rpc.core.invoke.SofaResponseCallback;
import com.alipay.sofa.rpc.transport.AbstractByteBuf;

import java.lang.reflect.Method;
import java.util.HashMap;
Expand Down Expand Up @@ -66,7 +67,7 @@ public void addRequestProp(String key, Object value) {
return;
}
if (requestProps == null) {
requestProps = new HashMap<String, Object>();
requestProps = new HashMap<String, Object>(16);
}
requestProps.put(key, value);
}
Expand All @@ -91,11 +92,11 @@ public void removeRequestProp(String key) {
* @param map the map
*/
public void addRequestProps(Map<String, Object> map) {
if (map == null || map.size() == 0) {
if (map == null || map.isEmpty()) {
return;
}
if (requestProps == null) {
requestProps = new HashMap<String, Object>();
requestProps = new HashMap<String, Object>(16);
}
requestProps.putAll(map);
}
Expand Down Expand Up @@ -143,6 +144,11 @@ public void setTargetAppName(String targetAppName) {
*/
private transient byte serializeType;

/**
* 请求数据
*/
private transient AbstractByteBuf data;

/**
* 调用类型(客户端使用)
*/
Expand Down Expand Up @@ -274,6 +280,26 @@ public SofaRequest setTimeout(Integer timeout) {
return this;
}

/**
* Gets data.
*
* @return the data
*/
public AbstractByteBuf getData() {
return data;
}

/**
* Sets data.
*
* @param data the data
* @return the data
*/
public SofaRequest setData(AbstractByteBuf data) {
this.data = data;
return this;
}

/**
* 是否异步请求
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
*/
package com.alipay.sofa.rpc.core.response;

import com.alipay.sofa.rpc.transport.AbstractByteBuf;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
Expand All @@ -27,27 +29,39 @@
*/
public final class SofaResponse implements Serializable {

static private final long serialVersionUID = -4364536436151723421L;
private static final long serialVersionUID = -4364536436151723421L;

/**
* 框架异常
*/
private boolean isError = false;
private boolean isError = false;

/**
* 框架异常的消息
*/
private String errorMsg;
private String errorMsg;

/**
* 业务返回或者业务异常
*/
private Object appResponse;
private Object appResponse;

/**
* extensional properties
*/
private Map<String, String> responseProps;
private Map<String, String> responseProps;

//====================== 下面是非传递属性 ===============

/**
* 序列化类型
*/
private transient byte serializeType;

/**
* 数据
*/
private transient AbstractByteBuf data;

/**
* Gets app response.
Expand Down Expand Up @@ -116,7 +130,7 @@ public Object getResponseProp(String key) {
*/
public void addResponseProp(String key, String value) {
if (responseProps == null) {
responseProps = new HashMap<String, String>();
responseProps = new HashMap<String, String>(16);
}
if (key != null && value != null) {
responseProps.put(key, value);
Expand Down Expand Up @@ -152,6 +166,46 @@ public void setResponseProps(Map<String, String> responseProps) {
this.responseProps = responseProps;
}

/**
* Gets serialize type.
*
* @return the serialize type
*/
public byte getSerializeType() {
return serializeType;
}

/**
* Sets serialize type.
*
* @param serializeType the serialize type
* @return the serialize type
*/
public SofaResponse setSerializeType(byte serializeType) {
this.serializeType = serializeType;
return this;
}

/**
* Gets data.
*
* @return the data
*/
public AbstractByteBuf getData() {
return data;
}

/**
* Sets data.
*
* @param data the data
* @return the data
*/
public SofaResponse setData(AbstractByteBuf data) {
this.data = data;
return this;
}

@Override
public String toString() {
StringBuilder sb = new StringBuilder(128);
Expand Down
Loading

0 comments on commit 9a014c7

Please sign in to comment.