Search in sources :

Example 1 with ZhydCommentException

use of com.zyd.blog.framework.exception.ZhydCommentException in project OneBlog by zhangyd-c.

the class BaiduPushUtil method doPush.

/**
 * 推送链接到百度站长平台
 *
 * @param urlString 百度站长平台地址
 * @param params    待推送的链接
 * @param cookie    百度平台的cookie
 * @return api接口响应内容
 */
public static String doPush(String urlString, String params, String cookie) {
    if (StringUtils.isEmpty(cookie)) {
        throw new ZhydCommentException("尚未设置百度站长平台的Cookie信息,该功能不可用!");
    }
    log.info("{} REST url: {}", new Date(), urlString);
    HttpURLConnection connection = null;
    try {
        connection = openConnection(urlString);
        connection.setRequestMethod("POST");
        // (如果不设此项,json数据 ,当WEB服务默认的不是这种类型时可能抛java.io.EOFException)
        connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        connection.setRequestProperty("Action", "1000");
        connection.setRequestProperty("User-Agent", USER_AGENT);
        connection.setRequestProperty("Connection", "keep-alive");
        connection.setRequestProperty("Cookie", cookie);
        connection.setDoOutput(true);
        connection.setDoInput(true);
        // 设置连接超时时间,单位毫秒
        connection.setConnectTimeout(10000);
        // 设置读取数据超时时间,单位毫秒
        connection.setReadTimeout(10000);
        // Post 请求不能使用缓存
        connection.setUseCaches(false);
        if (params != null) {
            final OutputStream outputStream = connection.getOutputStream();
            writeOutput(outputStream, params);
            outputStream.close();
        }
        log.info("RestClientUtil response: {} : {}", connection.getResponseCode(), connection.getResponseMessage());
        if (connection.getResponseCode() == HttpsURLConnection.HTTP_OK) {
            return readInput(connection.getInputStream(), "UTF-8");
        } else {
            return readInput(connection.getErrorStream(), "UTF-8");
        }
    } catch (Exception e) {
        log.error("推送到百度站长平台发生异常!", e);
        return "";
    } finally {
        if (null != connection) {
            connection.disconnect();
        }
    }
}
Also used : ZhydCommentException(com.zyd.blog.framework.exception.ZhydCommentException) HttpURLConnection(java.net.HttpURLConnection) OutputStream(java.io.OutputStream) Date(java.util.Date) ZhydCommentException(com.zyd.blog.framework.exception.ZhydCommentException)

Example 2 with ZhydCommentException

use of com.zyd.blog.framework.exception.ZhydCommentException in project OneBlog by zhangyd-c.

the class BizCommentServiceImpl method comment.

/**
 * 发表评论
 *
 * @param comment
 * @return
 */
@Override
@RedisCache(flush = true)
public Comment comment(Comment comment) throws ZhydCommentException {
    SysConfig sysConfig = configService.getByKey(ConfigKeyEnum.ANONYMOUS.getKey());
    boolean anonymous = true;
    if (null != sysConfig) {
        anonymous = "1".equals(sysConfig.getConfigValue());
    }
    // 非匿名且未登录
    if (!anonymous && !SessionUtil.isLogin()) {
        throw new ZhydCommentException("站长已关闭匿名评论,请先登录!");
    }
    // 过滤文本内容,防止xss
    this.filterContent(comment);
    // 已登录且非匿名,使用当前登录用户的信息评论
    if (SessionUtil.isLogin()) {
        this.setCurrentLoginUserInfo(comment);
    } else {
        this.setCurrentAnonymousUserInfo(comment);
    }
    // 用户没有头像时, 使用随机默认的头像
    if (StringUtils.isEmpty(comment.getAvatar())) {
        List<String> avatars = configService.getRandomUserAvatar();
        if (!CollectionUtils.isEmpty(avatars)) {
            Collections.shuffle(avatars);
            int randomIndex = new Random().nextInt(avatars.size());
            comment.setAvatar(avatars.get(randomIndex));
        }
    }
    if (StringUtils.isEmpty(comment.getStatus())) {
        comment.setStatus(CommentStatusEnum.VERIFYING.toString());
    }
    // set当前评论者的设备信息
    this.setCurrentDeviceInfo(comment);
    // set当前评论者的位置信息
    this.setCurrentLocation(comment);
    // 保存
    this.insert(comment);
    // 发送邮件通知
    this.sendEmail(comment);
    return comment;
}
Also used : SysConfig(com.zyd.blog.persistence.beans.SysConfig) ZhydCommentException(com.zyd.blog.framework.exception.ZhydCommentException) RedisCache(com.zyd.blog.business.annotation.RedisCache)

Example 3 with ZhydCommentException

use of com.zyd.blog.framework.exception.ZhydCommentException in project OneBlog by zhangyd-c.

the class SysUserServiceImpl method updateSelective.

@Override
@Transactional(rollbackFor = Exception.class)
public boolean updateSelective(User user) {
    Assert.notNull(user, "User不可为空!");
    user.setUpdateTime(new Date());
    if (!StringUtils.isEmpty(user.getPassword())) {
        try {
            user.setPassword(PasswordUtil.encrypt(user.getPassword(), user.getUsername()));
        } catch (Exception e) {
            e.printStackTrace();
            throw new ZhydCommentException("密码加密失败");
        }
    } else {
        user.setPassword(null);
    }
    return sysUserMapper.updateByPrimaryKeySelective(user.getSysUser()) > 0;
}
Also used : ZhydCommentException(com.zyd.blog.framework.exception.ZhydCommentException) Date(java.util.Date) ZhydException(com.zyd.blog.framework.exception.ZhydException) ZhydCommentException(com.zyd.blog.framework.exception.ZhydCommentException) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

ZhydCommentException (com.zyd.blog.framework.exception.ZhydCommentException)3 Date (java.util.Date)2 RedisCache (com.zyd.blog.business.annotation.RedisCache)1 ZhydException (com.zyd.blog.framework.exception.ZhydException)1 SysConfig (com.zyd.blog.persistence.beans.SysConfig)1 OutputStream (java.io.OutputStream)1 HttpURLConnection (java.net.HttpURLConnection)1 Transactional (org.springframework.transaction.annotation.Transactional)1