cn.jiguang.common.utils.Preconditions Java Examples

The following examples show how to use cn.jiguang.common.utils.Preconditions. 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: GroupPayload.java    From jmessage-api-java-client with MIT License 6 votes vote down vote up
public GroupPayload build() {

            Preconditions.checkArgument(StringUtils.isNotEmpty(owner), "The owner must not be empty.");
            Preconditions.checkArgument(StringUtils.isNotEmpty(name), "The group name must not be empty.");
            Preconditions.checkArgument(!StringUtils.isLineBroken(owner), 
            		"The owner name must not contain line feed character.");
            Preconditions.checkArgument(name.getBytes().length <= 64,
                    "The length of group name must not more than 64 bytes.");
            Preconditions.checkArgument( !StringUtils.isLineBroken(name),
                    "The group name must not contain line feed character.");

            if ( null != desc ) {
                Preconditions.checkArgument( desc.getBytes().length <= 250,
                        "The length of group description must not more than 250 bytes.");
            }

            return new GroupPayload(owner, name, members, desc, avatar, flag);
        }
 
Example #2
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 #3
Source File: UserPayload.java    From jmessage-api-java-client with MIT License 5 votes vote down vote up
public Builder addExtra(String key, JsonObject value) {
	Preconditions.checkArgument(! (null == key || null == value), "Key/Value should not be null.");
    if (null == jsonExtrasBuilder) {
    	jsonExtrasBuilder = new HashMap<String, JsonObject>();
    }
    jsonExtrasBuilder.put(key, value);
    return this;
}
 
Example #4
Source File: MessageBody.java    From jmessage-api-java-client with MIT License 5 votes vote down vote up
public Builder addExtras(Map<String, String> extras) {
    Preconditions.checkArgument(!(null == extras), "extras should not be null.");
    if (null == extrasBuilder) {
        extrasBuilder = new HashMap<String, String>();
    }
    for (String key : extras.keySet()) {
        extrasBuilder.put(key, extras.get(key));
    }
    return this;
}
 
Example #5
Source File: FriendNote.java    From jmessage-api-java-client with MIT License 5 votes vote down vote up
public FriendNote builder() {
    StringUtils.checkUsername(username);
    try {
        Preconditions.checkArgument(note_name.trim().getBytes("UTF-8").length <= 250, "length of note name should less than 250");
        Preconditions.checkArgument(others.getBytes("UTF-8").length <= 250, "length of others should not larger than 250");
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }

    return new FriendNote(note_name, others, username);
}
 
Example #6
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 #7
Source File: RegisterInfo.java    From jmessage-api-java-client with MIT License 5 votes vote down vote up
public Builder addExtras(Map<String, String> extras) {
    Preconditions.checkArgument(! (null == extras), "extras should not be null.");
    if (null == extrasBuilder) {
        extrasBuilder = new HashMap<String, String>();
    }
    for (String key : extras.keySet()) {
        extrasBuilder.put(key, extras.get(key));
    }
    return this;
}
 
Example #8
Source File: HttpProxy.java    From jiguang-java-client-common with MIT License 5 votes vote down vote up
public HttpProxy(String host, int port, String username, String password) {
    this(host, port);
    
    Preconditions.checkArgument(! (null == username), "username should not be null");
    Preconditions.checkArgument(! (null == password), "password should not be null");
    
    this.username = username;
    this.password = password;
    authenticationNeeded = true;
    
    LOG.info("Http Proxy - host:" + host + ", port:" + port
            + ", username:" + username + ", password:" + password);
}
 
Example #9
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 #10
Source File: ReportClient.java    From jmessage-api-java-client with MIT License 5 votes vote down vote up
/**
 * Get group statistic, time unit only supports DAY now.
 * @param start Format: yyyy-MM-dd
 * @param duration duration must between 0 and 60
 * @return {@link GroupStatListResult}
 * @throws APIConnectionException connect exception
 * @throws APIRequestException request exception
 */
public GroupStatListResult getGroupStatistic(String start, int duration)
        throws APIConnectionException, APIRequestException {
    Preconditions.checkArgument(verifyDateFormat("DAY", start), "Illegal date format");
    Preconditions.checkArgument(0 <= duration && duration <= 60, " 0 <= duration <= 60");
    String url = mBaseReportPath + mV2StatisticPath + "/groups?time_unit=DAY&start=" + start + "&duration=" + duration;
    ResponseWrapper responseWrapper = _httpClient.sendGet(url);
    return GroupStatListResult.fromResponse(responseWrapper);
}
 
