
com.netease.cloud.DefaultRequest Maven / Gradle / Ivy
Show all versions of nos-sdk-java-publiccloud Show documentation
package com.netease.cloud;
import java.io.InputStream;
import java.net.URI;
import java.util.HashMap;
import java.util.Map;
import com.netease.cloud.http.HttpMethodName;
/**
* Default implementation of the {@linkplain com.netease.cloud.Request}
* interface.
*
* This class is only intended for internal use inside the client libraries.
* Callers shouldn't ever interact directly with objects of this class.
*/
public class DefaultRequest implements Request {
/** The resource path being requested */
private String resourcePath;
/** Map of the parameters being sent as part of this request */
private Map parameters = new HashMap();
/** Map of the headers included in this request */
private Map headers = new HashMap();
/** The service endpoint to which this request should be sent */
private URI endpoint;
/** The name of the service to which this request is being sent */
private String serviceName;
/**
* The original, user facing request object which this internal request
* object is representing
*/
private final WebServiceRequest originalRequest;
/** The HTTP method to use when sending this request. */
private HttpMethodName httpMethod = HttpMethodName.POST;
/** An optional stream from which to read the request payload. */
private InputStream content;
/**
* Constructs a new DefaultRequest with the specified service name and the
* original, user facing request object.
*
* @param serviceName
* The name of the service to which this request is being sent.
* @param originalRequest
* The original, user facing, request being represented by this
* internal request object.
*/
public DefaultRequest(WebServiceRequest originalRequest, String serviceName) {
this.serviceName = serviceName;
this.originalRequest = originalRequest;
}
/**
* Constructs a new DefaultRequest with the specified service name and no
* specified original, user facing request object.
*
* @param serviceName
* The name of the service to which this request is being sent.
*/
public DefaultRequest(String serviceName) {
this(null, serviceName);
}
/**
* Returns the original, user facing request object which this internal
* request object is representing.
*
* @return The original, user facing request object which this request
* object is representing.
*/
public WebServiceRequest getOriginalRequest() {
return originalRequest;
}
/**
* @see com.netease.cloud.Request#addHeader(java.lang.String,
* java.lang.String)
*/
public void addHeader(String name, String value) {
headers.put(name, value);
}
/**
* @see com.netease.cloud.Request#getHeaders()
*/
public Map getHeaders() {
return headers;
}
/**
* @see com.netease.cloud.Request#setResourcePath(java.lang.String)
*/
public void setResourcePath(String resourcePath) {
this.resourcePath = resourcePath;
}
/**
* @see com.netease.cloud.Request#getResourcePath()
*/
public String getResourcePath() {
return resourcePath;
}
/**
* @see com.netease.cloud.Request#addParameter(java.lang.String,
* java.lang.String)
*/
public void addParameter(String name, String value) {
parameters.put(name, value);
}
/**
* @see com.netease.cloud.Request#getParameters()
*/
public Map getParameters() {
return parameters;
}
/**
* @see com.netease.cloud.Request#withParameter(java.lang.String,
* java.lang.String)
*/
public Request withParameter(String name, String value) {
addParameter(name, value);
return this;
}
/**
* @see com.netease.cloud.Request#getHttpMethod()
*/
public HttpMethodName getHttpMethod() {
return httpMethod;
}
/**
* @see com.netease.cloud.Request#setHttpMethod(com.netease.cloud.http.HttpMethodName)
*/
public void setHttpMethod(HttpMethodName httpMethod) {
this.httpMethod = httpMethod;
}
/**
* @see com.netease.cloud.Request#setEndpoint(java.net.URI)
*/
public void setEndpoint(URI endpoint) {
this.endpoint = endpoint;
}
/**
* @see com.netease.cloud.Request#getEndpoint()
*/
public URI getEndpoint() {
return endpoint;
}
/**
* @see com.netease.cloud.Request#getServiceName()
*/
public String getServiceName() {
return serviceName;
}
/**
* @see com.netease.cloud.Request#getContent()
*/
public InputStream getContent() {
return content;
}
/**
* @see com.netease.cloud.Request#setContent(java.io.InputStream)
*/
public void setContent(InputStream content) {
this.content = content;
}
/**
* @see com.netease.cloud.Request#setHeaders(java.util.Map)
*/
public void setHeaders(Map headers) {
this.headers.clear();
this.headers.putAll(headers);
}
/**
* @see com.netease.cloud.Request#setParameters(java.util.Map)
*/
public void setParameters(Map parameters) {
this.parameters.clear();
this.parameters.putAll(parameters);
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append(getHttpMethod().toString() + " ");
builder.append(getEndpoint().toString() + " ");
builder.append("/" + (getResourcePath() != null ? getResourcePath() : "") + " ");
if (!getParameters().isEmpty()) {
builder.append("Parameters: (");
for (String key : getParameters().keySet()) {
String value = getParameters().get(key);
builder.append(key + ": " + value + ", ");
}
builder.append(") ");
}
if (!getHeaders().isEmpty()) {
builder.append("Headers: (");
for (String key : getHeaders().keySet()) {
String value = getHeaders().get(key);
builder.append(key + ": " + value + ", ");
}
builder.append(") ");
}
return builder.toString();
}
}