cn.jiguang.common.resp.APIConnectionException Java Examples

The following examples show how to use cn.jiguang.common.resp.APIConnectionException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: GroupClient.java    From jmessage-api-java-client with MIT License 5 votes vote down vote up
public GroupInfoResult getGroupInfo(long gid)
        throws APIConnectionException, APIRequestException {
    Preconditions.checkArgument(gid > 0, "gid should more than 0.");

    ResponseWrapper response = _httpClient.sendGet(_baseUrl + groupPath + "/" + gid);

    return GroupInfoResult.fromResponse(response, GroupInfoResult.class);
}
 
Example #2
Source File: JMessageClient.java    From jmessage-api-java-client with MIT License 5 votes vote down vote up
public CreateGroupResult createGroup(String owner, String gname, String desc,  String avatar, int flag, String... userlist)
        throws APIConnectionException, APIRequestException {
    Members members = Members.newBuilder().addMember(userlist).build();

    GroupPayload payload = GroupPayload.newBuilder()
            .setOwner(owner)
            .setName(gname)
            .setDesc(desc)
            .setMembers(members)
            .setAvatar(avatar)
            .setFlag(flag)
            .build();

    return _groupClient.createGroup(payload);
}
 
Example #3
Source File: MessageClient.java    From jmessage-api-java-client with MIT License 5 votes vote down vote up
public SendMessageResult sendMessage(MessagePayload payload)
        throws APIConnectionException, APIRequestException {
    Preconditions.checkArgument(!(null == payload), "Message payload should not be null");

    ResponseWrapper response = _httpClient.sendPost(_baseUrl + messagePath, payload.toString());
    return SendMessageResult.fromResponse(response, SendMessageResult.class);
}
 
Example #4
Source File: GroupClient.java    From jmessage-api-java-client with MIT License 5 votes vote down vote up
public ResponseWrapper updateGroupInfo(long gid, String groupName, String groupDesc, String avatar)
        throws APIConnectionException, APIRequestException {
    Preconditions.checkArgument(gid > 0, "gid should more than 0.");

    if (StringUtils.isTrimedEmpty(groupName) && StringUtils.isTrimedEmpty(groupDesc)
            && StringUtils.isTrimedEmpty(avatar)) {
        Preconditions.checkArgument(false, "group name, group description and group avatar should not be null at the same time.");
    }

    JsonObject json = new JsonObject();
    if (StringUtils.isNotEmpty(groupName)) {
        groupName = groupName.trim();
        Preconditions.checkArgument(groupName.getBytes().length <= 64,
                "The length of group name must not more than 64 bytes.");
        Preconditions.checkArgument(!StringUtils.isLineBroken(groupName),
                "The group name must not contain line feed character.");
        json.addProperty(GroupPayload.GROUP_NAME, groupName);
    }

    if (StringUtils.isNotEmpty(groupDesc)) {
        groupDesc = groupDesc.trim();
        Preconditions.checkArgument(groupDesc.getBytes().length <= 250,
                "The length of group description must not more than 250 bytes.");
        json.addProperty(GroupPayload.DESC, groupDesc);
    }

    if (StringUtils.isNotEmpty(avatar)) {
        avatar = avatar.trim();
        json.addProperty(GroupPayload.AVATAR, avatar);
    }

    return _httpClient.sendPut(_baseUrl + groupPath + "/" + gid, json.toString());
}
 
Example #5
Source File: MessageClient.java    From jmessage-api-java-client with MIT License 5 votes vote down vote up
/**
 * Please use {@link cn.jmessage.api.reportv2.ReportClient#v2GetUserMessagesByCursor(String, String)}
 * Get message list with cursor, the cursor will effective in 120 seconds. And will
 * return same count of messages as first request.
 * @param cursor First request will return cursor
 * @return MessageListResult
 * @throws APIConnectionException connect exception
 * @throws APIRequestException request exception
 */
@Deprecated
public MessageListResult getMessageListByCursor(String cursor)
        throws APIConnectionException, APIRequestException {
    if (null != cursor) {
        String requestUrl = reportBaseUrl + v2_messagePath + "?cursor=" + cursor;
        ResponseWrapper response = _httpClient.sendGet(requestUrl);
        return MessageListResult.fromResponse(response, MessageListResult.class);
    } else {
        throw new IllegalArgumentException("the cursor parameter should not be null");
    }
}
 
Example #6
Source File: ReportClient.java    From jmessage-api-java-client with MIT License 5 votes vote down vote up
/**
 * Get user statistic, now time unit only supports DAY
 * @param startTime start time, format: yyyy-MM-dd
 * @param duration 0-60
 * @return {@link UserStatListResult}
 * @throws APIConnectionException connect exception
 * @throws APIRequestException request exception
 */