Example #11
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 #12
Source File: RegisterInfo.java    From jmessage-api-java-client with MIT License 5 votes vote down vote up
public Builder addExtra(String key, Number value) {
    Preconditions.checkArgument(! (null == key || null == value), "Key/Value should not be null.");
    if (null == numberExtrasBuilder) {
        numberExtrasBuilder = new HashMap<String, Number>();
    }
    numberExtrasBuilder.put(key, value);
    return this;
}
 
Example #13
Source File: ResourceClient.java    From jmessage-api-java-client with MIT License 5 votes vote down vote up
/**
 * Download file with mediaId, will return DownloadResult which include url.
 * @param mediaId Necessary
 * @return DownloadResult
 * @throws APIConnectionException connect exception
 * @throws APIRequestException request exception
 */
public DownloadResult downloadFile(String mediaId)
        throws APIConnectionException, APIRequestException {
    Preconditions.checkArgument(null != mediaId, "mediaId is necessary");

    ResponseWrapper response = _httpClient.sendGet(_baseUrl + resourcePath + "?mediaId=" + mediaId);
    return DownloadResult.fromResponse(response, DownloadResult.class);
}
 
Example #14
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 #15
Source File: UserPayload.java    From jmessage-api-java-client with MIT License 5 votes vote down vote up
public Builder addExtra(String key, String value) {
    Preconditions.checkArgument(! (null == key || null == value), "Key/Value should not be null.");
    if (null == extrasBuilder) {
        extrasBuilder = new HashMap<String, String>();
    }
    extrasBuilder.put(key, value);
    return this;
}
 
Example #16
Source File: UserPayload.java    From jmessage-api-java-client with MIT License 5 votes vote down vote up
public Builder addExtra(String key, Number value) {
    Preconditions.checkArgument(! (null == key || null == value), "Key/Value should not be null.");
    if (null == numberExtrasBuilder) {
        numberExtrasBuilder = new HashMap<String, Number>();
    }
    numberExtrasBuilder.put(key, value);
    return this;
}
 
Example #17
Source File: ChatRoomClient.java    From jmessage-api-java-client with MIT License 5 votes vote down vote up
/**
 * Get chat room information by room ids
 *
 * @param roomIds Array of room id
 * @return {@link ChatRoomListResult}
 * @throws APIConnectionException connect exception
 * @throws APIRequestException    request exception
 */
public ChatRoomListResult getBatchChatRoomInfo(long... roomIds) throws APIConnectionException, APIRequestException {
    Preconditions.checkArgument(roomIds != null && roomIds.length > 0, "Room ids should not be null");
    JsonArray array = new JsonArray();
    for (long id : roomIds) {
        array.add(new JsonPrimitive(id));
    }
    ResponseWrapper responseWrapper = _httpClient.sendPost(_baseUrl + mChatRoomPath + "/batch", array.toString());
    return ChatRoomListResult.fromResponse(responseWrapper);
}
 
Example #18
Source File: ChatRoomClient.java    From jmessage-api-java-client with MIT License 5 votes vote down vote up
/**
 * Update chat room info
 *
 * @param roomId        room id
 * @param ownerUsername owner username
 * @param name          new chat room name
 * @param desc          chat room description
 * @return No content
 * @throws APIConnectionException connect exception
 * @throws APIRequestException    request exception
 */
public ResponseWrapper updateChatRoomInfo(long roomId, String ownerUsername, String name, String desc)
        throws APIConnectionException, APIRequestException {
    Preconditions.checkArgument(roomId > 0, "room id is invalid");
    StringUtils.checkUsername(ownerUsername);
    Preconditions.checkArgument(null != name, "Chat room name is null");
    Preconditions.checkArgument(null != desc, "Description is null");
    JsonObject jsonObject = new JsonObject();
    jsonObject.addProperty(ChatRoomPayload.OWNER, ownerUsername);
    jsonObject.addProperty(ChatRoomPayload.NAME, name);
    jsonObject.addProperty(ChatRoomPayload.DESC, desc);
    return _httpClient.sendPut(_baseUrl + mChatRoomPath + "/" + roomId, _gson.toJson(jsonObject));
}
 
