Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.aliyun.openservices.log.Client Maven / Gradle / Ivy
/*
* Copyright (C) Alibaba Cloud Computing All rights reserved.
*/
package com.aliyun.openservices.log;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.aliyun.openservices.log.common.*;
import com.aliyun.openservices.log.common.Consts.CompressType;
import com.aliyun.openservices.log.common.Consts.CursorMode;
import com.aliyun.openservices.log.common.auth.*;
import com.aliyun.openservices.log.exception.LogException;
import com.aliyun.openservices.log.http.client.*;
import com.aliyun.openservices.log.http.comm.*;
import com.aliyun.openservices.log.http.signer.SignVersion;
import com.aliyun.openservices.log.http.signer.SlsSigner;
import com.aliyun.openservices.log.http.signer.SlsSignerBase;
import com.aliyun.openservices.log.http.utils.CodingUtils;
import com.aliyun.openservices.log.internal.ErrorCodes;
import com.aliyun.openservices.log.request.*;
import com.aliyun.openservices.log.response.*;
import com.aliyun.openservices.log.util.*;
import org.apache.commons.codec.binary.Base64;
import org.apache.http.conn.HttpClientConnectionManager;
import java.io.*;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.*;
import static com.aliyun.openservices.log.common.Consts.CONST_LOGSTORE_REPLICATION;
/**
* Client class is the main class in the sdk, it implements the interfaces
* defined in LogService. It can be used to send request to the log service
* server to put/get data.
*
* It is highly recommended to use {@link ClientBuilder} to build {@link Client} instance.
*/
public class Client implements LogService {
private static final String DEFAULT_USER_AGENT = VersionInfoUtils.getDefaultUserAgent();
private String httpType;
private String hostName;
private CredentialsProvider credentialsProvider;
private String sourceIp;
private ServiceClient serviceClient;
private String realIpForConsole;
private Boolean useSSLForConsole;
private String userAgent = DEFAULT_USER_AGENT;
private boolean mUUIDTag = false;
private boolean useDirectMode = false;
/**
* Real backend server's IP address. If not null, skip resolving DNS
*/
private String realServerIP = null;
private String resourceOwnerAccount = null;
private SlsSigner signer;
public String getUserAgent() {
return userAgent;
}
public void setUserAgent(String userAgent) {
this.userAgent = userAgent;
}
public String getRealIpForConsole() {
return realIpForConsole;
}
public void setRealIpForConsole(String realIpForConsole) {
this.realIpForConsole = realIpForConsole;
}
public boolean isUseSSLForConsole() {
return useSSLForConsole;
}
public void setUseSSLForConsole(boolean useSSLForConsole) {
this.useSSLForConsole = useSSLForConsole;
}
public void ClearConsoleResources() {
realIpForConsole = null;
useSSLForConsole = null;
}
public void EnableUUIDTag() { mUUIDTag = true; }
public void DisableUUIDTag() { mUUIDTag = false; }
@Deprecated
public void EnableDirectMode() {
setUseDirectMode(true);
}
@Deprecated
public void DisableDirectMode() {
setUseDirectMode(false);
}
public void setUseDirectMode(boolean useDirectMode) {
this.useDirectMode = useDirectMode;
}
public boolean isUseDirectMode() {
return useDirectMode;
}
public String getResourceOwnerAccount() {
return resourceOwnerAccount;
}
public void setResourceOwnerAccount(String resourceOwnerAccount) {
this.resourceOwnerAccount = resourceOwnerAccount;
}
public HttpClientConnectionManager getConnectionManager() {
return serviceClient.getConnectionManager();
}
/**
* Construct the sls client with accessId, accessKey and server address, all
* other parameters will be set to default value
*
* @throws NullPointerException
* if the input parameter is null
* @throws IllegalArgumentException
* if the input parameter is empty
*
* @param endpoint
* the log service server address
* @param accessId
* aliyun accessId
* @param accessKey
* aliyun accessKey
*/
public Client(String endpoint, String accessId, String accessKey) {
this(endpoint, new DefaultCredentials(accessId, accessKey), null);
}
public Client(String endpoint, String accessId, String accessKey, ClientConfiguration configuration) {
this(endpoint, accessId, accessKey, null, configuration);
}
public Client(String endpoint, String roleName) {
this(endpoint, new ECSRoleCredentialsProvider(roleName), null);
}
/**
* Construct the sls client with accessId, accessKey , server address and
* client ip address, all other parameters will be set to default value
*
*
* @throws NullPointerException
* if the input parameter is null
* @throws IllegalArgumentException
* if the input parameter is empty
*
* @param endpoint
* the log service server address
* @param accessId
* aliyun accessId
* @param accessKey
* aliyun accessKey
* @param sourceIp
* client ip address
*/
public Client(String endpoint, String accessId, String accessKey, String sourceIp) {
this(endpoint, new DefaultCredentials(accessId, accessKey), sourceIp);
}
public Client(String endpoint, Credentials credentials, String sourceIp) {
ClientConfiguration clientConfig = getDefaultClientConfiguration();
this.serviceClient = buildServiceClient(clientConfig);
configure(endpoint, credentials, sourceIp);
}
public Client(String endpoint, CredentialsProvider credentialsProvider) {
this(endpoint, credentialsProvider, "");
}
static ClientConfiguration getDefaultClientConfiguration() {
ClientConfiguration clientConfig = new ClientConfiguration();
clientConfig.setMaxConnections(Consts.HTTP_CONNECT_MAX_COUNT);
clientConfig.setConnectionTimeout(Consts.HTTP_CONNECT_TIME_OUT);
clientConfig.setSocketTimeout(Consts.HTTP_SEND_TIME_OUT);
return clientConfig;
}
static ServiceClient buildServiceClient(ClientConfiguration clientConfig) {
if (clientConfig.isRequestTimeoutEnabled()) {
return new TimeoutServiceClient(clientConfig);
}
return new DefaultServiceClient(clientConfig);
}
/**
* @param endpoint required not null, the log service server address
* @param credentialsProvider required not null, interface which provide credentials
* @param sourceIp nullable, client ip address
*/
public Client(String endpoint, CredentialsProvider credentialsProvider, String sourceIp) {
this(endpoint, credentialsProvider, new DefaultServiceClient(getDefaultClientConfiguration()), sourceIp);
}
/**
* @param endpoint required not null, the log service server address
* @param credentialsProvider required not null, interface which provide credentials
* @param serviceClient required not null, customized service client
* @param sourceIp nullable, client ip address
*/
public Client(String endpoint, CredentialsProvider credentialsProvider, ServiceClient serviceClient, String sourceIp) {
this.serviceClient = serviceClient;
configure(endpoint, credentialsProvider, sourceIp);
}
/**
* @deprecated Use Client(String endpoint, String accessId, String accessKey, String sourceIp,
* ClientConfiguration config) instead.
*/
@Deprecated
public Client(String endpoint, String accessId, String accessKey, String sourceIp,
int connectMaxCount, int connectTimeout, int sendTimeout) {
ClientConfiguration clientConfig = new ClientConfiguration();
clientConfig.setMaxConnections(connectMaxCount);
clientConfig.setConnectionTimeout(connectTimeout);
clientConfig.setSocketTimeout(sendTimeout);
this.serviceClient = buildServiceClient(clientConfig);
configure(endpoint, new DefaultCredentials(accessId, accessKey), sourceIp);
}
public Client(String endpoint, String accessId, String accessKey, ServiceClient serviceClient) {
this.serviceClient = serviceClient;
configure(endpoint, new DefaultCredentials(accessId, accessKey), null);
}
public synchronized void setEndpoint(String endpoint) {
Args.notNullOrEmpty(endpoint, "endpoint");
endpoint = endpoint.trim();
if (endpoint.startsWith("http://")) {
this.hostName = endpoint.substring(7);
this.httpType = "http://";
} else if (endpoint.startsWith("https://")) {
this.hostName = endpoint.substring(8);
this.httpType = "https://";
} else {
this.hostName = endpoint;
this.httpType = "http://";
}
hostName = Utils.normalizeHostName(hostName);
if (hostName == null) {
throw new IllegalArgumentException("Invalid endpoint: " + endpoint);
}
if (NetworkUtils.isIPAddr(this.hostName)) {
throw new IllegalArgumentException("The ip address is not supported");
}
if (getClientConfiguration().getRegion() == null) {
setSignV4IfInAcdr(endpoint);
}
}
private void configure(String endpoint, Credentials credentials, String sourceIp) {
configure(endpoint, new StaticCredentialsProvider(credentials), sourceIp);
}
private void configure(String endpoint, CredentialsProvider credentialsProvider, String sourceIp) {
setEndpoint(endpoint);
this.sourceIp = sourceIp;
if (sourceIp == null || sourceIp.isEmpty()) {
this.sourceIp = NetworkUtils.getLocalMachineIP();
}
this.credentialsProvider = credentialsProvider;
updateSigner(credentialsProvider);
}
/**
* @param endpoint required not null, the log service server address
* @param credentialsProvider required not null, interface which provide credentials
* @param config required not null, client configuration
* @param sourceIp nullable, client ip address
*/
public Client(String endpoint, CredentialsProvider credentialsProvider,
ClientConfiguration config,
String sourceIp) {
Args.notNull(config, "Config");
this.serviceClient = buildServiceClient(config);
configure(endpoint, credentialsProvider, sourceIp);
}
/**
* @param endpoint required not null, the log service server address
* @param accessId required not null, access key id
* @param accessKey required not null, access key secret
* @param config required not null, client configuration
* @param sourceIp nullable, client ip address
*/
@Deprecated
public Client(String endpoint, String accessId, String accessKey, String sourceIp,
ClientConfiguration config) {
this(endpoint, new StaticCredentialsProvider(new DefaultCredentials(accessId, accessKey)), config, sourceIp);
}
/**
* set credentialProvider and update the signer.
*
* @param credentialsProvider
*/
public void setCredentialsProvider(CredentialsProvider credentialsProvider) {
this.credentialsProvider = credentialsProvider;
this.updateSigner(credentialsProvider);
}
private void updateSigner(CredentialsProvider credentialsProvider) {
ClientConfiguration clientConfiguration = serviceClient.getClientConfiguration();
this.signer = SlsSignerBase.createRequestSigner(clientConfiguration, credentialsProvider);
}
private void ensureStaticCredentialProvider() {
if (!(this.credentialsProvider instanceof StaticCredentialsProvider)) {
throw new RuntimeException("can only set or get AccessId/AccessKey/SecurityToken on StaticCredentialProvider");
}
}
/**
* If client's credentialsProvider is not of type StaticCredentialsProvider, a RuntimeException will be thrown.
*/
public String getAccessId() {
ensureStaticCredentialProvider();
return credentialsProvider.getCredentials().getAccessKeyId();
}
/**
* If client's credentialsProvider is not of type StaticCredentialsProvider, a RuntimeException will be thrown.
*/
public void setAccessId(String accessId) {
ensureStaticCredentialProvider();
StaticCredentialsProvider scp = (StaticCredentialsProvider) this.credentialsProvider;
scp.setAccessKeyId(accessId);
}
/**
* If client's credentialsProvider is not of type StaticCredentialsProvider, a RuntimeException will be thrown.
*/
public String getAccessKey() {
ensureStaticCredentialProvider();
return credentialsProvider.getCredentials().getAccessKeySecret();
}
/**
* If client's credentialsProvider is not of type StaticCredentialsProvider, a RuntimeException will be thrown.
*/
public void setAccessKey(String accessKey) {
ensureStaticCredentialProvider();
StaticCredentialsProvider scp = (StaticCredentialsProvider) this.credentialsProvider;
scp.setAccessKeySecret(accessKey);
}
/**
* If client's credentialsProvider is not of type StaticCredentialsProvider, a RuntimeException will be thrown.
*/
public String getSecurityToken() {
ensureStaticCredentialProvider();
return credentialsProvider.getCredentials().getSecurityToken();
}
/**
* If client's credentialsProvider is not of type StaticCredentialsProvider, a RuntimeException will be thrown.
*/
public void setSecurityToken(String securityToken) {
ensureStaticCredentialProvider();
StaticCredentialsProvider scp = (StaticCredentialsProvider) this.credentialsProvider;
scp.setSecurityToken(securityToken);
}
public void shutdown() {
serviceClient.shutdown();
}
public ClientConfiguration getClientConfiguration() {
return serviceClient.getClientConfiguration();
}
URI GetHostURI(String project) {
String endPointUrl = this.httpType + this.hostName;
if (project != null && !project.isEmpty()) {
if (!Utils.validateProject(project)) {
throw new IllegalArgumentException("Invalid project: " + project);
}
endPointUrl = this.httpType + project + "." + this.hostName;
}
try {
return new URI(endPointUrl);
} catch (URISyntaxException e) {
throw new IllegalArgumentException("Invalid endpoint: " + endPointUrl, e);
}
}
private URI GetHostURIByIp(String ip_addr) throws LogException {
String endPointUrl = this.httpType + ip_addr;
try {
return new URI(endPointUrl);
} catch (URISyntaxException e) {
throw new LogException(ErrorCodes.ENDPOINT_INVALID,
"Failed to get real server ip when direct mode in enabled", "");
}
}
private static byte[] encodeToUtf8(String source) throws LogException {
try {
return source.getBytes(Consts.UTF_8_ENCODING);
} catch (UnsupportedEncodingException e) {
throw new LogException(ErrorCodes.ENCODING_EXCEPTION, e.getMessage(), "");
}
}
private static String encodeResponseBodyToUtf8String(ResponseMessage response, String requestId) throws LogException {
byte[] body = response.GetRawBody();
if (body == null) {
throw new LogException(ErrorCodes.BAD_RESPONSE, "The response body is null", null, requestId);
}
try {
return new String(body, Consts.UTF_8_ENCODING);
} catch (UnsupportedEncodingException e) {
throw new LogException(ErrorCodes.BAD_RESPONSE,
"The response is not valid utf-8 string: ", e, requestId);
}
}
public GetLogtailProfileResponse ExtractLogtailProfile(Map resHeaders, JSONObject object) throws LogException {
try {
int count = object.getIntValue("count");
int total = object.getIntValue("total");
JSONArray array = object.getJSONArray("profile");
List logtailProfiles = new ArrayList();
if (array != null) {
for (int i = 0; i < array.size(); i++) {
JSONObject profileObj = array.getJSONObject(i);
if (profileObj == null) {
continue;
}
LogtailProfile logtailProfile = new LogtailProfile();
logtailProfile.FromJsonObject(profileObj);
logtailProfiles.add(logtailProfile);
}
}
return new GetLogtailProfileResponse(resHeaders, count, total, logtailProfiles);
} catch (LogException e) {
throw new LogException(e.getErrorCode(), e.getMessage(),
e.getCause(), GetRequestId(resHeaders));
}
}
public TagResourcesResponse tagResources(String tagResourcesStr) throws LogException {
CodingUtils.assertParameterNotNull(tagResourcesStr, "tagResourcesStr");
Map headParameter = GetCommonHeadPara("");
byte[] body = encodeToUtf8(tagResourcesStr);
String resourceUri = "/tag";
Map urlParameter = new HashMap();
ResponseMessage response = SendData("", HttpMethod.POST, resourceUri, urlParameter, headParameter, body);
Map resHeaders = response.getHeaders();
return new TagResourcesResponse(resHeaders);
}
public TagResourcesResponse tagResources(TagResourcesRequest request) throws LogException {
Args.notNull(request, "request");
return tagResources(JsonUtils.serialize(request));
}
public UntagResourcesResponse untagResources(String untagResourcesStr) throws LogException {
CodingUtils.assertParameterNotNull(untagResourcesStr, "tagResourcesStr");
Map headParameter = GetCommonHeadPara("");
byte[] body = encodeToUtf8(untagResourcesStr);
String resourceUri = "/untag";
Map urlParameter = new HashMap();
ResponseMessage response = SendData("", HttpMethod.POST, resourceUri, urlParameter, headParameter, body);
Map resHeaders = response.getHeaders();
return new UntagResourcesResponse(resHeaders);
}
public UntagResourcesResponse untagResources(UntagResourcesRequest request) throws LogException {
Args.notNull(request, "request");
return untagResources(JsonUtils.serialize(request));
}
public TagResourcesResponse tagResourcesSystemTags(TagResourcesSystemTagsRequest request) throws LogException {
Args.notNull(request, "request");
Map headParameter = GetCommonHeadPara("");
String tagResourcesStr = JsonUtils.serialize(request);
byte[] body = encodeToUtf8(tagResourcesStr);
String resourceUri = "/systemtag";
Map urlParameter = new HashMap();
ResponseMessage response = SendData("", HttpMethod.POST, resourceUri, urlParameter, headParameter, body);
Map resHeaders = response.getHeaders();
return new TagResourcesResponse(resHeaders);
}
public UntagResourcesResponse untagResourcesSystemTags(UntagResourcesSystemTagsRequest request) throws LogException {
Args.notNull(request, "request");
String untagResourcesStr = JsonUtils.serialize(request);
Map headParameter = GetCommonHeadPara("");
byte[] body = encodeToUtf8(untagResourcesStr);
String resourceUri = "/systemuntag";
Map urlParameter = new HashMap();
ResponseMessage response = SendData("", HttpMethod.POST, resourceUri, urlParameter, headParameter, body);
Map resHeaders = response.getHeaders();
return new UntagResourcesResponse(resHeaders);
}
public ListTagResourcesResponse listSystemTagResources(ListSystemTagResourcesRequest request) throws LogException {
CodingUtils.assertParameterNotNull(request, "request");
Map urlParameter = request.GetAllParams();
Map headParameter = GetCommonHeadPara("");
String resourceUri = "/systemtags";
ResponseMessage response = SendData("", HttpMethod.GET, resourceUri, urlParameter, headParameter);
Map resHeaders = response.getHeaders();
String requestId = GetRequestId(response.getHeaders());
JSONObject object = parseResponseBody(response, requestId);
List tagResources = ExtractTagResources(object, requestId);
String nextToken = object.getString("nextToken");
return new ListTagResourcesResponse(resHeaders, nextToken, tagResources);
}
protected List ExtractTagResources(JSONObject object, String requestId)
throws LogException {
List tagResources = new ArrayList();
if (object == null) {
return tagResources;
}
try {
JSONArray array = object.getJSONArray("tagResources");
if (array == null) {
return tagResources;
}
for (int index = 0; index < array.size(); index++) {
JSONObject item = array.getJSONObject(index);
if(item != null) {
tagResources.add(TagResource.FromJsonObject(item));
}
}
} catch (JSONException e) {
throw new LogException(ErrorCodes.BAD_RESPONSE, "The response is not valid config json array string : " + object.toString(), e, requestId);
}
return tagResources;
}
public ListTagResourcesResponse listTagResources(ListTagResourcesRequest request) throws LogException {
CodingUtils.assertParameterNotNull(request, "request");
Map urlParameter = request.GetAllParams();
Map headParameter = GetCommonHeadPara("");
String resourceUri = "/tags";
ResponseMessage response = SendData("", HttpMethod.GET, resourceUri, urlParameter, headParameter);
Map resHeaders = response.getHeaders();
String requestId = GetRequestId(response.getHeaders());
JSONObject object = parseResponseBody(response, requestId);
List tagResources = ExtractTagResources(object, requestId);
String nextToken = object.getString("nextToken");
return new ListTagResourcesResponse(resHeaders, nextToken, tagResources);
}
public GetLogtailProfileResponse GetLogtailProfile(String project, String logstore, String source,
int line, int offset) throws LogException {
CodingUtils.assertStringNotNullOrEmpty(project, "project");
CodingUtils.assertStringNotNullOrEmpty(logstore, "logstore");
GetLogtailProfileRequest request = new GetLogtailProfileRequest(project, logstore, source, line, offset);
return GetLogtailProfile(request);
}
public GetLogtailProfileResponse GetLogtailProfile(GetLogtailProfileRequest request) throws LogException {
CodingUtils.assertParameterNotNull(request, "request");
Map urlParameter = request.GetAllParams();
String project = request.GetProject();
Map headParameter = GetCommonHeadPara(project);
CodingUtils.validateLogstore(request.getLogStore());
String resourceUri = "/logstores/" + request.getLogStore() + Consts.CONST_GETLOGTAILPROFILE_URI;
ResponseMessage response = SendData(project, HttpMethod.GET,
resourceUri, urlParameter, headParameter);
Map resHeaders = response.getHeaders();
String requestId = GetRequestId(resHeaders);
JSONObject object = parseResponseBody(response, requestId);
return ExtractLogtailProfile(resHeaders, object);
}
public GetHistogramsResponse GetHistograms(String project, String logStore,
int from, int to, String topic, String query) throws LogException {
CodingUtils.assertStringNotNullOrEmpty(project, "project");
CodingUtils.assertStringNotNullOrEmpty(logStore, "logStore");
CodingUtils.assertParameterNotNull(topic, "topic");
CodingUtils.assertParameterNotNull(query, "query");
GetHistogramsRequest request = new GetHistogramsRequest(project,
logStore, topic, query, from, to);
return GetHistograms(request);
}
public GetHistogramsResponse GetHistograms(GetHistogramsRequest request)
throws LogException {
CodingUtils.assertParameterNotNull(request, "request");
Map urlParameter = request.GetAllParams();
String project = request.GetProject();
Map headParameter = GetCommonHeadPara(project);
CodingUtils.validateLogstore(request.GetLogStore());
String resourceUri = "/logstores/" + request.GetLogStore() + "/index";
ResponseMessage response = SendData(project, HttpMethod.GET,
resourceUri, urlParameter, headParameter);
Map resHeaders = response.getHeaders();
String requestId = GetRequestId(resHeaders);
JSONArray object = ParseResponseMessageToArray(response, requestId);
GetHistogramsResponse histogramResponse = new GetHistogramsResponse(resHeaders);
histogramResponse.fromJSON(object);
return histogramResponse;
}
public PutLogsResponse PutLogs(String project, String logStore, byte[] logGroupBytes, String compressType, String shardHash) throws LogException {
CodingUtils.assertStringNotNullOrEmpty(project, "project");
CodingUtils.assertStringNotNullOrEmpty(logStore, "logStore");
CodingUtils.assertParameterNotNull(logGroupBytes, "logGroupBytes");
PutLogsRequest request = new PutLogsRequest(project, logStore, null,
null, logGroupBytes, shardHash);
request.SetCompressType(CompressType.fromString(compressType));
return PutLogs(request);
}
@Override
public PutLogsResponse PutLogs(String project, String logStore,
String topic, List logItems, String source,
String shardHash) throws LogException {
CodingUtils.assertStringNotNullOrEmpty(project, "project");
CodingUtils.assertStringNotNullOrEmpty(logStore, "logStore");
CodingUtils.assertParameterNotNull(topic, "topic");
CodingUtils.assertParameterNotNull(logItems, "logGroup");
PutLogsRequest request = new PutLogsRequest(project, logStore, topic,
source, logItems, shardHash);
request.SetCompressType(CompressType.LZ4);
return PutLogs(request);
}
@Override
public PutLogsResponse PutLogs(String project, String logStore,
String topic, List logItems, String source)
throws LogException {
CodingUtils.assertStringNotNullOrEmpty(project, "project");
CodingUtils.assertStringNotNullOrEmpty(logStore, "logStore");
CodingUtils.assertParameterNotNull(topic, "topic");
CodingUtils.assertParameterNotNull(logItems, "logGroup");
PutLogsRequest request = new PutLogsRequest(project, logStore, topic,
source, logItems, null);
request.SetCompressType(CompressType.LZ4);
return PutLogs(request);
}
@Override
public BatchPutLogsResponse batchPutLogs(BatchPutLogsRequest request)
throws LogException {
String project = request.GetProject();
CodingUtils.assertStringNotNullOrEmpty(project, "project");
CodingUtils.assertStringNotNullOrEmpty(request.getLogStore(), "logStore");
final CompressType compressType = request.getCompressType();
CodingUtils.assertParameterNotNull(compressType, "compressType");
if (compressType != CompressType.ZSTD && compressType != CompressType.LZ4) {
throw new LogException(ErrorCodes.INVALID_PARAMETER, "Unsupported compress type:" + compressType, "");
}
List logGroups = request.getLogGroups();
if (logGroups == null || logGroups.isEmpty()) {
throw new LogException(ErrorCodes.INVALID_PARAMETER, "LogGroups is empty", "");
}
byte[] logBytes = request.serializeToPb();
Map headParameter = GetCommonHeadPara(project);
headParameter.put(Consts.CONST_X_LOG_MODE, Consts.CONST_BATCH_GROUP);
headParameter.put(Consts.CONST_CONTENT_TYPE, Consts.CONST_PROTO_BUF);
headParameter.put(Consts.CONST_X_SLS_BODYRAWSIZE, String.valueOf(logBytes.length));
headParameter.put(Consts.CONST_X_SLS_COMPRESSTYPE, compressType.toString());
checkBodyRawSize(logBytes.length);
String resourceUri = "/logstores/" + request.getLogStore();
String shardKey = request.getHashKey();
Map urlParameter = request.GetAllParams();
if (shardKey == null || shardKey.isEmpty()) {
resourceUri += "/shards/lb";
} else {
resourceUri += "/shards/route";
urlParameter.put("key", shardKey);
}
ResponseMessage response = sendLogBytes(project, logBytes, resourceUri, urlParameter, headParameter);
if (response != null) {
return new BatchPutLogsResponse(response.getHeaders());
}
// never happen
return null;
}
public PutLogsResponse PutLogs(PutLogsRequest request) throws LogException {
CodingUtils.assertParameterNotNull(request, "request");
String project = request.GetProject();
CodingUtils.assertStringNotNullOrEmpty(project, "project");
String logStore = request.GetLogStore();
CodingUtils.assertStringNotNullOrEmpty(logStore, "logStore");
String shardKey = request.getHashKey();
CompressType compressType = request.getCompressType();
CodingUtils.assertParameterNotNull(compressType, "compressType");
byte[] logBytes = request.GetLogGroupBytes();
if (logBytes == null) {
List logItems = request.GetLogItems();
String topic = request.GetTopic();
CodingUtils.assertParameterNotNull(topic, "topic");
String source = request.GetSource();
if (!Consts.CONST_SLS_JSON.equals(request.getContentType())) {
Logs.LogGroup.Builder logs = Logs.LogGroup.newBuilder();
logs.setTopic(topic);
if (source == null || source.isEmpty()) {
logs.setSource(this.sourceIp);
} else {
logs.setSource(source);
}
ArrayList tags = request.GetTags();
if (tags != null && tags.size() > 0) {
for (TagContent tag : tags) {
Logs.LogTag.Builder tagBuilder = logs.addLogTagsBuilder();
tagBuilder.setKey(tag.getKey());
tagBuilder.setValue(tag.getValue());
}
}
if (this.mUUIDTag) {
Logs.LogTag.Builder tagBuilder = logs.addLogTagsBuilder();
tagBuilder.setKey("__pack_unique_id__");
tagBuilder.setValue(UUID.randomUUID().toString() + "-" + Math.random());
}
for (LogItem item : logItems) {
Logs.Log.Builder log = logs.addLogsBuilder();
log.setTime(item.mLogTime);
if (item.mLogTimeNsPart != 0)
log.setTimeNs(item.mLogTimeNsPart);
for (LogContent content : item.mContents) {
CodingUtils.assertStringNotNullOrEmpty(content.mKey, "key");
Logs.Log.Content.Builder contentBuilder = log
.addContentsBuilder();
contentBuilder.setKey(content.mKey);
if (content.mValue == null) {
contentBuilder.setValue("");
} else {
contentBuilder.setValue(content.mValue);
}
}
}
logBytes = logs.build().toByteArray();
} else {
JSONObject jsonObj = new JSONObject();
jsonObj.put("__topic__", topic);
if (source == null || source.isEmpty()) {
jsonObj.put("__source__", this.sourceIp);
} else {
jsonObj.put("__source__", source);
}
JSONArray logsArray = new JSONArray();
for (LogItem item : logItems) {
JSONObject jsonObjInner = new JSONObject();
jsonObjInner.put(Consts.CONST_RESULT_TIME, item.mLogTime);
if (item.mLogTimeNsPart != 0)
jsonObjInner.put(Consts.CONST_RESULT_TIME_NS_PART, item.mLogTimeNsPart);
for (LogContent content : item.mContents) {
jsonObjInner.put(content.mKey, content.mValue);
}
logsArray.add(jsonObjInner);
}
jsonObj.put("__logs__", logsArray);
JSONObject tagObj = new JSONObject();
ArrayList tags = request.GetTags();
if (tags != null && tags.size() > 0) {
for (TagContent tag : tags) {
tagObj.put(tag.getKey(), tag.getValue());
}
}
if (this.mUUIDTag) {
tagObj.put("__pack_unique_id__", UUID.randomUUID().toString() + "-" + Math.random());
}
if (tagObj.size() > 0) {
jsonObj.put("__tags__", tagObj);
}
logBytes = encodeToUtf8(jsonObj.toString());
}
}
checkBodyRawSize(logBytes.length);
Map headParameter = GetCommonHeadPara(project);
headParameter.put(Consts.CONST_CONTENT_TYPE, request.getContentType());
headParameter.put(Consts.CONST_X_SLS_BODYRAWSIZE, String.valueOf(logBytes.length));
if (request.getCompressType() != CompressType.NONE) {
headParameter.put(Consts.CONST_X_SLS_COMPRESSTYPE, request.getCompressType().toString());
}
logBytes = Utils.compressLogBytes(logBytes, request.getCompressType());
Map urlParameter = request.GetAllParams();
String resourceUri = "/logstores/" + logStore;
if (shardKey == null || shardKey.length() == 0) {
resourceUri += "/shards/lb";
} else {
resourceUri += "/shards/route";
urlParameter.put("key", shardKey);
if (request.getHashRouteKeySeqId() != null) {
urlParameter.put("seqid", String.valueOf(request.getHashRouteKeySeqId()));
}
}
ResponseMessage response = sendLogBytes(project, logBytes, resourceUri, urlParameter, headParameter);
if (response != null) {
return new PutLogsResponse(response.getHeaders());
}
// never happen
return null;
}
private ResponseMessage sendLogBytes(String project, byte[] logBytes, String resourceUri, Map urlParameter, Map headParameter) throws LogException {
for (int i = 0; i < 2; i++) {
String server_ip = this.realServerIP;
ClientConnectionStatus connection_status = null;
if (this.useDirectMode) {
connection_status = GetGlobalConnectionStatus();
server_ip = connection_status.GetIpAddress();
}
try {
ResponseMessage response = SendData(project, HttpMethod.POST, resourceUri, urlParameter, headParameter,
logBytes, null, server_ip);
if (connection_status != null) {
connection_status.AddSendDataSize(logBytes.length);
connection_status.UpdateLastUsedTime(System.nanoTime());
}
return response;
} catch (LogException e) {
String requestId = e.getRequestId();
if (i == 1 || (requestId != null && !requestId.isEmpty()) || getClientConfiguration().getRetryDisabled())
{
throw e;
}
if (connection_status != null)
{
connection_status.DisableConnection();
}
}
}
return null;
}
private void checkBodyRawSize(int bodySize) throws LogException {
if (bodySize > Consts.CONST_MAX_PUT_SIZE) {
throw new LogException("InvalidLogSize",
"logItems' size exceeds maximum limitation : "
+ Consts.CONST_MAX_PUT_SIZE
+ " bytes",
"");
} else if (bodySize > Consts.CONST_MAX_POST_BODY_SIZE) {
throw new LogException("PostBodyTooLarge",
"body size " + bodySize + " must little than " + Consts.CONST_MAX_POST_BODY_SIZE, "");
}
}
private ClientConnectionStatus GetGlobalConnectionStatus() throws LogException {
ClientConnectionContainer connection_container = ClientConnectionHelper.getInstance()
.GetConnectionContainer(this.hostName, credentialsProvider);
ClientConnectionStatus connection_status = connection_container.GetGlobalConnection();
if (connection_status == null || !connection_status.IsValidConnection()) {
connection_container.UpdateGlobalConnection();
connection_status = connection_container.GetGlobalConnection();
if (connection_status == null || connection_status.GetIpAddress() == null
|| connection_status.GetIpAddress().isEmpty()) {
throw new LogException(ErrorCodes.ENDPOINT_INVALID, "Failed to get real server ip when direct mode is enabled",
"");
}
}
return connection_status;
}
private ClientConnectionStatus GetShardConnectionStatus(String project, String logstore, int shard_id)
throws LogException {
ClientConnectionContainer connection_container = ClientConnectionHelper.getInstance()
.GetConnectionContainer(this.hostName, credentialsProvider);
ClientConnectionStatus connection_status = connection_container.GetShardConnection(project, logstore, shard_id);
if (connection_status != null && connection_status.IsValidConnection()) {
return connection_status;
}
return GetGlobalConnectionStatus();
}
public GetLogsResponse GetLogs(String project, String logStore, int from,
int to, String topic, String query) throws LogException {
CodingUtils.assertStringNotNullOrEmpty(project, "project");
CodingUtils.assertStringNotNullOrEmpty(logStore, "logStore");
CodingUtils.assertParameterNotNull(topic, "topic");
CodingUtils.assertParameterNotNull(query, "query");
GetLogsRequest request = new GetLogsRequest(project, logStore, from, to, topic, query);
return GetLogs(request);
}
public GetLogsResponse GetLogs(String project, String logStore, int from,
int to, String topic, String query, long line, long offset,
boolean reverse) throws LogException
{
return GetLogs(project,logStore,from,to,topic,query,line,offset,reverse,false);
}
public GetLogsResponse GetLogs(String project, String logStore, int from,
int to, String topic, String query, long line, long offset,
boolean reverse, boolean powerSql) throws LogException {
CodingUtils.assertStringNotNullOrEmpty(project, "project");
CodingUtils.assertStringNotNullOrEmpty(logStore, "logStore");
CodingUtils.assertParameterNotNull(topic, "topic");
CodingUtils.assertParameterNotNull(query, "query");
CodingUtils.validateOffset(offset);
GetLogsRequest request = new GetLogsRequest(project, logStore, from,
to, topic, query, offset, line, reverse,powerSql);
return GetLogs(request);
}
public GetLogsResponse GetLogs(String project, String logStore, int from,
int to, String topic, String query, long line, long offset,
boolean reverse,
boolean powerSql,
boolean forward) throws LogException {
CodingUtils.assertStringNotNullOrEmpty(project, "project");
CodingUtils.assertStringNotNullOrEmpty(logStore, "logStore");
CodingUtils.assertParameterNotNull(topic, "topic");
CodingUtils.assertParameterNotNull(query, "query");
CodingUtils.validateOffset(offset);
GetLogsRequest request = new GetLogsRequest(project, logStore, from,
to, topic, query, offset, line, reverse,powerSql, forward);
return GetLogs(request);
}
public GetLogsResponse GetLogs(String project, String logStore, int from,
int to, String topic, String query, long line, long offset,
boolean reverse, int shard) throws LogException {
CodingUtils.assertStringNotNullOrEmpty(project, "project");
CodingUtils.assertStringNotNullOrEmpty(logStore, "logStore");
CodingUtils.assertParameterNotNull(topic, "topic");
CodingUtils.assertParameterNotNull(query, "query");
CodingUtils.validateOffset(offset);
GetLogsRequest request = new GetLogsRequest(project, logStore, from,
to, topic, query, offset, line, reverse, shard);
return GetLogs(request);
}
public GetLogsResponse GetLogs(String project, String logStore, int from,
int to, String topic, String query, long line, long offset,
boolean reverse, boolean forward, String session) throws LogException {
CodingUtils.assertStringNotNullOrEmpty(project, "project");
CodingUtils.assertStringNotNullOrEmpty(logStore, "logStore");
CodingUtils.assertParameterNotNull(topic, "topic");
CodingUtils.assertParameterNotNull(query, "query");
CodingUtils.validateOffset(offset);
GetLogsRequest request = new GetLogsRequest(project, logStore, from,
to, topic, query, offset, line, reverse, forward, session);
return GetLogs(request);
}
public GetLogsResponse executeLogstoreSql(String project, String logStore, int from,
int to, String sql, boolean powerSql) throws LogException {
CodingUtils.assertStringNotNullOrEmpty(project, "project");
CodingUtils.assertStringNotNullOrEmpty(logStore, "logStore");
CodingUtils.assertParameterNotNull(sql, "sql");
GetLogsRequest request = new GetLogsRequest(project, logStore, from,
to, "", sql);
request.SetPowerSql(powerSql);
return GetLogs(request);
}
/**
* getContextLogs uses @packID and @packMeta to specify a log as start log and queries logs around it.
*
* @param packID package ID of the start log, such as 895CEA449A52FE-1 ({hex prefix}-{hex sequence number}).
* @param packMeta package meta of the start log, such as 0|MTU1OTI4NTExMjg3NTQ2MjQ3MQ==|2|1.
* @param backLines the number of logs to request backward, at most 100.
* @param forwardLines the number of logs to request forward, at most 100.
* @return see getter in GetContextLogsResponse for more information.
* @throws LogException
*/
public GetContextLogsResponse getContextLogs(String project, String logstore,
String packID, String packMeta,
int backLines, int forwardLines) throws LogException{
CodingUtils.assertStringNotNullOrEmpty(project, "project");
CodingUtils.assertStringNotNullOrEmpty(logstore, "logStore");
GetContextLogsRequest request = new GetContextLogsRequest(project, logstore,
packID, packMeta, backLines, forwardLines);
return getContextLogs(request);
}
public GetLogsResponse GetProjectLogs(String project,String query) throws LogException {
CodingUtils.assertStringNotNullOrEmpty(project, "project");
CodingUtils.assertParameterNotNull(query, "query");
GetProjectLogsRequest request = new GetProjectLogsRequest(project, query);
return GetProjectLogs(request);
}
public GetLogsResponse GetProjectLogs(GetProjectLogsRequest request) throws LogException {
CodingUtils.assertParameterNotNull(request, "request");
Map urlParameter = request.GetAllParams();
String project = request.GetProject();
Map headParameter = GetCommonHeadPara(project);
String resourceUri = "/logs";
ResponseMessage response = SendData(project, HttpMethod.GET,
resourceUri, urlParameter, headParameter);
Map resHeaders = response.getHeaders();
GetLogsResponse getLogsResponse = new GetLogsResponse(resHeaders);
String requestId = GetRequestId(resHeaders);
JSONArray object = ParseResponseMessageToArrayWithFastJson(response, requestId);
getLogsResponse.setLogs(QueryResult.parseData(object, requestId));
return getLogsResponse;
}
public GetLogsResponse executeProjectSql(String project,
String sql, boolean powerSql) throws LogException {
CodingUtils.assertStringNotNullOrEmpty(project, "project");
CodingUtils.assertParameterNotNull(sql, "sql");
GetProjectLogsRequest request = new GetProjectLogsRequest(project, sql);
request.SetPowerSql(powerSql);
return GetProjectLogs(request);
}
private JSONArray ParseResponseMessageToArrayWithFastJson(ResponseMessage response,
String requestId) throws LogException {
String returnStr = encodeResponseBodyToUtf8String(response, requestId);
try {
return (JSONArray) JSONObject.parse(returnStr, Feature.DisableSpecialKeyDetect);
} catch (com.alibaba.fastjson.JSONException e) {
throw new LogException(ErrorCodes.BAD_RESPONSE,
"The response is not valid json string : " + returnStr, e, requestId);
}
}
public GetLogsResponse GetRawLogs(GetLogsRequest request) throws LogException {
return getLogsInternal(request, false);
}
private GetLogsResponse getLogsInternal(GetLogsRequest request, boolean deserialize) throws LogException {
CodingUtils.assertParameterNotNull(request, "request");
Map urlParameter = request.GetAllParams();
String project = request.GetProject();
String logStore = request.GetLogStore();
CodingUtils.validateLogstoreSearch(logStore);
Map headParameter = GetCommonHeadPara(project);
CompressType compressType = request.getCompressType();
if (compressType != null && compressType != CompressType.NONE) {
headParameter.put(Consts.CONST_ACCEPT_ENCODING, compressType.toString());
}
String resourceUri = "/logstores/" + logStore + "/logs";
ResponseMessage response = SendData(project, HttpMethod.POST,
resourceUri, urlParameter, headParameter, request.getRequestBody());
return GetLogsResponse.deserializeFrom(response, deserialize);
}
public GetLogsResponse GetLogs(GetLogsRequest request) throws LogException {
return getLogsInternal(request, true);
}
@Deprecated
public GetLogsResponseV2 GetLogsV2(GetLogsRequestV2 request) throws LogException {
CodingUtils.assertParameterNotNull(request, "request");
Map urlParameter = request.GetAllParams();
String project = request.GetProject();
String logstore = request.getLogstore();
Map headParameter = GetCommonHeadPara(project);
headParameter.put(Consts.CONST_ACCEPT_ENCODING, request.getAcceptEncoding());
CodingUtils.validateLogstoreSearch(logstore);
String resourceUri = "/logstores/" + logstore + "/logs";
ResponseMessage response = SendData(project, HttpMethod.POST,
resourceUri, urlParameter, headParameter, request.getRequestBody());
return GetLogsResponseV2.deserializeFrom(response);
}
public GetContextLogsResponse getContextLogs(GetContextLogsRequest request) throws LogException {
CodingUtils.assertParameterNotNull(request, "request");
Map urlParameter = request.GetAllParams();
String project = request.GetProject();
String logStore = request.getLogstore();
Map headParameter = GetCommonHeadPara(project);
CodingUtils.validateLogstore(logStore);
String resourceUri = "/logstores/" + logStore;
ResponseMessage response = SendData(project, HttpMethod.GET,
resourceUri, urlParameter, headParameter);
Map resHeaders = response.getHeaders();
String requestId = GetRequestId(resHeaders);
JSONObject object = parseResponseBody(response, requestId);
GetContextLogsResponse logsResponse = new GetContextLogsResponse(resHeaders, object);
logsResponse.setLogs(QueryResult.parseData(object.getJSONArray("logs"), requestId));
return logsResponse;
}
public ListLogStoresResponse ListLogStores(String project, int offset, int size) throws LogException {
return ListLogStores(project, offset, size, "");
}
public ListLogStoresResponse ListLogStores(String project, int offset,
int size, String logstoreName) throws LogException {
CodingUtils.assertStringNotNullOrEmpty(project, "project");
ListLogStoresRequest request = new ListLogStoresRequest(project,
offset, size, logstoreName);
return ListLogStores(request);
}
public ListLogStoresResponse ListLogStores(ListLogStoresRequest request)
throws LogException {
CodingUtils.assertParameterNotNull(request, "request");
Map urlParameter = request.GetAllParams();
String resourceUri = "/logstores";
String project = request.GetProject();
Map headParameter = GetCommonHeadPara(project);
ResponseMessage response = SendData(project, HttpMethod.GET,
resourceUri, urlParameter, headParameter);
Map resHeaders = response.getHeaders();
String requestId = GetRequestId(resHeaders);
JSONObject object = parseResponseBody(response, requestId);
ListLogStoresResponse listLogStoresResponse = new ListLogStoresResponse(resHeaders);
listLogStoresResponse.SetLogStores(ExtractJsonArray(
Consts.CONST_RESULT_LOG_STORES, object));
listLogStoresResponse.SetTotal(object.getIntValue(Consts.CONST_TOTAL));
return listLogStoresResponse;
}
public GetCursorResponse GetCursor(String project, String logStore,
int shardId, long fromTime) throws LogException {
CodingUtils.assertStringNotNullOrEmpty(project, "project");
CodingUtils.assertStringNotNullOrEmpty(logStore, "logStore");
GetCursorRequest request = new GetCursorRequest(project, logStore, shardId, fromTime);
return GetCursor(request);
}
public GetCursorResponse GetCursor(String project, String logStore,
int shardId, Date fromTime) throws LogException {
CodingUtils.assertStringNotNullOrEmpty(project, "project");
CodingUtils.assertStringNotNullOrEmpty(logStore, "logStream");
CodingUtils.assertParameterNotNull(fromTime, "fromTime");
long timeStamp = Utils.dateToTimestamp(fromTime);
GetCursorRequest request = new GetCursorRequest(project, logStore, shardId, timeStamp);
return GetCursor(request);
}
public GetCursorResponse GetCursor(String project, String logStream,
int shardId, CursorMode mode) throws LogException {
CodingUtils.assertStringNotNullOrEmpty(project, "project");
CodingUtils.assertStringNotNullOrEmpty(logStream, "logStream");
GetCursorRequest request = new GetCursorRequest(project, logStream, shardId, mode);
return GetCursor(request);
}
public GetCursorResponse GetCursor(GetCursorRequest request) throws LogException {
CodingUtils.assertParameterNotNull(request, "request");
String project = request.GetProject();
CodingUtils.assertStringNotNullOrEmpty(project, "project");
String logStore = request.GetLogStore();
String shardId = String.valueOf(request.GetShardId());
CodingUtils.validateLogstore(logStore);
Map headParameter = GetCommonHeadPara(project);
String resourceUri = "/logstores/" + logStore + "/shards/" + shardId;
headParameter.put(Consts.CONST_CONTENT_LENGTH, String.valueOf(0));
Map urlParameter = request.GetAllParams();
ResponseMessage response = new ResponseMessage();
try {
response = SendData(project, HttpMethod.GET, resourceUri,
urlParameter, headParameter);
Map resHeaders = response.getHeaders();
String requestId = GetRequestId(resHeaders);
JSONObject object = parseResponseBody(response, requestId);
return new GetCursorResponse(resHeaders, object.getString("cursor"));
} catch (JSONException e) {
throw new LogException("FailToCreateCursor", e.getMessage(), e,
GetRequestId(response.getHeaders()));
}
}
public GetCursorTimeResponse GetCursorTime(GetCursorTimeRequest request)
throws LogException {
CodingUtils.assertParameterNotNull(request, "request");
String project = request.GetProject();
CodingUtils.assertStringNotNullOrEmpty(project, "project");
String logStore = request.GetLogStore();
String shardId = String.valueOf(request.GetShardId());
CodingUtils.validateLogstore(logStore);
Map headParameter = GetCommonHeadPara(project);
String resourceUri = "/logstores/" + logStore + "/shards/" + shardId;
headParameter.put(Consts.CONST_CONTENT_LENGTH, String.valueOf(0));
Map urlParameter = request.GetAllParams();
ResponseMessage response = new ResponseMessage();
GetCursorTimeResponse getCursorTimeResponse;
try {
response = SendData(project, HttpMethod.GET, resourceUri,
urlParameter, headParameter);
Map resHeaders = response.getHeaders();
String requestId = GetRequestId(resHeaders);
JSONObject object = parseResponseBody(response, requestId);
getCursorTimeResponse = new GetCursorTimeResponse(resHeaders,
object.getIntValue("cursor_time"));
} catch (JSONException e) {
throw new LogException("FailToCreateCursor", e.getMessage(), e,
GetRequestId(response.getHeaders()));
}
return getCursorTimeResponse;
}
public GetCursorTimeResponse GetPrevCursorTime(String project, String logStore,int shardId, String cursor) throws LogException {
if(cursor.isEmpty())
throw new LogException(ErrorCodes.INVALID_CURSOR, "empty cursor string", "");
long prv = Long.parseLong(new String(Base64.decodeBase64(cursor))) - 1;
if(prv >= 0){
cursor = new String(Base64.encodeBase64(Long.toString(prv).getBytes()));
}
else{
throw new LogException(ErrorCodes.INVALID_CURSOR, "this cursor has no prev value", "");
}
GetCursorTimeRequest request = new GetCursorTimeRequest(project,
logStore, shardId, cursor);
return GetCursorTime(request);
}
public GetCursorTimeResponse GetCursorTime(String project, String logStore,
int shardId, String cursor) throws LogException {
GetCursorTimeRequest request = new GetCursorTimeRequest(project,
logStore, shardId, cursor);
return GetCursorTime(request);
}
public ListShardResponse SplitShard(String project, String logStore,
int shardId, String midHash) throws LogException {
CodingUtils.assertStringNotNullOrEmpty(project, "project");
CodingUtils.assertStringNotNullOrEmpty(logStore, "logStore");
CodingUtils.assertStringNotNullOrEmpty(logStore, "shardId");
return SplitShard(new SplitShardRequest(project, logStore, shardId, midHash));
}
public ListShardResponse SplitShard(SplitShardRequest request)
throws LogException {
CodingUtils.assertParameterNotNull(request, "request");
String project = request.GetProject();
CodingUtils.assertStringNotNullOrEmpty(project, "project");
String logStore = request.GetLogStore();
CodingUtils.validateLogstore(logStore);
int shardId = request.GetShardId();
Map headParameter = GetCommonHeadPara(project);
String resourceUri = "/logstores/" + logStore + "/shards/" + shardId;
Map urlParameter = request.GetAllParams();
ResponseMessage response = SendData(project, HttpMethod.POST, resourceUri,
urlParameter, headParameter);
Map resHeaders = response.getHeaders();
String requestId = GetRequestId(resHeaders);
JSONArray array = ParseResponseMessageToArray(response, requestId);
ArrayList shards = ExtractShards(array, requestId);
return new ListShardResponse(resHeaders, shards);
}
public ListShardResponse MergeShards(String project, String logStore,
int shardId) throws LogException {
CodingUtils.assertStringNotNullOrEmpty(project, "project");
CodingUtils.assertStringNotNullOrEmpty(logStore, "logStore");
CodingUtils.assertStringNotNullOrEmpty(logStore, "shardId");
return MergeShards(new MergeShardsRequest(project, logStore, shardId));
}
public ListShardResponse MergeShards(MergeShardsRequest request)
throws LogException {
CodingUtils.assertParameterNotNull(request, "request");
String project = request.GetProject();
CodingUtils.assertStringNotNullOrEmpty(project, "project");
String logStore = request.GetLogStore();
CodingUtils.validateLogstore(logStore);
int shardId = request.GetShardId();
Map headParameter = GetCommonHeadPara(project);
String resourceUri = "/logstores/" + logStore + "/shards/" + shardId;
Map urlParameter = request.GetAllParams();
ResponseMessage response = SendData(project, HttpMethod.POST, resourceUri,
urlParameter, headParameter);
Map resHeaders = response.getHeaders();
String requestId = GetRequestId(resHeaders);
JSONArray array = ParseResponseMessageToArray(response, requestId);
ArrayList shards = ExtractShards(array, requestId);
return new ListShardResponse(resHeaders, shards);
}
public ListShardResponse ListShard(String project, String logStore)
throws LogException {
CodingUtils.assertStringNotNullOrEmpty(project, "project");
CodingUtils.assertStringNotNullOrEmpty(logStore, "logStore");
return ListShard(new ListShardRequest(project, logStore));
}
public String GetServerIpAddress(String project) {
Map headParameter = GetCommonHeadPara(project);
String resourceUri = "/direct_mode_ip";
Map urlParameter = new HashMap();
Map out_header = new HashMap();
try {
SendData(project, HttpMethod.GET, resourceUri, urlParameter,
headParameter, new byte[0], out_header, null);
} catch (LogException e) {
// ignore
}
return Utils.getOrEmpty(out_header, Consts.CONST_X_SLS_HOSTIP);
}
public ListShardResponse ListShard(ListShardRequest request) throws LogException {
CodingUtils.assertParameterNotNull(request, "request");
String project = request.GetProject();
CodingUtils.assertStringNotNullOrEmpty(project, "project");
String logStore = request.GetLogStore();
CodingUtils.validateLogstore(logStore);
Map headParameter = GetCommonHeadPara(project);
String resourceUri = "/logstores/" + logStore + "/shards";
Map urlParameter = request.GetAllParams();
ResponseMessage response = SendData(project, HttpMethod.GET, resourceUri, urlParameter,
headParameter);
Map resHeaders = response.getHeaders();
String requestId = GetRequestId(resHeaders);
JSONArray array = ParseResponseMessageToArray(response, requestId);
ArrayList shards = ExtractShards(array, requestId);
return new ListShardResponse(resHeaders, shards);
}
protected String GetRequestId(Map headers) {
return Utils.getOrEmpty(headers, Consts.CONST_X_SLS_REQUESTID);
}
@Deprecated
@Override
public BatchGetLogResponse BatchGetLog(String project, String logStore,
int shardId, int count, String cursor) throws LogException {
CodingUtils.assertStringNotNullOrEmpty(project, "project");
CodingUtils.assertStringNotNullOrEmpty(logStore, "logStore");
return BatchGetLog(new BatchGetLogRequest(project, logStore, shardId, count, cursor));
}
@Deprecated
@Override
public BatchGetLogResponse BatchGetLog(String project, String logStore,
int shardId, int count, String cursor, String end_cursor)
throws LogException {
CodingUtils.assertStringNotNullOrEmpty(project, "project");
CodingUtils.assertStringNotNullOrEmpty(logStore, "logStore");
return BatchGetLog(new BatchGetLogRequest(project, logStore, shardId, count, cursor, end_cursor));
}
@Deprecated
@Override
public BatchGetLogResponse BatchGetLog(BatchGetLogRequest request) throws LogException {
CodingUtils.assertParameterNotNull(request, "request");
String project = request.GetProject();
CodingUtils.assertStringNotNullOrEmpty(project, "project");
String logStore = request.GetLogStore();
CodingUtils.validateLogstore(logStore);
Map headParameter = GetCommonHeadPara(project);
String resourceUri = "/logstores/" + logStore + "/shards/" + request.GetShardId();
CompressType compressType = request.getCompressType();
if (compressType == null || compressType == CompressType.NONE) {
compressType = CompressType.LZ4;
}
headParameter.put(Consts.CONST_ACCEPT_ENCODING, compressType.toString());
headParameter.put(Consts.CONST_HTTP_ACCEPT, Consts.CONST_PROTO_BUF);
Map urlParameter = request.GetAllParams();
ResponseMessage response;
BatchGetLogResponse batchGetLogResponse;
for (int i = 0; i < 2; i++) {
String server_ip = this.realServerIP;
ClientConnectionStatus connection_status = null;
if (this.useDirectMode) {
connection_status = GetShardConnectionStatus(project, logStore, request.GetShardId());
server_ip = connection_status.GetIpAddress();
}
try {
response = SendData(project, HttpMethod.GET, resourceUri, urlParameter, headParameter, new byte[0], null,
server_ip);
Map resHeaders = response.getHeaders();
byte[] rawData = response.GetRawBody();
batchGetLogResponse = new BatchGetLogResponse(resHeaders, rawData);
if (connection_status != null) {
connection_status.UpdateLastUsedTime(System.nanoTime());
connection_status.AddPullDataSize(batchGetLogResponse.GetRawSize());
}
return batchGetLogResponse;
} catch (LogException e) {
if (i == 1 || e.getRequestId() != null && !e.getRequestId().isEmpty()) {
throw e;
}
if (connection_status != null) {
connection_status.DisableConnection();
}
}
}
return null; // never happen
}
@Override
public PullLogsResponse pullLogs(PullLogsRequest request) throws LogException {
CodingUtils.assertParameterNotNull(request, "request");
String project = request.GetProject();
String logStore = request.getLogStore();
CodingUtils.validateLogstore(logStore);
Map headers = GetCommonHeadPara(project);
String resourceUri = "/logstores/" + logStore + "/shards/" + request.getShardId();
CompressType compressType = request.getCompressType();
if (compressType == null || compressType == CompressType.NONE) {
compressType = CompressType.LZ4;
}
headers.put(Consts.CONST_ACCEPT_ENCODING, compressType.toString());
headers.put(Consts.CONST_HTTP_ACCEPT, Consts.CONST_PROTO_BUF);
Map urlParameter = request.GetAllParams();
for (int i = 0; i < 2; i++) {
String serverIp = this.realServerIP;
ClientConnectionStatus connectionStatus = null;
if (useDirectMode) {
connectionStatus = GetShardConnectionStatus(project, logStore, request.getShardId());
serverIp = connectionStatus.GetIpAddress();
}
try {
ResponseMessage response = SendData(project, HttpMethod.GET, resourceUri, urlParameter, headers, new byte[0], null, serverIp);
Map resHeaders = response.getHeaders();
PullLogsResponse plr = new PullLogsResponse(resHeaders, response.GetRawBody());
if (connectionStatus != null) {
connectionStatus.UpdateLastUsedTime(System.nanoTime());
connectionStatus.AddPullDataSize(plr.getRawSize());
}
return plr;
} catch (LogException ex) {
if (i == 1 || (ex.getRequestId() != null && !ex.getRequestId().isEmpty()) || getClientConfiguration().getRetryDisabled()) {
throw ex;
}
if (connectionStatus != null) {
connectionStatus.DisableConnection();
}
}
}
return null;
}
public CreateConfigResponse CreateConfig(String project, Config config) throws LogException {
CodingUtils.assertStringNotNullOrEmpty(project, "project");
CodingUtils.assertParameterNotNull(config, "config");
return CreateConfig(new CreateConfigRequest(project, config));
}
public CreateConfigResponse CreateConfig(CreateConfigRequest request) throws LogException {
CodingUtils.assertParameterNotNull(request, "request");
String project = request.GetProject();
CodingUtils.assertStringNotNullOrEmpty(project, "project");
Config config = request.GetConfig();
CodingUtils.assertParameterNotNull(config, "config");
Map headParameter = GetCommonHeadPara(project);
byte[] body = encodeToUtf8(config.ToRequestString());
headParameter.put(Consts.CONST_CONTENT_TYPE, Consts.CONST_SLS_JSON);
String resourceUri = "/configs";
Map urlParameter = new HashMap();
ResponseMessage response = SendData(project, HttpMethod.POST, resourceUri,
urlParameter, headParameter, body);
Map resHeaders = response.getHeaders();
return new CreateConfigResponse(resHeaders);
}
public UpdateConfigResponse UpdateConfig(String project, Config config) throws LogException {
CodingUtils.assertStringNotNullOrEmpty(project, "project");
CodingUtils.assertParameterNotNull(config, "config");
return UpdateConfig(new UpdateConfigRequest(project, config));
}
public UpdateConfigResponse UpdateConfig(UpdateConfigRequest request) throws LogException {
CodingUtils.assertParameterNotNull(request, "request");
String project = request.GetProject();
CodingUtils.assertStringNotNullOrEmpty(project, "project");
Config config = request.GetConfig();
CodingUtils.assertParameterNotNull(config, "config");
String configName = config.GetConfigName();
CodingUtils.validateConfig(configName);
Map headParameter = GetCommonHeadPara(project);
byte[] body = encodeToUtf8(config.ToRequestString());
headParameter.put(Consts.CONST_CONTENT_TYPE, Consts.CONST_SLS_JSON);
String resourceUri = "/configs/" + configName;
Map urlParameter = new HashMap();
ResponseMessage response = SendData(project, HttpMethod.PUT, resourceUri, urlParameter,
headParameter, body);
Map resHeaders = response.getHeaders();
return new UpdateConfigResponse(resHeaders);
}
protected Config ExtractConfigFromResponse(JSONObject dict, String requestId) throws LogException {
Config config = new Config();
try {
config.FromJsonString(dict.toString());
} catch (LogException e) {
throw new LogException(e.getErrorCode(), e.getMessage(),
e.getCause(), requestId);
}
return config;
}
public GetConfigResponse GetConfig(String project, String configName) throws LogException {
CodingUtils.assertStringNotNullOrEmpty(project, "project");
CodingUtils.assertStringNotNullOrEmpty(configName, "configName");
return GetConfig(new GetConfigRequest(project, configName));
}
public GetConfigResponse GetConfig(GetConfigRequest request) throws LogException {
CodingUtils.assertParameterNotNull(request, "request");
String project = request.GetProject();
CodingUtils.assertStringNotNullOrEmpty(project, "project");
String configName = request.GetConfigName();
CodingUtils.validateConfig(configName);
Map headParameter = GetCommonHeadPara(project);
String resourceUri = "/configs/" + configName;
Map urlParameter = request.GetAllParams();
ResponseMessage response = SendData(project, HttpMethod.GET, resourceUri, urlParameter, headParameter);
Map resHeaders = response.getHeaders();
String requestId = GetRequestId(resHeaders);
JSONObject object = parseResponseBody(response, requestId);
Config config = ExtractConfigFromResponse(object, requestId);
return new GetConfigResponse(resHeaders, config);
}
public DeleteConfigResponse DeleteConfig(String project, String configName)
throws LogException {
CodingUtils.assertStringNotNullOrEmpty(project, "project");
CodingUtils.assertStringNotNullOrEmpty(configName, "configName");
return DeleteConfig(new DeleteConfigRequest(project, configName));
}
public DeleteConfigResponse DeleteConfig(DeleteConfigRequest request) throws LogException {
CodingUtils.assertParameterNotNull(request, "request");
String project = request.GetProject();
CodingUtils.assertStringNotNullOrEmpty(project, "project");
String configName = request.GetConfigName();
CodingUtils.validateConfig(configName);
Map headParameter = GetCommonHeadPara(project);
String resourceUri = "/configs/" + configName;
Map urlParameter = request.GetAllParams();
ResponseMessage response = SendData(project, HttpMethod.DELETE,
resourceUri, urlParameter, headParameter);
Map resHeaders = response.getHeaders();
return new DeleteConfigResponse(resHeaders);
}
protected List ExtractConfigs(JSONObject object, String requestId)
throws LogException {
List configs = new ArrayList();
if (object == null) {
return configs;
}
JSONArray array = new JSONArray();
try {
array = object.getJSONArray("configs");
if (array == null) {
return configs;
}
for (int i = 0; i < array.size(); i++) {
String configName = array.getString(i);
configs.add(configName);
}
} catch (JSONException e) {
throw new LogException(ErrorCodes.BAD_RESPONSE,
"The response is not valid config json array string : "
+ array.toString(), e, requestId);
}
return configs;
}
public ListConfigResponse ListConfig(String project) throws LogException {
CodingUtils.assertStringNotNullOrEmpty(project, "project");
return ListConfig(new ListConfigRequest(project));
}
public ListConfigResponse ListConfig(String project, int offset, int size)
throws LogException {
CodingUtils.assertStringNotNullOrEmpty(project, "project");
return ListConfig(new ListConfigRequest(project, offset, size));
}
public ListConfigResponse ListConfig(String project, String configName,
int offset, int size) throws LogException {
CodingUtils.assertStringNotNullOrEmpty(project, "project");
CodingUtils.assertStringNotNullOrEmpty(configName, "configName");
return ListConfig(new ListConfigRequest(project, configName, offset, size));
}
public ListConfigResponse ListConfig(String project, String configName,
String logstoreName, int offset, int size) throws LogException {
CodingUtils.assertStringNotNullOrEmpty(project, "project");
CodingUtils.assertStringNotNullOrEmpty(configName, "configName");
CodingUtils.assertStringNotNullOrEmpty(logstoreName, "logstoreName");
return ListConfig(new ListConfigRequest(project, configName, logstoreName, offset, size));
}
public ListConfigResponse ListConfig(ListConfigRequest request) throws LogException {
CodingUtils.assertParameterNotNull(request, "request");
String project = request.GetProject();
CodingUtils.assertStringNotNullOrEmpty(project, "project");
Map headParameter = GetCommonHeadPara(project);
String resourceUri = "/configs";
Map urlParameter = request.GetAllParams();
ResponseMessage response = new ResponseMessage();
ListConfigResponse listConfigResponse;
JSONObject object = null;
try {
response = SendData(project, HttpMethod.GET, resourceUri,
urlParameter, headParameter);
Map resHeaders = response.getHeaders();
String requestId = GetRequestId(resHeaders);
object = parseResponseBody(response, requestId);
int total = object.getIntValue("total");
int count = object.getIntValue("count");
List configs = ExtractConfigs(object, requestId);
listConfigResponse = new ListConfigResponse(resHeaders, count,
total, configs);
} catch (JSONException e) {
throw new LogException(ErrorCodes.BAD_RESPONSE,
"The response is not valid list config json string : "
+ Utils.safeToString(object), e,
GetRequestId(response.getHeaders()));
}
return listConfigResponse;
}
public CreateMachineGroupResponse CreateMachineGroup(String project, MachineGroup group) throws LogException {
CodingUtils.assertParameterNotNull(project, "project");
CodingUtils.assertParameterNotNull(group, "group");
return CreateMachineGroup(new CreateMachineGroupRequest(project, group));
}
public CreateMachineGroupResponse CreateMachineGroup(CreateMachineGroupRequest request) throws LogException {
CodingUtils.assertParameterNotNull(request, "request");
String project = request.GetProject();
CodingUtils.assertStringNotNullOrEmpty(project, "project");
MachineGroup group = request.GetMachineGroup();
CodingUtils.assertParameterNotNull(group, "group");
Map headParameter = GetCommonHeadPara(project);
headParameter.put(Consts.CONST_CONTENT_TYPE, Consts.CONST_SLS_JSON);
byte[] body = encodeToUtf8(group.ToRequestString());
String resourceUri = "/machinegroups";
Map urlParameter = new HashMap();
ResponseMessage response = SendData(project, HttpMethod.POST,
resourceUri, urlParameter, headParameter, body);
Map resHeaders = response.getHeaders();
return new CreateMachineGroupResponse(resHeaders);
}
public UpdateMachineGroupResponse UpdateMachineGroup(String project, MachineGroup group) throws LogException {
CodingUtils.assertStringNotNullOrEmpty(project, "project");
CodingUtils.assertParameterNotNull(group, "group");
return UpdateMachineGroup(new UpdateMachineGroupRequest(project, group));
}
public UpdateMachineGroupResponse UpdateMachineGroup(UpdateMachineGroupRequest request) throws LogException {
CodingUtils.assertParameterNotNull(request, "request");
String project = request.GetProject();
CodingUtils.assertStringNotNullOrEmpty(project, "project");
MachineGroup group = request.GetMachineGroup();
CodingUtils.assertParameterNotNull(group, "group");
String groupName = group.GetGroupName();
CodingUtils.validateMachineGroup(groupName);
Map headParameter = GetCommonHeadPara(project);
byte[] body = encodeToUtf8(group.ToRequestString());
headParameter.put(Consts.CONST_CONTENT_TYPE, Consts.CONST_SLS_JSON);
String resourceUri = "/machinegroups/" + groupName;
Map urlParameter = new HashMap();
ResponseMessage response = SendData(project, HttpMethod.PUT,
resourceUri, urlParameter, headParameter, body);
Map resHeaders = response.getHeaders();
return new UpdateMachineGroupResponse(resHeaders);
}
protected MachineGroup ExtractMachineGroupFromResponse(JSONObject dict, String requestId) throws LogException {
MachineGroup group = new MachineGroup();
try {
group.FromJsonString(dict.toString());
} catch (LogException e) {
throw new LogException(e.getErrorCode(), e.getMessage(),
e.getCause(), requestId);
}
return group;
}
public GetAppliedConfigResponse GetAppliedConfig(String project, String groupName) throws LogException {
CodingUtils.assertStringNotNullOrEmpty(project, "project");
CodingUtils.assertStringNotNullOrEmpty(groupName, "groupName");
return GetAppliedConfig(new GetAppliedConfigsRequest(project, groupName));
}
public GetAppliedConfigResponse GetAppliedConfig(GetAppliedConfigsRequest request) throws LogException {
CodingUtils.assertParameterNotNull(request, "request");
String project = request.GetProject();
CodingUtils.assertStringNotNullOrEmpty(project, "project");
String groupName = request.GetGroupName();
CodingUtils.validateMachineGroup(groupName);
Map headParameter = GetCommonHeadPara(project);
String resourceUri = "/machinegroups/" + groupName + "/configs";
Map urlParameter = request.GetAllParams();
ResponseMessage response = SendData(project, HttpMethod.GET,
resourceUri, urlParameter, headParameter);
Map resHeaders = response.getHeaders();
String requestId = GetRequestId(resHeaders);
JSONObject object = parseResponseBody(response, requestId);
List group = ExtractJsonArray("configs", object);
return new GetAppliedConfigResponse(resHeaders, group);
}
public GetAppliedMachineGroupsResponse GetAppliedMachineGroups(
String project, String configName) throws LogException {
CodingUtils.assertStringNotNullOrEmpty(project, "project");
CodingUtils.assertStringNotNullOrEmpty(configName, "configName");
return GetAppliedMachineGroups(new GetAppliedMachineGroupRequest(
project, configName));
}
public GetAppliedMachineGroupsResponse GetAppliedMachineGroups(
GetAppliedMachineGroupRequest request) throws LogException {
CodingUtils.assertParameterNotNull(request, "request");
String project = request.GetProject();
CodingUtils.assertStringNotNullOrEmpty(project, "project");
String configName = request.GetConfigName();
CodingUtils.assertStringNotNullOrEmpty(configName, "groupName");
Map headParameter = GetCommonHeadPara(project);
String resourceUri = "/configs/" + configName + "/machinegroups";
Map urlParameter = request.GetAllParams();
ResponseMessage response = SendData(project, HttpMethod.GET,
resourceUri, urlParameter, headParameter);
Map resHeaders = response.getHeaders();
String requestId = GetRequestId(resHeaders);
JSONObject object = parseResponseBody(response, requestId);
List group = ExtractJsonArray("machinegroups", object);
return new GetAppliedMachineGroupsResponse(resHeaders, group);
}
public GetMachineGroupResponse GetMachineGroup(String project,
String groupName) throws LogException {
CodingUtils.assertStringNotNullOrEmpty(project, "project");
CodingUtils.assertStringNotNullOrEmpty(groupName, "groupName");
return GetMachineGroup(new GetMachineGroupRequest(project, groupName));
}
public GetMachineGroupResponse GetMachineGroup(
GetMachineGroupRequest request) throws LogException {
CodingUtils.assertParameterNotNull(request, "request");
String project = request.GetProject();
CodingUtils.assertStringNotNullOrEmpty(project, "project");
String groupName = request.GetGroupName();
CodingUtils.validateMachineGroup(groupName);
Map headParameter = GetCommonHeadPara(project);
String resourceUri = "/machinegroups/" + groupName;
Map urlParameter = request.GetAllParams();
ResponseMessage response = SendData(project, HttpMethod.GET,
resourceUri, urlParameter, headParameter);
Map resHeaders = response.getHeaders();
String requestId = GetRequestId(resHeaders);
JSONObject object = parseResponseBody(response, requestId);
MachineGroup group = ExtractMachineGroupFromResponse(object, requestId);
return new GetMachineGroupResponse(resHeaders, group);
}
@Override
public ListMachinesResponse ListMachines(String project,
String machineGroup, int offset, int size) throws LogException {
CodingUtils.assertStringNotNullOrEmpty(project, "project");
CodingUtils.validateMachineGroup(machineGroup);
Map headParameter = GetCommonHeadPara(project);
String resourceUri = "/machinegroups/" + machineGroup + "/machines";
headParameter.put(Consts.CONST_CONTENT_LENGTH, String.valueOf(0));
Map urlParameter = new HashMap();
urlParameter.put("offset", String.valueOf(offset));
urlParameter.put("size", String.valueOf(size));
ResponseMessage response = SendData(project, HttpMethod.GET,
resourceUri, urlParameter, headParameter);
Map resHeaders = response.getHeaders();
String requestId = GetRequestId(resHeaders);
JSONObject object = parseResponseBody(response, requestId);
return ExtractMachinesFromResponse(resHeaders, object);
}
private ListMachinesResponse ExtractMachinesFromResponse(
Map resHeaders, JSONObject dict)
throws LogException {
try {
int count = dict.getIntValue("count");
int total = dict.getIntValue("total");
JSONArray array = dict.getJSONArray("machines");
List machines = new ArrayList();
if (array != null) {
for (int i = 0; i < array.size(); i++) {
JSONObject machine_obj = array.getJSONObject(i);
if (machine_obj == null) {
continue;
}
Machine machine = new Machine();
machine.FromJsonObject(machine_obj);
machines.add(machine);
}
}
return new ListMachinesResponse(resHeaders, count, total, machines);
} catch (LogException e) {
throw new LogException(e.GetErrorCode(), e.GetErrorMessage(),
e.getCause(), GetRequestId(resHeaders));
}
}
public ApproveMachineGroupResponse ApproveMachineGroup(String project,
String groupName) throws LogException {
CodingUtils.assertStringNotNullOrEmpty(project, "project");
CodingUtils.assertStringNotNullOrEmpty(groupName, "groupName");
return ApproveMachineGroup(new ApproveMachineGroupRequest(project, groupName));
}
public ApproveMachineGroupResponse ApproveMachineGroup(
ApproveMachineGroupRequest request) throws LogException {
CodingUtils.assertParameterNotNull(request, "request");
String project = request.GetProject();
CodingUtils.assertStringNotNullOrEmpty(project, "project");
String groupName = request.GetGroupName();
CodingUtils.validateMachineGroup(groupName);
Map headParameter = GetCommonHeadPara(project);
String resourceUri = "/machinegroups/" + groupName + "/approve";
Map urlParameter = request.GetAllParams();
ResponseMessage response = SendData(project, HttpMethod.PUT,
resourceUri, urlParameter, headParameter);
Map resHeaders = response.getHeaders();
return new ApproveMachineGroupResponse(resHeaders);
}
public DeleteMachineGroupResponse DeleteMachineGroup(String project,
String groupName) throws LogException {
CodingUtils.assertStringNotNullOrEmpty(project, "project");
CodingUtils.assertStringNotNullOrEmpty(groupName, "groupName");
return DeleteMachineGroup(new DeleteMachineGroupRequest(project, groupName));
}
public DeleteMachineGroupResponse DeleteMachineGroup(
DeleteMachineGroupRequest request) throws LogException {
CodingUtils.assertParameterNotNull(request, "request");
String project = request.GetProject();
CodingUtils.assertStringNotNullOrEmpty(project, "project");
String groupName = request.GetGroupName();
CodingUtils.validateMachineGroup(groupName);
Map headParameter = GetCommonHeadPara(project);
String resourceUri = "/machinegroups/" + groupName;
Map urlParameter = request.GetAllParams();
ResponseMessage response = SendData(project, HttpMethod.DELETE,
resourceUri, urlParameter, headParameter);
Map resHeaders = response.getHeaders();
return new DeleteMachineGroupResponse(resHeaders);
}
protected List ExtractMachineGroups(JSONObject object,
String requestId) throws LogException {
try {
return JsonUtils.readOptionalStrings(object, "machinegroups");
} catch (JSONException e) {
throw new LogException(ErrorCodes.BAD_RESPONSE,
"The response is not valid machine group json array string : " + object, e, requestId);
}
}
public ListMachineGroupResponse ListMachineGroup(String project) throws LogException {
CodingUtils.assertStringNotNullOrEmpty(project, "project");
return ListMachineGroup(new ListMachineGroupRequest(project));
}
public ListMachineGroupResponse ListMachineGroup(String project, int offset, int size) throws LogException {
CodingUtils.assertStringNotNullOrEmpty(project, "project");
return ListMachineGroup(new ListMachineGroupRequest(project, offset, size));
}
public ListMachineGroupResponse ListMachineGroup(String project,
String groupName, int offset, int size) throws LogException {
CodingUtils.assertStringNotNullOrEmpty(project, "project");
CodingUtils.assertStringNotNullOrEmpty(groupName, "groupName");
return ListMachineGroup(new ListMachineGroupRequest(project, groupName, offset, size));
}
public ListMachineGroupResponse ListMachineGroup(
ListMachineGroupRequest request) throws LogException {
CodingUtils.assertParameterNotNull(request, "request");
String project = request.GetProject();
CodingUtils.assertStringNotNullOrEmpty(project, "project");
Map headParameter = GetCommonHeadPara(project);
String resourceUri = "/machinegroups";
Map urlParameter = request.GetAllParams();
ResponseMessage response = new ResponseMessage();
ListMachineGroupResponse listMachineGroupResponse = null;
JSONObject object = null;
try {
response = SendData(project, HttpMethod.GET, resourceUri,
urlParameter, headParameter);
Map resHeaders = response.getHeaders();
String requestId = GetRequestId(resHeaders);
object = parseResponseBody(response, requestId);
int total = object.getIntValue("total");
int count = object.getIntValue("count");
List groups = ExtractMachineGroups(object, requestId);
listMachineGroupResponse = new ListMachineGroupResponse(resHeaders,
count, total, groups);
} catch (JSONException e) {
throw new LogException(ErrorCodes.BAD_RESPONSE,
"The response is not valid config json string : "
+ Utils.safeToString(object), e,
GetRequestId(response.getHeaders()));
}
return listMachineGroupResponse;
}
public ApplyConfigToMachineGroupResponse ApplyConfigToMachineGroup(
String project, String groupName, String configName)
throws LogException {
CodingUtils.assertStringNotNullOrEmpty(project, "project");
CodingUtils.assertStringNotNullOrEmpty(groupName, "groupName");
CodingUtils.assertStringNotNullOrEmpty(configName, "configName");
return ApplyConfigToMachineGroup(new ApplyConfigToMachineGroupRequest(
project, groupName, configName));
}
public ApplyConfigToMachineGroupResponse ApplyConfigToMachineGroup(
ApplyConfigToMachineGroupRequest request) throws LogException {
CodingUtils.assertParameterNotNull(request, "request");
String project = request.GetProject();
CodingUtils.assertStringNotNullOrEmpty(project, "project");
String groupName = request.GetGroupName();
CodingUtils.validateMachineGroup(groupName);
String configName = request.GetConfigName();
CodingUtils.validateConfig(configName);
Map headParameter = GetCommonHeadPara(project);
String resourceUri = "/machinegroups/" + groupName + "/configs/" + configName;
Map urlParameter = request.GetAllParams();
ResponseMessage response = SendData(project, HttpMethod.PUT,
resourceUri, urlParameter, headParameter);
Map resHeaders = response.getHeaders();
return new ApplyConfigToMachineGroupResponse(resHeaders);
}
public RemoveConfigFromMachineGroupResponse RemoveConfigFromMachineGroup(
String project, String groupName, String configName)
throws LogException {
CodingUtils.assertStringNotNullOrEmpty(project, "project");
CodingUtils.assertStringNotNullOrEmpty(groupName, "groupName");
CodingUtils.assertStringNotNullOrEmpty(configName, "configName");
return RemoveConfigFromMachineGroup(new RemoveConfigFromMachineGroupRequest(
project, groupName, configName));
}
public RemoveConfigFromMachineGroupResponse RemoveConfigFromMachineGroup(
RemoveConfigFromMachineGroupRequest request) throws LogException {
CodingUtils.assertParameterNotNull(request, "request");
String project = request.GetProject();
CodingUtils.assertStringNotNullOrEmpty(project, "project");
String groupName = request.GetGroupName();
CodingUtils.validateMachineGroup(groupName);
String configName = request.GetConfigName();
CodingUtils.validateConfig(configName);
Map headParameter = GetCommonHeadPara(project);
String resourceUri = "/machinegroups/" + groupName + "/configs/" + configName;
Map urlParameter = request.GetAllParams();
ResponseMessage response = SendData(project, HttpMethod.DELETE,
resourceUri, urlParameter, headParameter);
Map resHeaders = response.getHeaders();
return new RemoveConfigFromMachineGroupResponse(resHeaders);
}
private List ExtractJsonArray(String nodeKey, JSONObject object) {
try {
return JsonUtils.readOptionalStrings(object, nodeKey);
} catch (JSONException e) {
return new ArrayList();
}
}
void ErrorCheck(JSONObject object, String requestId, int httpCode, String response)
throws LogException {
if (object.containsKey(Consts.CONST_ERROR_CODE)) {
try {
String errorCode = object.getString(Consts.CONST_ERROR_CODE);
String errorMessage = object.getString(Consts.CONST_ERROR_MESSAGE);
throw new LogException(httpCode, errorCode, errorMessage, requestId, response);
} catch (JSONException e) {
throw new LogException(httpCode, "InvalidErrorResponse",
"Error response is not a valid error json : \n"
+ object.toString(), requestId);
}
} else {
throw new LogException(httpCode, "InvalidErrorResponse",
"Error response is not a valid error json : \n"
+ object.toString(), requestId);
}
}
private void ExtractResponseBody(ResponseMessage response)
throws LogException {
InputStream in = response.getContent();
if (in == null) {
return;
}
ByteArrayOutputStream bytestream = new ByteArrayOutputStream();
String requestId = GetRequestId(response.getHeaders());
int ch;
try {
byte[] cache = new byte[1024];
while ((ch = in.read(cache, 0, 1024)) != -1) {
bytestream.write(cache, 0, ch);
}
} catch (IOException e) {
throw new LogException(ErrorCodes.BAD_RESPONSE,
"Io exception happened when parse the response data : ", e,
requestId);
}
response.SetBody(bytestream.toByteArray());
}
private JSONObject parseResponseBody(ResponseMessage response, String requestId) throws LogException {
String body = encodeResponseBodyToUtf8String(response, requestId);
try {
return JSONObject.parseObject(body, Feature.DisableSpecialKeyDetect);
} catch (JSONException ex) {
throw new LogException(ErrorCodes.BAD_RESPONSE,
"The response is not valid json string : " + body, ex, requestId);
}
}
JSONArray ParseResponseMessageToArray(ResponseMessage response,
String requestId) throws LogException {
String returnStr = encodeResponseBodyToUtf8String(response, requestId);
try {
return JSONArray.parseArray(returnStr);
} catch (JSONException e) {
throw new LogException(ErrorCodes.BAD_RESPONSE,
"The response is not valid json string : " + returnStr, e,
requestId);
}
}
private Map GetCommonHeadPara(String project) {
HashMap headParameter = new HashMap();
headParameter.put(Consts.CONST_USER_AGENT, userAgent);
headParameter.put(Consts.CONST_X_SLS_BODYRAWSIZE, "0");
headParameter.put(Consts.CONST_CONTENT_TYPE, Consts.CONST_PROTO_BUF);
if (!project.isEmpty()) {
headParameter.put(Consts.CONST_HOST, project + "." + this.hostName);
} else {
headParameter.put(Consts.CONST_HOST, this.hostName);
}
headParameter.put(Consts.CONST_X_SLS_APIVERSION, Consts.DEFAULT_API_VESION);
if (realIpForConsole != null && !realIpForConsole.isEmpty()) {
headParameter.put(Consts.CONST_X_SLS_IP, realIpForConsole);
}
if (useSSLForConsole != null) {
headParameter.put(Consts.CONST_X_SLS_SSL, useSSLForConsole ? "true" : "false");
}
ClientConfiguration clientConfiguration = serviceClient.getClientConfiguration();
if (clientConfiguration != null) {
headParameter.putAll(clientConfiguration.getDefaultHeaders());
}
return headParameter;
}
private ResponseMessage SendDataWithResolveResponse(String project, HttpMethod method,
String resourceUri, Map urlParams,
Map headParams) throws LogException {
return SendDataWithResolveResponse(project, method, resourceUri, urlParams, headParams, new byte[0]);
}
protected ResponseMessage SendDataWithResolveResponse(String project, HttpMethod method, String resourceUri,
Map parameters, Map headers, byte[] body) throws LogException {
return SendDataWithResolveResponse(project, method, resourceUri, parameters, headers, body, null, null);
}
private ResponseMessage SendDataWithResolveResponse(String project, HttpMethod method, String resourceUri,
Map parameters, Map headers, byte[] body,
Map outputHeader, String serverIp)
throws LogException {
if (resourceOwnerAccount != null && !resourceOwnerAccount.isEmpty()) {
headers.put(Consts.CONST_X_LOG_RESOURCEOWNERACCOUNT, resourceOwnerAccount);
}
try {
signer.sign(method, headers, resourceUri, parameters, body);
} catch (Exception e) {
throw new LogException("ClientSignatureError",
"Fail to calculate signature for request, error:" + e.getMessage(), "");
}
URI uri;
if (serverIp == null) {
uri = GetHostURI(project);
} else {
uri = GetHostURIByIp(serverIp);
}
RequestMessage request = BuildRequest(uri, method,
resourceUri, parameters, headers,
new ByteArrayInputStream(body), body.length);
ResponseMessage response = null;
try {
response = this.serviceClient.sendRequest(request, Consts.UTF_8_ENCODING);
} catch (ServiceException e) {
throw new LogException("RequestError", "Web request failed: " + e.getMessage(), e, "");
} catch (ClientException e) {
throw new LogException("RequestError", "Web request failed: " + e.getMessage(), e, "");
}
return response;
}
private ResponseMessage SendData(String project, HttpMethod method,
String resourceUri, Map urlParams,
Map headParams) throws LogException {
return SendData(project, method, resourceUri, urlParams, headParams, new byte[0]);
}
protected ResponseMessage SendData(String project, HttpMethod method,
String resourceUri, Map parameters,
Map headers, String requestBody) throws LogException {
byte[] body = encodeToUtf8(requestBody);
return SendData(project, method, resourceUri, parameters, headers, body);
}
protected ResponseMessage SendData(String project, HttpMethod method, String resourceUri,
Map parameters, Map headers, byte[] body) throws LogException {
return SendData(project, method, resourceUri, parameters, headers, body, null, null);
}
private ResponseMessage SendData(String project, HttpMethod method, String resourceUri,
Map parameters, Map headers, byte[] body,
Map outputHeader, String serverIp)
throws LogException {
if (resourceOwnerAccount != null && !resourceOwnerAccount.isEmpty()) {
headers.put(Consts.CONST_X_LOG_RESOURCEOWNERACCOUNT, resourceOwnerAccount);
}
try {
signer.sign(method, headers, resourceUri, parameters, body);
} catch (Exception e) {
throw new LogException("ClientSignatureError",
"Fail to calculate signature for request, error:" + e.getMessage(), "");
}
URI uri;
if (serverIp == null) {
uri = GetHostURI(project);
} else {
uri = GetHostURIByIp(serverIp);
}
RequestMessage request = BuildRequest(uri, method,
resourceUri, parameters, headers,
new ByteArrayInputStream(body), body.length);
ResponseMessage response = null;
try {
response = this.serviceClient.sendRequest(request, Consts.UTF_8_ENCODING);
ExtractResponseBody(response);
if (outputHeader != null) {
outputHeader.putAll(response.getHeaders());
}
int statusCode = response.getStatusCode();
if (statusCode != Consts.CONST_HTTP_OK) {
String requestId = GetRequestId(response.getHeaders());
try {
String responseBody = encodeResponseBodyToUtf8String(response, requestId);
try {
JSONObject object = JSONObject.parseObject(responseBody, Feature.DisableSpecialKeyDetect);
ErrorCheck(object, requestId, statusCode, responseBody);
} catch (JSONException ex) {
throw new LogException(ErrorCodes.BAD_RESPONSE,
"The response is not valid json string : " + body, ex, requestId);
}
} catch (LogException ex) {
ex.setHttpCode(response.getStatusCode());
throw ex;
}
}
} catch (ServiceException e) {
throw new LogException("RequestError", "Web request failed: " + e.getMessage(), e, "");
} catch (ClientException e) {
throw new LogException("RequestError", "Web request failed: " + e.getMessage(), e, "");
} finally {
try {
if (response != null) {
response.close();
}
} catch (IOException ignore) {}
}
return response;
}
private static RequestMessage BuildRequest(URI endpoint,
HttpMethod httpMethod, String resourceUri,
Map parameters, Map headers,
InputStream content, long size) {
RequestMessage request = new RequestMessage();
request.setMethod(httpMethod);
request.setEndpoint(endpoint);
request.setResourcePath(resourceUri);
request.setParameters(parameters);
request.setHeaders(headers);
request.setContent(content);
request.setContentLength(size);
return request;
}
ArrayList ExtractShards(JSONArray array, String requestId) throws LogException {
ArrayList shards = new ArrayList();
if (array == null) {
return shards;
}
try {
for (int i = 0; i < array.size(); i++) {
JSONObject shardDict = array.getJSONObject(i);
if (shardDict == null) {
continue;
}
int shardId = shardDict.getIntValue("shardID");
String status = shardDict.getString("status");
String begin = shardDict.getString("inclusiveBeginKey");
String end = shardDict.getString("exclusiveEndKey");
int createTime = shardDict.getIntValue("createTime");
Shard shard = new Shard(shardId, status, begin, end, createTime);
if (shardDict.containsKey("serverIp")) {
shard.setServerIp(shardDict.getString("serverIp"));
}
shards.add(shard);
}
} catch (JSONException e) {
throw new LogException(ErrorCodes.BAD_RESPONSE,
"The response is not valid shard json array string : "
+ array.toString() + e.getMessage(), e, requestId);
}
return shards;
}
@Override
public CreateLogStoreResponse CreateLogStore(String project,
LogStore logStore) throws LogException {
CodingUtils.assertStringNotNullOrEmpty(project, "project");
CodingUtils.assertParameterNotNull(logStore, "logStore");
return CreateLogStore(new CreateLogStoreRequest(project, logStore));
}
@Override
public CreateLogStoreResponse CreateLogStore(CreateLogStoreRequest request)
throws LogException {
CodingUtils.assertParameterNotNull(request, "request");
String project = request.GetProject();
CodingUtils.assertStringNotNullOrEmpty(project, "project");
LogStore logStore = request.GetLogStore();
CodingUtils.assertParameterNotNull(logStore, "logStore");
Map headParameter = GetCommonHeadPara(project);
byte[] body = encodeToUtf8(logStore.ToRequestString());
headParameter.put(Consts.CONST_CONTENT_TYPE, Consts.CONST_SLS_JSON);
String resourceUri = "/logstores";
Map urlParameter = new HashMap();
ResponseMessage response = SendData(project, HttpMethod.POST,
resourceUri, urlParameter, headParameter, body);
Map resHeaders = response.getHeaders();
return new CreateLogStoreResponse(resHeaders);
}
@Override
public CreateLinkStoreResponse CreateLinkStore(String project, LinkStore linkStore) throws LogException {
CodingUtils.assertStringNotNullOrEmpty(project, "project");
CodingUtils.assertParameterNotNull(linkStore, "logStore");
return CreateLinkStore(new CreateLinkStoreRequest(project, linkStore));
}
@Override
public CreateLinkStoreResponse CreateLinkStore(CreateLinkStoreRequest request) throws LogException {
CodingUtils.assertParameterNotNull(request, "request");
String project = request.GetProject();
CodingUtils.assertStringNotNullOrEmpty(project, "project");
LinkStore linkStore = request.getLinkStore();
CodingUtils.assertParameterNotNull(linkStore, "linkStore");
Map headParameter = GetCommonHeadPara(project);
byte[] body = encodeToUtf8(linkStore.ToRequestString());
headParameter.put(Consts.CONST_CONTENT_TYPE, Consts.CONST_SLS_JSON);
String resourceUri = "/logstores";
Map urlParameter = request.GetAllParams();
ResponseMessage response = SendData(project, HttpMethod.POST,
resourceUri, urlParameter, headParameter, body);
Map resHeaders = response.getHeaders();
return new CreateLinkStoreResponse(resHeaders);
}
@Override
public DeleteLogStoreResponse DeleteLogStore(String project,
String logStoreName) throws LogException {
CodingUtils.assertStringNotNullOrEmpty(project, "project");
CodingUtils.assertStringNotNullOrEmpty(logStoreName, "logStoreName");
return DeleteLogStore(new DeleteLogStoreRequest(project, logStoreName));
}
@Override
public DeleteLogStoreResponse DeleteLogStore(DeleteLogStoreRequest request)
throws LogException {
CodingUtils.assertParameterNotNull(request, "request");
String project = request.GetProject();
CodingUtils.assertStringNotNullOrEmpty(project, "project");
String logStoreName = request.GetLogStoreName();
CodingUtils.validateLogstore(logStoreName);
Map headParameter = GetCommonHeadPara(project);
String resourceUri = "/logstores/" + logStoreName;
Map urlParameter = request.GetAllParams();
ResponseMessage response = SendData(project, HttpMethod.DELETE,
resourceUri, urlParameter, headParameter);
Map resHeaders = response.getHeaders();
return new DeleteLogStoreResponse(resHeaders);
}
@Override
public DeleteLinkStoreResponse DeleteLinkStore(String project, String linkStoreName) throws LogException {
CodingUtils.assertStringNotNullOrEmpty(project, "project");
CodingUtils.assertStringNotNullOrEmpty(linkStoreName, "linkStoreName");
return DeleteLinkStore(new DeleteLinkStoreRequest(project, linkStoreName));
}
@Override
public DeleteLinkStoreResponse DeleteLinkStore(DeleteLinkStoreRequest request) throws LogException {
CodingUtils.assertParameterNotNull(request, "request");
String project = request.GetProject();
CodingUtils.assertStringNotNullOrEmpty(project, "project");
String linkStoreName = request.getLinkStoreName();
CodingUtils.validateLogstore(linkStoreName);
Map headParameter = GetCommonHeadPara(project);
String resourceUri = "/logstores/" + linkStoreName;
Map urlParameter = request.GetAllParams();
ResponseMessage response = SendData(project, HttpMethod.DELETE,
resourceUri, urlParameter, headParameter);
Map resHeaders = response.getHeaders();
return new DeleteLinkStoreResponse(resHeaders);
}
@Override
public ClearLogStoreStorageResponse ClearLogStoreStorage(String project,
String logStoreName) throws LogException {
CodingUtils.assertStringNotNullOrEmpty(project, "project");
CodingUtils.assertStringNotNullOrEmpty(logStoreName, "logStoreName");
return ClearLogStoreStorage(new ClearLogStoreStorageRequest(project, logStoreName));
}
@Override
public CreateProjectConsumerGroupResponse CreateProjectConsumerGroup(CreateProjectConsumerGroupRequest request) throws LogException {
CodingUtils.assertParameterNotNull(request, "request");
String project = request.GetProject();
CodingUtils.assertStringNotNullOrEmpty(project, "project");
ProjectConsumerGroup projectConsumerGroup = request.getConsumerGroup();
CodingUtils.assertParameterNotNull(projectConsumerGroup, "consumerGroup");
Map headParameter = GetCommonHeadPara(project);
byte[] body = encodeToUtf8(projectConsumerGroup.ToRequestString());
headParameter.put(Consts.CONST_CONTENT_TYPE, Consts.CONST_SLS_JSON);
String resourceUri = "/consumergroups";
Map urlParameter = new HashMap();
ResponseMessage response = SendData(project, HttpMethod.POST,
resourceUri, urlParameter, headParameter, body);
Map resHeaders = response.getHeaders();
return new CreateProjectConsumerGroupResponse(resHeaders);
}
@Override
public CreateProjectConsumerGroupResponse CreateProjectConsumerGroup(String project, ProjectConsumerGroup consumerGroup) throws LogException {
CodingUtils.assertStringNotNullOrEmpty(project, "project");
return CreateProjectConsumerGroup(new CreateProjectConsumerGroupRequest(
project, consumerGroup));
}
@Override
public DeleteProjectConsumerGroupResponse DeleteProjectConsumerGroup(String project, String consumerGroup) throws LogException {
CodingUtils.assertStringNotNullOrEmpty(project, "project");
CodingUtils.validateConsumerGroup(consumerGroup);
Map headParameter = GetCommonHeadPara(project);
String resourceUri = "/consumergroups/" + consumerGroup;
headParameter.put(Consts.CONST_CONTENT_TYPE, String.valueOf(0));
Map urlParameter = new HashMap();
ResponseMessage response = SendData(project, HttpMethod.DELETE,
resourceUri, urlParameter, headParameter);
Map resHeaders = response.getHeaders();
return new DeleteProjectConsumerGroupResponse(resHeaders);
}
@Override
public ListProjectConsumerGroupResponse ListProjectConsumerGroup(String project) throws LogException {
CodingUtils.assertStringNotNullOrEmpty(project, "project");
String resourceUri = "/consumergroups";
Map headParameter = GetCommonHeadPara(project);
headParameter.put(Consts.CONST_CONTENT_TYPE, Consts.CONST_SLS_JSON);
Map urlParameter = new HashMap();
ResponseMessage response = SendData(project, HttpMethod.GET,
resourceUri, urlParameter, headParameter);
ArrayList consumerGroups = new ArrayList();
Map resHeaders = response.getHeaders();
String requestId = GetRequestId(resHeaders);
JSONArray array = ParseResponseMessageToArray(response, requestId);
ExtractProjectConsumerGroup(array, requestId, consumerGroups);
ListProjectConsumerGroupResponse listProjectConsumerGroupResponse = new ListProjectConsumerGroupResponse(resHeaders);
listProjectConsumerGroupResponse.setConsumerGroups(consumerGroups);
return listProjectConsumerGroupResponse;
}
@Override
public UpdateProjectConsumerGroupResponse UpdateProjectConsumerGroup(String project,
String consumerGroup,
boolean inOrder,
int timeoutInSec) throws LogException {
CodingUtils.assertStringNotNullOrEmpty(project, "project");
CodingUtils.validateConsumerGroup(consumerGroup);
Map headParameter = GetCommonHeadPara(project);
final JSONObject asJson = new JSONObject();
asJson.put("order", inOrder);
asJson.put("timeout", timeoutInSec);
byte[] body = encodeToUtf8(asJson.toString());
headParameter.put(Consts.CONST_CONTENT_TYPE, Consts.CONST_SLS_JSON);
String resourceUri = "/consumergroups/" + consumerGroup;
Map urlParameter = new HashMap();
ResponseMessage response = SendData(project, HttpMethod.PUT,
resourceUri, urlParameter, headParameter, body);
Map resHeaders = response.getHeaders();
return new UpdateProjectConsumerGroupResponse(resHeaders);
}
@Override
public ProjectConsumerGroupUpdateCheckPointResponse UpdateProjectConsumerGroupCheckPoint(String project, String consumerGroup, String consumer, String logStore, int shard, String checkpoint) throws LogException {
CodingUtils.assertStringNotNullOrEmpty(project, "project");
CodingUtils.validateConsumerGroup(consumerGroup);
CodingUtils.assertStringNotNullOrEmpty(consumer, "consumer");
CodingUtils.assertStringNotNullOrEmpty(logStore, "logStore");
CodingUtils.assertStringNotNullOrEmpty(checkpoint, "checkpoint");
return UpdateProjectConsumerGroupCheckPoint(project, consumerGroup, consumer, logStore, shard, checkpoint, false);
}
@Override
public ProjectConsumerGroupUpdateCheckPointResponse UpdateProjectConsumerGroupCheckPoint(String project, String consumerGroup, String logStore, int shard, String checkpoint) throws LogException {
CodingUtils.assertStringNotNullOrEmpty(project, "project");
CodingUtils.validateConsumerGroup(consumerGroup);
CodingUtils.assertStringNotNullOrEmpty(logStore, "logStore");
CodingUtils.assertStringNotNullOrEmpty(checkpoint, "checkpoint");
return UpdateProjectConsumerGroupCheckPoint(project, consumerGroup, "", logStore, shard, checkpoint, true);
}
private ProjectConsumerGroupUpdateCheckPointResponse UpdateProjectConsumerGroupCheckPoint(
String project, String consumerGroup, String consumer, String logStore, int shard, String checkpoint,
boolean forceSuccess) throws LogException {
String resourceUri = "/consumergroups/" + consumerGroup;
ProjectConsumerGroupUpdateCheckPointRequest request = new ProjectConsumerGroupUpdateCheckPointRequest(
project, consumerGroup, consumer, logStore, shard, checkpoint, forceSuccess);
Map headParameter = GetCommonHeadPara(project);
headParameter.put(Consts.CONST_CONTENT_TYPE, Consts.CONST_SLS_JSON);
Map urlParameter = request.GetAllParams();
byte[] body = encodeToUtf8(request.GetRequestBody());
ResponseMessage response = SendData(project, HttpMethod.POST,
resourceUri, urlParameter, headParameter, body);
Map resHeaders = response.getHeaders();
return new ProjectConsumerGroupUpdateCheckPointResponse(resHeaders);
}
@Override
public ProjectConsumerGroupCheckPointResponse GetProjectConsumerGroupCheckPoint(String project, String consumerGroup, String logStore, int shard) throws LogException {
ProjectConsumerGroupGetCheckPointRequest request = new ProjectConsumerGroupGetCheckPointRequest(
project, consumerGroup, logStore, shard);
Map urlParameter = request.GetAllParams();
String resourceUri = "/consumergroups/" + consumerGroup;
Map headParameter = GetCommonHeadPara(project);
headParameter.put(Consts.CONST_CONTENT_TYPE, Consts.CONST_SLS_JSON);
ResponseMessage response = SendData(project, HttpMethod.GET,
resourceUri, urlParameter, headParameter);
Map resHeaders = response.getHeaders();
String requestId = GetRequestId(resHeaders);
JSONObject object = parseResponseBody(response, requestId);
return new ProjectConsumerGroupCheckPointResponse(resHeaders, object);
}
@Override
public ProjectConsumerGroupCheckPointResponse GetProjectConsumerGroupCheckPoint(String project, String consumerGroup, String logStore) throws LogException {
CodingUtils.assertStringNotNullOrEmpty(project, "project");
CodingUtils.validateConsumerGroup(consumerGroup);
CodingUtils.assertStringNotNullOrEmpty(logStore, "logStore");
return GetProjectConsumerGroupCheckPoint(project, consumerGroup, logStore, -1);
}
@Override
public ProjectConsumerGroupCheckPointResponse GetProjectConsumerGroupCheckPoint(String project, String consumerGroup) throws LogException {
CodingUtils.assertStringNotNullOrEmpty(project, "project");
CodingUtils.validateConsumerGroup(consumerGroup);
return GetProjectConsumerGroupCheckPoint(project, consumerGroup, "", -1);
}
@Override
public ProjectConsumerGroupHeartBeatResponse ProjectConsumerGroupHeartBeat(String project, String consumerGroup, String consumer, Map> logStoreShards) throws LogException {
CodingUtils.assertStringNotNullOrEmpty(project, "project");
CodingUtils.validateConsumerGroup(consumerGroup);
CodingUtils.assertStringNotNullOrEmpty(consumer, "consumer");
String resourceUri = "/consumergroups/" + consumerGroup;
ProjectConsumerGroupHeartBeatRequest request = new ProjectConsumerGroupHeartBeatRequest(
project, consumer, logStoreShards == null ? new HashMap>() : logStoreShards);
Map headParameter = GetCommonHeadPara(project);
Map urlParameter = request.GetAllParams();
byte[] body = encodeToUtf8(request.GetRequestBody());
headParameter.put(Consts.CONST_CONTENT_TYPE, Consts.CONST_SLS_JSON);
ResponseMessage response = SendData(project, HttpMethod.POST,
resourceUri, urlParameter, headParameter, body);
Map resHeaders = response.getHeaders();
String requestId = GetRequestId(resHeaders);
JSONObject object = parseResponseBody(response, requestId);
return new ProjectConsumerGroupHeartBeatResponse(resHeaders, object);
}
@Override
public ClearLogStoreStorageResponse ClearLogStoreStorage(ClearLogStoreStorageRequest request)
throws LogException {
CodingUtils.assertParameterNotNull(request, "request");
String project = request.GetProject();
CodingUtils.assertStringNotNullOrEmpty(project, "project");
String logStoreName = request.GetLogStoreName();
CodingUtils.validateLogstore(logStoreName);
Map headParameter = GetCommonHeadPara(project);
String resourceUri = "/logstores/" + logStoreName + "/storage";
Map urlParameter = request.GetAllParams();
ResponseMessage response = SendData(project, HttpMethod.DELETE,
resourceUri, urlParameter, headParameter);
Map resHeaders = response.getHeaders();
return new ClearLogStoreStorageResponse(resHeaders);
}
@Override
public UpdateLogStoreResponse UpdateLogStore(String project,
LogStore logStore) throws LogException {
CodingUtils.assertStringNotNullOrEmpty(project, "project");
CodingUtils.assertParameterNotNull(logStore, "logStore");
return UpdateLogStore(new UpdateLogStoreRequest(project, logStore));
}
@Override
public UpdateLogStoreResponse UpdateLogStore(UpdateLogStoreRequest request)
throws LogException {
CodingUtils.assertParameterNotNull(request, "request");
String project = request.GetProject();
CodingUtils.assertStringNotNullOrEmpty(project, "project");
LogStore logStore = request.GetLogStore();
CodingUtils.assertParameterNotNull(logStore, "logStore");
String logStoreName = logStore.GetLogStoreName();
CodingUtils.validateLogstore(logStoreName);
Map headParameter = GetCommonHeadPara(project);
byte[] body = encodeToUtf8(logStore.ToRequestString());
headParameter.put(Consts.CONST_CONTENT_TYPE, Consts.CONST_SLS_JSON);
String resourceUri = "/logstores/" + logStoreName;
Map urlParameter = new HashMap();
ResponseMessage response = SendData(project, HttpMethod.PUT,
resourceUri, urlParameter, headParameter, body);
Map resHeaders = response.getHeaders();
return new UpdateLogStoreResponse(resHeaders);
}
@Override
public GetLogStoreResponse GetLogStore(String project, String logStoreName)
throws LogException {
CodingUtils.validateProject(project);
CodingUtils.validateLogstore(logStoreName);
return GetLogStore(new GetLogStoreRequest(project, logStoreName));
}
private LogStore ExtractLogStoreFromResponse(JSONObject dict,
String requestId) throws LogException {
LogStore logStore = new LogStore();
try {
logStore.FromJsonString(dict.toString());
} catch (LogException e) {
throw new LogException(e.GetErrorCode(), e.GetErrorMessage(),
e.getCause(), requestId);
}
return logStore;
}
@Override
public GetLogStoreResponse GetLogStore(GetLogStoreRequest request)
throws LogException {
CodingUtils.assertParameterNotNull(request, "request");
String project = request.GetProject();
CodingUtils.assertStringNotNullOrEmpty(project, "project");
String logStoreName = request.GetLogStore();
CodingUtils.validateLogstore(logStoreName);
Map headParameter = GetCommonHeadPara(project);
String resourceUri = "/logstores/" + logStoreName;
Map urlParameter = request.GetAllParams();
ResponseMessage response = SendData(project, HttpMethod.GET,
resourceUri, urlParameter, headParameter);
Map resHeaders = response.getHeaders();
String requestId = GetRequestId(resHeaders);
JSONObject object = parseResponseBody(response, requestId);
LogStore logStore = ExtractLogStoreFromResponse(object, requestId);
return new GetLogStoreResponse(resHeaders, logStore);
}
@Override
public CreateLogStoreResponse createMetricStore(String project,
LogStore metricStore) throws LogException {
metricStore.setTelemetryType("Metrics");
CreateLogStoreResponse createLogStoreResponse = CreateLogStore(project, metricStore);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new LogException("Sleep Interrupted", e.getMessage(), "");
}
List list = new ArrayList();
list.add(new SubStoreKey("__name__", "text"));
list.add(new SubStoreKey("__labels__", "text"));
list.add(new SubStoreKey("__time_nano__", "long"));
list.add(new SubStoreKey("__value__", "double"));
SubStore subStore = new SubStore("prom", metricStore.GetTtl(), 2, 2, list);
createSubStore(project, metricStore.GetLogStoreName(), subStore);
return createLogStoreResponse;
}
@Override
public CreateLogStoreResponse createMetricStore(CreateLogStoreRequest request)
throws LogException {
return createMetricStore(request.GetProject(), request.GetLogStore());
}
@Override
public UpdateLogStoreResponse updateMetricStore(String project,
LogStore metricStore) throws LogException {
metricStore.setTelemetryType("Metrics");
UpdateLogStoreResponse updateLogStoreResponse = UpdateLogStore(project, metricStore);
updateSubStoreTTL(project, metricStore.GetLogStoreName(), metricStore.GetTtl());
return updateLogStoreResponse;
}
@Override
public UpdateLogStoreResponse updateMetricStore(UpdateLogStoreRequest request)
throws LogException {
return updateMetricStore(request.GetProject(), request.GetLogStore());
}
@Override
public DeleteLogStoreResponse deleteMetricStore(String project,
String metricStoreName) throws LogException {
return DeleteLogStore(project, metricStoreName);
}
@Override
public DeleteLogStoreResponse deleteMetricStore(DeleteLogStoreRequest request)
throws LogException {
return deleteMetricStore(request.GetProject(), request.GetLogStoreName());
}
@Override
public GetLogStoreResponse getMetricStore(String project, String metricStoreName)
throws LogException {
return GetLogStore(project, metricStoreName);
}
@Override
public GetLogStoreResponse getMetricStore(GetLogStoreRequest request) throws LogException {
return getMetricStore(request.GetProject(), request.GetLogStore());
}
@Override
public ListSubStoreResponse listSubStore(String project, String logstore) throws LogException {
CodingUtils.assertStringNotNullOrEmpty(project, "project");
CodingUtils.assertStringNotNullOrEmpty(logstore, "logstore");
return listSubStore(new ListSubStoreRequest(project, logstore));
}
@Override
public ListSubStoreResponse listSubStore(ListSubStoreRequest request) throws LogException {
CodingUtils.assertParameterNotNull(request, "request");
String project = request.GetProject();
CodingUtils.assertStringNotNullOrEmpty(project, "project");
Map urlParameter = request.GetAllParams();
CodingUtils.validateLogstore(request.getLogstoreName());
String resourceUri = "/logstores/" + request.getLogstoreName() + "/substores";
Map headParameter = GetCommonHeadPara(project);
ResponseMessage response = SendData(project, HttpMethod.GET,
resourceUri, urlParameter, headParameter);
Map resHeaders = response.getHeaders();
String requestId = GetRequestId(resHeaders);
JSONObject object = parseResponseBody(response, requestId);
ListSubStoreResponse listSubStoreResponse = new ListSubStoreResponse(resHeaders);
listSubStoreResponse.setSubStoreNames(ExtractJsonArray(
Consts.CONST_RESULT_SUB_STORES, object));
return listSubStoreResponse;
}
@Override
public GetSubStoreResponse getSubStore(String project, String logstore, String subStoreName) throws LogException {
CodingUtils.assertStringNotNullOrEmpty(project, "project");
CodingUtils.assertStringNotNullOrEmpty(logstore, "logstore");
CodingUtils.assertStringNotNullOrEmpty(subStoreName, "subStoreName");
return getSubStore(new GetSubStoreRequest(project, logstore, subStoreName));
}
@Override
public GetSubStoreResponse getSubStore(GetSubStoreRequest request) throws LogException {
CodingUtils.assertParameterNotNull(request, "request");
String project = request.GetProject();
CodingUtils.assertStringNotNullOrEmpty(project, "project");
String subStoreName = request.getSubStoreName();
CodingUtils.assertStringNotNullOrEmpty(project, "subStoreName");
Map urlParameter = request.GetAllParams();
CodingUtils.validateLogstore(request.getLogstoreName());
String resourceUri = "/logstores/" + request.getLogstoreName() + "/substores/" + subStoreName;
Map headParameter = GetCommonHeadPara(project);
ResponseMessage response = SendData(project, HttpMethod.GET,
resourceUri, urlParameter, headParameter);
Map resHeaders = response.getHeaders();
String requestId = GetRequestId(resHeaders);
JSONObject object = parseResponseBody(response, requestId);
SubStore subStore = new SubStore();
subStore.fromJsonString(object.toString());
return new GetSubStoreResponse(resHeaders, subStore);
}
@Override
public CreateSubStoreResponse createSubStore(String project, String logStoreName, SubStore subStore) throws LogException {
CodingUtils.assertStringNotNullOrEmpty(project, "project");
CodingUtils.assertParameterNotNull(logStoreName, "logStoreName");
CodingUtils.assertParameterNotNull(subStore, "subStore");
return createSubStore(new CreateSubStoreRequest(project, logStoreName, subStore));
}
@Override
public CreateSubStoreResponse createSubStore(CreateSubStoreRequest request) throws LogException {
CodingUtils.assertParameterNotNull(request, "request");
String project = request.GetProject();
CodingUtils.assertStringNotNullOrEmpty(project, "project");
String logStoreName = request.getLogStoreName();
CodingUtils.validateLogstore(logStoreName);
SubStore subStore = request.getSubStore();
CodingUtils.assertParameterNotNull(subStore, "subStore");
if (!subStore.isValid()) {
throw new IllegalArgumentException("SubStore is invalid");
}
Map headParameter = GetCommonHeadPara(project);
byte[] body = encodeToUtf8(subStore.toRequestString());
headParameter.put(Consts.CONST_CONTENT_TYPE, Consts.CONST_SLS_JSON);
String resourceUri = "/logstores/" + logStoreName + "/substores";
Map urlParameter = new HashMap();
ResponseMessage response = SendData(project, HttpMethod.POST,
resourceUri, urlParameter, headParameter, body);
Map resHeaders = response.getHeaders();
return new CreateSubStoreResponse(resHeaders);
}
@Override
public UpdateSubStoreResponse updateSubStore(String project, String logStoreName, SubStore subStore) throws LogException {
CodingUtils.assertStringNotNullOrEmpty(project, "project");
CodingUtils.assertParameterNotNull(logStoreName, "logStoreName");
CodingUtils.assertParameterNotNull(subStore, "subStore");
return updateSubStore(new UpdateSubStoreRequest(project, logStoreName, subStore));
}
@Override
public UpdateSubStoreResponse updateSubStore(UpdateSubStoreRequest request) throws LogException {
CodingUtils.assertParameterNotNull(request, "request");
String project = request.GetProject();
CodingUtils.assertStringNotNullOrEmpty(project, "project");
String logStoreName = request.getLogStoreName();
CodingUtils.validateLogstore(logStoreName);
SubStore subStore = request.getSubStore();
CodingUtils.assertParameterNotNull(subStore, "subStore");
if (!subStore.isValid()) {
throw new IllegalArgumentException("SubStore is invalid");
}
String subStoreName = subStore.getName();
CodingUtils.assertStringNotNullOrEmpty(subStoreName, "subStoreName");
Map headParameter = GetCommonHeadPara(project);
byte[] body = encodeToUtf8(subStore.toRequestString());
headParameter.put(Consts.CONST_CONTENT_TYPE, Consts.CONST_SLS_JSON);
String resourceUri = "/logstores/" + logStoreName + "/substores/" + subStoreName;
Map urlParameter = new HashMap();
ResponseMessage response = SendData(project, HttpMethod.PUT,
resourceUri, urlParameter, headParameter, body);
Map resHeaders = response.getHeaders();
return new UpdateSubStoreResponse(resHeaders);
}
@Override
public DeleteSubStoreResponse deleteSubStore(String project, String logStoreName, String subStoreName) throws LogException {
CodingUtils.assertStringNotNullOrEmpty(project, "project");
CodingUtils.assertStringNotNullOrEmpty(logStoreName, "logStoreName");
CodingUtils.assertStringNotNullOrEmpty(subStoreName, "subStoreName");
return deleteSubStore(new DeleteSubStoreRequest(project, logStoreName, subStoreName));
}
@Override
public DeleteSubStoreResponse deleteSubStore(DeleteSubStoreRequest request) throws LogException {
CodingUtils.assertParameterNotNull(request, "request");
String project = request.GetProject();
CodingUtils.assertStringNotNullOrEmpty(project, "project");
String logStoreName = request.getLogStoreName();
CodingUtils.validateLogstore(logStoreName);
String subStoreName = request.getSubStoreName();
CodingUtils.assertStringNotNullOrEmpty(subStoreName, "subStoreName");
Map headParameter = GetCommonHeadPara(project);
String resourceUri = "/logstores/" + logStoreName + "/substores/" + subStoreName;
Map