public UserStatListResult getUserStatistic(String startTime, int duration)
        throws APIConnectionException, APIRequestException {
    Preconditions.checkArgument(verifyDateFormat("DAY", startTime), "Illegal date format");
    Preconditions.checkArgument(0 <= duration && duration <= 60, " 0 <= duration <= 60");
    String url = mBaseReportPath + mV2StatisticPath + "/users?time_unit=DAY&start=" + startTime + "&duration=" + duration;
    ResponseWrapper responseWrapper = _httpClient.sendGet(url);
    return UserStatListResult.fromResponse(responseWrapper);
}
 
Example #7
Source File: JMessageClient.java    From jmessage-api-java-client with MIT License 5 votes vote down vote up
public String registerAdmins(String username, String password)
        throws APIConnectionException, APIRequestException {
    RegisterInfo payload = RegisterInfo.newBuilder()
            .setUsername(username)
            .setPassword(password)
            .build();

    return _userClient.registerAdmins(payload).responseContent;
}
 
Example #8
Source File: SMSClient.java    From jsms-api-java-client with MIT License 5 votes vote down vote up
/**
 * create sign
 * 其实两个接口可以合并 但没时间搞
 *
 * @param payload {@link SignPayload }
 * @return SignResult
 * @throws APIConnectionException connect exception
 * @throws APIRequestException    request exception
 */
public SignResult createSign(SignPayload payload) throws APIConnectionException, APIRequestException {
    Preconditions.checkArgument(null != payload, "sign payload should not be null");
    Preconditions.checkArgument(payload.getType() > 0 && payload.getType() <= 7,
            "type should be between 1 and 7");
    Map<String, Object> params = new HashMap<String, Object>();
    params.put(SignPayload.getSIGN(), payload.getSign());
    if (!StringUtils.isEmpty(payload.getRemark())) {
        Preconditions.checkArgument(payload.getRemark().length() <= 100,
                "remark too long");
        params.put(SignPayload.getREMARK(), payload.getRemark());
    }
    params.put(SignPayload.getTYPE(), payload.getType());
    Map<String, byte[]> fileParams = new HashMap<String, byte[]>();
    File[] images = payload.getImages();
    if (images != null && images.length > 0) {
        for (File file : payload.getImages()) {
            fileParams.put(file.getName(), getBytes(file));
        }
    }
    String url = _baseUrl + _signPath;
    try {
        ResponseWrapper wrapper = doPostSign(url, params, fileParams, SignPayload.getIMAGES());
        if (wrapper.responseCode != 200) {
            throw new APIRequestException(wrapper);
        }
        return SignResult.fromResponse(wrapper, SignResult.class);
    } catch (IOException e) {
        e.printStackTrace();
        throw new IllegalArgumentException("fail to creat sign");
    }
}
 
Example #9
Source File: CrossAppClient.java    From jmessage-api-java-client with MIT License 5 votes vote down vote up
/**
 * Add blacklist whose users belong to another app to a given user.
 * @param username The owner of the blacklist
 * @param blacklists CrossBlacklist array
 * @return No Content
 * @throws APIConnectionException connect exception
 * @throws APIRequestException request exception
 */
public ResponseWrapper addCrossBlacklist(String username, CrossBlacklist[] blacklists)
        throws APIConnectionException, APIRequestException {
    StringUtils.checkUsername(username);
    CrossBlacklistPayload payload = new CrossBlacklistPayload.Builder()
            .setCrossBlacklists(blacklists)
            .build();
    return _httpClient.sendPut(_baseUrl + crossUserPath + "/" + username +"/blacklist", payload.toString());
}
 
Example #10
Source File: ReportClient.java    From jmessage-api-java-client with MIT License 5 votes vote down vote up
/**
 * Get user's message list with cursor, the cursor will effective in 120 seconds.
 * And will return same count of messages as first request.
 * @param username Necessary parameter.
 * @param cursor First request will return cursor
 * @return MessageListResult
 * @throws APIConnectionException connect exception
 * @throws APIRequestException request exception
 */
public MessageListResult v2GetUserMessagesByCursor(String username, String cursor)
        throws APIConnectionException, APIRequestException {
    StringUtils.checkUsername(username);
    if (null != cursor) {
        String requestUrl = mBaseReportPath + mV2UserPath + "/" + username + "/messages?cursor=" + cursor;
        ResponseWrapper response = _httpClient.sendGet(requestUrl);
        return MessageListResult.fromResponse(response, MessageListResult.class);
    } else {
        throw new IllegalArgumentException("the cursor parameter should not be null");
    }
}
 
Example #11
Source File: GroupClient.java    From jmessage-api-java-client with MIT License 5 votes vote down vote up
public MemberListResult getGroupMembers(long gid)
        throws APIConnectionException, APIRequestException {
    Preconditions.checkArgument(gid > 0, "gid should more than 0.");

    ResponseWrapper response = _httpClient.sendGet(_baseUrl + groupPath + "/" + gid + "/members");

    return MemberListResult.fromResponse(response);
}
 
