Search in sources :

Example 1 with DownInfo

use of org.pdown.rest.entity.DownInfo in project proxyee-down by monkeyWie.

the class HttpDownAppCallback method commonHook.

private void commonHook(HttpDownBootstrap httpDownBootstrap, String event, boolean async) {
    DownInfo downInfo = findDownInfo(httpDownBootstrap);
    Map<String, Object> taskInfo = buildTaskInfo(downInfo);
    if (taskInfo != null) {
        // 遍历扩展模块是否有对应的处理
        List<ExtensionInfo> extensionInfos = ExtensionContent.get();
        for (ExtensionInfo extensionInfo : extensionInfos) {
            if (extensionInfo.getMeta().isEnabled()) {
                if (extensionInfo.getHookScript() != null && !StringUtils.isEmpty(extensionInfo.getHookScript().getScript())) {
                    Event e = extensionInfo.getHookScript().hasEvent(event, HttpDownUtil.getUrl(httpDownBootstrap.getRequest()));
                    if (e != null) {
                        try {
                            // 执行钩子函数
                            Object result = ExtensionUtil.invoke(extensionInfo, e, taskInfo, async);
                            if (result != null) {
                                ObjectMapper objectMapper = new ObjectMapper();
                                String temp = objectMapper.writeValueAsString(result);
                                TaskForm taskForm = objectMapper.readValue(temp, TaskForm.class);
                                if (taskForm.getRequest() != null) {
                                    httpDownBootstrap.setRequest(HttpDownUtil.buildRequest(taskForm.getRequest().getMethod(), taskForm.getRequest().getUrl(), taskForm.getRequest().getHeads(), taskForm.getRequest().getBody()));
                                }
                                if (taskForm.getResponse() != null) {
                                    httpDownBootstrap.setResponse(taskForm.getResponse());
                                }
                                if (taskForm.getData() != null) {
                                    downInfo.setData(taskForm.getData());
                                }
                                HttpDownContent.getInstance().save();
                            }
                        } catch (Exception ex) {
                            LOGGER.error("An hook exception occurred while " + event + "()", ex);
                        }
                    }
                }
            }
        }
    }
}
Also used : DownInfo(org.pdown.rest.entity.DownInfo) ExtensionInfo(org.pdown.gui.extension.ExtensionInfo) TaskForm(org.pdown.rest.form.TaskForm) Event(org.pdown.gui.extension.HookScript.Event) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ExtensionInfo (org.pdown.gui.extension.ExtensionInfo)1 Event (org.pdown.gui.extension.HookScript.Event)1 DownInfo (org.pdown.rest.entity.DownInfo)1 TaskForm (org.pdown.rest.form.TaskForm)1