cn.jiguang.common.utils.StringUtils Java Examples

The following examples show how to use cn.jiguang.common.utils.StringUtils. 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: SignPayload.java    From jsms-api-java-client with MIT License 6 votes vote down vote up
public JsonElement toJSON() {
    JsonObject json = new JsonObject();

    if (!StringUtils.isEmpty(sign)) {
        json.addProperty(SIGN, sign);
    }

    if (type !=null && type > 0 && type <=7) {
        json.addProperty(TYPE, type);
    }

    if (remark != null) {
        json.addProperty(REMARK, remark);
    }
    return json;
}
 
Example #2
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 #3
Source File: SignPayload.java    From jsms-api-java-client with 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 #4
Source File: SMSPayload.java    From jsms-api-java-client with MIT License 5 votes vote down vote up
public SMSPayload build() {
    Preconditions.checkArgument(null != mobile, "mobile number should not be null");
    Preconditions.checkArgument(StringUtils.isNotEmpty(mobile), "mobile number should not be empty");
    Preconditions.checkArgument(ttl >= 0, "ttl should not less 0");
    Preconditions.checkArgument(temp_id >= 0, "temp id should not less 0");

    return new SMSPayload(mobile, sign_id, temp_id, ttl, code, voice_lang, tempParaBuilder);
}
 
Example #5
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 #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: SMSClient.java    From jsms-api-java-client with MIT License 5 votes vote down vote up
/**
 * update sign
 *
 * @param payload {@link SignPayload }
 * @param signId necessary
 * @return SignResult
 * @throws APIConnectionException connect exception
 * @throws APIRequestException    request exception
 */
public SignResult updateSign(SignPayload payload, int signId) 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 + "/" + signId;
    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 update sign");
    }
}
 
Example #8
Source File: ServiceHelper.java    From jiguang-java-client-common with MIT License 5 votes vote down vote up
public static void checkBasic(String appKey, String masterSecret) {
    if (StringUtils.isEmpty(appKey)
            || StringUtils.isEmpty(masterSecret)) {
        throw new IllegalArgumentException("appKey and masterSecret are both required.");
    }
    if (appKey.length() != 24 
            || masterSecret.length() != 24
            || PUSH_PATTERNS.matcher(appKey).find()
            || PUSH_PATTERNS.matcher(masterSecret).find()) {
        throw new IllegalArgumentException("appKey and masterSecret format is incorrect. "
                + "They should be 24 size, and be composed with alphabet and numbers. "
                + "Please confirm that they are coming from JPush Web Portal.");
    }
}
 
Example #9
Source File: UserPayload.java    From jmessage-api-java-client with 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 #10
Source File: RecipientPayload.java    From jsms-api-java-client with MIT License 4 votes vote down vote up
public RecipientPayload build() {
    Preconditions.checkArgument(null != mobile, "mobile number should not be null");
    Preconditions.checkArgument(StringUtils.isNotEmpty(mobile), "mobile number should not be empty");

    return new RecipientPayload(mobile, tempPara);
}
 
Example #11
Source File: NettyHttpClient.java    From jiguang-java-client-common with MIT License 4 votes vote down vote up
public void sendRequest(HttpMethod method, String content, URI uri, BaseCallback callback) {
    FullHttpRequest request;
    b = new Bootstrap();
    if (b.group() == null) {
        b.group(_workerGroup);
    }
    b.channel(NioSocketChannel.class);
    b.option(ChannelOption.SO_KEEPALIVE, true);
    b.handler(new NettyClientInitializer(_sslCtx, callback, null));
    String scheme = uri.getScheme() == null ? "http" : uri.getScheme();
    int port = uri.getPort();
    if (port == -1) {
        if ("http".equalsIgnoreCase(scheme)) {
            port = 80;
        } else if ("https".equalsIgnoreCase(scheme)) {
            port = 443;
        }
    }
    _channel = b.connect(uri.getHost(), port).syncUninterruptibly().channel();
    if (null != content) {
        ByteBuf byteBuf = Unpooled.copiedBuffer(content.getBytes(CharsetUtil.UTF_8));
        request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, method, uri.getRawPath(), byteBuf);
        request.headers().set(HttpHeaderNames.CONTENT_LENGTH, (long) byteBuf.readableBytes());
    } else {
        request = new DefaultFullHttpRequest(HTTP_1_1, method, uri.getRawPath());
    }
    if (!StringUtils.isEmpty(_encryptType)) {
        request.headers().set("X-Encrypt-Type", _encryptType);
    }
    request.headers().set(HttpHeaderNames.HOST, uri.getHost());
    request.headers().set(HttpHeaderNames.AUTHORIZATION, _authCode);
    request.headers().set("Content-Type", "application/json;charset=utf-8");

    LOG.info("Sending request. " + request);
    LOG.info("Send body: " + content);
    _channel.writeAndFlush(request);
    try {
        _channel.closeFuture().sync();
        _workerGroup.shutdownGracefully();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

}
 
Example #12
Source File: SMSClient.java    From jsms-api-java-client with MIT License 3 votes vote down vote up
/**
 * Submit a mission that sending a template SMS with pointed schedule
 *
 * @param payload ScheduleSMSPayload
 * @return ScheduleResult which includes schedule_id
 * @throws APIConnectionException connect exception
 * @throws APIRequestException    request exception
 */
public ScheduleResult sendScheduleSMS(ScheduleSMSPayload payload) throws APIConnectionException, APIRequestException {
    Preconditions.checkArgument(null != payload, "Schedule SMS payload 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.sendPost(_baseUrl + _schedulePath, payload.toString());
    return ScheduleResult.fromResponse(responseWrapper, ScheduleResult.class);
}