Search in sources :

Example 6 with JSON

use of com.blade.mvc.annotation.JSON in project blade by biezhi.

the class RouteViewResolve method handle.

public void handle(Request request, Response response, Route route) throws Exception {
    try {
        Method actionMethod = route.getAction();
        Object target = route.getTarget();
        int len = actionMethod.getParameterTypes().length;
        Object returnParam;
        if (len > 0) {
            Object[] args = MethodArgument.getArgs(request, response, actionMethod);
            returnParam = ReflectKit.invokeMehod(target, actionMethod, args);
        } else {
            returnParam = ReflectKit.invokeMehod(target, actionMethod);
        }
        if (null != returnParam) {
            Class<?> returnType = returnParam.getClass();
            RestController restController = target.getClass().getAnnotation(RestController.class);
            JSON json = actionMethod.getAnnotation(JSON.class);
            if (null != restController || null != json) {
                response.json(viewSettings.toJSONString(returnParam));
            } else {
                if (returnType == String.class) {
                    response.render(returnParam.toString());
                } else if (returnType == ModelAndView.class) {
                    ModelAndView modelAndView = (ModelAndView) returnParam;
                    response.render(modelAndView);
                }
            }
        }
    } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
        throw new BladeException(e.getCause());
    } catch (Exception e) {
        throw e;
    }
}
Also used : BladeException(com.blade.exception.BladeException) ModelAndView(com.blade.mvc.view.ModelAndView) RestController(com.blade.mvc.annotation.RestController) JSON(com.blade.mvc.annotation.JSON) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) BladeException(com.blade.exception.BladeException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 7 with JSON

use of com.blade.mvc.annotation.JSON in project tale by otale.

the class AttachController method delete.

@Route(value = "delete")
@JSON
public RestResponse delete(@QueryParam Integer id, Request request) {
    try {
        Attach attach = attachService.byId(id);
        if (null == attach)
            return RestResponse.fail("不存在该附件");
        attachService.delete(id);
        siteService.cleanCache(Types.C_STATISTICS);
        String upDir = CLASSPATH.substring(0, CLASSPATH.length() - 1);
        FileKit.delete(upDir + attach.getFkey());
        logService.save(LogActions.DEL_ATTACH, attach.getFkey(), request.address(), this.getUid());
    } catch (Exception e) {
        String msg = "附件删除失败";
        if (e instanceof TipException)
            msg = e.getMessage();
        else
            LOGGER.error(msg, e);
        return RestResponse.fail(msg);
    }
    return RestResponse.ok();
}
Also used : Attach(com.tale.model.Attach) TipException(com.tale.exception.TipException) IOException(java.io.IOException) TipException(com.tale.exception.TipException) JSON(com.blade.mvc.annotation.JSON) Route(com.blade.mvc.annotation.Route)

Example 8 with JSON

use of com.blade.mvc.annotation.JSON in project tale by otale.

the class AttachController method upload.

/**
     * 上传文件接口
     *
     *  返回格式
     * @param request
     * @return
     */
@Route(value = "upload", method = HttpMethod.POST)
@JSON
public RestResponse upload(Request request) {
    LOGGER.info("UPLOAD DIR = {}", TaleUtils.upDir);
    Users users = this.user();
    Integer uid = users.getUid();
    Map<String, FileItem> fileItemMap = request.fileItems();
    Collection<FileItem> fileItems = fileItemMap.values();
    List<Attach> errorFiles = new ArrayList<>();
    List<Attach> urls = new ArrayList<>();
    try {
        fileItems.forEach((FileItem f) -> {
            String fname = f.fileName();
            if (f.file().length() / 1024 <= TaleConst.MAX_FILE_SIZE) {
                String fkey = TaleUtils.getFileKey(fname);
                String ftype = TaleUtils.isImage(f.file()) ? Types.IMAGE : Types.FILE;
                String filePath = TaleUtils.upDir + fkey;
                File file = new File(filePath);
                try {
                    Tools.copyFileUsingFileChannels(f.file(), file);
                    f.file().delete();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                Attach attach = attachService.save(fname, fkey, ftype, uid);
                urls.add(attach);
                siteService.cleanCache(Types.C_STATISTICS);
            } else {
                errorFiles.add(new Attach(fname));
            }
        });
        if (errorFiles.size() > 0) {
            RestResponse restResponse = new RestResponse();
            restResponse.setSuccess(false);
            restResponse.setPayload(errorFiles);
            return restResponse;
        }
        return RestResponse.ok(urls);
    } catch (Exception e) {
        String msg = "文件上传失败";
        if (e instanceof TipException) {
            msg = e.getMessage();
        } else {
            LOGGER.error(msg, e);
        }
        return RestResponse.fail(msg);
    }
}
Also used : Attach(com.tale.model.Attach) RestResponse(com.blade.mvc.view.RestResponse) ArrayList(java.util.ArrayList) Users(com.tale.model.Users) IOException(java.io.IOException) TipException(com.tale.exception.TipException) IOException(java.io.IOException) TipException(com.tale.exception.TipException) FileItem(com.blade.mvc.multipart.FileItem) File(java.io.File) JSON(com.blade.mvc.annotation.JSON) Route(com.blade.mvc.annotation.Route)

Example 9 with JSON

use of com.blade.mvc.annotation.JSON in project tale by otale.

the class CommentController method reply.

@Route(value = "", method = HttpMethod.POST)
@JSON
public RestResponse reply(@QueryParam Integer coid, @QueryParam String content, Request request) {
    if (null == coid || StringKit.isBlank(content)) {
        return RestResponse.fail("请输入完整后评论");
    }
    if (content.length() > 2000) {
        return RestResponse.fail("请输入2000个字符以内的回复");
    }
    Comments c = commentsService.byId(coid);
    if (null == c) {
        return RestResponse.fail("不存在该评论");
    }
    Users users = this.user();
    content = TaleUtils.cleanXSS(content);
    content = EmojiParser.parseToAliases(content);
    Comments comments = new Comments();
    comments.setAuthor(users.getUsername());
    comments.setAuthor_id(users.getUid());
    comments.setCid(c.getCid());
    comments.setIp(request.address());
    comments.setUrl(users.getHome_url());
    comments.setContent(content);
    if (StringKit.isNotBlank(users.getEmail())) {
        comments.setMail(users.getEmail());
    } else {
        comments.setMail("support@tale.me");
    }
    comments.setParent(coid);
    try {
        commentsService.saveComment(comments);
        siteService.cleanCache(Types.C_STATISTICS);
        return RestResponse.ok();
    } catch (Exception e) {
        String msg = "回复失败";
        if (e instanceof TipException) {
            msg = e.getMessage();
        } else {
            LOGGER.error(msg, e);
        }
        return RestResponse.fail(msg);
    }
}
Also used : Comments(com.tale.model.Comments) Users(com.tale.model.Users) TipException(com.tale.exception.TipException) TipException(com.tale.exception.TipException) JSON(com.blade.mvc.annotation.JSON) Route(com.blade.mvc.annotation.Route)

Example 10 with JSON

use of com.blade.mvc.annotation.JSON in project tale by otale.

the class IndexController method saveSetting.

/**
     * 保存系统设置
     */
@Route(value = "setting", method = HttpMethod.POST)
@JSON
public RestResponse saveSetting(@QueryParam String site_theme, Request request) {
    try {
        Map<String, String> querys = request.querys();
        optionsService.saveOptions(querys);
        Config config = new Config();
        config.addAll(optionsService.getOptions());
        TaleConst.OPTIONS = config;
        logService.save(LogActions.SYS_SETTING, JSONKit.toJSONString(querys), request.address(), this.getUid());
        return RestResponse.ok();
    } catch (Exception e) {
        String msg = "保存设置失败";
        if (e instanceof TipException) {
            msg = e.getMessage();
        } else {
            LOGGER.error(msg, e);
        }
        return RestResponse.fail(msg);
    }
}
Also used : Config(com.blade.kit.base.Config) TipException(com.tale.exception.TipException) TipException(com.tale.exception.TipException) JSON(com.blade.mvc.annotation.JSON) Route(com.blade.mvc.annotation.Route)

Aggregations

JSON (com.blade.mvc.annotation.JSON)14 Route (com.blade.mvc.annotation.Route)13 TipException (com.tale.exception.TipException)12 Users (com.tale.model.Users)6 Config (com.blade.kit.base.Config)3 Comments (com.tale.model.Comments)3 Attach (com.tale.model.Attach)2 IOException (java.io.IOException)2 BladeException (com.blade.exception.BladeException)1 RestController (com.blade.mvc.annotation.RestController)1 FileItem (com.blade.mvc.multipart.FileItem)1 ModelAndView (com.blade.mvc.view.ModelAndView)1 RestResponse (com.blade.mvc.view.RestResponse)1 BackResponse (com.tale.dto.BackResponse)1 Metas (com.tale.model.Metas)1 File (java.io.File)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1