Example #12
Source File: SMSClient.java    From jsms-api-java-client with MIT License 5 votes vote down vote up
/**
 * Modify SMS with schedule
 *
 * @param payload    ScheduleSMSPayload
 * @param scheduleId id
 * @return ScheduleResult which includes schedule_id
 * @throws APIConnectionException connect exception
 * @throws APIRequestException    request exception
 */
public ScheduleResult updateScheduleSMS(ScheduleSMSPayload payload, String scheduleId)
        throws APIConnectionException, APIRequestException {
    Preconditions.checkArgument(null != payload, "Schedule SMS payload should not be null");
    Preconditions.checkArgument(null != scheduleId, "Schedule id should not be null");
    Preconditions.checkArgument(null != payload.getMobile(), "Mobile should not be null");
    Preconditions.checkArgument(StringUtils.isMobileNumber(payload.getMobile()), "Invalid mobile number");
    ResponseWrapper responseWrapper = _httpClient.sendPut(_baseUrl + _schedulePath + "/" + scheduleId, payload.toString());
    return ScheduleResult.fromResponse(responseWrapper, ScheduleResult.class);
}
 
Example #13
Source File: GroupClient.java    From jmessage-api-java-client with MIT License 5 votes vote down vote up
public GroupListResult getGroupListByAppkey(int start, int count)
        throws APIConnectionException, APIRequestException {
    if (start < 0 || count <= 0 || count > 500) {
        throw new IllegalArgumentException("negative index or count must more than 0 and less than 501");
    }
    ResponseWrapper response = _httpClient.sendGet(_baseUrl + groupPath + "?start=" + start + "&count=" + count);
    return GroupListResult.fromResponse(response, GroupListResult.class);
}
 
Example #14
Source File: GroupClient.java    From jmessage-api-java-client with MIT License 5 votes vote down vote up
public CreateGroupResult createGroup(GroupPayload payload)
        throws APIConnectionException, APIRequestException {
    Preconditions.checkArgument(!(null == payload), "group payload should not be null");

    ResponseWrapper response = _httpClient.sendPost(_baseUrl + groupPath, payload.toString());
    return CreateGroupResult.fromResponse(response, CreateGroupResult.class);
}
 
Example #15
Source File: UserClient.java    From jmessage-api-java-client with MIT License 5 votes vote down vote up
/**
 * Get admins by appkey
 *
 * @param start The start index of the list
 * @param count The number that how many you want to get from list
 * @return admin user info list
 * @throws APIConnectionException connect exception
 * @throws APIRequestException    request exception
 */
public UserListResult getAdminListByAppkey(int start, int count)
        throws APIConnectionException, APIRequestException {
    if (start < 0 || count <= 0 || count > 500) {
        throw new IllegalArgumentException("negative index or count must more than 0 and less than 501");
    }
    ResponseWrapper response = _httpClient.sendGet(_baseUrl + adminPath + "?start=" + start + "&count=" + count);
    return UserListResult.fromResponse(response, UserListResult.class);

}
 
Example #16
Source File: NettyHttpClient.java    From jiguang-java-client-common with MIT License 4 votes vote down vote up
public ResponseWrapper sendDelete(String url, String content) throws APIConnectionException, APIRequestException {
    return sendHttpRequest(HttpMethod.DELETE, url, content);
}
 
Example #17
Source File: JMessageClient.java    From jmessage-api-java-client with MIT License 4 votes vote down vote up
public ResponseWrapper removeBlacklist(String username, String...users)
        throws APIConnectionException, APIRequestException {
    return _userClient.removeBlackList(username, users);
}
 
Example #18
Source File: JMessageClient.java    From jmessage-api-java-client with MIT License 4 votes vote down vote up
public UserStateResult getUserState(String username)
        throws APIConnectionException, APIRequestException {
    return _userClient.getUserState(username);
}
 
Example #19
Source File: NettyHttpClient.java    From jiguang-java-client-common with MIT License 4 votes vote down vote up
@Override
public ResponseWrapper sendPost(String url, String content) throws APIConnectionException, APIRequestException {
    return sendHttpRequest(HttpMethod.POST, url, content);
}
 
Example #20
Source File: NettyHttpClient.java    From jiguang-java-client-common with MIT License 4 votes vote down vote up
@Override
public ResponseWrapper sendPut(String url, String content) throws APIConnectionException, APIRequestException {
    return sendHttpRequest(HttpMethod.PUT, url, content);
}
 
Example #21
Source File: IHttpClient.java    From jiguang-java-client-common with MIT License 4 votes vote down vote up
public ResponseWrapper sendDelete(String url, String content)
throws APIConnectionException, APIRequestException;
 
