All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.aliyun.datahub.model.serialize.QuerySubscriptionResultJsonDeser Maven / Gradle / Ivy

There is a newer version: 2.25.6
Show newest version
package com.aliyun.datahub.model.serialize;

import com.aliyun.datahub.common.transport.Response;
import com.aliyun.datahub.common.util.JacksonParser;
import com.aliyun.datahub.exception.DatahubServiceException;
import com.aliyun.datahub.model.GetSubscriptionResult;
import com.aliyun.datahub.model.QuerySubscriptionRequest;
import com.aliyun.datahub.model.QuerySubscriptionResult;
import com.aliyun.datahub.rest.DatahubHttpHeaders;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class QuerySubscriptionResultJsonDeser implements Deserializer {
    @Override
    public QuerySubscriptionResult deserialize(QuerySubscriptionRequest request, Response response) throws DatahubServiceException {
        if (!response.isOK()) {
            throw JsonErrorParser.getInstance().parse(response);
        }

        QuerySubscriptionResult rs = new QuerySubscriptionResult();
        rs.setRequestId(response.getHeader(DatahubHttpHeaders.HEADER_DATAHUB_REQUEST_ID));

        ObjectMapper mapper = JacksonParser.getObjectMapper();

        JsonNode tree = null;
        try {
            tree = mapper.readTree(response.getBody());
        } catch (IOException e) {
            throw new DatahubServiceException(
                    "JsonParseError", "Parse body failed:" + response.getBody(), response);
        }

        rs.setTotalCount(tree.get("TotalCount").asLong());

        JsonNode subscriptionsNode = tree.get("Subscriptions");
        List subscriptions = new ArrayList();
        Iterator itSub = subscriptionsNode.elements();
        while (itSub.hasNext()) {
            JsonNode subNode = itSub.next();
        	GetSubscriptionResult sub = new GetSubscriptionResult();
            sub.setIsOwner(subNode.get("IsOwner").asBoolean());
            sub.setSubId(subNode.get("SubId").asText());
            sub.setTopicName(subNode.get("TopicName").asText());
            sub.setComment(subNode.get("Comment").asText());
            sub.setType(subNode.get("Type").asInt());
            sub.setState(subNode.get("State").asInt());
            sub.setCreateTime(subNode.get("CreateTime").asLong() * 1000);
            sub.setLastModifyTime(subNode.get("LastModifyTime").asLong() * 1000);
            subscriptions.add(sub);
        }
        rs.setSubscriptions(subscriptions);

        return rs;
    }

    private QuerySubscriptionResultJsonDeser() {}

    private static QuerySubscriptionResultJsonDeser instance;

    public static QuerySubscriptionResultJsonDeser getInstance() {
        if (instance == null) {
            instance = new QuerySubscriptionResultJsonDeser();
        }
        return instance;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy