Java Code Examples for cn.jiguang.common.utils.Preconditions

The following examples show how to use cn.jiguang.common.utils.Preconditions. These examples are extracted from open source projects. 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 Project: jmessage-api-java-client   Author: jpush   File: GroupPayload.java    License: 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 Project: jsms-api-java-client   Author: jpush   File: SignPayload.java    License: MIT License 5 votes vote down vote up
public SignPayload build() {
    Preconditions.checkArgument(!StringUtils.isEmpty(sign)&&sign.length()>=2
                    &&sign.length()<=8,
            "sign should not be null or too long");
    Preconditions.checkArgument(type != null&&type > 0 && type <= 7, "type should be between 1 and 7");
    return new SignPayload(this);
}
 
Example #3
Source Project: jmessage-api-java-client   Author: jpush   File: UserPayload.java    License: 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 #4
Source Project: jmessage-api-java-client   Author: jpush   File: RegisterInfo.java    License: 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 #5
Source Project: jmessage-api-java-client   Author: jpush   File: MessageBody.java    License: 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 #6
Source Project: jmessage-api-java-client   Author: jpush   File: FriendNote.java    License: 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 #7
Source Project: jmessage-api-java-client   Author: jpush   File: UserPayload.java    License: MIT License 5 votes vote down vote up
public UserPayload build() {

            if ( null != nickname ) {
                Preconditions.checkArgument(nickname.getBytes().length <= 64,
                        "The length of nickname must less than 64 bytes.");
                Preconditions.checkArgument(!StringUtils.isLineBroken(nickname),
                        "The nickname must not contain line feed character.");
            }

            if ( null != birthday) {
                Preconditions.checkArgument( ServiceHelper.isValidBirthday(birthday),
                        "Invalid birthday.");
            }

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

            Preconditions.checkArgument( gender >= -1 && gender <= 2,
                    "Invalid gender. 0 for unknown , 1 for male and 2 for female." );

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

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

            return new UserPayload(nickname, birthday, signature, gender, region, address, avatar, 
            		extrasBuilder, numberExtrasBuilder, booleanExtrasBuilder,jsonExtrasBuilder);
        }
 
Example #8
Source Project: jmessage-api-java-client   Author: jpush   File: MessageBody.java    License: 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 #9
Source Project: jsms-api-java-client   Author: jpush   File: SMSPayload.java    License: MIT License 5 votes vote down vote up
public Builder setTempPara(Map<String, String> temp_para) {
    Preconditions.checkArgument(! (null == temp_para), "temp_para should not be null.");
    if (null == tempParaBuilder) {
        tempParaBuilder = new HashMap<String, String>();
    }
    for (String key : temp_para.keySet()) {
        tempParaBuilder.put(key, temp_para.get(key));
    }
    return this;
}
 
Example #10
Source Project: jsms-api-java-client   Author: jpush   File: SMSPayload.java    License: MIT License 5 votes vote down vote up
public Builder addTempPara(String key, String value) {
    Preconditions.checkArgument(! (null == key || null == value), "Key/Value should not be null.");
    if (null == tempParaBuilder) {
        tempParaBuilder = new HashMap<String, String>();
    }
    tempParaBuilder.put(key, value);
    return this;
}
 
Example #11
Source Project: jmessage-api-java-client   Author: jpush   File: UserPayload.java    License: 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 #12
Source Project: jsms-api-java-client   Author: jpush   File: SMSClient.java    License: MIT License 5 votes vote down vote up
/**
 * Send SMS verification code to server, to verify if the code valid
 *
 * @param msgId The message id of the verification code
 * @param code  Verification code
 * @return return ValidSMSResult includes is_valid
 * @throws APIConnectionException connection exception
 * @throws APIRequestException    request exception
 */
public ValidSMSResult sendValidSMSCode(String msgId, String code)
        throws APIConnectionException, APIRequestException {
    Preconditions.checkArgument(null != msgId, "Message id should not be null");
    Pattern codePattern = Pattern.compile("^[0-9]{6}");
    Preconditions.checkArgument(codePattern.matcher(code).matches(), "The verification code shoude be consist of six number");
    JsonObject json = new JsonObject();
    json.addProperty(SMS_CODE, code);

    ResponseWrapper response = _httpClient.sendPost(_baseUrl + _smsCodePath + "/" + msgId + _validPath, json.toString());
    return ValidSMSResult.fromResponse(response, ValidSMSResult.class);
}
 
Example #13
Source Project: jsms-api-java-client   Author: jpush   File: SMSClient.java    License: 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 #14
Source Project: jmessage-api-java-client   Author: jpush   File: MessagePayload.java    License: MIT License 5 votes vote down vote up
public MessagePayload build() {
    Preconditions.checkArgument(null != mVersion, "The version must not be empty!");
    Preconditions.checkArgument(StringUtils.isNotEmpty(mTargetType), "The target type must not be empty!");
    StringUtils.checkUsername(mTargetId);
    Preconditions.checkArgument(StringUtils.isNotEmpty(mFromType), "The from type must not be empty!");
    StringUtils.checkUsername(mFromId);
    Preconditions.checkArgument(mMsgType != null, "The message type must not be empty!");
    Preconditions.checkArgument(null != mMsgBody, "The message body must not be empty!");

    return new MessagePayload(mVersion, mTargetType, mTargetId, mFromType, mFromId, mTargetAppKey, mFromName,
            mTargetName, mNoOffline, mNoNotification, mMsgType, mMsgBody, mNotification);
}
 
Example #15
Source Project: jmessage-api-java-client   Author: jpush   File: RegisterInfo.java    License: 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 Project: jmessage-api-java-client   Author: jpush   File: UserPayload.java    License: MIT License 5 votes vote down vote up
public Builder addExtra(String key, Boolean value) {
    Preconditions.checkArgument(! (null == key || null == value), "Key/Value should not be null.");
    if (null == booleanExtrasBuilder) {
        booleanExtrasBuilder = new HashMap<String, Boolean>();
    }
    booleanExtrasBuilder.put(key, value);
    return this;
}
 
Example #17
Source Project: jsms-api-java-client   Author: jpush   File: SMSClient.java    License: 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 #18
Source Project: jmessage-api-java-client   Author: jpush   File: RegisterInfo.java    License: 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 #19
Source Project: jiguang-java-client-common   Author: jpush   File: HttpProxy.java    License: 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 #20
Source Project: jmessage-api-java-client   Author: jpush   File: ReportClient.java    License: 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 #21
Source Project: jmessage-api-java-client   Author: jpush   File: ReportClient.java    License: 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 #22
Source Project: jmessage-api-java-client   Author: jpush   File: MessageClient.java    License: 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 #23
Source Project: jmessage-api-java-client   Author: jpush   File: RegisterInfo.java    License: 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 #24
Source Project: jmessage-api-java-client   Author: jpush   File: ResourceClient.java    License: 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 #25
Source Project: jmessage-api-java-client   Author: jpush   File: GroupClient.java    License: 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 #26
Source Project: jmessage-api-java-client   Author: jpush   File: UserPayload.java    License: 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 #27
Source Project: jmessage-api-java-client   Author: jpush   File: GroupClient.java    License: 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 #28
Source Project: jmessage-api-java-client   Author: jpush   File: ChatRoomClient.java    License: 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 #29
Source Project: jmessage-api-java-client   Author: jpush   File: ChatRoomClient.java    License: 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 #30
Source Project: jmessage-api-java-client   Author: jpush   File: ChatRoomClient.java    License: 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());
}