Example #22
Source File: IHttpClient.java    From jiguang-java-client-common with MIT License 4 votes vote down vote up
public ResponseWrapper sendDelete(String url)
throws APIConnectionException, APIRequestException;
 
Example #23
Source File: ApacheHttpClient.java    From jiguang-java-client-common with MIT License 4 votes vote down vote up
public void processResponse(CloseableHttpResponse response, ResponseWrapper wrapper)
        throws APIConnectionException, APIRequestException, IOException {
    HttpEntity entity = response.getEntity();
    LOG.debug("Response", response.toString());
    int status = response.getStatusLine().getStatusCode();
    String responseContent = "";
    if(entity != null){
    	responseContent = EntityUtils.toString(entity, "utf-8");
    }
    wrapper.responseCode = status;
    wrapper.responseContent = responseContent;
    String quota = getFirstHeader(response, RATE_LIMIT_QUOTA);
    String remaining = getFirstHeader(response, RATE_LIMIT_Remaining);
    String reset = getFirstHeader(response, RATE_LIMIT_Reset);
    wrapper.setRateLimit(quota, remaining, reset);

    LOG.debug(wrapper.responseContent);
    EntityUtils.consume(entity);
    if (status >= 200 && status < 300) {
        LOG.debug("Succeed to get response OK - responseCode:" + status);
        LOG.debug("Response Content - " + responseContent);

    } else if (status >= 300 && status < 400) {
        LOG.warn("Normal response but unexpected - responseCode:" + status + ", responseContent:" + responseContent);

    } else {
        LOG.warn("Got error response - responseCode:" + status + ", responseContent:" + responseContent);

        switch (status) {
            case 400:
                LOG.error("Your request params is invalid. Please check them according to error message.");
                wrapper.setErrorObject();
                break;
            case 401:
                LOG.error("Authentication failed! Please check authentication params according to docs.");
                wrapper.setErrorObject();
                break;
            case 403:
                LOG.error("Request is forbidden! Maybe your appkey is listed in blacklist or your params is invalid.");
                wrapper.setErrorObject();
                break;
            case 404:
                LOG.error("Request page is not found! Maybe your params is invalid.");
                wrapper.setErrorObject();
                break;
            case 410:
                LOG.error("Request resource is no longer in service. Please according to notice on official website.");
                wrapper.setErrorObject();
            case 429:
                LOG.error("Too many requests! Please review your appkey's request quota.");
                wrapper.setErrorObject();
                break;
            case 500:
            case 502:
            case 503:
            case 504:
                LOG.error("Seems encountered server error. Maybe JPush is in maintenance? Please retry later.");
                break;
            default:
                LOG.error("Unexpected response.");
        }

        throw new APIRequestException(wrapper);
    }
}
 
Example #24
Source File: JMessageClient.java    From jmessage-api-java-client with MIT License 4 votes vote down vote up
public void deleteUser(String username)
        throws APIConnectionException, APIRequestException {
    _userClient.deleteUser(username);
}
 
Example #25
Source File: JMessageClient.java    From jmessage-api-java-client with MIT License 4 votes vote down vote up
public void updateUserInfo(String username, UserPayload payload)
        throws APIConnectionException, APIRequestException {
    _userClient.updateUserInfo(username, payload);
}
 
Example #26
Source File: NativeHttpClient.java    From jiguang-java-client-common with MIT License 4 votes vote down vote up
public ResponseWrapper sendDelete(String url, String content)
		throws APIConnectionException, APIRequestException {
	return doRequest(url, content, RequestMethod.DELETE);
}
 
Example #27
Source File: NativeHttpClient.java    From jiguang-java-client-common with MIT License 4 votes vote down vote up
public ResponseWrapper sendPost(String url, String content) 
           throws APIConnectionException, APIRequestException {
	return doRequest(url, content, RequestMethod.POST);
}
 
Example #28
Source File: NativeHttpClient.java    From jiguang-java-client-common with MIT License 4 votes vote down vote up
public ResponseWrapper sendPut(String url, String content)
		throws APIConnectionException, APIRequestException {
	return doRequest(url, content, RequestMethod.PUT);
}
 
Example #29
Source File: JMessageClient.java    From jmessage-api-java-client with MIT License 4 votes vote down vote up
public GroupListResult getGroupListByAppkey(int start, int count)
        throws APIConnectionException, APIRequestException {
    return _groupClient.getGroupListByAppkey(start, count);
}
 
Example #30
Source File: NativeHttpClient.java    From jiguang-java-client-common with MIT License 4 votes vote down vote up
public ResponseWrapper sendGet(String url) 
           throws APIConnectionException, APIRequestException {
	return sendGet(url, null);
}