Example #19
Source File: ChatRoomClient.java    From jmessage-api-java-client with MIT License 5 votes vote down vote up
/**
 * Add members to chat room
 *
 * @param roomId  chat room id
 * @param members username array
 * @return No content
 * @throws APIConnectionException connect exception
 * @throws APIRequestException    request exception
 */
public ResponseWrapper addChatRoomMember(long roomId, String... members)
        throws APIConnectionException, APIRequestException {
    Preconditions.checkArgument(roomId > 0, "room id is invalid");
    Preconditions.checkArgument(members != null && members.length > 0, "member should not be empty");
    JsonArray array = new JsonArray();
    for (String username : members) {
        array.add(new JsonPrimitive(username));
    }
    return _httpClient.sendPut(_baseUrl + mChatRoomPath + "/" + roomId + "/members", array.toString());
}
 
Example #20
Source File: ChatRoomClient.java    From jmessage-api-java-client with MIT License 5 votes vote down vote up
/**
 * remove members from chat room
 *
 * @param roomId  chat room id
 * @param members username array
 * @return No content
 * @throws APIConnectionException connect exception
 * @throws APIRequestException    request exception
 */
public ResponseWrapper removeChatRoomMembers(long roomId, String... members)
        throws APIConnectionException, APIRequestException {
    Preconditions.checkArgument(roomId > 0, "room id is invalid");
    Preconditions.checkArgument(members != null && members.length > 0, "member should not be empty");
    JsonArray array = new JsonArray();
    for (String username : members) {
        array.add(new JsonPrimitive(username));
    }
    return _httpClient.sendDelete(_baseUrl + mChatRoomPath + "/" + roomId + "/members", array.toString());
}
 
Example #21
Source File: CrossGroup.java    From jmessage-api-java-client with MIT License 5 votes vote down vote up
public CrossGroup build() {
    Preconditions.checkArgument(null != appKey, "AppKey must not be null");
    if (null == add_users && null == remove_users) {
        throw new IllegalArgumentException("At least one of add array or remove array should not be null");
    }
    return new CrossGroup(appKey, add_users, remove_users);
}
 
Example #22
Source File: UserClient.java    From jmessage-api-java-client with MIT License 5 votes vote down vote up
public ResponseWrapper updateUserInfo(String username, UserPayload payload)
        throws APIConnectionException, APIRequestException {
    StringUtils.checkUsername(username);
    Preconditions.checkArgument(null != payload, "payload should not be null");

    return _httpClient.sendPut(_baseUrl + userPath + "/" + username, payload.toString());
}
 
Example #23
Source File: UserClient.java    From jmessage-api-java-client with MIT License 5 votes vote down vote up
/**
 * Add Users to black list
 *
 * @param username The owner of the black list
 * @param users    The users that will add to black list
 * @return add users to black list
 * @throws APIConnectionException connect exception
 * @throws APIRequestException    request exception
 */
public ResponseWrapper addBlackList(String username, String... users)
        throws APIConnectionException, APIRequestException {
    StringUtils.checkUsername(username);
    Preconditions.checkArgument(null != users && users.length > 0, "black list should not be empty");

    JsonArray array = new JsonArray();
    for (String user : users) {
        array.add(new JsonPrimitive(user));
    }
    return _httpClient.sendPut(_baseUrl + userPath + "/" + username + "/blacklist", array.toString());
}
 
Example #24
Source File: UserClient.java    From jmessage-api-java-client with MIT License 5 votes vote down vote up
public ResponseWrapper removeBlackList(String username, String... users)
        throws APIConnectionException, APIRequestException {
    StringUtils.checkUsername(username);
    Preconditions.checkArgument(null != users && users.length > 0, "black list should not be empty");
    JsonArray array = new JsonArray();
    for (String user : users) {
        array.add(new JsonPrimitive(user));
    }

    return _httpClient.sendDelete(_baseUrl + userPath + "/" + username + "/blacklist", array.toString());
}
 
Example #25
Source File: UserClient.java    From jmessage-api-java-client with MIT License 5 votes vote down vote up
/**
 * Add friends to username
 *
 * @param username Necessary
 * @param users    username to be add
 * @return No content
 * @throws APIConnectionException connect exception
 * @throws APIRequestException    request exception
 */
public ResponseWrapper addFriends(String username, String... users)
        throws APIConnectionException, APIRequestException {
    StringUtils.checkUsername(username);
    Preconditions.checkArgument(null != users && users.length > 0, "friend list should not be empty");

    JsonArray array = new JsonArray();
    for (String user : users) {
        array.add(new JsonPrimitive(user));
    }

    return _httpClient.sendPost(_baseUrl + userPath + "/" + username + "/friends", array.toString());
}
 
Example #26
Source File: UserClient.java    From jmessage-api-java-client with MIT License 5 votes vote down vote up
/**
 * Delete friends
 *
 * @param username Friends you want to delete to
 * @param users    username to be delete
 * @return No content
 * @throws APIConnectionException connect exception
 * @throws APIRequestException    request exception
 */
public ResponseWrapper deleteFriends(String username, String... users)
        throws APIConnectionException, APIRequestException {
    StringUtils.checkUsername(username);
    Preconditions.checkArgument(null != users && users.length > 0, "friend list should not be empty");

    JsonArray array = new JsonArray();
    for (String user : users) {
        array.add(new JsonPrimitive(user));
    }
    return _httpClient.sendDelete(_baseUrl + userPath + "/" + username + "/friends", array.toString());
}
 
Example #27
Source File: UserClient.java    From jmessage-api-java-client with MIT License 5 votes vote down vote up
/**
 * Update friends' note information. The size is limit to 500.
 *
 * @param username Necessary
 * @param array    FriendNote array
 * @return No content
 * @throws APIConnectionException connect exception
 * @throws APIRequestException    request exception
 */
public ResponseWrapper updateFriendsNote(String username, FriendNote[] array)
        throws APIConnectionException, APIRequestException {
    StringUtils.checkUsername(username);
    FriendNotePayload payload = new FriendNotePayload.Builder()
            .setFriendNotes(array)
            .build();
    Preconditions.checkArgument(null != payload, "FriendNotePayload should not be null");
    return _httpClient.sendPut(_baseUrl + userPath + "/" + username + "/friends", payload.toString());
}
 
Example #28
Source File: SensitiveWordClient.java    From jmessage-api-java-client with MIT License 5 votes vote down vote up
/**
 * Add sensitive words
 * @param words String array
 * @return No content
 * @throws APIConnectionException connect exception
 * @throws APIRequestException request exception
 */
public ResponseWrapper addSensitiveWords(String...words) throws APIConnectionException, APIRequestException {
    JsonArray array = new JsonArray();
    for (String word: words) {
        Preconditions.checkArgument(word.length() <= 10, "one word's max length is 10");
        array.add(new JsonPrimitive(word));
    }
    return _httpClient.sendPost(_baseUrl + sensitiveWordPath, array.toString());
}
 
Example #29
Source File: SensitiveWordClient.java    From jmessage-api-java-client with MIT License 5 votes vote down vote up
/**
 * Update sensitive word
 * @param newWord new word
 * @param oldWord old word
 * @return No content
 * @throws APIConnectionException connect exception
 * @throws APIRequestException request exception
 */
public ResponseWrapper updateSensitiveWord(String newWord, String oldWord)
        throws APIConnectionException, APIRequestException {
    Preconditions.checkArgument(newWord.length() <= 10, "one word's max length is 10");
    Preconditions.checkArgument(oldWord.length() <= 10, "one word's max length is 10");
    JsonObject jsonObject = new JsonObject();
    jsonObject.addProperty("new_word", newWord);
    jsonObject.addProperty("old_word", oldWord);
    return _httpClient.sendPut(_baseUrl + sensitiveWordPath, jsonObject.toString());
}
 
Example #30
Source File: CrossAppClient.java    From jmessage-api-java-client with MIT License 5 votes vote down vote up
/**
 * Add or remove group members from a given group id.
 * @param gid Necessary, target group id.
 * @param groups Necessary
 * @return No content
 * @throws APIConnectionException connect exception
 * @throws APIRequestException request exception
 */
public ResponseWrapper addOrRemoveCrossGroupMembers(long gid, CrossGroup[] groups)
        throws APIConnectionException, APIRequestException {
    CrossGroupPayload payload = new CrossGroupPayload.Builder()
            .setCrossGroups(groups)
            .build();
    Preconditions.checkArgument(0 != gid, "gid should not be empty");
    Preconditions.checkArgument(null != payload, "CrossGroup must not be null");
    return _httpClient.sendPost(_baseUrl + crossGroupPath + "/" + gid + "/members", payload.toString());
}