/*
* Copyright 2009-2016 Weibo, Inc.
*
* Licensed 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.weibo.api.motan.rpc;
import java.io.Serializable;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import com.weibo.api.motan.exception.MotanServiceException;
import com.weibo.api.motan.protocol.rpc.RpcProtocolVersion;
/**
*
* Response received via rpc.
*
* @author fishermen
* @version V1.0 created at: 2013-5-16
*/
public class DefaultResponse implements Response, Serializable {
private static final long serialVersionUID = 4281186647291615871L;
private Object value;
private Exception exception;
private long requestId;
private long processTime;
private int timeout;
private Map<String, String> attachments;// rpc协议版本兼容时可以回传一些额外的信息
private byte rpcProtocolVersion = RpcProtocolVersion.VERSION_1.getVersion();
public DefaultResponse() {}
public DefaultResponse(long requestId) {
this.requestId = requestId;
}
public DefaultResponse(Response response) {
this.value = response.getValue();
this.exception = response.getException();
this.requestId = response.getRequestId();
this.processTime = response.getProcessTime();
this.timeout = response.getTimeout();
}
public DefaultResponse(Object value) {
this.value = value;
}
public DefaultResponse(Object value, long requestId) {
this.value = value;
}
public Object getValue() {
if (exception != null) {
throw (exception instanceof RuntimeException) ? (RuntimeException) exception : new MotanServiceException(
exception.getMessage(), exception);
}
return value;
}
public void setValue(Object value) {
this.value = value;
}
public Exception getException() {
return exception;
}
public void setException(Exception exception) {
this.exception = exception;
}
public long getRequestId() {
return requestId;
}
public void setRequestId(long requestId) {
this.requestId = requestId;
}
@Override
public long getProcessTime() {
return processTime;
}
@Override
public void setProcessTime(long time) {
this.processTime = time;
}
public int getTimeout() {
return this.timeout;
}
@SuppressWarnings("unchecked")
public Map<String, String> getAttachments() {
return attachments != null ? attachments : Collections.EMPTY_MAP;
}
@Override
public void setAttachment(String key, String value) {
if (this.attachments == null) {
this.attachments = new HashMap<String, String>();
}
this.attachments.put(key, value);
}
public void setAttachments(Map<String, String> attachments) {
this.attachments = attachments;
}
@Override
public byte getRpcProtocolVersion() {
return rpcProtocolVersion;
}
public void setRpcProtocolVersion(byte rpcProtocolVersion) {
this.rpcProtocolVersion = rpcProtocolVersion;
}
}